@serifpersia/dango 3.3.8 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/client/dist/assets/{AnimeCard-D4qo4289.js → AnimeCard-CJy1SXH3.js} +1 -1
- package/client/dist/assets/{AnimeInfo-BdRRm4So.js → AnimeInfo-BoXlVvZ-.js} +1 -1
- package/client/dist/assets/{AnimeInfoPage-DeJNAKMR.js → AnimeInfoPage-BxbA_7AV.js} +2 -2
- package/client/dist/assets/Asmr--Zc8G9zv.js +2 -0
- package/client/dist/assets/AsmrResetProgressModal-CKYm-8cp.js +1 -0
- package/client/dist/assets/AvSyncCalibrator-3yjZS7s6.js +10 -0
- package/client/dist/assets/Home-rrhOGQcg.js +1 -0
- package/client/dist/assets/Insights-Bay90Yeg.js +1 -0
- package/client/dist/assets/{LibraryListPage-D3hTqTRU.js → LibraryListPage-HzuDavbb.js} +1 -1
- package/client/dist/assets/{ListeningList-BgWsfu40.js → ListeningList-DYcQnnsG.js} +1 -1
- package/client/dist/assets/{Manga-Dn-CYins.js → Manga-D8CT9aO0.js} +1 -1
- package/client/dist/assets/{MangaInfoPage-EmQoK7Bv.js → MangaInfoPage-DGK6CYp1.js} +1 -1
- package/client/dist/assets/MangaReadPage-B9XoDKF9.js +1 -0
- package/client/dist/assets/{MangaResetProgressModal-Dian37PQ.js → MangaResetProgressModal-QjZyIAjl.js} +1 -1
- package/client/dist/assets/Mature-CHt_-Hz5.js +1 -0
- package/client/dist/assets/MediaCard-B5584Cq9.js +1 -0
- package/client/dist/assets/Music-HeLrEZ2d.js +1 -0
- package/client/dist/assets/OptionListMenu-CJ90CdwI.js +1 -0
- package/client/dist/assets/Player-BYtEax_z.js +56 -0
- package/client/dist/assets/PlayerSettings-4-cUAY4B.js +1 -0
- package/client/dist/assets/QueueOptionsButton-Ba0dg_82.js +1 -0
- package/client/dist/assets/{QueueRail-B7DeSFoA.js → QueueRail-O7f4RqjB.js} +1 -1
- package/client/dist/assets/Radio-DPZxQnyD.js +1 -0
- package/client/dist/assets/{ReadingList-BT8kUjCB.js → ReadingList-n6rr8ygy.js} +1 -1
- package/client/dist/assets/{ResetProgressModal-DWeiO-hs.js → ResetProgressModal-CcStlbhP.js} +1 -1
- package/client/dist/assets/Search-C2wrWOdy.js +1 -0
- package/client/dist/assets/Settings-CluUYs8U.js +21 -0
- package/client/dist/assets/{SkeletonGrid-B5loz7ii.js → SkeletonGrid-CLEPY9nP.js} +1 -1
- package/client/dist/assets/Trackers-CTGPxs0V.js +2 -0
- package/client/dist/assets/Tv-BwHlDdi8.js +4 -0
- package/client/dist/assets/Tv-ZEA45gXK.css +1 -0
- package/client/dist/assets/TvInfo-CXnrLekr.js +1 -0
- package/client/dist/assets/TvPopup-BekfiNcX.js +1 -0
- package/client/dist/assets/{TvSearch-BYwCf5kO.js → TvSearch-DuqR-z3v.js} +1 -1
- package/client/dist/assets/{TvWatchlist-BiJU6pKT.js → TvWatchlist-CBvgFrA0.js} +1 -1
- package/client/dist/assets/Watchlist-mj8mTOam.js +1 -0
- package/client/dist/assets/{hls-C11FOWHj.js → hls-Boje3IOE.js} +1 -1
- package/client/dist/assets/index-BXwhFWB_.js +178 -0
- package/client/dist/assets/useAnimeInfoData-DJ7A4B4T.js +1 -0
- package/client/dist/assets/useAsmr-DIwIsJXF.js +1 -0
- package/client/dist/assets/{useAsmrPopup-cT_Uu41e.js → useAsmrPopup-e4zNN9ig.js} +1 -1
- package/client/dist/assets/useMangaLibrary-C9Lci5N4.js +1 -0
- package/client/dist/assets/{useMangaPopup-PnmSctHu.js → useMangaPopup-B8nnpvyi.js} +1 -1
- package/client/dist/assets/useProviders-B5Qr4I1y.js +1 -0
- package/client/dist/assets/useTv-Dm8MCYlL.js +1 -0
- package/client/dist/assets/useTvLibrary-B7jEk2Q7.js +1 -0
- package/client/dist/assets/{vendor-query-BXRECSI6.js → vendor-query-BUTye89G.js} +1 -1
- package/client/dist/index.html +2 -2
- package/orchestrator.js +1 -2
- package/package.json +2 -6
- package/server/dist/app-auth.js +9 -29
- package/server/dist/app-hono.js +621 -0
- package/server/dist/config.js +0 -1
- package/server/dist/db/drizzle.js +32 -0
- package/server/dist/db/schema-anime.js +64 -0
- package/server/dist/db/schema-asmr.js +25 -0
- package/server/dist/db/schema-manga.js +31 -0
- package/server/dist/db/schema-tv.js +33 -0
- package/server/dist/db.js +75 -4
- package/server/dist/discord-gateway.js +1 -1
- package/server/dist/google.js +0 -7
- package/server/dist/hono/asmr.js +1 -0
- package/server/dist/hono/auth.js +56 -0
- package/server/dist/hono/music.js +419 -0
- package/server/dist/{controllers/proxy.controller.js → hono/proxy.js} +390 -394
- package/server/dist/hono/tv.js +197 -0
- package/server/dist/hono/watchlist.js +733 -0
- package/server/dist/lib/anilist.js +3 -3
- package/server/dist/lib/insights.js +185 -0
- package/server/dist/lib/ipc.js +1 -2
- package/server/dist/lib/kitsu.js +1 -28
- package/server/dist/lib/mal-import.js +291 -0
- package/server/dist/lib/migration.js +20 -34
- package/server/dist/lib/offline-db.js +36 -36
- package/server/dist/lib/tracker/manga-sync.service.js +21 -21
- package/server/dist/lib/tracker/sync.service.js +20 -20
- package/server/dist/lib/translate.js +79 -0
- package/server/dist/lib/ytmusic.js +15 -4
- package/server/dist/providers/title-matching.js +0 -1
- package/server/dist/repositories/asmr.repository.js +90 -87
- package/server/dist/repositories/insights.repository.js +24 -13
- package/server/dist/repositories/library.repository.js +33 -93
- package/server/dist/repositories/manga.repository.js +108 -116
- package/server/dist/repositories/notifications.repository.js +50 -31
- package/server/dist/repositories/queue.repository.js +36 -25
- package/server/dist/repositories/settings.repository.js +18 -8
- package/server/dist/repositories/shows-meta.repository.js +54 -41
- package/server/dist/repositories/temp-show-ids.repository.js +43 -31
- package/server/dist/repositories/tv.repository.js +98 -115
- package/server/dist/repositories/watched-episodes.repository.js +52 -20
- package/server/dist/repositories/watchlist.repository.js +36 -48
- package/server/dist/server.js +59 -151
- package/server/dist/sync.js +41 -4
- package/server/dist/trpc/context.js +7 -0
- package/server/dist/trpc/errors.js +21 -0
- package/server/dist/trpc/index.js +19 -0
- package/server/dist/trpc/router.js +49 -0
- package/server/dist/trpc/routers/asmr-library.js +122 -0
- package/server/dist/trpc/routers/asmr-progress.js +133 -0
- package/server/dist/trpc/routers/asmr.js +87 -0
- package/server/dist/trpc/routers/auth.js +247 -0
- package/server/dist/trpc/routers/continue-watching.js +83 -0
- package/server/dist/trpc/routers/data.js +1039 -0
- package/server/dist/trpc/routers/discord.js +288 -0
- package/server/dist/trpc/routers/discovery.js +15 -0
- package/server/dist/trpc/routers/insights.js +45 -0
- package/server/dist/trpc/routers/manga-library.js +280 -0
- package/server/dist/trpc/routers/manga-progress.js +181 -0
- package/server/dist/trpc/routers/manga.js +201 -0
- package/server/dist/{controllers/music.controller.js → trpc/routers/music.js} +235 -295
- package/server/dist/trpc/routers/notifications.js +80 -0
- package/server/dist/trpc/routers/progress.js +15 -0
- package/server/dist/trpc/routers/queue.js +95 -0
- package/server/dist/{routes/radio.routes.js → trpc/routers/radio.js} +30 -23
- package/server/dist/trpc/routers/settings.js +105 -0
- package/server/dist/trpc/routers/tracker.js +219 -0
- package/server/dist/trpc/routers/tv-library.js +145 -0
- package/server/dist/trpc/routers/tv-progress.js +171 -0
- package/server/dist/trpc/routers/tv.js +565 -0
- package/server/dist/trpc/routers/watchlist.js +564 -0
- package/server/dist/trpc/validation.js +159 -0
- package/server/dist/utils/security.utils.js +0 -18
- package/server/package.json +5 -10
- package/client/dist/assets/Asmr-D0EGRYsv.js +0 -2
- package/client/dist/assets/AsmrResetProgressModal-Gs0GmVaT.js +0 -1
- package/client/dist/assets/AvSyncCalibrator-D8hqarVB.js +0 -10
- package/client/dist/assets/Home-BnBzjFkW.js +0 -1
- package/client/dist/assets/Insights-i9QgY3nE.js +0 -1
- package/client/dist/assets/MangaReadPage-xJB8K2YU.js +0 -1
- package/client/dist/assets/Mature-DQtXKOXc.js +0 -1
- package/client/dist/assets/MediaCard-DpKoY-1j.js +0 -1
- package/client/dist/assets/Music-BP43Zqmq.js +0 -1
- package/client/dist/assets/OptionListMenu-Bj9ebsW3.js +0 -1
- package/client/dist/assets/Player-O2Cy2GF1.js +0 -56
- package/client/dist/assets/PlayerSettings-DYZ272Yp.js +0 -1
- package/client/dist/assets/QueueOptionsButton-lv26-6jp.js +0 -1
- package/client/dist/assets/Radio-qQBYnVfY.js +0 -1
- package/client/dist/assets/Search-BkvPYN33.js +0 -1
- package/client/dist/assets/Settings-CyZEBF6S.js +0 -21
- package/client/dist/assets/Trackers-JK-aXBYC.js +0 -2
- package/client/dist/assets/Tv-BwXWJxMy.js +0 -4
- package/client/dist/assets/Tv-C2SWcVzN.css +0 -1
- package/client/dist/assets/TvInfo-Dzyq6ucH.js +0 -1
- package/client/dist/assets/TvPopup-PlCU30FO.js +0 -1
- package/client/dist/assets/Watchlist-BOEeGXH9.js +0 -1
- package/client/dist/assets/index-CtoKRNLc.js +0 -178
- package/client/dist/assets/useAnimeInfoData-kr3WOIWH.js +0 -1
- package/client/dist/assets/useAsmr-CB9THf0m.js +0 -1
- package/client/dist/assets/useMangaLibrary-rM6z3_0E.js +0 -1
- package/client/dist/assets/useProviders-B3VB7A9M.js +0 -1
- package/client/dist/assets/useTv-BTUKMH7Q.js +0 -1
- package/client/dist/assets/useTvLibrary-C0sX-ilZ.js +0 -1
- package/server/dist/controllers/asmr-library.controller.js +0 -288
- package/server/dist/controllers/auth.controller.js +0 -225
- package/server/dist/controllers/data.controller.js +0 -767
- package/server/dist/controllers/insights.controller.js +0 -203
- package/server/dist/controllers/manga-library.controller.js +0 -350
- package/server/dist/controllers/settings.controller.js +0 -609
- package/server/dist/controllers/tv-library.controller.js +0 -314
- package/server/dist/controllers/watchlist.controller.js +0 -1069
- package/server/dist/request-logger.js +0 -16
- package/server/dist/routes/asmr-library.routes.js +0 -23
- package/server/dist/routes/asmr.routes.js +0 -81
- package/server/dist/routes/auth.routes.js +0 -25
- package/server/dist/routes/data.routes.js +0 -47
- package/server/dist/routes/discord-gateway.routes.js +0 -51
- package/server/dist/routes/insights.routes.js +0 -35
- package/server/dist/routes/lan-auth.routes.js +0 -46
- package/server/dist/routes/manga-library.routes.js +0 -25
- package/server/dist/routes/manga.routes.js +0 -141
- package/server/dist/routes/music.routes.js +0 -20
- package/server/dist/routes/providers.routes.js +0 -17
- package/server/dist/routes/proxy.routes.js +0 -12
- package/server/dist/routes/settings.routes.js +0 -24
- package/server/dist/routes/tracker.routes.js +0 -287
- package/server/dist/routes/translate.routes.js +0 -88
- package/server/dist/routes/tv-library.routes.js +0 -23
- package/server/dist/routes/tv.routes.js +0 -629
- package/server/dist/routes/watchlist.routes.js +0 -341
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
import { TRPCError } from '@trpc/server';
|
|
2
|
+
import { protectedProcedure, router } from '../index.js';
|
|
3
|
+
import { defineSchema, optStr, reqObj, reqStr } from '../validation.js';
|
|
4
|
+
import { getTmdbKey, TMDB_BASE, TMDB_IMAGE } from '../../lib/tmdb.js';
|
|
5
|
+
import { parseJsonBody } from '../../utils/http.utils.js';
|
|
6
|
+
async function resolveTvMeta(mediaType, numericTmdbId, partial) {
|
|
7
|
+
const meta = { ...partial };
|
|
8
|
+
if (meta.title && meta.imdbId && meta.year)
|
|
9
|
+
return meta;
|
|
10
|
+
try {
|
|
11
|
+
const tmdbKey = await getTmdbKey();
|
|
12
|
+
if (tmdbKey) {
|
|
13
|
+
const tmdbRes = await fetch(`${TMDB_BASE}/${mediaType}/${numericTmdbId}?api_key=${tmdbKey}&append_to_response=external_ids`, { headers: { 'User-Agent': 'Mozilla/5.0' }, signal: AbortSignal.timeout(4000) });
|
|
14
|
+
if (tmdbRes.ok) {
|
|
15
|
+
const d = await parseJsonBody(tmdbRes);
|
|
16
|
+
if (!meta.title)
|
|
17
|
+
meta.title = d.title || d.name || '';
|
|
18
|
+
if (!meta.year)
|
|
19
|
+
meta.year = (d.release_date || d.first_air_date || '').split('-')[0] || '';
|
|
20
|
+
if (!meta.imdbId)
|
|
21
|
+
meta.imdbId = d.external_ids?.imdb_id || d.imdb_id || '';
|
|
22
|
+
if (mediaType === 'tv' && d.number_of_seasons)
|
|
23
|
+
meta.totalSeasons = String(d.number_of_seasons);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
// ignore
|
|
29
|
+
}
|
|
30
|
+
return meta;
|
|
31
|
+
}
|
|
32
|
+
function toSearchResult(item, type) {
|
|
33
|
+
return {
|
|
34
|
+
id: item.id,
|
|
35
|
+
title: item.title || item.name || '',
|
|
36
|
+
year: (item.release_date || item.first_air_date || '').split('-')[0],
|
|
37
|
+
type,
|
|
38
|
+
image: item.poster_path ? `${TMDB_IMAGE}/w500${item.poster_path}` : '',
|
|
39
|
+
backdrop: item.backdrop_path ? `${TMDB_IMAGE}/w780${item.backdrop_path}` : '',
|
|
40
|
+
overview: item.overview || '',
|
|
41
|
+
vote_average: item.vote_average || 0,
|
|
42
|
+
adult: item.adult === true,
|
|
43
|
+
genre_ids: item.genre_ids || [],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function failed(message) {
|
|
47
|
+
return new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message });
|
|
48
|
+
}
|
|
49
|
+
function optNum(obj, name, fallback) {
|
|
50
|
+
const value = obj[name];
|
|
51
|
+
if (value === undefined || value === null || value === '')
|
|
52
|
+
return fallback;
|
|
53
|
+
return parseInt(String(value), 10) || fallback;
|
|
54
|
+
}
|
|
55
|
+
function reqId(obj, name) {
|
|
56
|
+
const value = obj[name];
|
|
57
|
+
if ((typeof value !== 'string' && typeof value !== 'number') || String(value).length === 0) {
|
|
58
|
+
throw new Error(`${name} must be a string`);
|
|
59
|
+
}
|
|
60
|
+
return String(value);
|
|
61
|
+
}
|
|
62
|
+
const tvSearchInput = () => defineSchema((value) => {
|
|
63
|
+
const obj = reqObj(value);
|
|
64
|
+
const out = {};
|
|
65
|
+
const q = optStr(obj, 'q');
|
|
66
|
+
if (q !== undefined)
|
|
67
|
+
out.q = q;
|
|
68
|
+
if (obj.page !== undefined)
|
|
69
|
+
out.page = optNum(obj, 'page', 1);
|
|
70
|
+
const type = optStr(obj, 'type');
|
|
71
|
+
if (type !== undefined)
|
|
72
|
+
out.type = type;
|
|
73
|
+
const genre = optStr(obj, 'genre');
|
|
74
|
+
if (genre !== undefined)
|
|
75
|
+
out.genre = genre;
|
|
76
|
+
const year = optStr(obj, 'year');
|
|
77
|
+
if (year !== undefined)
|
|
78
|
+
out.year = year;
|
|
79
|
+
const sortBy = optStr(obj, 'sortBy');
|
|
80
|
+
if (sortBy !== undefined)
|
|
81
|
+
out.sortBy = sortBy;
|
|
82
|
+
return out;
|
|
83
|
+
});
|
|
84
|
+
const tvTrendingInput = () => defineSchema((value) => {
|
|
85
|
+
const obj = reqObj(value);
|
|
86
|
+
const out = {};
|
|
87
|
+
const mediaType = optStr(obj, 'mediaType');
|
|
88
|
+
if (mediaType !== undefined)
|
|
89
|
+
out.mediaType = mediaType;
|
|
90
|
+
const timeWindow = optStr(obj, 'timeWindow');
|
|
91
|
+
if (timeWindow !== undefined)
|
|
92
|
+
out.timeWindow = timeWindow;
|
|
93
|
+
if (obj.page !== undefined)
|
|
94
|
+
out.page = optNum(obj, 'page', 1);
|
|
95
|
+
return out;
|
|
96
|
+
});
|
|
97
|
+
const tvDetailsInput = () => defineSchema((value) => {
|
|
98
|
+
const obj = reqObj(value);
|
|
99
|
+
return { type: reqStr(obj, 'type'), id: reqId(obj, 'id') };
|
|
100
|
+
});
|
|
101
|
+
const tvEpisodesInput = () => defineSchema((value) => {
|
|
102
|
+
const obj = reqObj(value);
|
|
103
|
+
return { id: reqId(obj, 'id'), season: reqId(obj, 'season') };
|
|
104
|
+
});
|
|
105
|
+
const tvLookupImdbInput = () => defineSchema((value) => ({
|
|
106
|
+
imdbId: reqStr(reqObj(value), 'imdbId'),
|
|
107
|
+
}));
|
|
108
|
+
const tvSearchImdbInput = () => defineSchema((value) => {
|
|
109
|
+
const obj = reqObj(value);
|
|
110
|
+
const out = {};
|
|
111
|
+
const q = optStr(obj, 'q');
|
|
112
|
+
if (q !== undefined)
|
|
113
|
+
out.q = q;
|
|
114
|
+
return out;
|
|
115
|
+
});
|
|
116
|
+
const tvSubtitlesInput = () => defineSchema((value) => {
|
|
117
|
+
const obj = reqObj(value);
|
|
118
|
+
const out = { type: reqStr(obj, 'type'), tmdbId: reqId(obj, 'tmdbId') };
|
|
119
|
+
const season = optStr(obj, 'season');
|
|
120
|
+
if (season !== undefined)
|
|
121
|
+
out.season = season;
|
|
122
|
+
const episode = optStr(obj, 'episode');
|
|
123
|
+
if (episode !== undefined)
|
|
124
|
+
out.episode = episode;
|
|
125
|
+
return out;
|
|
126
|
+
});
|
|
127
|
+
const tvSourcesInput = () => defineSchema((value) => {
|
|
128
|
+
const obj = reqObj(value);
|
|
129
|
+
const out = {
|
|
130
|
+
provider: reqStr(obj, 'provider'),
|
|
131
|
+
type: reqStr(obj, 'type'),
|
|
132
|
+
tmdbId: reqId(obj, 'tmdbId'),
|
|
133
|
+
};
|
|
134
|
+
if (obj.season !== undefined)
|
|
135
|
+
out.season = optNum(obj, 'season', 1);
|
|
136
|
+
if (obj.episode !== undefined)
|
|
137
|
+
out.episode = optNum(obj, 'episode', 1);
|
|
138
|
+
const server = optStr(obj, 'server');
|
|
139
|
+
if (server !== undefined)
|
|
140
|
+
out.server = server;
|
|
141
|
+
const title = optStr(obj, 'title');
|
|
142
|
+
if (title !== undefined)
|
|
143
|
+
out.title = title;
|
|
144
|
+
const year = optStr(obj, 'year');
|
|
145
|
+
if (year !== undefined)
|
|
146
|
+
out.year = year;
|
|
147
|
+
const imdbId = optStr(obj, 'imdbId');
|
|
148
|
+
if (imdbId !== undefined)
|
|
149
|
+
out.imdbId = imdbId;
|
|
150
|
+
const totalSeasons = optStr(obj, 'totalSeasons');
|
|
151
|
+
if (totalSeasons !== undefined)
|
|
152
|
+
out.totalSeasons = totalSeasons;
|
|
153
|
+
return out;
|
|
154
|
+
});
|
|
155
|
+
export const tvRouter = router({
|
|
156
|
+
search: protectedProcedure.input(tvSearchInput()).query(async ({ ctx, input }) => {
|
|
157
|
+
const query = input.q || '';
|
|
158
|
+
const page = input.page || 1;
|
|
159
|
+
const type = input.type || 'multi';
|
|
160
|
+
const genre = input.genre || '';
|
|
161
|
+
const year = input.year || '';
|
|
162
|
+
const sortBy = input.sortBy || 'popularity.desc';
|
|
163
|
+
const cacheKey = `tv-search-${query.toLowerCase()}-${page}-${type}-${genre}-${year}-${sortBy}`;
|
|
164
|
+
const cached = ctx.apiCache.get(cacheKey);
|
|
165
|
+
if (cached)
|
|
166
|
+
return cached;
|
|
167
|
+
const key = await getTmdbKey();
|
|
168
|
+
if (!key)
|
|
169
|
+
throw failed('No TMDB API key available');
|
|
170
|
+
try {
|
|
171
|
+
let url;
|
|
172
|
+
if (type === 'tv' || type === 'movie') {
|
|
173
|
+
if (!query.trim()) {
|
|
174
|
+
const params = new URLSearchParams({
|
|
175
|
+
api_key: key,
|
|
176
|
+
page: String(page),
|
|
177
|
+
sort_by: sortBy,
|
|
178
|
+
});
|
|
179
|
+
if (genre)
|
|
180
|
+
params.set('with_genres', genre);
|
|
181
|
+
if (year) {
|
|
182
|
+
if (type === 'tv')
|
|
183
|
+
params.set('first_air_date_year', year);
|
|
184
|
+
else
|
|
185
|
+
params.set('primary_release_year', year);
|
|
186
|
+
}
|
|
187
|
+
url = `${TMDB_BASE}/discover/${type}?${params.toString()}`;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
const params = new URLSearchParams({
|
|
191
|
+
api_key: key,
|
|
192
|
+
query: encodeURIComponent(query.trim()),
|
|
193
|
+
page: String(page),
|
|
194
|
+
include_adult: 'true',
|
|
195
|
+
});
|
|
196
|
+
url = `${TMDB_BASE}/search/${type}?${params.toString()}`;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
else if (!query.trim()) {
|
|
200
|
+
const params = new URLSearchParams({
|
|
201
|
+
api_key: key,
|
|
202
|
+
page: String(page),
|
|
203
|
+
sort_by: sortBy,
|
|
204
|
+
});
|
|
205
|
+
if (genre)
|
|
206
|
+
params.set('with_genres', genre);
|
|
207
|
+
if (year)
|
|
208
|
+
params.set('first_air_date_year', year);
|
|
209
|
+
url = `${TMDB_BASE}/discover/tv?${params.toString()}`;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const params = new URLSearchParams({
|
|
213
|
+
api_key: key,
|
|
214
|
+
query: encodeURIComponent(query),
|
|
215
|
+
page: String(page),
|
|
216
|
+
include_adult: 'true',
|
|
217
|
+
});
|
|
218
|
+
url = `${TMDB_BASE}/search/multi?${params.toString()}`;
|
|
219
|
+
}
|
|
220
|
+
const r = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0' } });
|
|
221
|
+
if (!r.ok)
|
|
222
|
+
throw failed('TMDB search failed');
|
|
223
|
+
const d = await parseJsonBody(r);
|
|
224
|
+
let results;
|
|
225
|
+
if (type === 'tv' || type === 'movie') {
|
|
226
|
+
results = (d.results || []).map((item) => toSearchResult(item, type));
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
results = (d.results || [])
|
|
230
|
+
.filter((item) => item.media_type !== 'person')
|
|
231
|
+
.map((item) => toSearchResult(item, item.media_type || 'tv'));
|
|
232
|
+
}
|
|
233
|
+
const payload = {
|
|
234
|
+
results,
|
|
235
|
+
total_results: d.total_results || 0,
|
|
236
|
+
total_pages: d.total_pages || 0,
|
|
237
|
+
page: d.page || page,
|
|
238
|
+
};
|
|
239
|
+
ctx.apiCache.set(cacheKey, payload, 1800);
|
|
240
|
+
return payload;
|
|
241
|
+
}
|
|
242
|
+
catch (e) {
|
|
243
|
+
if (e instanceof TRPCError)
|
|
244
|
+
throw e;
|
|
245
|
+
throw failed(e.message);
|
|
246
|
+
}
|
|
247
|
+
}),
|
|
248
|
+
trending: protectedProcedure.input(tvTrendingInput()).query(async ({ ctx, input }) => {
|
|
249
|
+
const mediaType = input.mediaType || 'all';
|
|
250
|
+
const timeWindow = input.timeWindow || 'week';
|
|
251
|
+
const page = input.page || 1;
|
|
252
|
+
const cacheKey = `tv-trending-${mediaType}-${timeWindow}-${page}`;
|
|
253
|
+
const cached = ctx.apiCache.get(cacheKey);
|
|
254
|
+
if (cached)
|
|
255
|
+
return cached;
|
|
256
|
+
const key = await getTmdbKey();
|
|
257
|
+
if (!key)
|
|
258
|
+
throw failed('No TMDB API key available');
|
|
259
|
+
try {
|
|
260
|
+
const r = await fetch(`${TMDB_BASE}/trending/${mediaType}/${timeWindow}?api_key=${key}&page=${page}`, { headers: { 'User-Agent': 'Mozilla/5.0' } });
|
|
261
|
+
if (!r.ok)
|
|
262
|
+
throw failed('TMDB trending failed');
|
|
263
|
+
const d = await parseJsonBody(r);
|
|
264
|
+
const results = (d.results || [])
|
|
265
|
+
.filter((item) => item.media_type !== 'person')
|
|
266
|
+
.map((item) => toSearchResult(item, item.media_type || 'tv'));
|
|
267
|
+
const payload = {
|
|
268
|
+
results,
|
|
269
|
+
total_results: d.total_results || 0,
|
|
270
|
+
total_pages: d.total_pages || 0,
|
|
271
|
+
page: d.page || page,
|
|
272
|
+
};
|
|
273
|
+
ctx.apiCache.set(cacheKey, payload, 3600);
|
|
274
|
+
return payload;
|
|
275
|
+
}
|
|
276
|
+
catch (e) {
|
|
277
|
+
if (e instanceof TRPCError)
|
|
278
|
+
throw e;
|
|
279
|
+
throw failed(e.message);
|
|
280
|
+
}
|
|
281
|
+
}),
|
|
282
|
+
genres: protectedProcedure.query(async ({ ctx }) => {
|
|
283
|
+
const cacheKey = 'tv-genres-list';
|
|
284
|
+
const cached = ctx.apiCache.get(cacheKey);
|
|
285
|
+
if (cached)
|
|
286
|
+
return cached;
|
|
287
|
+
const key = await getTmdbKey();
|
|
288
|
+
if (!key)
|
|
289
|
+
throw failed('No TMDB API key available');
|
|
290
|
+
try {
|
|
291
|
+
const [tvRes, movieRes] = await Promise.all([
|
|
292
|
+
fetch(`${TMDB_BASE}/genre/tv/list?api_key=${key}`, {
|
|
293
|
+
headers: { 'User-Agent': 'Mozilla/5.0' },
|
|
294
|
+
}),
|
|
295
|
+
fetch(`${TMDB_BASE}/genre/movie/list?api_key=${key}`, {
|
|
296
|
+
headers: { 'User-Agent': 'Mozilla/5.0' },
|
|
297
|
+
}),
|
|
298
|
+
]);
|
|
299
|
+
const tvData = tvRes.ok
|
|
300
|
+
? await parseJsonBody(tvRes)
|
|
301
|
+
: { genres: [] };
|
|
302
|
+
const movieData = movieRes.ok
|
|
303
|
+
? await parseJsonBody(movieRes)
|
|
304
|
+
: { genres: [] };
|
|
305
|
+
const payload = {
|
|
306
|
+
tv: (tvData.genres || []).map((g) => ({
|
|
307
|
+
id: g.id,
|
|
308
|
+
name: g.name,
|
|
309
|
+
})),
|
|
310
|
+
movie: (movieData.genres || []).map((g) => ({
|
|
311
|
+
id: g.id,
|
|
312
|
+
name: g.name,
|
|
313
|
+
})),
|
|
314
|
+
};
|
|
315
|
+
ctx.apiCache.set(cacheKey, payload, 86400);
|
|
316
|
+
return payload;
|
|
317
|
+
}
|
|
318
|
+
catch (e) {
|
|
319
|
+
if (e instanceof TRPCError)
|
|
320
|
+
throw e;
|
|
321
|
+
throw failed(e.message);
|
|
322
|
+
}
|
|
323
|
+
}),
|
|
324
|
+
details: protectedProcedure.input(tvDetailsInput()).query(async ({ input }) => {
|
|
325
|
+
const type = input.type;
|
|
326
|
+
const id = input.id;
|
|
327
|
+
const key = await getTmdbKey();
|
|
328
|
+
if (!key)
|
|
329
|
+
throw failed('No TMDB API key available');
|
|
330
|
+
try {
|
|
331
|
+
const r = await fetch(`${TMDB_BASE}/${type}/${id}?api_key=${key}&append_to_response=external_ids`, { headers: { 'User-Agent': 'Mozilla/5.0' } });
|
|
332
|
+
if (!r.ok)
|
|
333
|
+
throw failed('TMDB details failed');
|
|
334
|
+
const d = await parseJsonBody(r);
|
|
335
|
+
const result = {
|
|
336
|
+
id: d.id,
|
|
337
|
+
imdb_id: d.external_ids?.imdb_id || d.imdb_id || null,
|
|
338
|
+
title: d.title || d.name,
|
|
339
|
+
overview: d.overview,
|
|
340
|
+
vote_average: d.vote_average,
|
|
341
|
+
year: (d.release_date || d.first_air_date || '').split('-')[0],
|
|
342
|
+
poster: d.poster_path ? `${TMDB_IMAGE}/w500${d.poster_path}` : '',
|
|
343
|
+
backdrop: d.backdrop_path ? `${TMDB_IMAGE}/original${d.backdrop_path}` : '',
|
|
344
|
+
adult: d.adult === true,
|
|
345
|
+
};
|
|
346
|
+
if (type === 'tv') {
|
|
347
|
+
result.seasons = (d.seasons || [])
|
|
348
|
+
.filter((s) => s.season_number > 0)
|
|
349
|
+
.map((s) => ({
|
|
350
|
+
season_number: s.season_number,
|
|
351
|
+
episode_count: s.episode_count,
|
|
352
|
+
}));
|
|
353
|
+
result.number_of_seasons = d.number_of_seasons;
|
|
354
|
+
}
|
|
355
|
+
return result;
|
|
356
|
+
}
|
|
357
|
+
catch (e) {
|
|
358
|
+
if (e instanceof TRPCError)
|
|
359
|
+
throw e;
|
|
360
|
+
throw failed(e.message);
|
|
361
|
+
}
|
|
362
|
+
}),
|
|
363
|
+
episodes: protectedProcedure.input(tvEpisodesInput()).query(async ({ input }) => {
|
|
364
|
+
const id = input.id;
|
|
365
|
+
const season = input.season;
|
|
366
|
+
const key = await getTmdbKey();
|
|
367
|
+
if (!key)
|
|
368
|
+
throw failed('No TMDB API key available');
|
|
369
|
+
try {
|
|
370
|
+
const r = await fetch(`${TMDB_BASE}/tv/${id}/season/${season}?api_key=${key}`, {
|
|
371
|
+
headers: { 'User-Agent': 'Mozilla/5.0' },
|
|
372
|
+
});
|
|
373
|
+
if (!r.ok)
|
|
374
|
+
throw failed('TMDB episodes failed');
|
|
375
|
+
const d = await parseJsonBody(r);
|
|
376
|
+
const episodes = (d.episodes || []).map((ep) => ({
|
|
377
|
+
episode_number: ep.episode_number,
|
|
378
|
+
name: ep.name,
|
|
379
|
+
vote_average: ep.vote_average,
|
|
380
|
+
overview: ep.overview,
|
|
381
|
+
still_path: ep.still_path,
|
|
382
|
+
}));
|
|
383
|
+
return { episodes };
|
|
384
|
+
}
|
|
385
|
+
catch (e) {
|
|
386
|
+
if (e instanceof TRPCError)
|
|
387
|
+
throw e;
|
|
388
|
+
throw failed(e.message);
|
|
389
|
+
}
|
|
390
|
+
}),
|
|
391
|
+
lookupImdb: protectedProcedure.input(tvLookupImdbInput()).query(async ({ input }) => {
|
|
392
|
+
const key = await getTmdbKey();
|
|
393
|
+
if (!key)
|
|
394
|
+
return { tmdbId: null, error: 'No TMDB API key available' };
|
|
395
|
+
try {
|
|
396
|
+
const r = await fetch(`${TMDB_BASE}/find/${input.imdbId}?api_key=${key}&external_source=imdb_id`, { headers: { 'User-Agent': 'Mozilla/5.0' } });
|
|
397
|
+
if (!r.ok)
|
|
398
|
+
return { tmdbId: null, error: `TMDB error ${r.status}` };
|
|
399
|
+
const d = await parseJsonBody(r);
|
|
400
|
+
const movie = d.movie_results?.[0];
|
|
401
|
+
const tv = d.tv_results?.[0];
|
|
402
|
+
const result = movie || tv;
|
|
403
|
+
return {
|
|
404
|
+
tmdbId: result?.id || null,
|
|
405
|
+
type: movie ? 'movie' : tv ? 'tv' : null,
|
|
406
|
+
title: result?.title || result?.name || null,
|
|
407
|
+
year: (result?.release_date || result?.first_air_date || '').substring(0, 4) || null,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
catch (e) {
|
|
411
|
+
return { tmdbId: null, error: e.message };
|
|
412
|
+
}
|
|
413
|
+
}),
|
|
414
|
+
searchImdb: protectedProcedure.input(tvSearchImdbInput()).query(async ({ ctx, input }) => {
|
|
415
|
+
const query = input.q || '';
|
|
416
|
+
if (!query)
|
|
417
|
+
return [];
|
|
418
|
+
const cacheKey = `tv-imdb-search-${query.toLowerCase()}`;
|
|
419
|
+
const cached = ctx.apiCache.get(cacheKey);
|
|
420
|
+
if (cached)
|
|
421
|
+
return cached;
|
|
422
|
+
try {
|
|
423
|
+
const response = await fetch(`https://v3.sg.media-imdb.com/suggestion/x/${encodeURIComponent(query)}.json`, { headers: { 'User-Agent': 'Mozilla/5.0' } });
|
|
424
|
+
if (!response.ok)
|
|
425
|
+
throw failed('IMDB search failed');
|
|
426
|
+
const data = await parseJsonBody(response);
|
|
427
|
+
const TV_TYPES = new Set(['tvSeries', 'tvMiniSeries', 'movie']);
|
|
428
|
+
const matches = (data.d || [])
|
|
429
|
+
.filter((entry) => Boolean(entry.id && entry.l) && TV_TYPES.has(entry.qid || ''))
|
|
430
|
+
.slice(0, 10)
|
|
431
|
+
.map((entry) => ({
|
|
432
|
+
id: entry.id,
|
|
433
|
+
title: entry.l,
|
|
434
|
+
year: entry.y,
|
|
435
|
+
type: entry.qid,
|
|
436
|
+
image: entry.i?.imageUrl || '',
|
|
437
|
+
}));
|
|
438
|
+
ctx.apiCache.set(cacheKey, matches, 3600);
|
|
439
|
+
return matches;
|
|
440
|
+
}
|
|
441
|
+
catch (e) {
|
|
442
|
+
if (e instanceof TRPCError)
|
|
443
|
+
throw e;
|
|
444
|
+
throw failed(e.message);
|
|
445
|
+
}
|
|
446
|
+
}),
|
|
447
|
+
subtitles: protectedProcedure.input(tvSubtitlesInput()).query(async ({ ctx, input }) => {
|
|
448
|
+
const type = input.type;
|
|
449
|
+
const tmdbId = input.tmdbId;
|
|
450
|
+
const numericTmdbId = parseInt(tmdbId, 10);
|
|
451
|
+
if (!numericTmdbId)
|
|
452
|
+
return { subtitles: [] };
|
|
453
|
+
const mediaType = type === 'movie' ? 'movie' : 'tv';
|
|
454
|
+
const season = input.season || '1';
|
|
455
|
+
const episode = input.episode || '1';
|
|
456
|
+
const cacheKey = mediaType === 'tv'
|
|
457
|
+
? `tv-subs-${numericTmdbId}-${season}-${episode}`
|
|
458
|
+
: `tv-subs-${numericTmdbId}-movie`;
|
|
459
|
+
const cached = ctx.apiCache.get(cacheKey);
|
|
460
|
+
if (cached)
|
|
461
|
+
return cached;
|
|
462
|
+
try {
|
|
463
|
+
const searchUrl = mediaType === 'tv'
|
|
464
|
+
? `https://subtitles.vidy.st/search?id=${numericTmdbId}&season=${encodeURIComponent(season)}&episode=${encodeURIComponent(episode)}`
|
|
465
|
+
: `https://subtitles.vidy.st/search?id=${numericTmdbId}`;
|
|
466
|
+
const r = await fetch(searchUrl, {
|
|
467
|
+
headers: { 'User-Agent': 'Mozilla/5.0' },
|
|
468
|
+
signal: AbortSignal.timeout(10000),
|
|
469
|
+
});
|
|
470
|
+
if (!r.ok)
|
|
471
|
+
return { subtitles: [] };
|
|
472
|
+
const items = (await parseJsonBody(r));
|
|
473
|
+
const subtitles = (Array.isArray(items) ? items : [])
|
|
474
|
+
.filter((s) => typeof s.url === 'string' && s.url.startsWith('https://'))
|
|
475
|
+
.map((s) => ({
|
|
476
|
+
language: String(s.language || 'en'),
|
|
477
|
+
label: String(s.display || s.language || 'Unknown'),
|
|
478
|
+
url: String(s.url),
|
|
479
|
+
trusted: s.isTrusted === true,
|
|
480
|
+
hi: s.isHearingImpaired === true,
|
|
481
|
+
downloads: Number(s.downloadCount) || 0,
|
|
482
|
+
media: String(s.media || s.release || ''),
|
|
483
|
+
}))
|
|
484
|
+
.sort((a, b) => {
|
|
485
|
+
const aEn = a.language.toLowerCase().startsWith('en') ? 0 : 1;
|
|
486
|
+
const bEn = b.language.toLowerCase().startsWith('en') ? 0 : 1;
|
|
487
|
+
if (aEn !== bEn)
|
|
488
|
+
return aEn - bEn;
|
|
489
|
+
if (a.trusted !== b.trusted)
|
|
490
|
+
return a.trusted ? -1 : 1;
|
|
491
|
+
return b.downloads - a.downloads;
|
|
492
|
+
})
|
|
493
|
+
.slice(0, 50);
|
|
494
|
+
const payload = { subtitles };
|
|
495
|
+
ctx.apiCache.set(cacheKey, payload, 3600);
|
|
496
|
+
return payload;
|
|
497
|
+
}
|
|
498
|
+
catch {
|
|
499
|
+
return { subtitles: [] };
|
|
500
|
+
}
|
|
501
|
+
}),
|
|
502
|
+
sources: protectedProcedure.input(tvSourcesInput()).query(async ({ ctx, input }) => {
|
|
503
|
+
const provider = input.provider;
|
|
504
|
+
const type = input.type;
|
|
505
|
+
const tmdbId = input.tmdbId;
|
|
506
|
+
const mediaType = type === 'movie' ? 'movie' : 'tv';
|
|
507
|
+
const numericTmdbId = parseInt(tmdbId, 10);
|
|
508
|
+
if (!numericTmdbId)
|
|
509
|
+
return { sources: [], audioTracks: [], error: 'Bad tmdb id' };
|
|
510
|
+
const mod = ctx.getTvProvider(String(provider).toLowerCase());
|
|
511
|
+
if (!mod || typeof mod.getSources !== 'function') {
|
|
512
|
+
return { sources: [], audioTracks: [], error: `Unknown TV provider ${provider}` };
|
|
513
|
+
}
|
|
514
|
+
const season = input.season || 1;
|
|
515
|
+
const episode = input.episode || 1;
|
|
516
|
+
const server = input.server;
|
|
517
|
+
const meta = await resolveTvMeta(mediaType, numericTmdbId, {
|
|
518
|
+
title: input.title || '',
|
|
519
|
+
year: input.year || '',
|
|
520
|
+
imdbId: input.imdbId || '',
|
|
521
|
+
totalSeasons: input.totalSeasons || '1',
|
|
522
|
+
});
|
|
523
|
+
const media = {
|
|
524
|
+
tmdbId: numericTmdbId,
|
|
525
|
+
type: mediaType,
|
|
526
|
+
season,
|
|
527
|
+
episode,
|
|
528
|
+
title: meta.title,
|
|
529
|
+
year: meta.year,
|
|
530
|
+
imdbId: meta.imdbId,
|
|
531
|
+
totalSeasons: parseInt(meta.totalSeasons, 10) || 1,
|
|
532
|
+
};
|
|
533
|
+
try {
|
|
534
|
+
const result = await mod.getSources(media, server);
|
|
535
|
+
if (!result || !Array.isArray(result.sources) || result.sources.length === 0) {
|
|
536
|
+
return {
|
|
537
|
+
provider: mod.name,
|
|
538
|
+
server: result?.server || server,
|
|
539
|
+
sources: [],
|
|
540
|
+
audioTracks: [],
|
|
541
|
+
valid: false,
|
|
542
|
+
error: 'No sources found',
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
return {
|
|
546
|
+
provider: mod.name,
|
|
547
|
+
server: result.server || server,
|
|
548
|
+
sources: result.sources,
|
|
549
|
+
audioTracks: result.audioTracks || [],
|
|
550
|
+
subtitles: result.subtitles || [],
|
|
551
|
+
referer: result.referer || '',
|
|
552
|
+
valid: true,
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
catch (e) {
|
|
556
|
+
return {
|
|
557
|
+
provider: mod.name,
|
|
558
|
+
sources: [],
|
|
559
|
+
audioTracks: [],
|
|
560
|
+
valid: false,
|
|
561
|
+
error: e.message,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
}),
|
|
565
|
+
});
|