Skip to content

Commit

Permalink
Finish CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mmgoodnow committed Jun 11, 2020
1 parent f72f87a commit 3424f7c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
4 changes: 3 additions & 1 deletion config.template.js
@@ -1,3 +1,5 @@
"use strict";

// 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
Expand All @@ -9,7 +11,7 @@ module.exports = {

// Pause at least this much in between each Jackett search. Higher is safer.
// It is not recommended to set this to less than 2 seconds.
delayMs: 10 * 1000,
delay: 10,

// Tracker to search. As of right now, can be one of:
// - the string "all"
Expand Down
35 changes: 16 additions & 19 deletions index.js
@@ -1,5 +1,5 @@
#!/usr/bin/env node
"use strict";

const parseTorrent = require("parse-torrent");
const fs = require("fs");
const path = require("path");
Expand All @@ -9,26 +9,18 @@ const querystring = require("querystring");
const axios = require("axios");
const chalk = require("chalk");

const config = require("config");

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

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

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

function makeJackettRequest(query) {
const jackettPath = `/api/v2.0/indexers/${CONFIG.tracker}/results`;
const params = querystring.stringify({
apikey: config.jackettApiKey,
apikey: CONFIG.jackettApiKey,
Query: query,
});
return axios.get(`${config.jackettServerUrl}${jackettPath}?${params}`);
return axios.get(`${CONFIG.jackettServerUrl}${jackettPath}?${params}`);
}

function parseTorrentFromFilename(filename) {
Expand Down Expand Up @@ -129,9 +121,9 @@ function saveTorrentFile(tracker, type, info) {

async function batchDownloadCrossSeeds() {
const dirContents = fs
.readdirSync(config.torrentDir)
.readdirSync(CONFIG.torrentDir)
.filter((fn) => path.extname(fn) === ".torrent")
.map((fn) => path.join(config.torrentDir, fn));
.map((fn) => path.join(CONFIG.torrentDir, fn));
const parsedTorrents = dirContents.map(parseTorrentFromFilename);
const hashesToExclude = parsedTorrents.map((t) => t.infoHash);
const filteredTorrents = parsedTorrents.filter(filterTorrentFile);
Expand All @@ -142,11 +134,11 @@ async function batchDownloadCrossSeeds() {
filteredTorrents.length
);

fs.mkdirSync(config.outputDir, { recursive: true });
const samples = filteredTorrents.slice(config.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, config.delayMs));
const sleep = new Promise((r) => setTimeout(r, CONFIG.delay * 1000));
const name = sample.name.replace(/.mkv$/, "");
const progress = chalk.blue(`[${i + 1}/${samples.length}]`);
console.log(progress, chalk.dim("Searching for"), name);
Expand All @@ -161,4 +153,9 @@ async function batchDownloadCrossSeeds() {
);
}

module.exports = batchDownloadCrossSeeds;
async function main(config) {
CONFIG = config;
return batchDownloadCrossSeeds();
}

module.exports = main;
12 changes: 9 additions & 3 deletions package.json
@@ -1,12 +1,19 @@
{
"name": "cross-seed",
"version": "1.0.0",
"description": "",
"description": "Query Jackett for cross-seedable torrents",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"bin": {
"cross-seed": "bin/cmd.js"
},
"repository": {
"type": "git",
"url": "git://github.com/mmgoodnow/cross-seed.git"
},
"author": "Michael Goodnow",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
Expand All @@ -15,7 +22,6 @@
"commander": "^5.1.0",
"parse-torrent": "^7.1.3"
},
"type": "module",
"devDependencies": {
"prettier": "^2.0.5"
},
Expand Down

0 comments on commit 3424f7c

Please sign in to comment.