Skip to content

Instantly share code, notes, and snippets.

@rhyzx
Created June 14, 2014 16:36
Show Gist options
  • Save rhyzx/7a30f9963cccea962237 to your computer and use it in GitHub Desktop.
Save rhyzx/7a30f9963cccea962237 to your computer and use it in GitHub Desktop.
Magnet parsing API in node
// parse magnet
var magnetUri = require('magnet-uri')
var readTorrent = require('read-torrent')
var humanForamt = require('human-format')
app.get('/magnet', function (req, res) {
var uri = req.param('u')
var info = magnetUri(uri)
readTorrent('http://torrage.com/torrent/' + info.infoHash.toUpperCase() + '.torrent', function(err, torrent) {
if (err) return res.jsonp(500, { error: err })
// clean info
res.jsonp({
infoHash: torrent.infoHash,
name: torrent.name,
files: torrent.files.map(function (item, i) {
return {
index: i,
name: item.name,
length: item.length,
length_f: humanForamt(item.length)
}
}).filter(function (item, i) {
// remove padding file
// BUG? SPEC?
return !/^_____padding_file_/.test(item.name)
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment