using node to download magnet url

This commit is contained in:
Abhishek Sinha 2018-08-25 16:40:28 +05:30
parent abb7175818
commit 52a750f657
4 changed files with 1288 additions and 68 deletions

1228
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,8 @@
"npm": "^6.3.0",
"popper.js": "^1.14.4",
"watchify": "^3.11.0",
"webtorrent": "^0.102.1"
"webtorrent": "^0.102.1",
"webtorrent-hybrid": "^1.0.6"
},
"devDependencies": {
"nodemon": "^1.18.3"

View File

@ -3,6 +3,7 @@ const router = express.Router();
const { check, validationResult } = require('express-validator/check')
const { matchedData } = require('express-validator/filter')
const _ = require('lodash');
const WebtorrentHybrid = require('webtorrent-hybrid')
const client = require('./server.js');
const funcs = require('./public/js/funcs')
@ -42,6 +43,56 @@ router.get('/download-magnetic-uri', (req, res)=>{
})
})
router.post('/download-magnetic-uri', (req, res)=>{
let params = _.pick(req.body, ['job', 'torrentId'])
if (params.job != 'download-magnetic-uri') {
return
}
var client = new WebtorrentHybrid()
client.on('error', function (err) {
console.error('ERROR: ' + err.message)
})
var torrentId = params.torrentId
if (_.trim(torrentId) == '') {
//alert("Please specify a magnetic url.");
console.error("Empty torrent id");
return;
}
client.add(torrentId, function (torrent) {
var files = torrent.files
var length = files.length
// Stream each file to the disk
files.forEach(function (file) {
console.log(file);
let fullpath = path.resolve(file.path)
var source = file.createReadStream()
var destination = fs.createWriteStream(`files/${file.name}`)
source.on('end', function () {
console.log('file:\t\t', file.name)
//console.log(destination);
//res.json({file:file.name, location:fullpath})
res.json({file:file.name, location:fullpath})
// close after all files are saved
if (!--length) process.exit()
}).pipe(destination)
})
})
})
router.get('/send-to-blockchain', (req, res)=>{
res.render('to_flo', {
data: {},
@ -156,7 +207,7 @@ router.post('/fetch-from-blockchain', [
for (let t = 0; t < lt.length; t++) {
const elem = lt[t];
if (elem.address==floaddr && elem.category=='send') {
if (elem.address==floaddr && elem.category=='receive') {
tx_arr.push(elem.txid)
}
}

View File

@ -19,55 +19,41 @@
$(document).on('click', '#dwn-seed', function() {
var client = new WebTorrent()
// var client = new WebTorrent()
var torrentId = document.getElementById('mag_text').value;
// client.on('error', function (err) {
// console.error('ERROR: ' + err.message)
// })
if (torrentId.trim() == '') {
alert("Please specify a magnetic url.");
return;
}
var torrentId = document.getElementById('mag_text').value;
client.add(torrentId, function (torrent) {
// Torrents can contain many files. Let's use the .mp4 file
var file = torrent.files.find(function (file) {
console.log(file);
if (file.name.endsWith('.mp4')) {
return file.name.endsWith('.mp4')
} else if (file.name.endsWith('.webm')) {
return file.name.endsWith('.webm')
} else if (file.name.endsWith('.avi')) {
return file.name.endsWith('.avi')
} else if (file.name.endsWith('.pdf')) {
return file.name.endsWith('.pdf')
} else if (file.name.endsWith('.mp3')) {
return file.name.endsWith('.mp3')
} else if (file.name.endsWith('.jpg')) {
return file.name.endsWith('.jpg')
} else if (file.name.endsWith('.png')) {
return file.name.endsWith('.png')
} else if (file.name.endsWith('.gif')) {
return file.name.endsWith('.gif')
} else if (file.name.endsWith('.jpeg')) {
return file.name.endsWith('.jpeg')
}
})
if (torrentId.trim() == '') {
alert("Please specify a magnetic url.");
return;
}
console.log('Client is downloading:', torrent.infoHash)
// Display the file by adding it to the DOM. Supports video, audio, image, etc. files
file.appendTo('#mag-res')
var job = 'download-magnetic-uri';
file.getBlobURL(function (err, url) {
if (err) throw err
var a = document.createElement('a')
a.download = file.name
a.href = url
a.textContent = 'Download ' + file.name
document.getElementById("dwld").appendChild(a)
})
$.ajax({
url: '/download-magnetic-uri',
type: 'post',
data: {job:job, torrentId:torrentId},
success: function(response) {
console.log(response);
if (typeof response.file != undefined) {
let file = response.file;
var a = document.createElement('a')
a.download = `files/${response.file}`
a.href = response.location
a.textContent = 'Download ' + response.file
document.getElementById("dwld").appendChild(a)
}
})
},
error: function(e) {
console.error(e);
}
});
});