Files
tpbproxy/routes/search.js
wmantly 803a6db033 Add per-episode fallback for incomplete TV seasons
When a complete-season pack cannot be found for a missing/partial season,

fillMissing now searches each missing SxxExx individually and queues the

best episode torrent per gap. Also exposes episode-level detail in the

/fill response and season plan.

Signed-off-by: William Mantly <wmantly@gmail.com>
2026-07-20 14:35:08 -04:00

93 lines
2.5 KiB
JavaScript

'use strict';
const router = require('express').Router();
const search = require('>/controller/search');
const { Torrent, Wanted } = require('>/models');
// Step 1: fuzzy query -> a few TMDB title candidates( with posters) to confirm.
router.get('/title', async function(req, res, next){
try{
res.json({results: await search.tmdbSearch(req.query.q)});
}catch(error){
next(error);
}
});
// Step 2: confirmed title -> TPB search + LLM-curated release options.
router.post('/releases', async function(req, res, next){
try{
let result = await search.findReleases(req.body.tmdbId, req.body.mediaType);
// Nothing good yet( e.g. unreleased) -> remember it and grab it later.
if(!result.options.length && result.mediaType !== 'tv' && result.tmdbId){
await Wanted.upsert({
tmdbId: String(result.tmdbId),
mediaType: result.mediaType,
title: result.title,
year: result.year,
status: 'wanted',
requestedBy: req.user.username,
});
result.remembered = true;
}
res.json(result);
}catch(error){
next(error);
}
});
// Wishlist: things we couldn't find yet and are watching for.
router.get('/wanted', async function(req, res, next){
try{
res.json(await Wanted.findAll({ order: [['createdAt', 'DESC']] }));
}catch(error){
next(error);
}
});
router.delete('/wanted/:tmdbId', async function(req, res, next){
try{
let wanted = await Wanted.findByPk(req.params.tmdbId);
if(wanted) await wanted.destroy();
res.json({ deleted: true });
}catch(error){
next(error);
}
});
// TV: which seasons you have vs are missing (Emby inventory vs TMDB counts).
router.get('/seasons', async function(req, res, next){
try{
res.json(await search.seasonPlan(req.query.tmdbId));
}catch(error){
next(error);
}
});
// TV: queue the best complete-season pack for every missing/incomplete season.
router.post('/fill', async function(req, res, next){
try{
let plan = await search.fillMissing(req.body.tmdbId);
let queued = [];
for(let pick of plan.picks){
try{
await Torrent.create({
magnetLink: pick.magnetLink,
isPrivate: false,
added_by: req.user.username,
category: pick.category,
});
queued.push({ type: pick.type, season: pick.season, episode: pick.episode, name: pick.name });
}catch(error){
plan.skipped.push({ season: pick.season, episode: pick.episode, reason: error.message });
}
}
res.json({ title: plan.title, year: plan.year, queued, skipped: plan.skipped });
}catch(error){
next(error);
}
});
module.exports = router;