Skip to content

Commit

Permalink
[FEATURE] - Allow daemon mode to search for torrents via hash
Browse files Browse the repository at this point in the history
 * Fixes #118
  • Loading branch information
Afraaz Ali committed Jul 28, 2021
1 parent 7d161eb commit b6265c2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/pipeline.ts
Expand Up @@ -12,6 +12,7 @@ import { Searchee } from "./searchee";
import {
getInfoHashesToExclude,
getTorrentByName,
getTorrentByHash,
loadTorrentDirLight,
saveTorrentFile,
} from "./torrent";
Expand Down Expand Up @@ -145,6 +146,15 @@ export async function searchForSingleTorrentByName(
return findOnOtherSites(meta, hashesToExclude);
}

export async function searchForSingleTorrentByHash(
hash: string
): Promise<number> {
const meta = await getTorrentByHash(hash);
const hashesToExclude = getInfoHashesToExclude();
if (!filterByContent(meta)) return null;
return findOnOtherSites(meta, hashesToExclude);
}

async function findSearchableTorrents() {
const { offset } = getRuntimeConfig();
const parsedTorrents: Searchee[] = await loadTorrentDirLight();
Expand Down
30 changes: 23 additions & 7 deletions src/server.ts
Expand Up @@ -3,7 +3,10 @@ import http from "http";
import qs from "querystring";
import { validateJackettApi } from "./jackett";
import { Label, logger } from "./logger";
import { searchForSingleTorrentByName } from "./pipeline";
import {
searchForSingleTorrentByName,
searchForSingleTorrentByHash,
} from "./pipeline";
import { getRuntimeConfig } from "./runtimeConfig";

function getData(req) {
Expand All @@ -23,7 +26,7 @@ function parseData(data) {
return JSON.parse(data);
} catch (_) {
const parsed = qs.parse(data);
if ("name" in parsed) return parsed;
if ("name" in parsed || "hash" in parsed) return parsed;
throw new Error(`Unable to parse request body: "${data}"`);
}
}
Expand All @@ -40,21 +43,34 @@ async function handleRequest(req, res) {
return;
}
const dataStr = await getData(req);
const { name } = parseData(dataStr);
const { name, hash } = parseData(dataStr);
const criteria = name ? name : hash;
const message = `Received ${name ? "name" : "hash"} ${criteria}`;
res.writeHead(204);
res.end();
logger.info({ label: Label.SERVER, message: `Received name ${name}` });

logger.info({ label: Label.SERVER, message });

try {
const numFound = await searchForSingleTorrentByName(name);
let numFound = null;
if (name) {
numFound = await searchForSingleTorrentByName(name);
}

// Just in case both name and hash are passed.
if (hash && !numFound) {
numFound = await searchForSingleTorrentByHash(hash);
}

if (numFound === null) {
logger.info({
label: Label.SERVER,
message: `Did not search for ${name}`,
message: `Did not search for ${criteria}`,
});
} else {
logger.info({
label: Label.SERVER,
message: `Found ${numFound} torrents for ${name}`,
message: `Found ${numFound} torrents for ${criteria}`,
});
}
} catch (e) {
Expand Down
13 changes: 13 additions & 0 deletions src/torrent.ts
Expand Up @@ -156,3 +156,16 @@ export async function getTorrentByName(name: string): Promise<Metafile> {
}
return parseTorrentFromFilename(findResult.filepath);
}

export async function getTorrentByHash(hash: string): Promise<Metafile> {
await indexNewTorrents();
const findResult = db
.get(INDEXED_TORRENTS)
.value()
.find((e) => e.infoHash === hash);
if (findResult === undefined) {
const message = `could not find a torrent with the hash ${hash}`;
throw new Error(message);
}
return parseTorrentFromFilename(findResult.filepath);
}

0 comments on commit b6265c2

Please sign in to comment.