Skip to content

Commit

Permalink
Update naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mmgoodnow committed Jul 30, 2021
1 parent 6036ad4 commit 77223e8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
10 changes: 5 additions & 5 deletions src/pipeline.ts
Expand Up @@ -10,9 +10,9 @@ import { filterByContent, filterDupes, filterTimestamps } from "./preFilter";
import { getRuntimeConfig } from "./runtimeConfig";
import { Searchee } from "./searchee";
import {
TorrentInfo,
TorrentLocator,
getInfoHashesToExclude,
getTorrentByNameOrHash,
getTorrentByCriteria,
loadTorrentDirLight,
saveTorrentFile,
} from "./torrent";
Expand Down Expand Up @@ -137,10 +137,10 @@ async function findMatchesBatch(
return totalFound;
}

export async function searchForSingleTorrentByNameOrHash(
torrent: TorrentInfo
export async function searchForLocalTorrentByCriteria(
criteria: TorrentLocator
): Promise<number> {
const meta = await getTorrentByNameOrHash(torrent);
const meta = await getTorrentByCriteria(criteria);
const hashesToExclude = getInfoHashesToExclude();
if (!filterByContent(meta)) return null;
return findOnOtherSites(meta, hashesToExclude);
Expand Down
42 changes: 24 additions & 18 deletions src/server.ts
@@ -1,11 +1,11 @@
import fs from "fs";
import http from "http";
import qs from "querystring";
import { validateJackettApi } from "./jackett";
import { Label, logger } from "./logger";
import { searchForSingleTorrentByNameOrHash } from "./pipeline";
import { searchForLocalTorrentByCriteria } from "./pipeline";
import { getRuntimeConfig } from "./runtimeConfig";

import { TorrentLocator } from "./torrent";
import { inspect } from "util";
function getData(req) {
return new Promise((resolve) => {
const chunks = [];
Expand All @@ -19,13 +19,21 @@ function getData(req) {
}

function parseData(data) {
let parsed;
try {
return JSON.parse(data);
parsed = JSON.parse(data);
} catch (_) {
const parsed = qs.parse(data);
if ("name" in parsed || "hash" in parsed) return parsed;
throw new Error(`Unable to parse request body: "${data}"`);
parsed = qs.parse(data);
}

if ("infoHash" in parsed) {
parsed.infoHash = parsed.infoHash.toLowerCase();
}
if ("name" in parsed || "infoHash" in parsed) {
return parsed;
}

throw new Error(`Unable to parse request body: "${data}"`);
}

async function handleRequest(req, res) {
Expand All @@ -40,42 +48,40 @@ async function handleRequest(req, res) {
return;
}
const dataStr = await getData(req);
const { name, hash } = parseData(dataStr);
const criteria = name ? name : hash;
const criteria: TorrentLocator = parseData(dataStr);

if (!criteria) {
logger.error({
label: Label.SERVER,
message: "A name or info hash must be provided",
});
res.writeHead(422);
res.writeHead(400);
res.end();
}

const message = `Received ${name ? "name" : "hash"} ${criteria}`;
const criteriaStr = inspect({ ...criteria });

const message = `Received ${criteriaStr}`;
res.writeHead(204);
res.end();

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

try {
let numFound = null;
if (name) {
numFound = await searchForSingleTorrentByNameOrHash({
name,
infoHash: hash,
});
if (criteria) {
numFound = await searchForLocalTorrentByCriteria(criteria);
}

if (numFound === null) {
logger.info({
label: Label.SERVER,
message: `Did not search for ${criteria}`,
message: `Did not search for ${criteriaStr}`,
});
} else {
logger.info({
label: Label.SERVER,
message: `Found ${numFound} torrents for ${criteria}`,
message: `Found ${numFound} torrents for ${criteriaStr}`,
});
}
} catch (e) {
Expand Down
18 changes: 5 additions & 13 deletions src/torrent.ts
Expand Up @@ -10,7 +10,7 @@ import { getRuntimeConfig } from "./runtimeConfig";
import { createSearcheeFromTorrentFile, Searchee } from "./searchee";
import { ok, stripExtension } from "./utils";

export interface TorrentInfo {
export interface TorrentLocator {
infoHash?: string;
name?: string;
}
Expand Down Expand Up @@ -149,22 +149,14 @@ export async function loadTorrentDirLight(): Promise<Searchee[]> {
).then((searcheeResults) => searcheeResults.filter(ok));
}

export async function getTorrentByNameOrHash(
torrentInfo: TorrentInfo
export async function getTorrentByCriteria(
criteria: TorrentLocator
): Promise<Metafile> {
await indexNewTorrents();

const criteria = torrentInfo?.infoHash
? torrentInfo?.infoHash
: torrentInfo?.name;
const property = torrentInfo?.infoHash ? "infoHash" : "name";

const findResult = db
.get(INDEXED_TORRENTS)
.value()
.find((e) => e[property] === criteria);
const findResult = db.get(INDEXED_TORRENTS).find(criteria).value();
if (findResult === undefined) {
const message = `could not find a torrent with the name ${criteria}`;
const message = `could not find a torrent with the criteria`;
throw new Error(message);
}
return parseTorrentFromFilename(findResult.filepath);
Expand Down

0 comments on commit 77223e8

Please sign in to comment.