Skip to content

Commit

Permalink
#11: Fix Apple tv parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Navino16 committed Oct 11, 2023
1 parent 2ee1534 commit 518d70d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
22 changes: 17 additions & 5 deletions src/Flixpatrol/FlixPatrol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export class FlixPatrol {
const dom = new JSDOM(html);
let expression;
if (location !== 'world') {
expression = `//h3[text() = "TOP 10 ${type}"]/following-sibling::div//a[@class="hover:underline"]/@href`;
const searchType = platform === 'apple-tv' ? 'Overall' : type;
expression = `//h3[text() = "TOP 10 ${searchType}"]/following-sibling::div//a[@class="hover:underline"]/@href`;
} else {
const id = type === 'Movies' ? 1 : 2;
expression = `//div[@id="${platform}-${id}"]//a[contains(@class, "hover:underline")]/@href`;
Expand All @@ -106,7 +107,7 @@ export class FlixPatrol {
return results;
}

private async convertToTMDBId(result: FlixPatrolMatchResult) : Promise<FlixPatrolTMDBId> {
private async convertToTMDBId(result: FlixPatrolMatchResult, type: FlixPatrolType) : Promise<FlixPatrolTMDBId> {
const html = await this.getFlixPatrolHTMLPage(result);
if (html === null) {
logger.error('FlixPatrol Error: unable to get FlixPatrol detail page');
Expand All @@ -122,11 +123,22 @@ export class FlixPatrol {
null,
);

const regex = match.stringValue.match(/(themoviedb\.org)(\D*)(\d+)/i);
let regex;
if (type === 'Movies') {
regex = match.stringValue.match(/(themoviedb\.org\/movie)(\D*)(\d+)/i);
} else {
regex = match.stringValue.match(/(themoviedb\.org\/tv)(\D*)(\d+)/i);
}

return regex ? regex[3] : null;
}

public async getTop10(type: FlixPatrolType, platform: FlixPatrolPlatform, location: FlixPatrolLocation, fallback: FlixPatrolLocation | false): Promise<FlixPatrolTMDBIds> {
public async getTop10(
type: FlixPatrolType,
platform: FlixPatrolPlatform,
location: FlixPatrolLocation,
fallback: FlixPatrolLocation | false,
): Promise<FlixPatrolTMDBIds> {
const html = await this.getFlixPatrolHTMLPage(`/top10/${platform}/${location}`);
if (html === null) {
logger.error('FlixPatrol Error: unable to get FlixPatrol top10 page');
Expand All @@ -143,7 +155,7 @@ export class FlixPatrol {
// eslint-disable-next-line no-restricted-syntax
for (const result of results) {
// eslint-disable-next-line no-await-in-loop
const id = await this.convertToTMDBId(result);
const id = await this.convertToTMDBId(result, type);
if (id) {
TMDBIds.push(id);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Trakt/TraktAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export class TraktAPI {
// Avoid Trakt rate limite
await Utils.sleep(2000);
}
await this.addItemsToList(list, tmdbIDs, traktType);
if (tmdbIDs.length > 0) {
await this.addItemsToList(list, tmdbIDs, traktType);
}
}
}
17 changes: 5 additions & 12 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,18 @@ trakt.connect().then(async () => {
// eslint-disable-next-line no-restricted-syntax
for (const flixPatrolConfig of flixPatrolConfigs) {
const listName = `${flixPatrolConfig.platform}-${flixPatrolConfig.location}-top10-${flixPatrolConfig.fallback === false ? 'without-fallback' : `with-${flixPatrolConfig.fallback}-fallback`}`;

const top10Movies = await flixpatrol.getTop10('Movies', flixPatrolConfig.platform, flixPatrolConfig.location, flixPatrolConfig.fallback);
logger.debug(`${flixPatrolConfig.platform} movies: ${top10Movies}`);
if (top10Movies.length > 0) {
await trakt.pushToList(top10Movies, listName, 'Movies', flixPatrolConfig.privacy);
logger.info(`List ${listName} updated with new movies`);
} else {
logger.warn(`No movies found for ${flixPatrolConfig.platform}`);
}
await trakt.pushToList(top10Movies, listName, 'Movies', flixPatrolConfig.privacy);
logger.info(`List ${listName} updated with ${top10Movies.length} new movies`);

// Avoid rate limit on Trakt and spam FlixPatrol
await Utils.sleep(5000);

const top10Shows = await flixpatrol.getTop10('TV Shows', flixPatrolConfig.platform, flixPatrolConfig.location, flixPatrolConfig.fallback);
logger.debug(`${flixPatrolConfig.platform} shows: ${top10Shows}`);
if (top10Shows.length > 0) {
await trakt.pushToList(top10Shows, listName, 'TV Shows', flixPatrolConfig.privacy);
logger.info(`List ${listName} updated with new shows`);
} else {
logger.warn(`No shows found for ${flixPatrolConfig.platform}`);
}
await trakt.pushToList(top10Shows, listName, 'TV Shows', flixPatrolConfig.privacy);
logger.info(`List ${listName} updated with ${top10Shows.length} new shows`);
}
});

0 comments on commit 518d70d

Please sign in to comment.