<commit_before>const assert = require('assert');
const PythonShell = require('python-shell');

async function find(searchterm, callback) {
   const options = {
      pythonPath: '/usr/bin/python3',
      // pythonPath: '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3',
      args: [searchterm, '-s', 'piratebay', '--print'],
   };

   PythonShell.run('../app/torrent_search/torrentSearch/search.py', options, callback);
   // PythonShell does not support return
}


async function callPythonAddMagnet(magnet, callback) {
   const options = {
      pythonPath: '/usr/bin/python',
      // pythonPath: '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3',
      args: [magnet],
   };

   PythonShell.run('../app/magnet.py', options, callback);
}

async function SearchPiratebay(query) {
   return await new Promise((resolve, reject) => find(query, (err, results) => {
      if (err) {
         /* eslint-disable no-console */
         console.log('THERE WAS A FUCKING ERROR!');
         reject(Error('There was a error when searching for torrents'));
      }
      if (results) {
         /* eslint-disable no-console */
         console.log('result', results);
         resolve(JSON.parse(results, null, '\t'));
      }
   }));
}

async function AddMagnet(magnet) {
   return await new Promise(resolve => callPythonAddMagnet(magnet, (err, results) => {
      if (err) {
         /* eslint-disable no-console */
         console.log(err);
      }
      /* eslint-disable no-console */
      console.log(results);
      resolve({ success: true });
   }));
}

module.exports = { SearchPiratebay, AddMagnet };<commit_msg>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.<commit_after>const assert = require('assert');
const http = require('http');
const { URL } = require('url');
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) {
   const options = {
      pythonPath: '/usr/bin/python3',
      // pythonPath: '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3',
      args: [searchterm, '-s', 'jackett', '--print'],
   };

   PythonShell.run('../app/torrent_search/torrentSearch/search.py', options, callback);
   // PythonShell does not support return
}


async function callPythonAddMagnet(url, callback) {
   getMagnetFromURL(url)
   .then((magnet) => {
      const options = {
         pythonPath: '/usr/bin/python',
      	 // pythonPath: '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3',
      	 args: [magnet],
      };

      PythonShell.run('../app/magnet.py', options, callback);
   })
   .catch((err) => {
      console.log(err);
      throw new Error(err);
   })
}

async function SearchPiratebay(query) {
   return await new Promise((resolve, reject) => find(query, (err, results) => {
      if (err) {
         /* eslint-disable no-console */
         console.log('THERE WAS A FUCKING ERROR!', err);
         reject(Error('There was a error when searching for torrents'));
      }
      if (results) {
         /* eslint-disable no-console */
         console.log('result', results);
         resolve(JSON.parse(results, null, '\t'));
      }
   }));
}

async function AddMagnet(magnet) {
   return await new Promise((resolve, reject) => callPythonAddMagnet(magnet, (err, results) => {
      if (err) {
         /* eslint-disable no-console */
         console.log(err);
	 reject(Error('Enable to add torrent', err)) 
      }
      /* eslint-disable no-console */
      console.log('result/error:', err, results);
      resolve({ success: true });
   }));
}

module.exports = { SearchPiratebay, AddMagnet };
{'fraction_non_alphanumeric': 0.15445544554455445, 'fraction_numerical': 0.004207920792079208, 'mean_word_length': 2.783707865168539, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2547893', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1219, 'n_words': 280}