fix: add log for debug

This commit is contained in:
MasutaYunikon committed 2026-03-10 23:37:06 +01:00
1 parent 49d60aeabd
commit e18c6a1c54
2 files changed
+352 -288

No files matched your search

+273 -232
View File
@@ -1,271 +1,312 @@
/** import express from 'express'
* Organizer import cookieParser from 'cookie-parser'
* ========= import fs from 'fs'
* Déplace ou hardlinke les fichiers téléchargés vers la médiathèque Jellyfin.
* Incrémental : skip les fichiers déjà organisés.
*
* Structure dest :
* mediaPath / serie_title / Saison 001 / filename.mkv
*/
import fs from 'fs'
import path from 'path' import path from 'path'
import { readSettings } from './settings.js' import { authStatus, authSetup, authLogin, authLogout, requireAuth } from './auth.js'
import { readSettings, writeSettings } from './settings.js'
import {
registerDriver, getDriver, getAvailableClients,
listClients, addClient, removeClient, getClient,
sanitizeClient, dispatchDownload, dispatchList
} from './torrent-clients/index.js'
import qbittorrentDriver from './torrent-clients/qbittorrent.js'
import { organizeTorrent, autoOrganizeAll } from './organize.js'
const TORRENTS_PATH = path.join(process.cwd(), 'data', 'torrent_final.json') registerDriver(qbittorrentDriver)
const ORGANIZED_PATH = path.join(process.cwd(), 'data', 'organized.json')
// ─── Organized log ──────────────────────────────────────────── const app = express()
const PORT = 3001
const FANKAI_API = 'https://metadata.fankai.fr'
const TORRENTS_PATH = path.join(process.cwd(), 'data', 'torrent_final.json')
const GITHUB_RAW_URL = process.env.GITHUB_RAW_URL
?? 'https://raw.githubusercontent.com/masutayunikon/fankarr-scraper/main/data/torrent_final.json'
function loadOrganized(): Record<string, Record<string, string>> { app.use(express.json())
try { app.use(cookieParser())
if (!fs.existsSync(ORGANIZED_PATH)) return {}
return JSON.parse(fs.readFileSync(ORGANIZED_PATH, 'utf-8')) // ── Static frontend (prod) ─────────────────────────────────────
} catch { return {} } const PUBLIC_PATH = path.join(process.cwd(), 'public')
if (fs.existsSync(PUBLIC_PATH)) {
app.use(express.static(PUBLIC_PATH))
} }
function saveOrganized(data: Record<string, Record<string, string>>) { // ── Auth ───────────────────────────────────────────────────────
fs.writeFileSync(ORGANIZED_PATH, JSON.stringify(data, null, 2), 'utf-8') app.get('/api/auth/status', authStatus)
} app.post('/api/auth/setup', authSetup)
app.post('/api/auth/login', authLogin)
app.post('/api/auth/logout', authLogout)
function markOrganized(hash: string, filename: string) { // ── Settings ───────────────────────────────────────────────────
const data = loadOrganized() app.get('/api/settings', requireAuth, (_req, res) => {
if (!data[hash]) data[hash] = {} res.json(readSettings())
data[hash][filename] = new Date().toISOString() })
saveOrganized(data) app.post('/api/settings', requireAuth, (req, res) => {
} const { mediaPath, completePath, organizeMode, category } = req.body
res.json(writeSettings({ mediaPath, completePath, organizeMode, category }))
})
function isOrganized(hash: string, filename: string): boolean { // ── Torrent clients ────────────────────────────────────────────
const data = loadOrganized() app.get('/api/torrent-clients/available', requireAuth, (_req, res) => {
return !!data[hash]?.[filename] res.json(getAvailableClients())
} })
app.get('/api/torrent-clients', requireAuth, (_req, res) => {
// ─── Torrent lookup ─────────────────────────────────────────── res.json(listClients().map((c: any) => sanitizeClient(c)))
})
app.post('/api/torrent-clients', requireAuth, (req, res) => {
const { name, type, config } = req.body
if (!name || !type || !config) { res.status(400).json({ error: 'name, type et config requis' }); return }
if (!getDriver(type)) { res.status(400).json({ error: `Type inconnu : ${type}` }); return }
res.json(sanitizeClient(addClient(name, type, config)))
})
app.delete('/api/torrent-clients/:uuid', requireAuth, (req, res) => {
const ok = removeClient(String(req.params.uuid))
if (!ok) res.status(404).json({ error: 'Client introuvable' })
else res.json({ ok: true })
})
app.post('/api/torrent-clients/test-config', requireAuth, async (req, res) => {
const { type, config } = req.body
if (!type || !config) { res.status(400).json({ error: 'type et config requis' }); return }
const driver = getDriver(type)
if (!driver) { res.status(400).json({ error: `Type inconnu : ${type}` }); return }
res.json(await driver.test(config))
})
app.post('/api/torrent-clients/:uuid/test', requireAuth, async (req, res) => {
const client = getClient(String(req.params.uuid))
if (!client) { res.status(404).json({ error: 'Client introuvable' }); return }
const driver = getDriver(client.type)
if (!driver) { res.status(400).json({ error: 'Driver introuvable' }); return }
res.json(await driver.test(client.config))
})
app.get('/api/torrent-clients/:uuid/healthcheck', requireAuth, async (req, res) => {
const client = getClient(String(req.params.uuid))
if (!client) { res.status(404).json({ error: 'Client introuvable' }); return }
const driver = getDriver(client.type)
if (!driver) { res.status(400).json({ error: 'Driver introuvable' }); return }
res.json(await driver.healthcheck(client.config))
})
// ── Helpers ────────────────────────────────────────────────────
function readTorrents(): any[] { function readTorrents(): any[] {
try { try {
if (!fs.existsSync(TORRENTS_PATH)) return [] if (!fs.existsSync(TORRENTS_PATH)) return []
return JSON.parse(fs.readFileSync(TORRENTS_PATH, 'utf-8')) return JSON.parse(fs.readFileSync(TORRENTS_PATH, 'utf-8'))
} catch { return [] } } catch { return [] }
} }
async function fankaiGet(endpoint: string): Promise<any> {
function findTorrentByHash(hash: string): any | null { const res = await fetch(`${FANKAI_API}${endpoint}`)
const torrents = readTorrents() if (!res.ok) throw new Error(`Fankai API ${res.status}: ${endpoint}`)
return torrents.find(t => t.infohash?.toLowerCase() === hash.toLowerCase()) ?? null return res.json()
}
function indexBySerie(torrents: any[]): Map<number, any[]> {
const map = new Map<number, any[]>()
for (const t of torrents) {
if (!t.serie_id) continue
if (!map.has(t.serie_id)) map.set(t.serie_id, [])
map.get(t.serie_id)!.push(t)
}
return map
} }
// ─── Filesystem ops ─────────────────────────────────────────── // ── Series ─────────────────────────────────────────────────────
app.get('/api/series', requireAuth, async (_req, res) => {
function ensureDir(dir: string) {
fs.mkdirSync(dir, { recursive: true })
}
function seasonFolder(n: number): string {
return `Saison ${String(n).padStart(3, '0')}`
}
function tryHardlink(src: string, dest: string): boolean {
try { try {
fs.linkSync(src, dest) const [apiData, torrents] = await Promise.all([fankaiGet('/series'), Promise.resolve(readTorrents())])
return true const byId = indexBySerie(torrents)
const seriesList = Array.isArray(apiData) ? apiData : (apiData.series ?? [])
res.json({ series: seriesList.map((serie: any) => ({
...serie,
torrent_count: byId.get(serie.id)?.length ?? 0,
has_torrents : byId.has(serie.id)
}))})
} catch (err) { } catch (err) {
console.warn(`[organize] hardlink échoué (${err instanceof Error ? err.message : err})`) res.status(500).json({ error: err instanceof Error ? err.message : 'Erreur inconnue' })
return false
} }
} })
function copyFile(src: string, dest: string) { app.get('/api/series/:id', requireAuth, async (req, res) => {
fs.copyFileSync(src, dest) const id = Number(req.params.id)
} try {
const [serie, seasonsData] = await Promise.all([fankaiGet(`/series/${id}`), fankaiGet(`/series/${id}/seasons`)])
const seasons = Array.isArray(seasonsData) ? seasonsData : (seasonsData.seasons ?? [])
const seasonsWithEpisodes = await Promise.all(seasons.map(async (season: any) => {
const epsData = await fankaiGet(`/seasons/${season.id}/episodes`)
return { ...season, episodes: Array.isArray(epsData) ? epsData : (epsData.episodes ?? []) }
}))
// ─── Résultat d'organisation ────────────────────────────────── const torrents = readTorrents().filter(t => t.serie_id === id)
const allSeasonIds = new Set<number>(seasonsWithEpisodes.filter((s: any) => s.season_number !== 0).map((s: any) => s.id))
const availableEpisodeIds = new Set<number>()
const episodeTorrentMap: Record<number, any> = {}
const seasonTorrentMap : Record<number, any> = {}
const integraleTorrents: any[] = []
const packEpisodesTorrents: any[] = []
export interface OrganizeResult { for (const t of torrents) {
total : number const ref = { torrent_url: t.torrent_url, magnet: t.magnet, type: t.type, raw: t.raw }
skipped : number for (const ep of t.resolved_episodes ?? []) {
done : number availableEpisodeIds.add(ep.episode_id)
errors : { file: string; error: string }[] if (t.type === 'episode') episodeTorrentMap[ep.episode_id] = ref
} }
if (t.type === 'pack_saison') {
// ─── Organise un torrent ────────────────────────────────────── const rs: any[] = t.resolved_seasons ?? []
if (rs.length > 1) {
export async function organizeTorrent( packEpisodesTorrents.push(t)
hash : string, } else {
name : string, // nom du dossier torrent (retourné par qB) const sids = rs.length > 0 ? rs.map((r: any) => r.season_id) : [t.season_id]
savePath: string // save_path retourné par qBittorrent for (const sid of sids) { if (sid) seasonTorrentMap[sid] = ref }
): Promise<OrganizeResult> { }
const settings = readSettings() } else if (t.type === 'pack_integrale') {
const { mediaPath, completePath, organizeMode } = settings integraleTorrents.push(t)
} else if (t.type === 'pack_episodes') {
console.log(`[organize] ── Début organisation ──────────────────────`) packEpisodesTorrents.push(t)
console.log(`[organize] hash : ${hash}`) }
console.log(`[organize] name : ${name}`)
console.log(`[organize] save_path : ${savePath}`)
console.log(`[organize] mediaPath : ${mediaPath}`)
console.log(`[organize] completePath: ${completePath}`)
console.log(`[organize] mode : ${organizeMode}`)
const result: OrganizeResult = { total: 0, skipped: 0, done: 0, errors: [] }
// Trouver le torrent dans torrent_final.json via infohash
const torrent = findTorrentByHash(hash)
if (!torrent) {
console.error(`[organize] ✗ Aucun torrent avec infohash="${hash}" dans torrent_final.json`)
throw new Error(`Torrent introuvable pour le hash : "${hash}"`)
}
console.log(`[organize] ✓ Torrent trouvé : ${torrent.raw}`)
const serieTitle = torrent.serie_title || torrent.show_title || name
const torrentFiles: any[] = torrent.torrent_files ?? []
console.log(`[organize] serie_title : ${serieTitle}`)
console.log(`[organize] fichiers : ${torrentFiles.length}`)
if (torrentFiles.length === 0) {
throw new Error(`Aucun fichier trouvé dans le torrent "${name}"`)
}
// Map filename → season_number depuis resolved_episodes
const filenameSeasonMap = new Map<string, number>()
for (const ep of torrent.resolved_episodes ?? []) {
if (ep.filename && ep.season_number !== undefined) {
filenameSeasonMap.set(ep.filename, ep.season_number)
}
}
result.total = torrentFiles.length
for (const tf of torrentFiles) {
const filename = tf.filename
const filePath: string[] = tf.path
console.log(`[organize] ── Fichier : ${filename}`)
console.log(`[organize] path[] : ${JSON.stringify(filePath)}`)
// Skip si déjà organisé
if (isOrganized(hash, filename)) {
console.log(`[organize] → déjà organisé, skip`)
result.skipped++
continue
} }
// Essayer plusieurs chemins sources possibles let promotedIntegrale: any = null
const candidates = [ if (integraleTorrents.length === 0 && allSeasonIds.size > 0) {
path.join(savePath, name, ...filePath.slice(0, -1), filename), for (const t of torrents.filter(t => t.type === 'pack_saison')) {
path.join(savePath, ...filePath.slice(0, -1), filename), const rs: any[] = t.resolved_seasons ?? []
path.join(savePath, name, filename), const tIds = new Set<number>(rs.length > 0 ? rs.map((r: any) => r.season_id) : t.season_id ? [t.season_id] : [])
path.join(savePath, filename), if ([...allSeasonIds].every(sid => tIds.has(sid))) { promotedIntegrale = t; break }
path.join(completePath, name, ...filePath.slice(0, -1), filename), }
path.join(completePath, name, filename), }
path.join(completePath, filename),
if (integraleTorrents.length > 0 || promotedIntegrale) {
for (const key of Object.keys(seasonTorrentMap)) delete seasonTorrentMap[Number(key)]
if (promotedIntegrale) {
const idx = packEpisodesTorrents.indexOf(promotedIntegrale)
if (idx !== -1) packEpisodesTorrents.splice(idx, 1)
}
}
function packEpisodesLabel(t: any): string {
const eps: any[] = t.resolved_episodes ?? []
if (!eps.length) return t.raw
const bySeason = new Map<number, number[]>()
for (const ep of eps) {
const sn = ep.season_number ?? 0
if (!bySeason.has(sn)) bySeason.set(sn, [])
bySeason.get(sn)!.push(ep.episode_number)
}
return [...bySeason.entries()].sort(([a], [b]) => a - b).map(([sn, nums]) => {
const s = nums.sort((a, b) => a - b)
const label = sn === 0 ? 'S0' : `S${sn}`
return s[0] === s[s.length-1] ? `${label} Ep ${s[0]}` : `${label} Eps ${s[0]}-${s[s.length-1]}`
}).join(' · ')
}
const enrichedSeasons = seasonsWithEpisodes.map((season: any) => ({
...season,
torrent: seasonTorrentMap[season.id] ?? null,
episodes: season.episodes.map((ep: any) => ({
...ep,
available: availableEpisodeIds.has(ep.id),
torrent : episodeTorrentMap[ep.id] ?? null
}))
}))
const heroBundles: any[] = [
...integraleTorrents.map(t => ({ label: 'Intégrale', torrent_url: t.torrent_url, magnet: t.magnet, raw: t.raw })),
...(promotedIntegrale ? [{ label: 'Intégrale', torrent_url: promotedIntegrale.torrent_url, magnet: promotedIntegrale.magnet, raw: promotedIntegrale.raw }] : []),
...packEpisodesTorrents.map(t => ({ label: packEpisodesLabel(t), torrent_url: t.torrent_url, magnet: t.magnet, raw: t.raw }))
] ]
let src: string | null = null res.json({ serie, seasons: enrichedSeasons, torrents_integrale: heroBundles })
for (const candidate of candidates) {
console.log(`[organize] candidate: ${candidate} → ${fs.existsSync(candidate) ? '✓ EXISTS' : '✗ absent'}`)
if (fs.existsSync(candidate)) {
src = candidate
break
}
}
if (!src) {
const err = `Source introuvable (essayé ${candidates.length} chemins)`
console.error(`[organize] ✗ ${err}`)
result.errors.push({ file: filename, error: err })
continue
}
console.log(`[organize] ✓ source : ${src}`)
// Déterminer la saison
const seasonNum = filenameSeasonMap.get(filename) ?? 1
console.log(`[organize] saison : ${seasonNum}`)
// Chemin destination
const destDir = path.join(mediaPath, serieTitle, seasonFolder(seasonNum))
const dest = path.join(destDir, filename)
console.log(`[organize] dest : ${dest}`)
// Skip si dest existe déjà
if (fs.existsSync(dest)) {
console.log(`[organize] → dest existe déjà, skip`)
markOrganized(hash, filename)
result.skipped++
continue
}
try {
ensureDir(destDir)
if (organizeMode === 'hardlink') {
const ok = tryHardlink(src, dest)
if (!ok) {
console.warn(`[organize] hardlink impossible → fallback copy`)
copyFile(src, dest)
}
} else {
copyFile(src, dest)
fs.unlinkSync(src)
}
markOrganized(hash, filename)
result.done++
console.log(`[organize] ✓ ${organizeMode} OK`)
} catch (err) {
const msg = err instanceof Error ? err.message : 'Erreur inconnue'
console.error(`[organize] ✗ ${msg}`)
result.errors.push({ file: filename, error: msg })
}
}
console.log(`[organize] ── Résultat : ${result.done} OK, ${result.skipped} skippés, ${result.errors.length} erreurs`)
return result
}
// ─── Auto-organise polling ────────────────────────────────────
export async function autoOrganizeAll(
listFn: () => Promise<any[]>
): Promise<void> {
let torrents: any[]
try {
torrents = await listFn()
} catch (err) { } catch (err) {
console.error('[organize] autoOrganizeAll: impossible de récupérer la liste des torrents:', err) res.status(500).json({ error: err instanceof Error ? err.message : 'Erreur inconnue' })
}
})
// ── Download ───────────────────────────────────────────────────
app.post('/api/download', requireAuth, async (req, res) => {
const url = req.body.torrent_url ?? req.body.magnet
if (!url) { res.status(400).json({ error: 'torrent_url ou magnet requis' }); return }
const results = await dispatchDownload(url)
res.status(results.every((r: any) => r.ok) ? 200 : 207).json({ results })
})
// ── Downloads — liste torrents Fankai ──────────────────────────
app.get('/api/downloads', requireAuth, async (_req, res) => {
try {
const { category } = readSettings()
const torrents = await dispatchList(category ?? 'fankai')
res.json(torrents)
} catch (err) {
res.status(500).json({ error: err instanceof Error ? err.message : 'Erreur inconnue' })
}
})
// ── Organize ───────────────────────────────────────────────────
app.post('/api/organize', requireAuth, async (req, res) => {
const { hash, name, save_path } = req.body
if (!hash || !name || !save_path) {
res.status(400).json({ error: 'hash, name et save_path requis' })
return return
} }
try {
const result = await organizeTorrent(hash, name, save_path)
res.json(result)
} catch (err) {
res.status(500).json({ error: err instanceof Error ? err.message : 'Erreur inconnue' })
}
})
console.log(`[organize] autoOrganizeAll: ${torrents.length} torrents récupérés`) // ── Torrents status ────────────────────────────────────────────
app.get('/api/torrents/status', requireAuth, (_req, res) => {
try {
if (!fs.existsSync(TORRENTS_PATH)) { res.json({ exists: false, count: 0, empty: true }); return }
const raw = fs.readFileSync(TORRENTS_PATH, 'utf-8').trim()
if (!raw || raw === '[]') { res.json({ exists: true, count: 0, empty: true }); return }
const data = JSON.parse(raw)
const count = Array.isArray(data) ? data.length : 0
res.json({ exists: true, count, empty: count === 0 })
} catch { res.json({ exists: false, count: 0, empty: true }) }
})
const organized = loadOrganized() // ── Update torrent_final.json ──────────────────────────────────
app.post('/api/update', requireAuth, async (_req, res) => {
try {
const response = await fetch(GITHUB_RAW_URL)
if (!response.ok) throw new Error(`GitHub a répondu ${response.status}`)
const data = await response.json() as unknown[]
if (!Array.isArray(data)) throw new Error('Format invalide')
fs.mkdirSync(path.dirname(TORRENTS_PATH), { recursive: true })
fs.writeFileSync(TORRENTS_PATH, JSON.stringify(data, null, 2), 'utf-8')
res.json({ ok: true, count: data.length })
} catch (err) {
res.status(500).json({ error: err instanceof Error ? err.message : 'Erreur inconnue' })
}
})
for (const t of torrents) { // ── SPA fallback (prod) — doit être après toutes les routes API ─
if (t.state !== 'seeding') { if (fs.existsSync(PUBLIC_PATH)) {
console.log(`[organize] skip ${t.name} (state=${t.state})`) app.get('*path', (_req, res) => {
continue res.sendFile(path.join(PUBLIC_PATH, 'index.html'))
} })
}
const torrent = findTorrentByHash(t.hash) // ── Start ──────────────────────────────────────────────────────
if (!torrent) { app.listen(PORT, () => {
console.warn(`[organize] hash ${t.hash} (${t.name}) non trouvé dans torrent_final.json`) try {
continue const data = JSON.parse(fs.readFileSync(TORRENTS_PATH, 'utf-8'))
} const count = Array.isArray(data) ? data.length : 0
if (count === 0) console.warn('[fankarr] ⚠ torrent_final.json est vide')
else console.log(`[fankarr] ${count} torrents chargés`)
} catch { console.warn('[fankarr] ⚠ torrent_final.json introuvable ou illisible') }
console.log(`[fankarr] Serveur sur http://localhost:${PORT}`)
const files: any[] = torrent.torrent_files ?? [] // Auto-organise toutes les 30s
if (files.length === 0) continue const autoOrganize = async () => {
const allDone = files.every((f: any) => organized[t.hash]?.[f.filename])
if (allDone) {
console.log(`[organize] ${t.name} → déjà entièrement organisé`)
continue
}
console.log(`[organize] Auto-organisation : ${t.name}`)
try { try {
const result = await organizeTorrent(t.hash, t.name, t.save_path) const { category } = readSettings()
console.log(`[organize] ${t.name} → ${result.done} fichiers, ${result.skipped} skippés, ${result.errors.length} erreurs`) await autoOrganizeAll(() => dispatchList(category ?? 'fankai'))
} catch (err) { } catch (err) {
console.error(`[organize] Erreur pour ${t.name}:`, err) console.error('[organize] Erreur auto-organise:', err)
} }
} }
} // Premier run 30s après démarrage, puis toutes les 30s
setTimeout(() => {
autoOrganize()
setInterval(autoOrganize, 30_000)
}, 30_000)
})
+79 -56
View File
@@ -68,7 +68,8 @@ function tryHardlink(src: string, dest: string): boolean {
try { try {
fs.linkSync(src, dest) fs.linkSync(src, dest)
return true return true
} catch { } catch (err) {
console.warn(`[organize] hardlink échoué (${err instanceof Error ? err.message : err})`)
return false return false
} }
} }
@@ -90,35 +91,40 @@ export interface OrganizeResult {
export async function organizeTorrent( export async function organizeTorrent(
hash : string, hash : string,
name : string, // nom du dossier torrent dans completePath name : string, // nom du dossier torrent (retourné par qB)
savePath: string // save_path retourné par qBittorrent savePath: string // save_path retourné par qBittorrent
): Promise<OrganizeResult> { ): Promise<OrganizeResult> {
const settings = readSettings() const settings = readSettings()
const { mediaPath, completePath, organizeMode } = settings const { mediaPath, completePath, organizeMode } = settings
console.log(`[organize] ── Début organisation ──────────────────────`)
console.log(`[organize] hash : ${hash}`)
console.log(`[organize] name : ${name}`)
console.log(`[organize] save_path : ${savePath}`)
console.log(`[organize] mediaPath : ${mediaPath}`)
console.log(`[organize] completePath: ${completePath}`)
console.log(`[organize] mode : ${organizeMode}`)
const result: OrganizeResult = { total: 0, skipped: 0, done: 0, errors: [] } const result: OrganizeResult = { total: 0, skipped: 0, done: 0, errors: [] }
// Trouver le torrent dans torrent_final.json via infohash // Trouver le torrent dans torrent_final.json via infohash
const torrent = findTorrentByHash(hash) const torrent = findTorrentByHash(hash)
if (!torrent) { if (!torrent) {
throw new Error(`Torrent introuvable dans torrent_final.json pour le hash : "${hash}"`) console.error(`[organize] ✗ Aucun torrent avec infohash="${hash}" dans torrent_final.json`)
throw new Error(`Torrent introuvable pour le hash : "${hash}"`)
} }
console.log(`[organize] ✓ Torrent trouvé : ${torrent.raw}`)
const serieTitle = torrent.serie_title || torrent.show_title || name const serieTitle = torrent.serie_title || torrent.show_title || name
const torrentFiles: any[] = torrent.torrent_files ?? [] const torrentFiles: any[] = torrent.torrent_files ?? []
console.log(`[organize] serie_title : ${serieTitle}`)
console.log(`[organize] fichiers : ${torrentFiles.length}`)
if (torrentFiles.length === 0) { if (torrentFiles.length === 0) {
throw new Error(`Aucun fichier trouvé dans le torrent "${name}"`) throw new Error(`Aucun fichier trouvé dans le torrent "${name}"`)
} }
// Résoudre season_number par episode_id // Map filename → season_number depuis resolved_episodes
const epSeasonMap = new Map<number, number>()
for (const ep of torrent.resolved_episodes ?? []) {
if (ep.filename) {
epSeasonMap.set(ep.episode_number, ep.season_number)
}
}
// Map filename → season_number
const filenameSeasonMap = new Map<string, number>() const filenameSeasonMap = new Map<string, number>()
for (const ep of torrent.resolved_episodes ?? []) { for (const ep of torrent.resolved_episodes ?? []) {
if (ep.filename && ep.season_number !== undefined) { if (ep.filename && ep.season_number !== undefined) {
@@ -130,51 +136,59 @@ export async function organizeTorrent(
for (const tf of torrentFiles) { for (const tf of torrentFiles) {
const filename = tf.filename const filename = tf.filename
const filePath: string[] = tf.path // ex: ["Saga 1 - ...", "file.mkv"] const filePath: string[] = tf.path
console.log(`[organize] ── Fichier : ${filename}`)
console.log(`[organize] path[] : ${JSON.stringify(filePath)}`)
// Skip si déjà organisé // Skip si déjà organisé
if (isOrganized(hash, filename)) { if (isOrganized(hash, filename)) {
console.log(`[organize] → déjà organisé, skip`)
result.skipped++ result.skipped++
continue continue
} }
// Construire le chemin source // Essayer plusieurs chemins sources possibles
// save_path peut déjà contenir le nom du dossier ou pas selon le client const candidates = [
const torrentDir = path.join(savePath, name) path.join(savePath, name, ...filePath.slice(0, -1), filename),
const torrentDirAlt = savePath // au cas où save_path inclut déjà le nom path.join(savePath, ...filePath.slice(0, -1), filename),
path.join(savePath, name, filename),
path.join(savePath, filename),
path.join(completePath, name, ...filePath.slice(0, -1), filename),
path.join(completePath, name, filename),
path.join(completePath, filename),
]
// Chemin source = dossier torrent + sous-dossiers + fichier let src: string | null = null
const subPath = filePath.length > 1 for (const candidate of candidates) {
? path.join(...filePath.slice(0, -1), filename) console.log(`[organize] candidate: ${candidate} → ${fs.existsSync(candidate) ? '✓ EXISTS' : '✗ absent'}`)
: filename if (fs.existsSync(candidate)) {
src = candidate
break
}
}
let src = path.join(torrentDir, subPath) if (!src) {
if (!fs.existsSync(src)) { const err = `Source introuvable (essayé ${candidates.length} chemins)`
// Fallback: save_path sans le nom du torrent console.error(`[organize] ✗ ${err}`)
src = path.join(torrentDirAlt, subPath) result.errors.push({ file: filename, error: err })
}
if (!fs.existsSync(src)) {
// Fallback: fichier directement dans save_path/name
src = path.join(torrentDir, filename)
}
if (!fs.existsSync(src)) {
result.errors.push({ file: filename, error: `Source introuvable : ${src}` })
continue continue
} }
// Déterminer la saison console.log(`[organize] ✓ source : ${src}`)
let seasonNum = filenameSeasonMap.get(filename)
if (seasonNum === undefined) {
// Fallback : season_number = 1
seasonNum = 1
}
// Construire le chemin destination // Déterminer la saison
const seasonNum = filenameSeasonMap.get(filename) ?? 1
console.log(`[organize] saison : ${seasonNum}`)
// Chemin destination
const destDir = path.join(mediaPath, serieTitle, seasonFolder(seasonNum)) const destDir = path.join(mediaPath, serieTitle, seasonFolder(seasonNum))
const dest = path.join(destDir, filename) const dest = path.join(destDir, filename)
console.log(`[organize] dest : ${dest}`)
// Skip si dest existe déjà // Skip si dest existe déjà
if (fs.existsSync(dest)) { if (fs.existsSync(dest)) {
console.log(`[organize] → dest existe déjà, skip`)
markOrganized(hash, filename) markOrganized(hash, filename)
result.skipped++ result.skipped++
continue continue
@@ -186,33 +200,29 @@ export async function organizeTorrent(
if (organizeMode === 'hardlink') { if (organizeMode === 'hardlink') {
const ok = tryHardlink(src, dest) const ok = tryHardlink(src, dest)
if (!ok) { if (!ok) {
// Hardlink impossible (filesystems différents) → copy console.warn(`[organize] hardlink impossible → fallback copy`)
console.warn(`[organize] hardlink échoué pour ${filename}, fallback copy`)
copyFile(src, dest) copyFile(src, dest)
} }
} else { } else {
// move = copy + delete source
copyFile(src, dest) copyFile(src, dest)
fs.unlinkSync(src) fs.unlinkSync(src)
} }
markOrganized(hash, filename) markOrganized(hash, filename)
result.done++ result.done++
console.log(`[organize] ${organizeMode} ${filename} → ${dest}`) console.log(`[organize] ✓ ${organizeMode} OK`)
} catch (err) { } catch (err) {
result.errors.push({ const msg = err instanceof Error ? err.message : 'Erreur inconnue'
file : filename, console.error(`[organize] ✗ ${msg}`)
error: err instanceof Error ? err.message : 'Erreur inconnue' result.errors.push({ file: filename, error: msg })
})
} }
} }
console.log(`[organize] ── Résultat : ${result.done} OK, ${result.skipped} skippés, ${result.errors.length} erreurs`)
return result return result
} }
// ─── Auto-organise polling ──────────────────────────────────── // ─── Auto-organise polling ────────────────────────────────────
// Appelé depuis index.ts au démarrage, toutes les 30s
// Détecte les torrents "seeding" non encore organisés
export async function autoOrganizeAll( export async function autoOrganizeAll(
listFn: () => Promise<any[]> listFn: () => Promise<any[]>
@@ -220,22 +230,35 @@ export async function autoOrganizeAll(
let torrents: any[] let torrents: any[]
try { try {
torrents = await listFn() torrents = await listFn()
} catch { return } } catch (err) {
console.error('[organize] autoOrganizeAll: impossible de récupérer la liste des torrents:', err)
return
}
console.log(`[organize] autoOrganizeAll: ${torrents.length} torrents récupérés`)
const organized = loadOrganized() const organized = loadOrganized()
for (const t of torrents) { for (const t of torrents) {
if (t.state !== 'seeding') continue if (t.state !== 'seeding') {
console.log(`[organize] skip ${t.name} (state=${t.state})`)
continue
}
// Vérifier si tous les fichiers sont déjà organisés
const torrent = findTorrentByHash(t.hash) const torrent = findTorrentByHash(t.hash)
if (!torrent) continue if (!torrent) {
console.warn(`[organize] hash ${t.hash} (${t.name}) non trouvé dans torrent_final.json`)
continue
}
const files: any[] = torrent.torrent_files ?? [] const files: any[] = torrent.torrent_files ?? []
if (files.length === 0) continue if (files.length === 0) continue
const allDone = files.every(f => organized[t.hash]?.[f.filename]) const allDone = files.every((f: any) => organized[t.hash]?.[f.filename])
if (allDone) continue if (allDone) {
console.log(`[organize] ${t.name} → déjà entièrement organisé`)
continue
}
console.log(`[organize] Auto-organisation : ${t.name}`) console.log(`[organize] Auto-organisation : ${t.name}`)
try { try {