Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 118 additions & 65 deletions src/serializers/jsonapi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import camelCaseKeys, { CamelCaseKeys } from 'camelcase-keys';
import { defineSerializers } from '../xtream.ts';
import type { XtreamAudioInfo, XtreamCategory, XtreamSeason, XtreamVideoInfo } from '../types.ts';
import type {
XtreamAudioInfo,
XtreamCategory,
XtreamSeason,
XtreamVideoInfo,
} from '../types.ts';

/**
* JSON API serializers for the Xtream API
Expand All @@ -10,7 +15,14 @@ import type { XtreamAudioInfo, XtreamCategory, XtreamSeason, XtreamVideoInfo } f
*/
export const JSONAPISerializer = defineSerializers('JSON:API', {
profile: (input): { data: JSONAPIXtreamProfile } => {
const { auth, expDate, maxConnections, activeCons, createdAt, ...camelInput } = camelCaseKeys(input);
const {
auth,
expDate,
maxConnections,
activeCons,
createdAt,
...camelInput
} = camelCaseKeys(input);

return {
data: {
Expand Down Expand Up @@ -60,8 +72,18 @@ export const JSONAPISerializer = defineSerializers('JSON:API', {

return {
data: camelInput.map((channel) => {
const { added, num, streamId, categoryIds, streamIcon, epgChannelId, tvArchive, name, tvArchiveDuration, url } =
channel;
const {
added,
num,
streamId,
categoryIds,
streamIcon,
epgChannelId,
tvArchive,
name,
tvArchiveDuration,
url,
} = channel;

return {
type: 'channel',
Expand Down Expand Up @@ -184,7 +206,7 @@ export const JSONAPISerializer = defineSerializers('JSON:API', {
description,
plot,
informationUrl: kinopoiskUrl,
cover: backdropPath[0],
cover: backdropPath?.[0],
poster: movieImage,
duration: durationSecs,
durationFormatted: duration,
Expand Down Expand Up @@ -252,7 +274,7 @@ export const JSONAPISerializer = defineSerializers('JSON:API', {
genre: genre?.split(',').map((x) => x.trim()) ?? [],
voteAverage: Number(rating),
poster: cover,
cover: backdropPath[0],
cover: backdropPath?.[0],
duration: Number(episodeRunTime) * 60,
youtubeId: youtubeTrailer,
releaseDate: releaseDate ? new Date(releaseDate) : null,
Expand Down Expand Up @@ -306,46 +328,62 @@ export const JSONAPISerializer = defineSerializers('JSON:API', {

const flatEpisodes = Object.values(episodes).flat();

const episodesAsJSONAPI: JSONAPIXtreamEpisode[] = flatEpisodes.map((episode) => {
const { id, season, title, subtitles, url, episodeNum, added, info } = episode;
const episodesAsJSONAPI: JSONAPIXtreamEpisode[] = flatEpisodes.map(
(episode) => {
const { id, season, title, subtitles, url, episodeNum, added, info } =
episode;

const { releaseDate, rating, movieImage, coverBig, durationSecs, duration, tmdbId, plot, video, audio, bitrate } =
info;

const seasonId = seasons.find((x) => x.seasonNumber === season)?.id.toString() || season.toString();

return {
type: 'episode',
id,
attributes: {
number: Number(episodeNum),
title,
const {
releaseDate,
rating,
movieImage,
coverBig,
durationSecs,
duration,
tmdbId,
plot,
tmdbId: tmdbId?.toString(),
poster: movieImage,
cover: coverBig,
voteAverage: Number(rating),
duration: durationSecs,
durationFormatted: duration,
releaseDate: releaseDate ? new Date(releaseDate) : null,
createdAt: new Date(Number(added) * 1000),
subtitles,
url,
video,
audio,
bitrate,
},
relationships: {
season: {
data: { type: 'season', id: seasonId },
} = info;

const seasonId =
seasons.find((x) => x.seasonNumber === season)?.id.toString() ||
season.toString();

return {
type: 'episode',
id,
attributes: {
number: Number(episodeNum),
title,
plot,
tmdbId: tmdbId?.toString(),
poster: movieImage,
cover: coverBig,
voteAverage: Number(rating),
duration: durationSecs,
durationFormatted: duration,
releaseDate: releaseDate ? new Date(releaseDate) : null,
createdAt: new Date(Number(added) * 1000),
subtitles,
url,
video,
audio,
bitrate,
},
relationships: {
season: {
data: { type: 'season', id: seasonId },
},

show: {
data: { type: 'show', id: seriesId.toString() },
show: {
data: { type: 'show', id: seriesId.toString() },
},
},
},
} satisfies JSONAPIXtreamEpisode;
});
} satisfies JSONAPIXtreamEpisode;
},
);

let seasonsToMap = seasons;
if (seasonsToMap.length === 0) {
Expand All @@ -367,33 +405,36 @@ export const JSONAPISerializer = defineSerializers('JSON:API', {
});
}

const seasonsAsJSONAPI: JSONAPIXtreamSeason[] = seasonsToMap.map((season) => {
const { id, seasonNumber, cover, coverBig, airDate, ...restSeason } = season;
const seasonsAsJSONAPI: JSONAPIXtreamSeason[] = seasonsToMap.map(
(season) => {
const { id, seasonNumber, cover, coverBig, airDate, ...restSeason } =
season;

return {
type: 'season',
id: id.toString(),
attributes: {
...restSeason,
releaseDate: airDate ? new Date(airDate) : null,
number: seasonNumber,
cover: coverBig,
},
relationships: {
show: {
data: { type: 'show', id: seriesId.toString() },
return {
type: 'season',
id: id.toString(),
attributes: {
...restSeason,
releaseDate: airDate ? new Date(airDate) : null,
number: seasonNumber,
cover: coverBig,
},
episodes: {
data: flatEpisodes
.filter((episode) => episode.season === seasonNumber)
.map((episode) => ({
type: 'episode',
id: episode.id.toString(),
})),
relationships: {
show: {
data: { type: 'show', id: seriesId.toString() },
},
episodes: {
data: flatEpisodes
.filter((episode) => episode.season === seasonNumber)
.map((episode) => ({
type: 'episode',
id: episode.id.toString(),
})),
},
},
},
} satisfies JSONAPIXtreamSeason;
});
} satisfies JSONAPIXtreamSeason;
},
);

return {
data: {
Expand All @@ -404,7 +445,7 @@ export const JSONAPISerializer = defineSerializers('JSON:API', {
plot,
voteAverage: Number(rating),
poster: cover,
cover: backdropPath[0],
cover: backdropPath?.[0],
duration: Number(episodeRunTime) * 60,
cast: cast?.split(',').map((x) => x.trim()) ?? [],
director: director?.split(',').map((x) => x.trim()) ?? [],
Expand Down Expand Up @@ -443,7 +484,8 @@ export const JSONAPISerializer = defineSerializers('JSON:API', {

return {
data: epgListings.map((listing) => {
const { id, channelId, lang, start, end, title, description, epgId } = listing;
const { id, channelId, lang, start, end, title, description, epgId } =
listing;

return {
type: 'epg-listing',
Expand Down Expand Up @@ -474,7 +516,18 @@ export const JSONAPISerializer = defineSerializers('JSON:API', {

return {
data: epgListings.map((listing) => {
const { id, channelId, start, end, title, description, nowPlaying, hasArchive, lang, epgId } = listing;
const {
id,
channelId,
start,
end,
title,
description,
nowPlaying,
hasArchive,
lang,
epgId,
} = listing;

return {
type: 'epg-listing',
Expand Down
8 changes: 4 additions & 4 deletions src/serializers/standardized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const standardizedSerializer = defineSerializers('Standardized', {
country,
informationUrl: kinopoiskUrl,
originalName: oName,
cover: backdropPath[0],
cover: backdropPath?.[0],
poster: movieImage,
duration: durationSecs,
durationFormatted: duration,
Expand Down Expand Up @@ -196,7 +196,7 @@ export const standardizedSerializer = defineSerializers('Standardized', {
genre: genre?.split(',').map((x) => x.trim()) ?? [],
voteAverage: Number(rating),
poster: cover,
cover: backdropPath[0],
cover: backdropPath?.[0],
duration: Number(episodeRunTime) * 60,
releaseDate: releaseDate ? new Date(releaseDate) : null,
updatedAt: new Date(Number(lastModified) * 1000),
Expand Down Expand Up @@ -308,7 +308,7 @@ export const standardizedSerializer = defineSerializers('Standardized', {
plot,
voteAverage: Number(rating),
poster: cover,
cover: backdropPath[0],
cover: backdropPath?.[0],
duration: Number(episodeRunTime) * 60,
cast: cast?.split(',').map((x) => x.trim()) ?? [],
director: director?.split(',').map((x) => x.trim()) ?? [],
Expand Down Expand Up @@ -499,7 +499,7 @@ export type StandardXtreamMovieListing = {
/** The date when the movie was added to the system */
createdAt: Date;
/** All category IDs the movie belongs to */
categoryIds?: string[];
categoryIds: string[];
/** URL to access the stream */
url?: string;
};
Expand Down
Loading