'use strict'; const conf = require('>/conf'); const search = require('>/controller/search'); let lock = false; // Periodically re-run Smart Search for wishlisted movies; when a good copy finally // appears, queue it( which then flows through the normal download + organize pipeline). async function tick(){ if(lock) return; lock = true; try{ const { Wanted, Torrent } = require('>/models'); let wanted = await Wanted.findAll({ where: { status: 'wanted' } }); for(let w of wanted){ if(!w.requestedBy) continue; try{ let result = await search.findReleases(w.tmdbId, w.mediaType); let pick = result.options.find(o => o.role === 'recommended' && !o.alreadyOwned) || result.options.find(o => !o.alreadyOwned); if(!pick) continue; await Torrent.create({ magnetLink: pick.magnetLink, isPrivate: false, added_by: w.requestedBy, category: pick.category, }); w.status = 'fulfilled'; await w.save(); }catch(error){ // leave it wanted; try again next tick } } }catch(error){ // DB/Transmission transient — retry next tick }finally{ lock = false; } } setInterval(tick, conf.wanted.interval || 6 * 60 * 60 * 1000); tick(); // check once on boot module.exports = { tick };