This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·90 lines (73 loc) · 2.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const Transmission = require("transmission-client").Transmission;
const logger = require("./lib/logger");
const { config, resetRun, dontStartTorrents } = require("./lib/parameters");
const TorrentFeedLoader = require("./lib/torrent_feed_loader");
const LastRunHandler = require("./lib/last_run_handler");
const TransmissionHandler = require("./lib/transmission_handler");
const RssTorrentHandler = require("./lib/rss_torrent_handler");
const { rssConfigs, transmissionConfig } = config;
const transmissionClient = new Transmission(transmissionConfig);
const getLastRunFilePath = rssConfigId => `${__dirname}/lastrun_${rssConfigId}`;
if (resetRun) {
logger.info(`Last successful run is reseted`);
}
if (dontStartTorrents) {
logger.info(`Torrents won't be started`);
}
logger.info(`\nStarting...`);
const allRssTorrentHandlers = [];
Object.keys(rssConfigs).forEach(rssConfigId => {
const rssConfig = rssConfigs[rssConfigId];
const lastRunFilePath = getLastRunFilePath(rssConfigId);
const onError = error => {
logger.error(`${rssConfigId}: ${error.toString()}`);
};
const lastRunHandler = new LastRunHandler({ lastRunFilePath });
const torrentFeedLoader = new TorrentFeedLoader({ url: rssConfig.url });
const torrentClientHandler = new TransmissionHandler(
transmissionClient,
rssConfig.method
);
const rssTorrentHandler = new RssTorrentHandler({
name: rssConfigId,
lastRunHandler,
torrentFeedLoader,
torrentClientHandler,
torrentTitles: rssConfig.torrentTitles
});
allRssTorrentHandlers.push(rssTorrentHandler);
lastRunHandler.on("error", onError).on("loaded", lastRun => {
logger.info(`${rssConfigId}: Last successful run was ${lastRun}`);
});
torrentFeedLoader.on("error", onError).on("foundMatching", ({ title }) => {
logger.info(`${rssConfigId}: Found: ${title}`);
});
torrentClientHandler
.on("error", onError)
.on("added", title => {
logger.info(`${rssConfigId}: added torrent: ${title}`);
})
.on("duplicate", title => {
logger.warn(`${rssConfigId}: duplicate torrent: ${title}`);
});
rssTorrentHandler.start({ resetRun, dontStartTorrents });
});
process.on("exit", () => {
allRssTorrentHandlers.forEach(rssTorrentHandler => {
const {
torrentsFound,
torrentsAdded,
torrentsDuplicated,
torrentsErrors,
errors
} = rssTorrentHandler.stats();
logger.info(`${rssTorrentHandler.name}: Finished with
errors: ${errors},
torrents
found:${torrentsFound},
added: ${torrentsAdded},
duplicates: ${torrentsDuplicated},
errors: ${torrentsErrors}`);
});
logger.info("Exit");
});