Skip to content

Commit

Permalink
Refer to config for all configurable options
Browse files Browse the repository at this point in the history
  • Loading branch information
mmgoodnow committed Jun 11, 2020
1 parent 286b007 commit f72f87a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 39 deletions.
14 changes: 13 additions & 1 deletion config.template.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// All of the configuration can be overridden with command-line flags,
// and some are more useful as flags than permanent config (offset).
// If you find yourself always using the same command-line flag, you can set
// it here as a default.

module.exports = {
jackettServerUrl: "http://localhost:9117/jackett",
jackettApiKey: "YOUR_JACKETT_API_KEY_HERE",
Expand All @@ -10,11 +15,18 @@ module.exports = {
// - the string "all"
// - a single tracker id as found in its Torznab feed
// e.g. "oink"
trackers: "all",
tracker: "all",

// directory containing torrent files.
// For rtorrent, this is your session directory
// as configured in your .rtorrent.rc file.
// For deluge, this is ~/.config/deluge/state.
torrentDir: "/path/to/torrent/file/dir",

// where to put the torrent files that cross-seed finds for you.
outputDir: ".",

// Lets you start searching from the middle of the list if you experience
// a failure.
offset: 0,
};
81 changes: 43 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ const chalk = require("chalk");
const config = require("config");

const EPISODE_REGEX = /S\d\dE\d\d/i;
const jackettPath = "/api/v2.0/indexers/all/results";
const jackettPath = `/api/v2.0/indexers/${config.tracker}/results`;

let jackettServerUrl;
let jackettApiKey;
let torrentDir;
let outputDir;
let delay = 10000;
let offset = 0;
// let jackettServerUrl;
// let jackettApiKey;
// let torrentDir;
// let outputDir;
// let delay = 10000;
// let offset = 0;

const parseTorrentRemote = util.promisify(parseTorrent.remote);

function makeJackettRequest(query) {
const params = querystring.stringify({
apikey: jackettApiKey,
apikey: config.jackettApiKey,
Query: query,
});
return axios.get(`${jackettServerUrl}${jackettPath}?${params}`);
return axios.get(`${config.jackettServerUrl}${jackettPath}?${params}`);
}

function parseTorrentFromFilename(filename) {
Expand All @@ -41,7 +41,9 @@ function filterTorrentFile(info, index, arr) {
return false;
}

const allMkvs = info.files.every((file) => file.path.endsWith(".mkv"));
const allMkvs = info.files.every(
(file) => path.extname(file.path) === ".mkv"
);
if (!allMkvs) return false;

const cb = (file) => file.path.split(path.sep).length <= 2;
Expand Down Expand Up @@ -104,14 +106,16 @@ async function findOnOtherSites(info, hashesToExclude) {
const mapCb = (result) => assessResult(result, info, hashesToExclude);
const promises = results.map(mapCb);
const finished = await Promise.all(promises);
finished
.filter((e) => e !== null)
.forEach(({ tracker, type, info: newInfo }) => {
const styledName = chalk.green.bold(newInfo.name);
const styledTracker = chalk.bold(tracker);
console.log(`Found ${styledName} on ${styledTracker}`);
saveTorrentFile(tracker, type, newInfo);
});
const successful = finished.filter((e) => e !== null);

successful.forEach(({ tracker, type, info: newInfo }) => {
const styledName = chalk.green.bold(newInfo.name);
const styledTracker = chalk.bold(tracker);
console.log(`Found ${styledName} on ${styledTracker}`);
saveTorrentFile(tracker, type, newInfo);
});

return successful.length;
}

function saveTorrentFile(tracker, type, info) {
Expand All @@ -123,37 +127,38 @@ function saveTorrentFile(tracker, type, info) {
});
}

async function batchDownloadCrossSeeds(torrentFilenames) {
const parsedTorrents = torrentFilenames.map(parseTorrentFromFilename);
async function batchDownloadCrossSeeds() {
const dirContents = fs
.readdirSync(config.torrentDir)
.filter((fn) => path.extname(fn) === ".torrent")
.map((fn) => path.join(config.torrentDir, fn));
const parsedTorrents = dirContents.map(parseTorrentFromFilename);
const hashesToExclude = parsedTorrents.map((t) => t.infoHash);
const filteredTorrents = parsedTorrents.filter(filterTorrentFile);

console.log(
"Found %d torrents, %d suitable",
torrentFilenames.length,
"Found %d torrents, %d suitable to search for matches",
dirContents.length,
filteredTorrents.length
);

fs.mkdirSync(outputDir, { recursive: true });
const samples = filteredTorrents.slice(offset);

fs.mkdirSync(config.outputDir, { recursive: true });
const samples = filteredTorrents.slice(config.offset);
let totalFound = 0;
for (const [i, sample] of samples.entries()) {
const sleep = new Promise((r) => setTimeout(r, delay));
const sleep = new Promise((r) => setTimeout(r, config.delayMs));
const name = sample.name.replace(/.mkv$/, "");
const progress = chalk.blue(`[${i + 1}/${samples.length}]`);
console.log(progress, chalk.dim("Searching for"), name);
await Promise.all([findOnOtherSites(sample, hashesToExclude), sleep]);
let numFoundPromise = findOnOtherSites(sample, hashesToExclude);
const [numFound] = await Promise.all([numFoundPromise, sleep]);
totalFound += numFound;
}
console.log(
chalk.cyan("Done! Found %s cross seeds from %s original torrents"),
chalk.bold.white(totalFound),
chalk.bold.white(samples.length)
);
}

async function main() {
const successfulParse = parseCommandLineArgs();
if (!successfulParse) return;

const dirContents = fs
.readdirSync(torrentDir)
.map((fn) => path.join(torrentDir, fn));
await batchDownloadCrossSeeds(dirContents);
}

main();
module.exports = batchDownloadCrossSeeds;

0 comments on commit f72f87a

Please sign in to comment.