parse-torrent
Parse a torrent identifier (magnet uri, .torrent file, info hash)
Works in node and the browser (with browserify). This module is used by WebTorrent!
install
npm install parse-torrent
usage
parse
The return value of parseTorrent
will contain as much info as possible about the
torrent. The only property that is guaranteed to be present is infoHash
.
const parseTorrent = require('parse-torrent')
const fs = require('fs')
parseTorrent('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36')
parseTorrent(new Buffer('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36', 'hex'))
parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36')
parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves%20of%20Grass%20by%20Walt%20Whitman.epub')
parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&tr=http%3A%2F%2Ftracker.example.com%2Fannounce')
parseTorrent(fs.readFileSync(__dirname + '/torrents/leaves.torrent'))
encode
The reverse works too. To convert an object of keys/value to a magnet uri or .torrent file
buffer, use toMagnetURI
and toTorrentFile
.
const parseTorrent = require('parse-torrent')
const uri = parseTorrent.toMagnetURI({
infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36'
})
console.log(uri)
const buf = parseTorrent.toTorrentFile({
info: {
}
})
console.log(buf)
remote torrents
To support remote torrent identifiers (i.e. http/https links to .torrent files, or
filesystem paths), as well as Blobs use the parseTorrent.remote
function. It takes
a callback since these torrent types require async operations:
parseTorrent.remote(torrentId, (err, parsedTorrent) => {
if (err) throw err
console.log(parsedTorrent)
})
If the torrentId
is an http/https link to the .torrent file, then the request to the file
can be modified by passing simple-get
params. For example:
parseTorrent.remote(torrentId, { timeout: 60 * 1000 }, (err, parsedTorrent) => {
if (err) throw err
console.log(parsedTorrent)
})
command line program
This package also includes a command line program.
Usage: parse-torrent /path/to/torrent
parse-torrent magnet_uri
parse-torrent --stdin
To install it, run:
npm install parse-torrent -g
license
MIT. Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC.