This commit is contained in:
2022-01-03 17:56:00 +01:00
parent 4d3d8c874c
commit 1d25914ae0

View File

@@ -1,95 +1,100 @@
const assert = require('assert'); const assert = require("assert");
const http = require('http'); const http = require("http");
const { URL } = require('url'); const { URL } = require("url");
const PythonShell = require('python-shell'); const PythonShell = require("python-shell");
const establishedDatabase = require('src/database/database'); const establishedDatabase = require("src/database/database");
const RedisCache = require('src/cache/redis') const RedisCache = require("src/cache/redis");
const cache = new RedisCache() const cache = new RedisCache();
function getMagnetFromURL(url) { function getMagnetFromURL(url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const options = new URL(url); const options = new URL(url);
if (options.protocol.includes('magnet')) if (options.protocol.includes("magnet")) resolve(url);
resolve(url)
http.get(options, (res) => { http.get(options, res => {
if (res.statusCode == 301 || res.statusCode == 302) { if (res.statusCode == 301 || res.statusCode == 302) {
resolve(res.headers.location) resolve(res.headers.location);
} }
}); });
}); });
} }
async function find(searchterm, callback) { async function find(searchterm, callback) {
const options = { const options = {
pythonPath: '../torrent_search/env/bin/python3', pythonPath: "../torrent_search/env/bin/python3",
scriptPath: '../torrent_search', scriptPath: "../torrent_search",
args: [searchterm, '-s', 'jackett', '-f', '--print'] args: [searchterm, "-s", "jackett", "-f", "--print"]
} };
PythonShell.run('torrentSearch/search.py', options, callback); PythonShell.run("torrentSearch/search.py", options, callback);
// PythonShell does not support return // PythonShell does not support return
} }
async function callPythonAddMagnet(url, callback) { async function callPythonAddMagnet(url, callback) {
getMagnetFromURL(url) getMagnetFromURL(url)
.then((magnet) => { .then(magnet => {
const options = { const options = {
pythonPath: '../delugeClient/env/bin/python3', pythonPath: "../delugeClient/env/bin/python3",
scriptPath: '../delugeClient', scriptPath: "../delugeClient",
args: ['add', magnet] args: ["add", magnet]
}; };
PythonShell.run('deluge_cli.py', options, callback); PythonShell.run("deluge_cli.py", options, callback);
}) })
.catch((err) => { .catch(err => {
console.log(err); console.log(err);
throw new Error(err); throw new Error(err);
}) });
} }
async function SearchPiratebay(query) { async function SearchPiratebay(query) {
const cacheKey = `pirate/${query}` const cacheKey = `pirate/${query}`;
return new Promise((resolve, reject) => cache.get(cacheKey) return new Promise((resolve, reject) =>
.then(resolve) cache
.catch(() => find(query, (err, results) => { .get(cacheKey)
if (err) { .then(resolve)
console.log('THERE WAS A FUCKING ERROR!\n', err); .catch(() =>
reject(Error('There was a error when searching for torrents')); find(query, (err, results) => {
} if (err) {
console.log("THERE WAS A FUCKING ERROR!\n", err);
reject(Error("There was a error when searching for torrents"));
}
if (results) { if (results) {
const jsonData = JSON.parse(results, null, '\t') const jsonData = JSON.parse(results, null, "\t");
cache.set(cacheKey, jsonData) cache.set(cacheKey, jsonData);
resolve(jsonData); resolve(jsonData);
} }
})) })
)
); );
} }
async function AddMagnet(magnet, name, tmdb_id) { async function AddMagnet(magnet, name, tmdb_id) {
return await new Promise((resolve, reject) => callPythonAddMagnet(magnet, (err, results) => { return await new Promise((resolve, reject) =>
callPythonAddMagnet(magnet, (err, results) => {
if (err) { if (err) {
/* eslint-disable no-console */ /* eslint-disable no-console */
console.log(err); console.log(err);
reject(Error('Enable to add torrent', err)) reject(Error("Enable to add torrent", err));
} }
/* eslint-disable no-console */ /* eslint-disable no-console */
console.log('result/error:', err, results); console.log("result/error:", err, results);
database = establishedDatabase; database = establishedDatabase;
insert_query = "INSERT INTO requested_torrent(magnet,torrent_name,tmdb_id) \ insert_query =
"INSERT INTO requested_torrent(magnet,torrent_name,tmdb_id) \
VALUES (?,?,?)"; VALUES (?,?,?)";
let response = database.run(insert_query, [magnet, name, tmdb_id]); let response = database.run(insert_query, [magnet, name, tmdb_id]);
console.log('Response from requsted_torrent insert: ' + response); console.log("Response from requsted_torrent insert: " + response);
resolve({ success: true }); resolve({ success: true });
})); })
);
} }
module.exports = { SearchPiratebay, AddMagnet }; module.exports = { SearchPiratebay, AddMagnet };