import { FinalTorrent } from "./types" ; import axios from "axios" ; const urlencode = require ( "urlencode" ) ; const torrentApi = require ( "torrent-search-api" ) ; torrentApi . enableProvider ( "1337x" ) ; description param torrent returns const grabTorrents = async ( torrent ) : Promise < FinalTorrent [ ] > => { try { let torrents = await torrentApi . search ( torrent ) ; let finalTorrents = [ ] ; for ( let i = 0 ; i < 10 ; i ++ ) { if ( torrents [ i ] != null ) { finalTorrents . push ( torrents [ i ] ) ; } } for ( let i = 0 ; i < finalTorrents . length ; i ++ ) { const longMagnet = await torrentApi . getMagnet ( finalTorrents [ i ] ) ; finalTorrents [ i ] . magnet = await shorten ( longMagnet ) ; finalTorrents [ i ] . number = i + 1 ; } return finalTorrents ; } catch ( error ) { console . log ( error ) ; return [ ] ; } } ; description param magnet returns const shorten = async ( magnet ) : Promise < string > => { try { let encoded = urlencode ( magnet ) ; const resp = await axios . get ( `http://mgnet.me/api/create?&format=json&opt=&m=${ encoded }` ) ; return resp . data . shorturl ; } catch ( err ) { console . log ( err ) ; } } ; export { shorten , grabTorrents } ;