forked from csmit195/gptdarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
246 lines (199 loc) · 7.88 KB
/
config.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import fs from 'fs';
import readlineSync from 'readline-sync';
import { v4 as uuidv4 } from 'uuid';
import chalk from 'chalk';
//#region Helper Functions
const ask = (question, description, example, validator) => {
let input;
do {
console.log(chalk.green(question));
console.log(chalk.yellow(description));
if (example) {
console.log(chalk.blue(`Example: ${example}`));
}
input = readlineSync.question(chalk.magenta('> '));
if (validator && !validator(input)) {
console.log(chalk.red('Invalid input, please try again.'));
}
} while (validator && !validator(input));
return input;
};
const validateUrl = (url) => {
const regex = /^(http|https):\/\/[^ "]+$/;
return regex.test(url);
};
const validateSonarrApiKey = (apiKey) => {
// TODO: Validate API key
return true;
}
const validateRadarrApiKey = (apiKey) => {
// TODO: Validate API key
return true;
}
const validateOverseerrApiKey = (apiKey) => {
// TODO: Validate API key
return true;
}
function mergeUrls(...urls) {
let fullUrl = new URL(urls[0]);
let additionalPaths = urls.slice(1).map(path => path.replace(/^\//, '')).join('/');
fullUrl.pathname += (fullUrl.pathname.endsWith('/') ? '' : '/') + additionalPaths;
return fullUrl.href;
}
//#endregion
console.log(chalk.white('------------------------'));
console.log(chalk.cyan(' GPTDARR'));
console.log(chalk.cyan(' DotEnv Configuration'));
console.log(chalk.white('------------------------'));
//#region Sonarr
const sonarrUrl = mergeUrls(ask(
'Enter SONARR URL',
'The URL for your Sonarr server. Can be local or remote.',
'http://192.168.1.238:8989/sonarr',
validateUrl
), 'api/v3');
const sonarrApiKey = ask('Enter SONARR API Key', 'Your unique API key for Sonarr.', null, validateSonarrApiKey);
//#region Sonarr Quality Profile
const sonarrQResponse = await fetch(`${sonarrUrl}/qualityprofile`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Api-key': sonarrApiKey
}
});
if (sonarrQResponse.status == 401) {
console.log(chalk.red('Invalid API key, please try again.'));
process.exit();
}
if (sonarrQResponse.status != 200) {
console.log(chalk.red('Unexpected error occurred, please try again.'));
process.exit();
}
const sonarrQualityProfiles = await sonarrQResponse.json();
const sonarrQualityProfilesNames = sonarrQualityProfiles.map((profile, index) => {
return `${index + 1}: ${profile.name}`;
});
const sonarrQualityProfileIndex = readlineSync.keyInSelect(sonarrQualityProfilesNames, 'Select a quality profile for your series:');
const sonarrQualityProfileId = sonarrQualityProfiles[sonarrQualityProfileIndex].id;
console.log(chalk.green(`Selected quality profile: ${sonarrQualityProfiles[sonarrQualityProfileIndex].name}`));
//#endregion
//#region Sonarr Language Profile
// http://192.168.1.238:8989/sonarr/api/v3/languageprofile
const sonarrLanguageProfilesResponse = await fetch(`${sonarrUrl}/languageprofile`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Api-key': sonarrApiKey
}
});
// No need for API key validation here, it was already validated above
if (sonarrLanguageProfilesResponse.status != 200) {
console.log(chalk.red('Unexpected error occurred, please try again.'));
process.exit();
}
const sonarrLanguageProfiles = await sonarrLanguageProfilesResponse.json();
const sonarrLanguageProfilesNames = sonarrLanguageProfiles.map((profile, index) => {
return `${index + 1}: ${profile.name}`;
});
const sonarrLlanguageProfileIndex = readlineSync.keyInSelect(sonarrLanguageProfilesNames, 'Select a language profile for your series:');
const sonarrLanguageProfileId = sonarrLanguageProfiles[sonarrLlanguageProfileIndex].id;
console.log(chalk.green(`Selected language profile: ${sonarrLanguageProfiles[sonarrLlanguageProfileIndex].name}`));
//#endregion
//#endregion
//#region Radarr
const radarrUrl = mergeUrls(ask(
'Enter RADARR URL',
'The URL for your Radarr server. Can be local or remote.',
'http://192.168.1.238:7878/radarr',
validateUrl
), 'api/v3');
const radarrApiKey = ask('Enter RADARR API Key', 'Your unique API key for Radarr.', null, validateRadarrApiKey);
// RADARR_FORCE_SEARCH_ON_EXISTING (search radarr if already existing, but not downloaded)
const radarrForceSearchOnExisting = readlineSync.keyInYN('Force search for existing, but missing movies?') ? 'true' : 'false';
// #region Radarr Quality Profile
const radarrQResponse = await fetch(`${radarrUrl}/qualityprofile`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Api-key': radarrApiKey
}
});
if (radarrQResponse.status == 401) {
console.log(chalk.red('Invalid API key, please try again.'));
process.exit();
}
if (radarrQResponse.status != 200) {
console.log(chalk.red('Unexpected error occurred, please try again.'));
process.exit();
}
const radarrQualityProfiles = await radarrQResponse.json();
const radarrQualityProfilesNames = radarrQualityProfiles.map((profile, index) => {
return `${index + 1}: ${profile.name}`;
});
const radarrQualityProfileIndex = readlineSync.keyInSelect(radarrQualityProfilesNames, 'Select a quality profile for your movies:');
const radarrQualityProfileId = radarrQualityProfiles[radarrQualityProfileIndex].id;
console.log(chalk.green(`Selected quality profile: ${radarrQualityProfiles[radarrQualityProfileIndex].name}`));
// #endregion
//#endregion
//#region Overseerr
const overseerrUrl = mergeUrls(ask(
'Enter OVERSEERR URL',
'The URL for your Overseerr server. Can be local or remote.',
'http://192.168.1.238:5055',
validateUrl
), 'api/v1');
const overseerrApiKey = ask('Enter OVERSEERR API Key', 'Your unique API key for Overseerr.', null, validateOverseerrApiKey);
// #region Overseerr Quality Profile
const overseerrQResponse = await fetch(`${overseerrUrl}/settings/profiles/quality`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Api-Key': overseerrApiKey
}
});
if (overseerrQResponse.status == 401) {
console.log(chalk.red('Invalid API key, please try again.'));
process.exit();
}
if (overseerrQResponse.status != 200) {
console.log(chalk.red('Unexpected error occurred, please try again.'));
process.exit();
}
const overseerrQualityProfiles = await overseerrQResponse.json();
const overseerrQualityProfilesNames = overseerrQualityProfiles.map((profile, index) => {
return `${index + 1}: ${profile.name}`;
});
const overseerrQualityProfileIndex = readlineSync.keyInSelect(overseerrQualityProfilesNames, 'Select a quality profile for your movies:');
const overseerrQualityProfileId = overseerrQualityProfiles[overseerrQualityProfileIndex].id;
console.log(chalk.green(`Selected quality profile: ${overseerrQualityProfiles[overseerrQualityProfileIndex].name}`));
// #endregion
//#endregion
//#region Security
console.log(chalk.yellow('Security Settings'));
const onlyAllowOpenAI = readlineSync.keyInYN('Only allow OpenAI?') ? 'true' : 'false';
const proxyApiKey = uuidv4();
//#endregion
// Building .env content
const envContent = `
[SONARR]
SONARR_URL=${sonarrUrl}
SONARR_QUALITY_PROFILE_ID=${sonarrQualityProfileId}
SONARR_LANGUAGE_PROFILE_ID=${sonarrLanguageProfileId}
SONARR_API_KEY=${sonarrApiKey}
[RADARR]
RADARR_URL=${radarrUrl}
RADARR_API_KEY=${radarrApiKey}
RADARR_QUALITY_PROFILE_ID=${radarrQualityProfileId}
RADARR_FORCE_SEARCH_ON_EXISTING=${radarrForceSearchOnExisting}
[OVERSEERR]
OVERSEERR_URL=${overseerrUrl}
OVERSEERR_API_KEY=${overseerrApiKey}
OVERSEERR_QUALITY_PROFILE_ID=${overseerrQualityProfileId}
[SECURITY]
ONLY_ALLOW_OPENAI=${onlyAllowOpenAI}
PROXY_API_KEY=${proxyApiKey}
`;
// Writing to .env file
fs.writeFileSync('.env', envContent.trim());
console.log(chalk.green('Configuration saved to .env file.'));
console.log(chalk.green(`Your PROXY_API_KEY is: ${proxyApiKey}, it can be found in the .env file.`));