Skip to content

Commit

Permalink
[node] Working in fetch magnet code
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 3, 2016
1 parent 49b24fa commit 4c553bc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 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);
});
29 changes: 29 additions & 0 deletions node/jlibtorrent.js
Expand Up @@ -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;

}());

0 comments on commit 4c553bc

Please sign in to comment.