From 4c553bc58d26f55453699a5bcb963cbe629fa386 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Wed, 3 Feb 2016 15:44:30 -0500 Subject: [PATCH] [node] Working in fetch magnet code --- node/fetch_magnet.js | 17 +++++++++++++++++ node/jlibtorrent.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 node/fetch_magnet.js diff --git a/node/fetch_magnet.js b/node/fetch_magnet.js new file mode 100644 index 000000000..69eb40057 --- /dev/null +++ b/node/fetch_magnet.js @@ -0,0 +1,17 @@ +const jlibtorrent = require('./jlibtorrent.js'); + +console.log("Using libtorrent version: " + jlibtorrent.LibTorrent.fullVersion()); + +const l = function (a) { + console.log(a.type() + " - " + a.what() + " - " + a.message()); +} + +const sp = new jlibtorrent.SettingsPack(); +const s = new jlibtorrent.Session(sp, false, l); + +s.fetchMagnet("magnet:?xt=urn:btih:a83cc13bf4a07e85b938dcf06aa707955687ca7c"); + +process.stdout.write('Press ENTER to exit...'); +process.stdin.once('data', function (data) { + process.exit(0); +}); diff --git a/node/jlibtorrent.js b/node/jlibtorrent.js index 29db8cc40..a166f2320 100644 --- a/node/jlibtorrent.js +++ b/node/jlibtorrent.js @@ -170,6 +170,35 @@ const swig = require('./jlibtorrent.node'); return this.sp; } + Session.prototype.fetchMagnet = function (uri) { + var p = swig.add_torrent_params.create_instance_disabled_storage(); + var ec = new swig.error_code(); + swig.parse_magnet_uri(uri, p, ec); + + if (ec.value() != 0) { + throw ec.message(); + } + + var info_hash = p.info_hash; + + var th = this.s.find_torrent(info_hash); + if (th && th.is_valid()) { + // we have a download with the same info-hash, improve this + return; + } + + p.name = "fetch_magnet:" + uri; + p.save_path = "fetch_magnet/" + uri; + + var flags = p.flags; + flags &= ~swig.add_torrent_params.flag_auto_managed; + p.flags = flags; + + ec.clear(); + th = this.s.add_torrent(p, ec); + th.resume(); + } + exports.Session = Session; }());