From 32fb607f8adad7e79ba402a0cf3b4989d2cc8a79 Mon Sep 17 00:00:00 2001 From: stratuma Date: Tue, 25 Jun 2024 01:23:52 +0200 Subject: [PATCH] final better hardsub select --- pages/index.vue | 5 +- src/api/db/database.ts | 22 +++---- src/api/routes/service/service.controller.ts | 8 +-- src/api/routes/service/service.service.ts | 61 ++++++++++++++++---- 4 files changed, 68 insertions(+), 28 deletions(-) diff --git a/pages/index.vue b/pages/index.vue index 4f79fba..c3af76b 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -130,7 +130,8 @@
{{ audioQualities[p.qualityaudio-1] ?? '44.10 kHz' }}
{{ p.format }}
Dubs: {{ p.dub.map((t) => t.name).join(', ') }}
-
Subs: {{ p.sub.length !== 0 ? p.sub.map((t) => t.name).join(', ') : '-' }}
+
Subs: {{ p.sub.length !== 0 ? p.sub.map((t) => t.name).join(', ') : '-' }}
+
Hardsub: {{ p.hardsub ? `${p.hardsub.name} (${p.hardsub.format})` : '-' }}
{{ (p.totaldownloaded / Math.pow(1024, 2)).toFixed(2) }} MB
sub: Array<{ locale: string; name: string }> + hardsub: { name: string | undefined; locale: string, format: string } dir: string installDir: string partsleft: number @@ -192,6 +194,7 @@ const getPlaylist = async () => { media: CrunchyEpisode | ADNEpisode dub: Array<{ locale: string; name: string }> sub: Array<{ locale: string; name: string }> + hardsub: { name: string | undefined; locale: string, format: string } dir: string installDir: string partsleft: number diff --git a/src/api/db/database.ts b/src/api/db/database.ts index 9f1ece2..a27dad8 100644 --- a/src/api/db/database.ts +++ b/src/api/db/database.ts @@ -5,7 +5,7 @@ import { ADNEpisode } from '../types/adn' const sequelize = new Sequelize({ dialect: 'sqlite', - storage: app.getPath('documents') + '/Crunchyroll Downloader/databases/v2/data.db' + storage: app.getPath('documents') + '/Crunchyroll Downloader/databases/v3/data.db' }) interface AccountAttributes { @@ -44,9 +44,9 @@ interface PlaylistAttributes { | 'completed' | 'failed' media: CrunchyEpisode | ADNEpisode - dub: Array - sub: Array - hardsub: boolean + dub: { name: string | undefined; locale: string }[] + sub: { name: string | undefined; locale: string }[] + hardsub: { name: string | undefined; locale: string, format: string } quality: 1080 | 720 | 480 | 360 | 240 qualityaudio: 1 | 2 | 3 | undefined dir: string @@ -58,12 +58,12 @@ interface PlaylistAttributes { interface PlaylistCreateAttributes { media: CrunchyEpisode | ADNEpisode - dub: Array - sub: Array + dub: { name: string | undefined; locale: string }[] + sub: { name: string | undefined; locale: string }[] + hardsub: { name: string | undefined; locale: string, format: string } | undefined dir: string quality: 1080 | 720 | 480 | 360 | 240 qualityaudio: 1 | 2 | 3 | undefined - hardsub: boolean status: | 'waiting' | 'preparing' @@ -134,6 +134,10 @@ const Playlist: ModelDefined = seq allowNull: false, type: DataTypes.JSON }, + hardsub: { + allowNull: true, + type: DataTypes.JSON + }, dir: { allowNull: false, type: DataTypes.STRING @@ -142,10 +146,6 @@ const Playlist: ModelDefined = seq allowNull: true, type: DataTypes.STRING }, - hardsub: { - allowNull: false, - type: DataTypes.BOOLEAN - }, failedreason: { allowNull: true, type: DataTypes.STRING diff --git a/src/api/routes/service/service.controller.ts b/src/api/routes/service/service.controller.ts index 2aa847a..ecb1451 100644 --- a/src/api/routes/service/service.controller.ts +++ b/src/api/routes/service/service.controller.ts @@ -96,10 +96,10 @@ export async function addPlaylistController( request: FastifyRequest<{ Body: { episodes: CrunchyEpisodes - dubs: Array - subs: Array + dubs: { name: string | undefined; locale: string }[] + subs: { name: string | undefined; locale: string }[] + hardsub: { name: string | undefined; locale: string, format: string } | undefined dir: string - hardsub: boolean quality: 1080 | 720 | 480 | 360 | 240 qualityaudio: 1 | 2 | 3 | undefined service: 'CR' | 'ADN' @@ -111,7 +111,7 @@ export async function addPlaylistController( const body = request.body for (const e of body.episodes) { - await addEpisodeToPlaylist(e, body.subs, body.dubs, body.dir, body.hardsub, 'waiting', body.quality, body.qualityaudio, body.service, body.format) + await addEpisodeToPlaylist(e, body.subs, body.dubs, body.hardsub, body.dir, 'waiting', body.quality, body.qualityaudio, body.service, body.format) } return reply.code(201).send() diff --git a/src/api/routes/service/service.service.ts b/src/api/routes/service/service.service.ts index 53ff998..a0f0d2f 100644 --- a/src/api/routes/service/service.service.ts +++ b/src/api/routes/service/service.service.ts @@ -209,10 +209,10 @@ export async function updatePlaylistByID( // Add Episode to Playlist export async function addEpisodeToPlaylist( e: CrunchyEpisode, - s: Array, - d: Array, + s: { name: string | undefined; locale: string }[], + d: { name: string | undefined; locale: string }[], + hardsub: { name: string | undefined; locale: string, format: string } | undefined, dir: string, - hardsub: boolean, status: | 'waiting' | 'preparing' @@ -295,8 +295,8 @@ async function checkPlaylists() { if (e.dataValues.service === 'CR') { downloadCrunchyrollPlaylist( (e.dataValues.media as CrunchyEpisode).id, - (e as any).dataValues.dub.map((s: { locale: any }) => s.locale), - (e as any).dataValues.sub.map((s: { locale: any }) => s.locale), + e.dataValues.dub.map((s: { locale: any }) => s.locale), + e.dataValues.sub.map((s: { locale: any }) => s.locale), e.dataValues.hardsub, (e.dataValues.media as CrunchyEpisode).id, e.dataValues.id, @@ -471,7 +471,7 @@ export async function downloadCrunchyrollPlaylist( e: string, dubs: Array, subs: Array, - hardsub: boolean, + hardsub: { name: string | undefined; locale: string; format: string; }, episodeID: string, downloadID: number, name: string, @@ -926,7 +926,7 @@ export async function downloadCrunchyrollPlaylist( return } - const play = await crunchyGetPlaylist(code, geo) + var play = await crunchyGetPlaylist(code, geo) if (!play) { await updatePlaylistByID(downloadID, 'failed') @@ -945,24 +945,61 @@ export async function downloadCrunchyrollPlaylist( var downloadGEO - if (hardsub) { - const hardsubURL = play.data.hardSubs.find((h) => h.hlang === subs[0])?.url + if (hardsub && hardsub.locale) { + var hardsubURL: string | undefined; - const hardsubGEO = play.data.hardSubs.find((h) => h.hlang === subs[0])?.geo + var hardsubGEO: string | undefined;; + + if (hardsub.format === 'dub') { + const found = play.data.versions.find((h) => h.audio_locale === hardsub.locale) + if (!found) { + hardsubURL = undefined + } else { + const newplay = await crunchyGetPlaylist(found.guid, found.geo) + if (!newplay) { + hardsubURL = undefined + hardsubGEO = undefined + } else { + hardsubURL = newplay.data.hardSubs.find((h) => h.hlang === hardsub.locale)?.url + hardsubGEO = newplay.data.hardSubs.find((h) => h.hlang === subs[0])?.geo + } + } + } + + if (hardsub.format === 'sub') { + const found = play.data.versions.find((h) => h.audio_locale === 'ja-JP') + if (!found) { + hardsubURL = undefined + } else { + const newplay = await crunchyGetPlaylist(found.guid, found.geo) + if (!newplay) { + hardsubURL = undefined + hardsubGEO = undefined + } else { + hardsubURL = newplay.data.hardSubs.find((h) => h.hlang === hardsub.locale)?.url + hardsubGEO = newplay.data.hardSubs.find((h) => h.hlang === subs[0])?.geo + } + } + } if (hardsubURL) { downloadURL = hardsubURL downloadGEO = hardsubGEO - console.log('Hardsub Playlist found') } else { downloadURL = play.data.url downloadGEO = play.data.geo console.log('Hardsub Playlist not found') + messageBox('warning', ['Cancel'], 2, 'Hardsub Playlist not found', 'Hardsub Playlist not found', `${hardsub.locale} Hardsub Playlist not found, downloading japanese playlist instead.`) + server.logger.log({ + level: 'error', + message: `${hardsub.locale} Hardsub Playlist not found, downloading japanese playlist instead.`, + timestamp: new Date().toISOString(), + section: 'crunchyrollDownloadProcessVideo' + }) } } else { downloadURL = play.data.url downloadGEO = play.data.geo - console.log('Hardsub disabled, skipping') } var mdp = await crunchyGetPlaylistMPD(downloadURL, downloadGEO)