import { getTorrents , pauseTorrents } from '../qbittorrent/api' ; import { logger } from './logger' ; import { SEEDING_STATES } from './constants' ; import { SETTINGS } from '../../settings' ; import { torrentFromApi } from '../interfaces' ; export const preRaceCheck = ( ) => { return new Promise ( async ( resolve , reject ) => { logger . info ( `Getting torrent list` ) ; let torrents ; try { torrents = await getTorrents ( ) ; } catch ( error ) { logger . error ( `Failed to get torrents from qBittorrent` ) ; process . exit ( 1 ) ; } if ( SETTINGS . CONCURRENT_RACES !== - 1 ) { const downloading = torrents . filter ( torrent => torrent . state === 'downloading' ) ; if ( downloading . length > SETTINGS . CONCURRENT_RACES ) { resolve ( false ) ; return ; } logger . info ( `Currently downloading ${ downloading . length } torrents.` ) ; const reannounceYoungest = Date . now ( ) - ( SETTINGS . REANNOUNCE_INTERVAL * SETTINGS . REANNOUNCE_LIMIT ) ; const stalledDL = torrents . filter ( torrent => { if ( torrent . state === 'stalledDL' ) { if ( SETTINGS . COUNT_STALLED_DOWNLOADS === true ) { return true ; } if ( ( torrent . added_on * 1000 ) > reannounceYoungest ) { console . log ( 'This is still in reannounce phase' , torrent ) ; return true ; } return false ; } else { return false ; } } ) ; logger . info ( `Currently there are ${ stalledDL . length } stalled torrents, waiting for race to start` ) ; if ( ( downloading . length + stalledDL . length ) >= SETTINGS . CONCURRENT_RACES ) { resolve ( false ) ; return ; } } if ( SETTINGS . PAUSE_RATIO !== - 1 ) { const uploading = torrents . filter ( torrents => SEEDING_STATES . some ( state => state === torrents . state ) ) ; logger . info ( `Currently seeding ${ uploading . length } torrents.` ) ; const toPause = uploading . filter ( torrent => { if ( torrent . ratio < SETTINGS . PAUSE_RATIO ) { logger . info ( `Ratio for ${ torrent . name } is below threshold. Not pausing.` ) ; return false ; } if ( SETTINGS . PAUSE_SKIP_CATEGORIES . includes ( torrent . category ) ) { logger . info ( `Category ${ torrent . category } for ${ torrent . name } is in the skip list. Will not pause!` ) ; return false ; } const torrentTags = torrent . tags . split ( ',' ) ; if ( SETTINGS . PAUSE_SKIP_TAGS . some ( skipTag => torrentTags . includes ( skipTag ) ) ) { logger . info ( `Torrent ${ torrent . name } has tags (${ torrent . tags }) in common with skip tags. Will not pause!` ) ; return false ; } return true ; } ) ; logger . info ( `Going to pause ${ toPause . length } torrents.` ) ; try { await pauseTorrents ( toPause ) ; } catch ( error ) { logger . error ( `Failed to pause torrents` ) ; process . exit ( 1 ) ; } } resolve ( true ) ; } ) ; } ;