-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (113 loc) · 3.66 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/env node
const { Command } = require("commander");
const services = require("./services.js");
const program = new Command();
program
.option("-s, --sync", "Synchronize indexers and their options (instead of just adding them)")
// .option("-sd, --seeds <num>", "Minimum no. of seeds")
// .option("-k, --apikey <key>", "Jackett API Key")
// .option("-u, --url <url>", "Jackett URL", "http://127.0.0.1:9117")
// .option("-au, --alturl <url>", "Alternative Jackett URI (for use when adding to sevices, default = --uri)")
// .option("-su, --sonarrurl <url>", "Sonnar URL", "http://127.0.0.1:8989")
// .option("-sk, --sonarrkey <key>", "Sonnar API Key")
for (const i in services) {
const service = services[i];
for (const j in service.params) {
const param = service.params[j];
const defaults = service.defaults && service.defaults[j] ? service.defaults[j] : undefined;
const long = "--" + param;
const paramStr = long + " <" + (defaults ? typeof defaults : param) + ">";
if (!program.options.some(el => el.long === long)) {
program.option(paramStr, "", defaults);
}
}
}
if (process.argv.length == 2)
process.argv.push("--help");
program.parse(process.argv);
function findServices() {
const readyServices = {};
for (const i in services) {
const service = services[i];
if (service.required) {
let flag = true;
for (const j in service.required) {
if (!program[service.required[j]]) {
flag = false;
}
}
if (flag) {
readyServices[i] = service;
}
} else {
readyServices[i] = service;
}
}
return readyServices;
}
function serviceParams(service) {
const params = []
service.params.forEach((el, i) => {
let val = program[el];
if (service.process && service.process[i])
val = service.process[i](val);
params.push(val);
});
return params;
}
async function sync(service, to, name) {
// await service.sync(...serviceParams(service), jackettIndexers);
try {
const params = serviceParams(service);
const indexers = (await service.get(...params)).filter(el => el.id);
const idList = indexers.map(el => el.id);
const promises = [];
const toAddIds = [];
to.filter(el => !idList.includes(el.id))
.filter(el => service.shouldAdd(...params, el))
.forEach(el => {
toAddIds.push(el.id);
promises.push(service.add(...params, el));
});
if (program.sync && service.shouldUpdate) {
const existingIndexers = await service.get(...params);
const existingIds = existingIndexers.map(el => el.id);
// console.log(ex)
to
.filter(el => existingIds.includes(el.id))
.filter(el => service.shouldUpdate(...params, existingIndexers.find(e => e.id === el.id), el))
.forEach(el => {
// toAddIds.push(el.id);
promises.push(service.update(...params, existingIndexers.find(e => e.id === el.id), el));
});
}
if (toAddIds.length > 0) {
console.log(`[${name}] Adding ${toAddIds.join(', ')}`);
} else {
console.log(`[${name}] Nothing do add`);
}
await Promise.all(promises);
} catch (e) {
console.error(e);
}
}
(async () => {
try {
const readyServices = findServices();
const sources = Object.values(readyServices).filter(el => el.source);
const indexers = (await Promise.all(sources.map(async el => await el.get(...serviceParams(el))))).reduce((result, i) => result.concat(i), []);
// console.log(indexers)
const promises = [];
Object.keys(readyServices).forEach(async name => {
const service = services[name];
console.log(`Found config for ${name}`)
if (!service.source && service.add && service.shouldAdd) {
promises.push(sync(service, indexers, name));
}
})
await Promise.all(promises);
console.log("Done");
} catch (e) {
console.error(e);
}
})()