Skip to content

Commit

Permalink
Fix series matching of duplicate titles
Browse files Browse the repository at this point in the history
  • Loading branch information
Shraymonks committed Sep 17, 2023
1 parent 7427a0b commit 8560b09
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-needles-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'unmonitorr': patch
---

Fix series matching of duplicate titles
6 changes: 3 additions & 3 deletions src/sonarr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Response } from 'express';
import type { PlexPayload } from './types/plex';
import type { components } from './types/sonarr';

import { Api, getIds } from './utils.js';
import { Api, cleanTitle, getIds } from './utils.js';

export const DEFAULT_SONARR_HOST = 'http://127.0.0.1:8989';

Expand Down Expand Up @@ -48,12 +48,12 @@ export async function unmonitorEpisode(
const seriesList =
(await seriesResponse.json()) as components['schemas']['SeriesResource'][];

const cleanedTitle = cleanTitle(seriesTitle);
// Match potential series on title. Year metadata from Plex is for the episode
// so cannot be used for series filtering.
const seriesMatches = seriesList.filter(
({ title }) =>
// tvdb appends the year to some titles; remove it for matching
title?.match(/^(.+?)(?: \(\d+\))?$/)?.[1] === seriesTitle
typeof title === 'string' && cleanTitle(title) === cleanedTitle
);
if (seriesMatches.length === 0) {
console.warn(`Could not find ${seriesTitle} in sonarr library`);
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export class Api {
}
}

// Removes year from title. Media with duplicate titles may sometimes have the
// year appended to the end of the title.
export function cleanTitle(title: string): string {
return title.replace(/ \(\d{4}\)$/, '');
}

// gets the matching ids from Plex Metadata.Guid payload
// note Metadata.Guid can contain multiple ids of the same type.
// getGuid([{id: 'tvdb://1234'}], 'tvdb') => ['1234']
Expand Down

0 comments on commit 8560b09

Please sign in to comment.