Skip to content

Commit

Permalink
chore: update series transformers and mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
wax911 committed Nov 24, 2024
1 parent f57dbeb commit 8d3385b
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 200 deletions.
53 changes: 39 additions & 14 deletions src/series/repository/season.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,38 @@ export default class SeasonRepository {
private mergeEpisodes = (
tmdbEpisodes: TmdbEpisode[],
skyhookEpisodes: SkyhookEpisode[],
): MergedEpisode[] =>
zip(tmdbEpisodes, skyhookEpisodes).map((data) =>
deepmerge(data[0], data[1]) as MergedEpisode
);
): MergedEpisode[] => {
const zipped = zip(tmdbEpisodes, skyhookEpisodes).map((
[tmdbEpisode, skyhookEpisode],
) => ({
id: tmdbEpisode.id,
tvdbShowId: skyhookEpisode.tvdbShowId,
tvdbId: skyhookEpisode.tvdbId,
tmdbShowId: tmdbEpisode.show_id,
seasonNumber: skyhookEpisode.seasonNumber ?? tmdbEpisode.season_number,
episodeNumber: skyhookEpisode.episodeNumber ?? tmdbEpisode.episode_number,
absoluteEpisodeNumber: skyhookEpisode.absoluteEpisodeNumber,
airedBeforeSeasonNumber: skyhookEpisode.airedBeforeSeasonNumber,
airedBeforeEpisodeNumber: skyhookEpisode.airedBeforeEpisodeNumber,
airedAfterSeasonNumber: skyhookEpisode.airedAfterSeasonNumber,
airedAfterEpisodeNumber: skyhookEpisode.airedAfterEpisodeNumber,
title: skyhookEpisode.title,
airDate: skyhookEpisode.airDate ?? tmdbEpisode.air_date,
airDateUtc: skyhookEpisode.airDateUtc,
runtime: skyhookEpisode.runtime ?? tmdbEpisode.runtime ?? 0,
finaleType: skyhookEpisode.finaleType,
overview: skyhookEpisode.overview ?? tmdbEpisode.overview,
image: skyhookEpisode.image,
name: tmdbEpisode.name,
productionCode: tmdbEpisode.production_code,
stillPath: tmdbEpisode.still_path,
voteAverage: tmdbEpisode.vote_average,
voteCount: tmdbEpisode.vote_count,
crew: tmdbEpisode.crew,
guestStars: tmdbEpisode.guest_stars,
}));
return zipped;
};

private mergeSeasons = (
tmdbSeasons: TmdbSeason[],
Expand All @@ -38,20 +66,17 @@ export default class SeasonRepository {
.map((data) => deepmerge(data[0], data[1]))
.map((mergedSeason) => ({
...mergedSeason,
episodes: Array.isArray(mergedSeason?.episodes)
? this.mergeEpisodes(mergedSeason!.episodes, skyhookEpisodes)
episodes: mergedSeason?.episodes
? this.mergeEpisodes(
mergedSeason.episodes,
skyhookEpisodes.filter((episode) =>
episode.seasonNumber == mergedSeason.season_number
),
)
: [],
})) as MergedSeason[];
};

private mergeSeasonWithAnimeIds = (
_mergedSeasons: MergedSeason[],
_relations: AnimeRelationId[],
): MergedSeason[] => {
// find relationships between mergedSeason[x].tmdbId and relations[x].themoviedb
throw new Error('Not implemented');
};

getSeasons = async (
notify?: NotifyAnime,
skyhook?: SkyhookShow,
Expand Down
4 changes: 2 additions & 2 deletions src/series/service/arm/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface AnimeRelationId {
export type AnimeRelationId = {
anidb?: number;
anilist?: number;
animePlanet?: string;
Expand All @@ -10,4 +10,4 @@ export interface AnimeRelationId {
themoviedb?: number;
thetvdb?: number;
myanimelist?: number;
}
};
25 changes: 21 additions & 4 deletions src/series/service/skyhook/remote/types.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
interface ActorModel {
name: string;
character: string;
image?: string;
}

interface EpisodeModel {
export interface EpisodeModel {
tvdbShowId: number;
tvdbId: number;
seasonNumber: number;
episodeNumber: number;
absoluteEpisodeNumber?: number;
airedBeforeSeasonNumber?: number;
airedBeforeEpisodeNumber?: number;
airedAfterSeasonNumber?: number;
airedAfterEpisodeNumber?: number;
title?: string;
airDate: Date;
airDateUtc: Date;
runtime?: number;
finaleType?: string | 'season';
overview?: string;
image?: string;
}

interface ImageModel {
coverType: string | 'Banner' | 'Poster' | 'Fanart';
coverType: string | 'Banner' | 'Poster' | 'Fanart' | 'Clearlogo';
url: string;
}

Expand All @@ -35,24 +39,37 @@ interface TimeOfDayModel {
minutes: number;
}

export interface SkyhookModel {
interface RatingModel {
count: number,
value: string
}

interface AlternativeTitlesModel {
title: string
}

export type SkyhookModel = {
tvdbId: number;
title: string;
overview: string;
slug: string;
originalCountry: string;
originalLanguage: string;
language: string;
firstAired: Date;
lastAired: Date;
tvMazeId: number;
lastUpdated: Date;
status: string;
runtime: number;
timeOfDay: TimeOfDayModel;
originalNetwork: string;
network: string;
imdbId: string;
genres: string[];
contentRating: string;
rating?: RatingModel;
alternativeTitles?: AlternativeTitlesModel;
imdbId: string;
actors: ActorModel[];
images: ImageModel[];
seasons: SeasonModel[];
Expand Down
30 changes: 4 additions & 26 deletions src/series/service/skyhook/transformer/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
import { Transform } from '../../../../common/transformer/types.ts';
import { SkyhookModel } from '../remote/types.ts';
import { Show } from './types.ts';
import { toInstant } from '../../../../common/helpers/date.ts';
import { SkyhookShow } from '../types.ts';

export const transform: Transform<SkyhookModel, Show> = (
export const transform: Transform<SkyhookModel, SkyhookShow> = (
sourceData,
) => ({
tvdbId: sourceData.tvdbId,
tvMazeId: sourceData.tvMazeId,
title: sourceData.title,
overview: sourceData.overview,
slug: sourceData.slug,
...sourceData,
firstAired: toInstant(sourceData.firstAired),
lastUpdated: toInstant(sourceData.lastUpdated),
status: sourceData.status,
runtime: sourceData.runtime,
originalNetwork: sourceData.originalNetwork,
network: sourceData.network,
imdbId: sourceData.imdbId,
contentRating: sourceData.contentRating,
banner: sourceData.images.find((item) => item.coverType == 'Banner')?.url,
poster: sourceData.images.find((item) => item.coverType == 'Poster')?.url,
fanart: sourceData.images.find((item) => item.coverType == 'Fanart')?.url,
seasons: sourceData.seasons.map((season) => ({
...season,
poster: season?.images?.find((item) => item.coverType == 'Poster')?.url,
seasonNumber: season.seasonNumber,
})),
episodes: sourceData.episodes.map((episode) => ({
tvdbShowId: episode.tvdbShowId,
tvdbId: episode.tvdbId,
seasonNumber: episode.seasonNumber,
episodeNumber: episode.episodeNumber,
absoluteEpisodeNumber: episode.absoluteEpisodeNumber,
title: episode.title,
airDate: toInstant(episode.airDateUtc),
runtime: episode.runtime,
overview: episode.overview,
image: episode.image,
})),
});
37 changes: 1 addition & 36 deletions src/series/service/skyhook/transformer/types.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,9 @@
import { Instant } from '../../../../common/helpers/date.ts';

interface Episode {
tvdbShowId: number;
tvdbId: number;
seasonNumber: number;
episodeNumber: number;
absoluteEpisodeNumber?: number;
airedBeforeSeasonNumber?: number;
airedBeforeEpisodeNumber?: number;
title?: string;
airDate: Instant;
runtime?: number;
overview?: string;
image?: string;
}

interface Season {
seasonNumber: number;
poster?: string;
banner?: string;
fanart?: string;
}

export interface Show {
tvdbId: number;
tvMazeId: number;
title: string;
overview: string;
slug: string;
export type Show = {
firstAired: Instant;
lastUpdated: Instant;
status: string;
runtime: number;
originalNetwork: string;
network: string;
imdbId: string;
contentRating: string;
banner?: string;
poster?: string;
fanart?: string;
seasons: Season[];
episodes: Episode[];
}
46 changes: 4 additions & 42 deletions src/series/service/skyhook/types.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,6 @@
import { Instant } from '../../../common/helpers/date.ts';
import { EpisodeModel, SkyhookModel } from './remote/types.ts';
import { Show } from './transformer/types.ts';

export interface SkyhookEpisode {
tvdbShowId: number;
tvdbId: number;
seasonNumber: number;
episodeNumber: number;
absoluteEpisodeNumber?: number;
airedBeforeSeasonNumber?: number;
airedBeforeEpisodeNumber?: number;
title?: string;
airDate: Instant;
runtime?: number;
overview?: string;
image?: string;
}
export type SkyhookEpisode = EpisodeModel

interface SkyhookSeason {
seasonNumber: number;
poster?: string;
banner?: string;
fanart?: string;
}

export interface SkyhookShow {
tvdbId: number;
tvMazeId: number;
title: string;
overview: string;
slug: string;
firstAired: Instant;
lastUpdated: Instant;
status: string;
runtime: number;
originalNetwork: string;
network: string;
imdbId: string;
contentRating: string;
banner?: string;
poster?: string;
fanart?: string;
seasons: SkyhookSeason[];
episodes: SkyhookEpisode[];
}
export type SkyhookShow = Omit<SkyhookModel, 'firstAired' | 'lastUpdated'> & Show
4 changes: 2 additions & 2 deletions src/series/service/tmdb/remote/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface Season {
overview: string;
poster_path: string | null;
season_number: number;
episodes: Episode | null;
episodes: Episode[] | null;
images: Images | null;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ interface Episode {
name: string;
overview: string;
production_code: string;
runtime: string;
runtime: number;
season_number: number;
show_id: string;
still_path: string;
Expand Down
4 changes: 2 additions & 2 deletions src/series/service/tmdb/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface TmdbSeason {
overview: string;
poster_path: string | null;
season_number: number;
episodes: TmdbEpisode | null;
episodes: TmdbEpisode[] | null;
images: TmdbImages | null;
}

Expand Down Expand Up @@ -90,7 +90,7 @@ export interface TmdbEpisode {
name: string;
overview: string;
production_code: string;
runtime: string;
runtime: number;
season_number: number;
show_id: string;
still_path: string;
Expand Down
36 changes: 18 additions & 18 deletions src/series/transformer/season.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const transformImage = (
height: data.height,
width: data.width,
url: provider.getUrl(data, imageType),
}));
})) ?? [];

const transformCrew = (crew: TmdbCrew): SeriesEpisodeCrew => ({
job: crew.job ?? null,
department: crew.department ?? null,
job: crew.job,
department: crew.department,
creditId: crew.credit_id,
adult: crew.adult,
id: crew.id,
Expand All @@ -32,30 +32,30 @@ const transformCrew = (crew: TmdbCrew): SeriesEpisodeCrew => ({
originalName: crew.original_name,
popularity: crew.popularity,
image: provider.getImageUrl('original', crew.profile_path),
character: crew.character ?? null,
order: crew.order ?? null,
character: crew.character,
order: crew.order,
});

const transformEpisode = (episode: MergedEpisode): SeriesEpisode => ({
id: episode.id,
tvdbShowId: episode.tvdbShowId,
tvdbId: episode.tvdbId,
tmdbId: episode.id,
seasonNumber: episode.seasonNumber,
episodeNumber: episode.episodeNumber,
absoluteEpisodeNumber: episode.absoluteEpisodeNumber ?? null,
airedBeforeSeasonNumber: episode.airedBeforeSeasonNumber ?? null,
airedBeforeEpisodeNumber: episode.airedBeforeEpisodeNumber ?? null,
title: episode.title ?? null,
airDate: episode.airDate,
runtime: Number(episode.runtime) ?? 0,
overview: episode.overview ?? null,
image: episode.image ?? null,
absoluteEpisodeNumber: episode.absoluteEpisodeNumber,
airedBeforeSeasonNumber: episode.airedBeforeSeasonNumber,
airedBeforeEpisodeNumber: episode.airedBeforeEpisodeNumber,
airedAfterSeasonNumber: episode.airedAfterSeasonNumber,
airedAfterEpisodeNumber: episode.airedAfterEpisodeNumber,
title: episode.title,
airDate: episode.airDateUtc,
runtime: episode.runtime,
overview: episode.overview,
image: episode.image,
name: episode.name,
poster: episode.still_path != null
? provider.getImageUrl('original', episode.still_path)
: null,
poster: provider.getImageUrl('original', episode.stillPath),
crew: episode.crew.map(transformCrew),
guests: episode.guest_stars.map(transformCrew),
guests: episode.guestStars.map(transformCrew),
});

export const seasonTransformer = (
Expand Down
Loading

0 comments on commit 8d3385b

Please sign in to comment.