Skip to content

Instantly share code, notes, and snippets.

@teidesu
Created June 22, 2021 13:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teidesu/5e23c5f2af0b6b435ccbe47b805e6f82 to your computer and use it in GitHub Desktop.
Save teidesu/5e23c5f2af0b6b435ccbe47b805e6f82 to your computer and use it in GitHub Desktop.
Simple NodeJS function to convert .torrent files to magnet URIs
// This file is licensed under LGPLv3
// (c) teidesu 2020
const qs = require('querystring')
const crypto = require('crypto')
// https://gist.github.com/teidesu/a1eadf71ffd88166415e2ea8b94d3f3b
const bencode = require('./bencode')
function convert (file) {
const data = bencode.decode(f)
const infoHash = crypto.createHash('sha1').update(encode(data.info)).digest('hex')
let params = {
xt: `urn:btih:${infoHash}`,
tr: []
}
if ('length' in data.info) {
params.xl = data.info.length
}
if ('name' in data.info) {
params.dn = data.info.name.toString('utf-8')
}
if ('announce' in data) {
params.tr.push(data.announce.toString('utf-8'))
}
if ('announce-list' in data && Array.isArray(data['announce-list'])) {
params.tr.push(...data['announce-list'].map(ann => ann.toString('utf-8')))
}
return 'magnet:?' + qs.stringify(params)
}
// usage:
console.log(convert(require('fs').readFileSync('lorem-ipsum.torrent')))
@negu63
Copy link

negu63 commented Nov 21, 2022

Perhaps the 'encode()' on line 12 is 'qs.encode()'.

@teidesu
Copy link
Author

teidesu commented Nov 21, 2022

Perhaps the 'encode()' on line 12 is 'qs.encode()'.

perhaps
I never ended up using this 😅

@negu63
Copy link

negu63 commented Nov 22, 2022

This has been a great help to me thank you 😊

@Kiriox94
Copy link

When I try to import the magnets it tells me that it is invalid, did you have a solution?

@negu63
Copy link

negu63 commented Nov 28, 2022

When I try to import the magnets it tells me that it is invalid, did you have a solution?

Can you show the code and error message?

@Kiriox94
Copy link

image

@Kiriox94
Copy link

I manage to import the magnets, it's just the one that doesn't work

@negu63
Copy link

negu63 commented Nov 29, 2022

You should provide code so I can reproduce the error. 😅
And the execution environment must also be informed.
I can't tell just by looking at the magnet.

@Kiriox94
Copy link

Here is my magnet generated by the script:
magnet:?xt=urn%3Abtih%3Afbe00679d7f8ee32ebbee1a5b51060117fce0b94&tr=http%3A%2F%2Fp2p.yggtracking.org%3A8080%2FtqL6KCot0p6wvZLHREAYBU5qg1UBgDOp%2Fannounce&xl=4939938707&dn=Foot.CDM22.GroupeD.Tunisie.Australie.261122.MULTi.1080p.HDTV.x264-BABA.mkv
And as a client uses alldebrid

@negu63
Copy link

negu63 commented Nov 29, 2022

I'm sorry. You are right.
I investigated and found that the process of reading and interpreting the file was wrong.
So infoHash and announce etc were being entered incorrectly.
So I found a new way.
I'm attaching a new code that uses a package called 'parse-torrent'! 😄

import parseTorrent from "parse-torrent";
import fs from "fs";
import qs from "querystring";

function convert(file) {
  const data = parseTorrent(file);

  let params = {
    xt: `urn:btih:${data.infoHash}`,
    dn: data.name,
    tr: data.announce,
  };

  return "magnet:?" + qs.stringify(params)
}

// usage:
console.log(convert(fs.readFileSync("lorem-ipsum.torrent")));

@Kiriox94
Copy link

I still have the same error

@negu63
Copy link

negu63 commented Nov 30, 2022

I tested using Bittorrent and it worked fine. I'm not sure what's wrong. 😥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment