Skip to content

Instantly share code, notes, and snippets.

@pwnedDesal
Created July 30, 2021 03:45
Show Gist options
  • Save pwnedDesal/e7f77c8c4f9cbcda5d3a025c3bf6770c to your computer and use it in GitHub Desktop.
Save pwnedDesal/e7f77c8c4f9cbcda5d3a025c3bf6770c to your computer and use it in GitHub Desktop.
var express = require("express");
var app = express();
var path = require("path");
const fs = require("fs");
var WebTorrent = require("webtorrent");
var client = new WebTorrent();
let dataFile = require("./torrent.json");
let torrentInfo = [];
function downloadTorrent(magnet) {
try {
client.add(magnet, { path: "test" }, function (torrent) {
torrent.on("done", function () {
console.log("torrent download finished");
});
torrent.on("download", function (bytes) {
let torrentid = torrentInfo.findIndex(
(info) => info.infoHash === torrent.infoHash
);
if (torrentid >= 0) {
torrentInfo[torrentid].downloadSpeed = torrent.downloadSpeed;
torrentInfo[torrentid].progress = torrent.progress;
} else {
info = {
infoHash: torrent.infoHash,
downloadSpeed: torrent.downloadSpeed,
progress: torrent.progress,
name: torrent.name,
};
torrentInfo.push(info);
}
//console.log(torrentInfo);
//console.log('just downloaded: ' + bytes)
//console.log(torrent.infoHash);
//console.log("total downloaded: " + torrent.downloaded + " bytes");
//console.log('download speed: ' + torrent.downloadSpeed)
//console.log("progress: " + torrent.progress);
//res.send({ status: torrent.progress.toString() });
});
torrent.on("error", function (err) {
res.send(err);
});
});
} catch (err) {
console.error("got error", err);
}
}
//add new magnet into torrent.json
app.get("/add", function (req, res) {
let magnet = req.query.magnet;
//get magnet via url, put it in the torrent.json
//download it
if (magnet != null) {
downloadTorrent(magnet);
let data = {};
let torrent = null;
data = fs.readFileSync("torrent.json");
torrent = JSON.parse(data);
let urldecode = decodeURI(magnet);
data = {
name: "",
magnet: urldecode,
id: torrent["data"].length,
};
torrent["data"].push(data);
torrentstr = JSON.stringify(torrent);
fs.writeFile("torrent.json", torrentstr, (err) => {
if (err) throw err;
console.log("Data written to file");
});
res.send("added to torrent.json");
}
});
//download all torrents saved on torrent.json
app.get("/main", function (req, res) {
fs.readFile("torrent.json", (err, data) => {
if (err) throw err;
let torrent = JSON.parse(data);
torrent.data.map((torrent) => {
//console.log(torrent.magnet);
downloadTorrent(torrent.magnet);
});
res.send("finished");
});
});
//shows information about downloaded torrents
app.get("/info", function (req, res) {
if (torrentInfo.length === 0) {
res.send("no downloaded torrent info is provided");
} else {
res.set({
"Content-Type": "text/json",
});
res.send(torrentInfo);
}
});
app.get("/read", function (req, res) {
fs.readFile("torrent.json", (err, data) => {
if (err) throw err;
let torrent = JSON.parse(data);
console.log(torrent.data[0]);
});
});
//parses torrent.json
app.get("/jsontest", function (req, res) {
fs.readFile("torrent.json", (err, data) => {
if (err) throw err;
let torentFiles = JSON.parse(data);
console.log(torentFiles);
});
});
app.get("/parse", function (req, res) {
console.log(dataFile.data[0].name);
res.send(dataFile.data[1].name);
});
//outputs torrent.json
app.get("/torrent.json", function (req, res) {
res.sendFile(path.resolve("torrent.json"));
res.set("Content-Type", "text/json");
});
app.post("/submit-data", function (req, res) {
res.send("POST Request");
});
app.put("/update-data", function (req, res) {
res.send("PUT Request");
});
app.delete("/delete-data", function (req, res) {
res.send("DELETE Request");
});
var server = app.listen(5000, function () {
console.log("Node server is running..");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment