import axios from 'axios' ; import * as FormData from 'form-data' ; import * as fs from 'fs' ; import { QBIT_HOST , QBIT_PORT , COOKIE , HTTP_SCHEME , URL_PATH } from '../config' ; import { logger } from '../helpers/logger' ; import { torrentFromApi , TransferInfo } from '../interfaces' ; const basePath = `${ HTTP_SCHEME }://${ QBIT_HOST }:${ QBIT_PORT }${ URL_PATH }` ; export const getTorrentInfo = ( infohash ) : Promise < torrentFromApi > => { return new Promise ( ( resolve , reject ) => { axios . get ( `${ basePath }/api/v2/torrents/info` , { params : { hashes : infohash } , headers : { 'Cookie' : COOKIE } } ) . then ( response => { resolve ( response . data [ 0 ] ) ; } ) . catch ( error => { logger . error ( `Get torrent info API failed with error code ${ error . response . status }` ) ; reject ( error . response . status ) ; } ) ; } ) ; } ; export const getTorrents = ( ) : Promise < torrentFromApi [ ] > => { return new Promise ( ( resolve , reject ) => { axios . get ( `${ basePath }/api/v2/torrents/info` , { headers : { 'Cookie' : COOKIE } } ) . then ( response => { resolve ( response . data ) ; } ) . catch ( error => { logger . error ( `Get torrents API failed with error code ${ error . response . status }` ) ; reject ( error . response . status ) ; } ) ; } ) ; } ; export const pauseTorrents = ( torrents ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { if ( torrents . length === 0 ) { resolve ( ) ; return ; } const infohashes = torrents . map ( torrent => torrent . hash ) ; axios . get ( `${ basePath }/api/v2/torrents/pause` , { params : { hashes : infohashes . join ( '|' ) } , headers : { 'Cookie' : COOKIE } } ) . then ( response => { logger . info ( `Successfully paused ${ infohashes . length } torrents!` ) ; resolve ( ) ; } ) . catch ( error => { logger . error ( `Pause torrents API failed with error code ${ error . response . status }` ) ; reject ( ) ; } ) ; } ) ; } ; export const resumeTorrents = ( torrents ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { if ( torrents . length === 0 ) { resolve ( ) ; return ; } const infohashes = torrents . map ( torrent => torrent . hash ) ; axios . get ( `${ basePath }/api/v2/torrents/resume` , { params : { hashes : infohashes . join ( '|' ) } , headers : { 'Cookie' : COOKIE } } ) . then ( response => { logger . info ( `Successfully resumed ${ infohashes . length } torrents!` ) ; resolve ( ) ; } ) . catch ( error => { logger . error ( `Resume torrents API failed with error code ${ error . response . status }` ) ; reject ( ) ; } ) ; } ) ; } ; export const deleteTorrents = ( torrents ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { if ( torrents . length === 0 ) { resolve ( ) ; return ; } const infohashes = torrents . map ( torrent => torrent . hash ) ; axios . get ( `${ basePath }/api/v2/torrents/delete` , { params : { hashes : infohashes . join ( '|' ) , deleteFiles : true } , headers : { 'Cookie' : COOKIE } } ) . then ( response => { logger . info ( `Successfully deleted ${ torrents . length } torrents.` ) ; resolve ( ) ; } ) . catch ( error => { logger . error ( `Delete torrents API failed with error code ${ error . response . status }` ) ; reject ( ) ; } ) ; } ) ; } ; export const addTags = ( torrents , tags ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { if ( torrents . length === 0 ) { resolve ( ) ; return ; } if ( tags . length === 0 ) { resolve ( ) ; return ; } const infohashes = torrents . map ( torrent => torrent . hash ) ; let payload = `hashes=${ infohashes . join ( '|' ) }&tags=${ tags . join ( ',' ) }` ; axios . request ( { method : 'POST' , url : `${ basePath }/api/v2/torrents/addTags` , data : payload , headers : { 'Cookie' : COOKIE , 'Content-Type' : 'application/x-www-form-urlencoded' } } ) . then ( response => { logger . info ( `Successfully added ${ tags . length } tags to ${ torrents . length } torrents.` ) ; resolve ( ) ; } ) . catch ( error => { logger . error ( `Add tags API failed with error code ${ error . response . status }` ) ; } ) ; } ) ; } ; export const setCategory = ( infohash , category ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { let payload = `hashes=${ infohash }&category=${ category }` ; axios . request ( { method : 'POST' , url : `${ basePath }/api/v2/torrents/setCategory` , data : payload , headers : { 'Cookie' : COOKIE , 'Content-Type' : 'application/x-www-form-urlencoded' , } } ) . then ( response => { logger . info ( `Successfully set category for ${ infohash } to ${ category }` ) ; resolve ( ) ; } ) . catch ( error => { logger . error ( `Set category API failed with error code ${ error . response . status }. Make sure the category exists!` ) ; reject ( error . response . status ) ; } ) ; } ) ; } ; export const addTorrent = ( path , category ? ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { let formData = new FormData ( ) ; try { const torrentData = fs . readFileSync ( path ) ; formData . append ( "torrents" , torrentData , 'dummy.torrent' ) ; } catch ( error ) { logger . error ( `Unable to read file ${ path }` ) ; reject ( ) ; } if ( category !== null ) { logger . info ( `Setting category to ${ category }` ) ; formData . append ( 'category' , category ) ; } axios . request ( { method : 'POST' , url : `${ basePath }/api/v2/torrents/add` , headers : { 'Cookie' : COOKIE , ... formData . getHeaders ( ) , 'Content-Length' : formData . getLengthSync ( ) } , data : formData } ) . then ( response => { logger . info ( `Successfully added to qBittorrent!` ) ; resolve ( ) ; } ) . catch ( error => { logger . error ( `Add torrent API failed with error code ${ error . response . status }` ) ; reject ( ) ; } ) ; } ) ; } ; export const getTrackers = ( infohash ) : Promise < any [ ] > => { return new Promise ( ( resolve , reject ) => { axios . get ( `${ basePath }/api/v2/torrents/trackers` , { params : { hash : infohash } , headers : { 'Cookie' : COOKIE } } ) . then ( response => { resolve ( response . data ) ; } ) . catch ( error => { logger . error ( `Get trackers API failed with error code ${ error . response . status }` ) ; reject ( error ) ; } ) ; } ) ; } ; export const reannounce = ( infohash ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { axios . get ( `${ basePath }/api/v2/torrents/reannounce` , { params : { hashes : infohash } , headers : { 'Cookie' : COOKIE } } ) . then ( response => { resolve ( ) ; } ) . catch ( error => { logger . error ( `Reannounce API failed with error code ${ error . response . status }` ) ; reject ( error ) ; } ) ; } ) ; } ; export const getTransferInfo = ( ) : Promise < TransferInfo > => { return axios . get ( `${ basePath }/api/v2/transfer/info` , { headers : { 'Cookie' : COOKIE } } ) . then ( response => { return response . data ; } ) . catch ( err => { logger . error ( `Get transferInfo failed with error code ${ err . response . status }` ) ; throw err ; } ) ; } ;