Created
August 3, 2020 08:47
-
-
Save djalilhebal/dc320ac8b4747a65e4f0544b30a2e25f to your computer and use it in GitHub Desktop.
A JavaScript function that converts a BitTorrent info-hash to a magnet URI with "the best public trackers"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Convert a BitTorrent info-hash to a magnet URI that contains "the best public trackers" | |
* @see https://en.wikipedia.org/wiki/Magnet_URI_scheme | |
* @see https://github.com/ngosang/trackerslist | |
* @example | |
* toMagnetURI({hash: '286d2e5b4f8369855328336ac1263ae02a7a60d5', name: 'Ubuntu 18.04 x64'}); | |
* // returns a magnet link for http://releases.ubuntu.com/18.04/ubuntu-18.04.4-desktop-amd64.iso.torrent | |
* | |
* @param {({hash: string, name?: string})} info | |
* @returns {string} magnet uri | |
*/ | |
function toMagnetURI({hash, name = "nameless"}) { | |
// @ngosang/trackerslist - Updated 2020-08-03 - trackers_best.txt | |
const TRACKERS_LIST = | |
`udp://tracker.coppersurfer.tk:6969/announce | |
udp://tracker.opentrackr.org:1337/announce | |
udp://tracker.openbittorrent.com:80/announce | |
udp://tracker.internetwarriors.net:1337/announce | |
udp://tracker.leechers-paradise.org:6969/announce | |
udp://p4p.arenabg.com:1337/announce | |
udp://9.rarbg.to:2710/announce | |
udp://9.rarbg.me:2710/announce | |
udp://exodus.desync.com:6969/announce | |
udp://tracker.cyberia.is:6969/announce | |
udp://retracker.lanta-net.ru:2710/announce | |
udp://open.stealth.si:80/announce | |
udp://tracker3.itzmx.com:6961/announce | |
udp://tracker.tiny-vps.com:6969/announce | |
http://tracker1.itzmx.com:8080/announce | |
udp://tracker.torrent.eu.org:451/announce | |
udp://tracker.moeking.me:6969/announce | |
udp://ipv4.tracker.harry.lu:80/announce | |
udp://bt2.archive.org:6969/announce | |
udp://bt1.archive.org:6969/announce | |
`; | |
const trackers = TRACKERS_LIST.trim().split(/\s+/); | |
const uri = [ | |
`magnet:?xt=urn:btih:${hash}`, | |
`dn=${encodeURIComponent(name)}`, | |
...trackers.map(tr => `tr=${encodeURIComponent(tr)}`), | |
].join('&'); | |
return uri; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment