Jackett sometimes returns a link to self that redirects to a magnet. The response is now parsed for if it is a link and gets the url it redirects to.

This commit is contained in:
2018-03-08 16:35:17 +01:00
parent 3abd7ddc61
commit 47b499c072

View File

@@ -1,11 +1,27 @@
const assert = require('assert'); const assert = require('assert');
const http = require('http');
const { URL } = require('url');
const PythonShell = require('python-shell'); const PythonShell = require('python-shell');
function getMagnetFromURL(url) {
return new Promise((resolve, reject) => {
const options = new URL(url);
if (options.protocol.includes('magnet'))
resolve(url)
http.get(options, (res) => {
if (res.statusCode == 301) {
resolve(res.headers.location)
}
});
});
}
async function find(searchterm, callback) { async function find(searchterm, callback) {
const options = { const options = {
pythonPath: '/usr/bin/python3', pythonPath: '/usr/bin/python3',
// pythonPath: '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3', // pythonPath: '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3',
args: [searchterm, '-s', 'piratebay', '--print'], args: [searchterm, '-s', 'jackett', '--print'],
}; };
PythonShell.run('../app/torrent_search/torrentSearch/search.py', options, callback); PythonShell.run('../app/torrent_search/torrentSearch/search.py', options, callback);
@@ -13,21 +29,28 @@ async function find(searchterm, callback) {
} }
async function callPythonAddMagnet(magnet, callback) { async function callPythonAddMagnet(url, callback) {
const options = { getMagnetFromURL(url)
pythonPath: '/usr/bin/python', .then((magnet) => {
// pythonPath: '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3', const options = {
args: [magnet], pythonPath: '/usr/bin/python',
}; // pythonPath: '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3',
args: [magnet],
};
PythonShell.run('../app/magnet.py', options, callback); PythonShell.run('../app/magnet.py', options, callback);
})
.catch((err) => {
console.log(err);
throw new Error(err);
})
} }
async function SearchPiratebay(query) { async function SearchPiratebay(query) {
return await new Promise((resolve, reject) => find(query, (err, results) => { return await new Promise((resolve, reject) => find(query, (err, results) => {
if (err) { if (err) {
/* eslint-disable no-console */ /* eslint-disable no-console */
console.log('THERE WAS A FUCKING ERROR!'); console.log('THERE WAS A FUCKING ERROR!', err);
reject(Error('There was a error when searching for torrents')); reject(Error('There was a error when searching for torrents'));
} }
if (results) { if (results) {
@@ -39,13 +62,14 @@ async function SearchPiratebay(query) {
} }
async function AddMagnet(magnet) { async function AddMagnet(magnet) {
return await new Promise(resolve => 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))
} }
/* eslint-disable no-console */ /* eslint-disable no-console */
console.log(results); console.log('result/error:', err, results);
resolve({ success: true }); resolve({ success: true });
})); }));
} }