Skip to content

Commit

Permalink
Update usages of DECISIONS and INDEXED_TORRENTS
Browse files Browse the repository at this point in the history
  • Loading branch information
mmgoodnow committed Dec 28, 2021
1 parent 03019fe commit 10450a1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 57 deletions.
3 changes: 0 additions & 3 deletions src/constants.ts
Expand Up @@ -9,9 +9,6 @@ export const EXTENSIONS = ["mkv", "mp4", "avi"];
export const CONFIG_TEMPLATE_URL =
"https://github.com/mmgoodnow/cross-seed/blob/master/src/config.template.js";

export const DECISIONS = "decisions";
export const INDEXED_TORRENTS = "indexedTorrents";

export const TORRENT_CACHE_FOLDER = "torrent_cache";

export enum Action {
Expand Down
56 changes: 20 additions & 36 deletions src/decide.ts
Expand Up @@ -2,13 +2,12 @@ import { existsSync, writeFileSync } from "fs";
import parseTorrent, { Metafile } from "parse-torrent";
import path from "path";
import { appDir } from "./configuration.js";
import { Decision, DECISIONS, TORRENT_CACHE_FOLDER } from "./constants.js";
import { Decision, TORRENT_CACHE_FOLDER } from "./constants.js";
import db, { DecisionEntry } from "./db.js";
import { JackettResult } from "./jackett.js";
import { Label, logger } from "./logger.js";
import { Searchee } from "./searchee.js";
import { parseTorrentFromFilename, parseTorrentFromURL } from "./torrent.js";
import { partial } from "./utils.js";

export interface ResultAssessment {
decision: Decision;
Expand Down Expand Up @@ -97,7 +96,7 @@ async function assessResultHelper(
return { decision: Decision.MATCH, info };
}

function existsInCache(infoHash: string): boolean {
function existsInTorrentCache(infoHash: string): boolean {
return existsSync(
path.join(appDir(), TORRENT_CACHE_FOLDER, `${infoHash}.cached.torrent`)
);
Expand Down Expand Up @@ -128,60 +127,45 @@ async function assessResultCaching(
const { Guid, Title, TrackerId: tracker } = result;
const logReason = createReasonLogger(Title, tracker, searchee.name);

const cacheEntry: DecisionEntry = db
.get<typeof DECISIONS, typeof searchee.name, typeof Guid>([
DECISIONS,
searchee.name,
Guid,
])
.clone()
.value();

// handles path creation
db.get(DECISIONS)
.defaultsDeep({
[searchee.name]: {
[Guid]: {
firstSeen: Date.now(),
},
},
})
.set([searchee.name, Guid, "lastSeen"], Date.now())
.value();
db.data.decisions[searchee.name] ??= {};
const cacheEntry: DecisionEntry = db.data.decisions[searchee.name][Guid];

let assessment: ResultAssessment;
if (cacheEntry && cacheEntry.decision !== Decision.MATCH) {
// cached rejection
assessment = { decision: cacheEntry.decision };
logReason(cacheEntry.decision, true);
} else if (cacheEntry && existsInCache(cacheEntry.infoHash)) {
} else if (cacheEntry && existsInTorrentCache(cacheEntry.infoHash)) {
// cached match
if (infoHashesToExclude.includes(cacheEntry.infoHash)) {
// has been added since the last run
assessment = { decision: Decision.INFO_HASH_ALREADY_EXISTS };
db.set(
[DECISIONS, searchee.name, Guid, "decision"],
assessment.decision
).value();
db.data.decisions[searchee.name][Guid].lastSeen = Date.now();
db.data.decisions[searchee.name][Guid].decision =
assessment.decision;
} else {
assessment = {
decision: cacheEntry.decision,
info: await getCachedTorrentFile(cacheEntry.infoHash),
};
}
} else {
// uncached case, send it
assessment = await assessResultHelper(
result,
searchee,
infoHashesToExclude
);
db.set(
[DECISIONS, searchee.name, Guid, "decision"],
assessment.decision
).value();

db.data.decisions[searchee.name][Guid] = {
decision: assessment.decision,
lastSeen: Date.now(),
firstSeen: Date.now(),
};

if (assessment.decision === Decision.MATCH) {
db.set(
[DECISIONS, searchee.name, Guid, "infoHash"],
assessment.info.infoHash
).value();
db.data.decisions[searchee.name][Guid].infoHash =
assessment.info.infoHash;
cacheTorrentFile(assessment.info);
}
logReason(assessment.decision, false);
Expand Down
33 changes: 15 additions & 18 deletions src/torrent.ts
Expand Up @@ -3,7 +3,6 @@ import parseTorrent, { Metafile } from "parse-torrent";
import path from "path";
import { concat } from "simple-get";
import { inspect } from "util";
import { INDEXED_TORRENTS } from "./constants.js";
import db from "./db.js";
import { CrossSeedError } from "./errors.js";
import { logger } from "./logger.js";
Expand Down Expand Up @@ -93,11 +92,11 @@ export async function indexNewTorrents(): Promise<void> {
const { torrentDir } = getRuntimeConfig();
const dirContents = await findAllTorrentFilesInDir(torrentDir);

// index new torrents in the torrentDir
for (const filepath of dirContents) {
const doesAlreadyExist = db
.get(INDEXED_TORRENTS)
.find((indexEntry) => indexEntry.filepath === filepath)
.value();
const doesAlreadyExist = db.data.indexedTorrents.find(
(e) => e.filepath === filepath
);
if (!doesAlreadyExist) {
let meta;
try {
Expand All @@ -107,28 +106,22 @@ export async function indexNewTorrents(): Promise<void> {
logger.debug(e);
continue;
}
db.get(INDEXED_TORRENTS).value().push({
db.data.indexedTorrents.push({
filepath,
infoHash: meta.infoHash,
name: meta.name,
});
}
}
db.set(
INDEXED_TORRENTS,
db
.get(INDEXED_TORRENTS)
.value()
.filter((e) => dirContents.includes(e.filepath))
).value();
// clean up torrents that no longer exist in the torrentDir
db.data.indexedTorrents = db.data.indexedTorrents.filter((e) =>
dirContents.includes(e.filepath)
);
db.write();
}

export function getInfoHashesToExclude(): string[] {
return db
.get(INDEXED_TORRENTS)
.value()
.map((t) => t.infoHash);
return db.data.indexedTorrents.map((t) => t.infoHash);
}

export async function validateTorrentDir(): Promise<void> {
Expand Down Expand Up @@ -156,7 +149,11 @@ export async function getTorrentByCriteria(
): Promise<Metafile> {
await indexNewTorrents();

const findResult = db.get(INDEXED_TORRENTS).find(criteria).value();
const findResult = db.data.indexedTorrents.find(
(e) =>
(!criteria.infoHash || criteria.infoHash === e.infoHash) &&
(!criteria.name || criteria.name === e.name)
);
if (findResult === undefined) {
const message = `could not find a torrent with the criteria ${inspect(
criteria
Expand Down

0 comments on commit 10450a1

Please sign in to comment.