From 3c295d643110b1bb76efec750a28b29254f346c3 Mon Sep 17 00:00:00 2001 From: Mateus Souza Date: Sat, 10 Feb 2024 12:48:30 -0300 Subject: [PATCH] Refactor: improve metadata reading with alternative episode number reference --- src/scripts/history.ts | 15 ++++++++------- src/scripts/home.ts | 30 ++++++++++++++++-------------- src/scripts/serie.ts | 2 +- src/scripts/video.ts | 25 ++++++++++++++----------- src/scripts/watchlist.ts | 15 ++++++++------- 5 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/scripts/history.ts b/src/scripts/history.ts index c67aa0f..6a84fa4 100644 --- a/src/scripts/history.ts +++ b/src/scripts/history.ts @@ -47,19 +47,20 @@ const listHistory: Callback = async ({ state, render }) => { const items = response.data.filter((item) => { return item.panel.type === 'episode' }).map((item) => { + const metadata = item.panel.episode_metadata return { id: item.panel.id, image: App.getImage(item.panel.images.thumbnail).source, - number: item.panel.episode_metadata.episode_number, + number: metadata.episode_number || metadata.episode, name: item.panel.title, description: item.panel.description, - duration: item.panel.episode_metadata.duration_ms / 1000, + duration: metadata.duration_ms / 1000, playhead: item.playhead, - premium: item.panel.episode_metadata.is_premium_only, - season_id: item.panel.episode_metadata.season_id, - season_name: item.panel.episode_metadata.season_title, - serie_id: item.panel.episode_metadata.series_id, - serie_name: item.panel.episode_metadata.series_title, + premium: metadata.is_premium_only, + season_id: metadata.season_id, + season_name: metadata.season_title, + serie_id: metadata.series_id, + serie_name: metadata.series_title, } }) diff --git a/src/scripts/home.ts b/src/scripts/home.ts index 9cfda72..bb24214 100644 --- a/src/scripts/home.ts +++ b/src/scripts/home.ts @@ -64,37 +64,39 @@ const listResults: Callback = async ({ state, render }) => { const history = (historyResponse.data || []).filter((item) => { return item.panel.type === 'episode' }).map((item) => { + const metadata = item.panel.episode_metadata return { id: item.panel.id, image: App.getImage(item.panel.images.thumbnail).source, - number: item.panel.episode_metadata.episode_number, + number: metadata.episode_number || metadata.episode, name: item.panel.title, description: item.panel.description, - duration: item.panel.episode_metadata.duration_ms / 1000, + duration: metadata.duration_ms / 1000, playhead: item.playhead, - premium: item.panel.episode_metadata.is_premium_only, - season_id: item.panel.episode_metadata.season_id, - season_name: item.panel.episode_metadata.season_title, - serie_id: item.panel.episode_metadata.series_id, - serie_name: item.panel.episode_metadata.series_title, + premium: metadata.is_premium_only, + season_id: metadata.season_id, + season_name: metadata.season_title, + serie_id: metadata.series_id, + serie_name: metadata.series_title, } }) const watchlistResponse = await App.watchlist({'n': '4'}) const watchlist = (watchlistResponse.data || []).map((item) => { + const metadata = item.panel.episode_metadata return { id: item.panel.id, image: App.getImage(item.panel.images.thumbnail).source, - number: item.panel.episode_metadata.episode_number, + number: metadata.episode_number || metadata.episode, name: item.panel.title, description: item.panel.description, - duration: item.panel.episode_metadata.duration_ms / 1000, + duration: metadata.duration_ms / 1000, playhead: item.playhead, - premium: item.panel.episode_metadata.is_premium_only, - season_id: item.panel.episode_metadata.season_id, - season_name: item.panel.episode_metadata.season_title, - serie_id: item.panel.episode_metadata.series_id, - serie_name: item.panel.episode_metadata.series_title, + premium: metadata.is_premium_only, + season_id: metadata.season_id, + season_name: metadata.season_title, + serie_id: metadata.series_id, + serie_name: metadata.series_title, } }) diff --git a/src/scripts/serie.ts b/src/scripts/serie.ts index 7e0d5a1..0486e40 100644 --- a/src/scripts/serie.ts +++ b/src/scripts/serie.ts @@ -170,7 +170,7 @@ const listEpisodes: Callback = async ({ state, render }) => { return { id: item.id, image: App.getImage(item.images.thumbnail).source, - number: item.episode_number, + number: item.episode_number || item.episode, name: item.title, description: item.description, duration: item.duration_ms / 1000, diff --git a/src/scripts/video.ts b/src/scripts/video.ts index a91e4bc..314bc51 100644 --- a/src/scripts/video.ts +++ b/src/scripts/video.ts @@ -100,10 +100,11 @@ const loadEpisode: Callback = async ({ state }) => { const episodeId = state.episodeId const episodeResponse = await App.episode(episodeId, {}) const episodeInfo = episodeResponse.data[0] + const episodeMetadata = episodeInfo.episode_metadata - const serieName = episodeInfo.episode_metadata.series_title - const seasonNumber = episodeInfo.episode_metadata.season_number - const episodeNumber = Number(episodeInfo.episode_metadata.episode_number) + const serieName = episodeMetadata.series_title + const seasonNumber = episodeMetadata.season_number + const episodeNumber = episodeMetadata.episode_number || episodeMetadata.episode const episodeName = episodeInfo.title const streamsLink = String(episodeInfo.streams_link) @@ -140,11 +141,12 @@ const loadClosestEpisodes: Callback = async ({ state }) => { if( previousResponse.data && previousResponse.data.length ){ const item = previousResponse.data[0].panel - const serieId = item.episode_metadata.series_id - const seasonId = item.episode_metadata.season_id + const metadata = item.episode_metadata + const serieId = metadata.series_id + const seasonId = metadata.season_id const episodeId = item.id - const seasonNumber = item.episode_metadata.season_number - const episodeNumber = item.episode_metadata.episode_number + const seasonNumber = metadata.season_number + const episodeNumber = metadata.episode_number || metadata.episode const episodeUrl = '/serie/' + serieId + '/season/' + seasonId + '/episode/' + episodeId + '/video' previous.dataset.url = episodeUrl @@ -157,11 +159,12 @@ const loadClosestEpisodes: Callback = async ({ state }) => { if( nextResponse.data && nextResponse.data.length ){ const item = nextResponse.data[0].panel - const serieId = item.episode_metadata.series_id - const seasonId = item.episode_metadata.season_id + const metadata = item.episode_metadata + const serieId = metadata.series_id + const seasonId = metadata.season_id const episodeId = item.id - const seasonNumber = item.episode_metadata.season_number - const episodeNumber = item.episode_metadata.episode_number + const seasonNumber = metadata.season_number + const episodeNumber = metadata.episode_number || metadata.episode const episodeUrl = '/serie/' + serieId + '/season/' + seasonId + '/episode/' + episodeId + '/video' next.dataset.url = episodeUrl diff --git a/src/scripts/watchlist.ts b/src/scripts/watchlist.ts index deb281c..aba1ef8 100644 --- a/src/scripts/watchlist.ts +++ b/src/scripts/watchlist.ts @@ -47,19 +47,20 @@ const listWatchlist: Callback = async ({ state, render }) => { }) const items = response.data.map((item) => { + const metadata = item.panel.episode_metadata return { id: item.panel.id, image: App.getImage(item.panel.images.thumbnail).source, - number: item.panel.episode_metadata.episode_number, + number: metadata.episode_number || metadata.episode, name: item.panel.title, description: item.panel.description, - duration: item.panel.episode_metadata.duration_ms / 1000, + duration: metadata.duration_ms / 1000, playhead: item.playhead, - premium: item.panel.episode_metadata.is_premium_only, - season_id: item.panel.episode_metadata.season_id, - season_name: item.panel.episode_metadata.season_title, - serie_id: item.panel.episode_metadata.series_id, - serie_name: item.panel.episode_metadata.series_title, + premium: metadata.is_premium_only, + season_id: metadata.season_id, + season_name: metadata.season_title, + serie_id: metadata.series_id, + serie_name: metadata.series_title, } })