JimmyLaurent / torrent-search-api

Yet another node torrent scraper (supports iptorrents, torrentleech, torrent9, torrentz2, 1337x, thepiratebay, Yggtorrent, TorrentProject, Eztv, Yts, LimeTorrents)
MIT License
395 stars 99 forks source link

cannot read property "toLowerCase' of undefined when getting magnet links #111

Closed BrianRigii closed 4 years ago

BrianRigii commented 4 years ago

hi i keep getting this error when trying to get magnet links

// my code

const TorrentSearchApi = require('torrent-search-api')`
TorrentSearchApi.enableProvider('1337x')

async function getTorrents(){
 const torrent =  await TorrentSearchApi.search('1080', 'Movies', 20)
 const magnet = await TorrentSearchApi.getMagnet(torrent)

   console.log(magnet)
}
getTorrents()
BreakLime commented 4 years ago

That's your problem:

TorrentSearchApi.search() will return an array of torrent objects. TorrentSearchApi.getMagnet() expects one torrent object as a parameter.

So instead of doing this:

const magnet = await TorrentSearchApi.getMagnet(torrent)

Do this:

const magnet = await TorrentSearchApi.getMagnet(torrent[0])

Or a much better solution would be to loop over it:

const TorrentSearchApi = require('torrent-search-api')
TorrentSearchApi.enableProvider('1337x')

async function getTorrents() {
    const torrents = await TorrentSearchApi.search('1080', 'Movies', 20)

    // Get all magnets
    const magnets = await Promise.all(
        torrents.map(async torrent => {
            let magnet = await TorrentSearchApi.getMagnet(torrent)
            return magnet
        })
    )

    // Combine torrents with magnets
    torrents.map((torrent, i) => torrent.magnet = magnets[i])

    return torrents
}
getTorrents().then(value => {
    console.log(value)
})
    .catch(e => {
        console.log(e)
    })
BrianRigii commented 4 years ago

Oohh Thanks, also quick question how can you stream videos from the magnet links 😅

BreakLime commented 4 years ago

Check out Webtorrent