-
-
Save teidesu/5e23c5f2af0b6b435ccbe47b805e6f82 to your computer and use it in GitHub Desktop.
// 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'))) |
Perhaps the 'encode()' on line 12 is 'qs.encode()'.
perhaps
I never ended up using this 😅
This has been a great help to me thank you 😊
When I try to import the magnets it tells me that it is invalid, did you have a solution?
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?
I manage to import the magnets, it's just the one that doesn't work
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.
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
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")));
I still have the same error
I tested using Bittorrent and it worked fine. I'm not sure what's wrong. 😥
Perhaps the 'encode()' on line 12 is 'qs.encode()'.