Skip to content

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryck committed Feb 25, 2025
2 parents ed62e7c + 97abe84 commit 77bc72b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 85 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ LABEL org.opencontainers.image.licenses="MIT"

ENV NODE_ENV=production
ENV PORT=3090
ENV PORT $PORT
ENV PORT=$PORT
ENV LOG_LEVEL=info
ENV LOG_LEVEL $LOG_LEVEL
ENV LOG_LEVEL=$LOG_LEVEL

RUN apk --no-cache add curl

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "scrobblex",
"description": "Self-hosted app that enables Plex scrobbling integration with Trakt via webhooks",
"version": "1.2.1",
"version": "1.3.0",
"homepage": "https://github.com/ryck/scrobblex",
"repository": {
"type": "git",
Expand Down
154 changes: 77 additions & 77 deletions src/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { formatDistanceToNow } from 'date-fns';

import { logger } from './logger.js';
import { api } from './api.js';
import { GetGuid } from './utils.js';
import { GetGuids } from './utils.js';

export const scrobbleRequest = async ({ action, body, title }) => {
try {
Expand Down Expand Up @@ -40,96 +40,96 @@ export const rateRequest = async ({ body, title, rating }) => {


export const findMovieRequest = async (payload) => {
const { service, id } = GetGuid({ payload })

if (service && id) {
logger.info(`🔍 Finding movie info for ${payload.Metadata.title} (${payload.Metadata.year}) using ${service}://${id}`);

} else {
logger.error(`❌ ${chalk.red(`No GUID available`)}`);
return
}

try {
const response = await api.get(`https://api.trakt.tv/search/${service}/${id}?type=movie`, { ttl: 1000 * 60 * 180 });
if (!response.data.length) {
logger.error(`❌ ${chalk.red(`Response was empty!`)}`);
return
const guids = GetGuids({ payload });

for (const { service, id } of guids) {
if (service && id) {
logger.info(`🔍 Finding movie info for ${payload.Metadata.title} (${payload.Metadata.year}) using ${service}://${id}`);

try {
const response = await api.get(`https://api.trakt.tv/search/${service}/${id}?type=movie`, { ttl: 1000 * 60 * 180 });
logger.debug(JSON.stringify(response.data, null, 2));
if (response.data.length) {
const movie = response.data[0].movie;
const { title, year } = movie;
logger.info(`🎬 Movie found: ${title} (${year})`);
return movie;
} else {
logger.error(`❌ ${chalk.red(`Response from ${service} was empty!`)}`);
}
} catch (err) {
logger.error(`❌ ${chalk.red(`Search movie API error: ${err.message}`)}`);
}
} else {
logger.error(`❌ ${chalk.red(`No GUID available`)}`);
}
const movie = response.data[0].movie;
const { title, year } = movie;
logger.info(`🎬 Movie found: ${title} (${year})`);

return movie;
} catch (err) {
// Error handling here
logger.error(`❌ ${chalk.red(`Search movie API error: ${err.message}`)}`);
}

logger.error(`❌ ${chalk.red(`No movie found for any GUIDs`)}`);
return null;
};

export const findEpisodeRequest = async (payload) => {
const { service, id } = GetGuid({ payload })

if (service && id) {
logger.info(
`🔍 Finding episode info for ${payload.Metadata.grandparentTitle} (${payload.Metadata.year}) - ${payload.Metadata.parentTitle} - ${payload.Metadata.title} using ${service}://${id}`,
);
} else {
logger.error(`❌ ${chalk.red(`No GUID available`)}`);
return
}

try {
const response = await api.get(`/search/${service}/${id}?type=episode`, { ttl: 1000 * 60 * 180 });
logger.debug(`/search/${service}/${id}?type=episode`)
logger.debug(JSON.stringify(response.data, null, 2));
if (!response.data.length) {
logger.error(`❌ ${chalk.red(`Response was empty!`)}`);
return
const guids = GetGuids({ payload });

for (const { service, id } of guids) {
if (service && id) {
logger.info(
`🔍 Finding episode info for ${payload.Metadata.grandparentTitle} (${payload.Metadata.year}) - ${payload.Metadata.parentTitle} - ${payload.Metadata.title} using ${service}://${id}`,
)
try {
const response = await api.get(`https://api.trakt.tv/search/${service}/${id}?type=episode`, { ttl: 1000 * 60 * 180 });
logger.debug(JSON.stringify(response.data, null, 2));
if (response.data.length) {
const episode = response.data[0].episode;
const { title, season, number } = episode;
logger.info(`📺 Episode found: ${title} (Season ${season}, Episode ${number})`);
return episode;
} else {
logger.error(`❌ ${chalk.red(`Response from ${service} was empty!`)}`);
}
} catch (err) {
logger.error(`❌ ${chalk.red(`Search episode API error: ${err.message}`)}`);
}
} else {
logger.error(`❌ ${chalk.red(`No GUID available`)}`);
}
const { episode, show } = response.data[0];
logger.info(
`📺 Episode found: ${show.title} (${show.year}) - S${episode.season.toString().padStart(2, '0')}E${episode.number.toString().padStart(2, '0')} - ${episode.title}`,
);
return episode;
} catch (err) {
logger.error(`❌ ${chalk.red(`Search episode API error: ${err.message}`)}`);
}

logger.error(`❌ ${chalk.red(`No episode found for any GUIDs`)}`);
return null;
};


export const findShowRequest = async (payload) => {
const { service, id } = GetGuid({ payload })

if (service && id) {
logger.info(
`🔍 Finding show info for ${payload.Metadata.title} (${payload.Metadata.year}) using ${service}://${id}`,
);

} else {
logger.error(`❌ ${chalk.red(`No GUID available`)}`);
return
}

try {
const response = await api.get(`/search/${service}/${id}?type=show`, { ttl: 1000 * 60 * 180 });
logger.debug(JSON.stringify(payload, null, 2));
logger.debug(JSON.stringify(response.data, null, 2));
if (!response.data.length) {
logger.error(`❌ ${chalk.red(`Response was empty!`)}`);
return
const guids = GetGuids({ payload });

for (const { service, id } of guids) {
if (service && id) {
logger.info(`🔍 Finding show info for ${payload.Metadata.title} (${payload.Metadata.year}) using ${service}://${id}`);

try {
const response = await api.get(`https://api.trakt.tv/search/${service}/${id}?type=show`, { ttl: 1000 * 60 * 180 });
logger.debug(JSON.stringify(response.data, null, 2));
if (response.data.length) {
const show = response.data[0].show;
const { title, year } = show;
logger.info(`📺 Show found: ${title} (${year})`);
return show;
} else {
logger.error(`❌ ${chalk.red(`Response from ${service} was empty!`)}`);
}
} catch (err) {
logger.error(`❌ ${chalk.red(`Search show API error: ${err.message}`)}`);
}
} else {
logger.error(`❌ ${chalk.red(`No GUID available`)}`);
}
const { show } = response.data[0];
// logger.debug(JSON.stringify(show, null, 2));
logger.info(
`📺 Show found: ${show.title} (${show.year})`,
);
return show;
} catch (err) {
logger.error(`❌ ${chalk.red(`Search show API error: ${err.message}`)}`);
}
};

logger.error(`❌ ${chalk.red(`No show found for any GUIDs`)}`);
return null;
};

export const findSeasonRequest = async (payload) => {
logger.info(
Expand Down
13 changes: 8 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,21 @@ export const handleRatingMovie = async ({ payload }) => {
rateRequest({ body, title, rating });
};

export const GetGuid = ({ payload }) => {
export const GetGuids = ({ payload }) => {
const Guid = payload?.Metadata?.Guid;

if (!Guid) {
logger.error(`❌ ${chalk.red(`Couldn't find Guid`)}`);
return;
}

const service = Guid[0]?.id?.substring(0, 4)
const id = Guid[0]?.id?.substring(7)
const guids = Guid.map(guid => {
const service = guid?.id?.substring(0, 4);
const id = guid?.id?.substring(7);
return { service, id };
});

logger.debug(`service: ${service} | id: ${id}`);
logger.debug(`services and ids: ${JSON.stringify(guids)}`);

return { service, id }
return guids;
}

0 comments on commit 77bc72b

Please sign in to comment.