Guest User

Untitled

a guest
Jan 2nd, 2023
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fetch = require('node-fetch');
  2. const { readFileSync, writeFileSync } = require('fs');
  3.  
  4. const filePath = "./radarrTorrents.json" // where to store info about current torrents being downloaded
  5. const maxAcceptedDownloadTime = 6000000; // any torrent that takes longer than this time (in milliseconds) to download is abandonded
  6. const maxFileSize = 4000000000 // any torrent greater than this size (in bytes) is exempt from the time limit restriction
  7. const waitTime = 600000; // how much time (in milliseconds) to give the torrent to find peers and lower it's download time
  8. const dateLocale = "en-GB";
  9. const dateOptions = { dateStyle: 'short', timeStyle: 'short' };
  10.  
  11. const radarrUrl = "http://192.168.1.69:7878"; // change this to match your Radarr URL
  12. const apiKey = "ENTER_UR_KEY_HERE"; // change this to match your Radarr API Key
  13. const baseUrl = radarrUrl + "/api/v3/queue";
  14. const apiKeyString = `&apikey=${apiKey}`;
  15.  
  16. let fetchMoviesUrl = baseUrl + "?includeUnknownMovieItems=true&includeMovie=true" + apiKeyString;
  17.  
  18. let torrents;
  19. try {
  20.     const torrentsFile = readFileSync(filePath);
  21.     torrents = JSON.parse(torrentsFile);
  22.     if (!torrents.lastRun) {
  23.         torrents.lastRun = new Intl.DateTimeFormat(dateLocale, dateOptions).format(new Date());
  24.     }
  25.     if (!torrents.movies) {
  26.         torrents.movies = {};
  27.     }
  28. } catch (e) {
  29.     torrents = {
  30.         lastRun: new Intl.DateTimeFormat(dateLocale, dateOptions).format(new Date()),
  31.         movies: {},
  32.     }
  33. }
  34.  
  35.  
  36. const currTime = new Intl.DateTimeFormat(dateLocale, dateOptions).format(new Date());
  37. fetch(fetchMoviesUrl, { method: "Get" })
  38.     .then(res => res.json())
  39.     .then((json) => {
  40.         json.records.forEach(t => {
  41.             if (t.size && t.size > maxFileSize) return; // if size doesn't exist then its still processing the torrent
  42.  
  43.             // torrent is new
  44.             if (!torrents.movies[t.id]) {
  45.                 torrents.movies[t.id] = {
  46.                     title: t.title,
  47.                     id: t.id,
  48.                     timeleft: t.timeleft,
  49.                     timeleftInMS: hmsToMilliseconds(t.timeleft)
  50.                 }
  51.             }
  52.             // torrent was previously seen
  53.             else {
  54.                 const timeleftInMS = hmsToMilliseconds(t.timeleft);
  55.                 torrents.movies[t.id].timeleft = t.timeleft;
  56.                 torrents.movies[t.id].timeleftInMS = timeleftInMS;
  57.  
  58.                 // torrent will take too long to download
  59.                 if (timeleftInMS > maxAcceptedDownloadTime || timeleftInMS == null) {
  60.                     // torrent has previously been marked as gonna take too long to download
  61.                     if (torrents.movies[t.id].firstFailedAt) {
  62.                         const first = Number(new Date(torrents.movies[t.id].firstFailedAt));
  63.                         const now = Number(new Date());
  64.                         // its been past the wait time to let the torrent figure its shit out
  65.                         if (now - first > waitTime) {
  66.                             console.log(`[${currTime}] Third Strike [DELETE] (time left: ${msToTime(timeleftInMS)}): ${t.tile}`);
  67.                             const delUrl = `${baseUrl}/${t.id}?removeFromClient=true&blocklist=true${apiKeyString}`;
  68.                             fetch(delUrl, {method: 'DELETE'});
  69.                         } else {
  70.                             console.log(`[${currTime}] Second Strike (time left: ${msToTime(timeleftInMS)}): ${t.tile}`);
  71.                         }
  72.                     // first time torrent has been marked as gonna take too long to download
  73.                     } else {
  74.                         console.log(`[${currTime}] First Strike (time left: ${msToTime(timeleftInMS)}): ${t.tile}`);
  75.                         torrents.movies[t.id].firstFailedAt = new Date();
  76.                     }
  77.                 // torrent used to be marked as gonna take too long to download, but has since recovered
  78.                 } else if (torrents.movies[t.id].firstFailedAt) {
  79.                     console.log(`[${currTime}] Recovered: ${t.tile}`);
  80.                     delete torrents.movies[t.id].firstFailedAt;
  81.                 }
  82.             }
  83.         })
  84.  
  85.         // remove any torrents that were completed/deleted
  86.         Object.keys(torrents.movies).forEach(k => {
  87.             if (!json.records.find(r => r.id == k)) {
  88.                 console.log(`[${currTime}] Completed or deleted: `, torrents.movies[k].title)
  89.                 delete torrents.movies[k];
  90.             }
  91.         })
  92.         torrents.lastRun = new Intl.DateTimeFormat(dateLocale, dateOptions).format(new Date());
  93.  
  94.         // write the results to the file
  95.         try {
  96.             writeFileSync(filePath, JSON.stringify(torrents, null, 2), 'utf8');
  97.           } catch (error) {
  98.             console.log(`[${currTime}] An error has occurred writing to file`, error);
  99.           }
  100.     });
  101.  
  102. function hmsToMilliseconds(str) {
  103.     if (!str) return;
  104.     var p = str.split(':'),
  105.         s = 0, m = 1;
  106.  
  107.     while (p.length > 0) {
  108.         s += m * parseInt(p.pop(), 10);
  109.         m *= 60;
  110.     }
  111.  
  112.     return s * 1000;
  113. }
  114.  
  115. function msToTime(duration) {
  116.     if (!duration) return "infinite";
  117.     var milliseconds = parseInt((duration%1000)/100)
  118.         , seconds = parseInt((duration/1000)%60)
  119.         , minutes = parseInt((duration/(1000*60))%60)
  120.         , hours = parseInt((duration/(1000*60*60))%24);
  121.  
  122.     hours = (hours < 10) ? "0" + hours : hours;
  123.     minutes = (minutes < 10) ? "0" + minutes : minutes;
  124.     seconds = (seconds < 10) ? "0" + seconds : seconds;
  125.  
  126.     return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
  127. }
Add Comment
Please, Sign In to add comment