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.
import parseTorrent from 'parse-torrent'import fs from 'fs'// info hash (as a hex string)parseTorrent('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36')// { infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }// info hash (as a Buffer)parseTorrent(new Buffer('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36', 'hex'))// { infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }// magnet uri (as a utf8 string)parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36')// { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',// infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }// magnet uri with torrent nameparseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves%20of%20Grass%20by%20Walt%20Whitman.epub')// { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
encode
The reverse works too. To convert an object of keys/value to a magnet uri or .torrent file
buffer, use toMagnetURI and toTorrentFile.
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:
import { remote } from 'parse-torrent'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: