-
Notifications
You must be signed in to change notification settings - Fork 1
/
tvdb.js
64 lines (55 loc) · 1.54 KB
/
tvdb.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
const api = require("./api");
const fs = require("./fs-io");
const subWeeks = require("date-fns/sub_weeks");
const authToken = fs.getToken();
if (!authToken) {
console.error(
"No authToken found. Please login to thetvdb first by running npm run login"
);
process.exit(1);
}
async function getShow(showName) {
try {
const cachedShow = fs.read().find(cache => cache.searchTerm == showName);
if (cachedShow) {
console.log(`${showName} => already in cache, skipping`);
return cachedShow;
} else {
const show = await api.search(showName, authToken);
console.log(
`Found show for query "${showName}": "${
show.seriesName
}", getting episodes`
);
const episodes = await api.getEpisodes(show.id, authToken);
const mappedEpisodes = episodes.map(e => ({
season: e.airedSeason,
episode: e.airedEpisodeNumber,
title: e.episodeName,
aired: new Date(e.firstAired),
buzzStart: subWeeks(new Date(e.firstAired), 1)
}));
return {
show: show.seriesName,
episodes: mappedEpisodes,
searchTerm: showName
};
}
} catch (e) {
console.error(`Error while getting Show "${showName}". Error was: ${e}`);
process.exit(1);
}
}
async function run() {
const series = ["Weeds"];
const promises = [];
await series.forEach(async function(serie) {
promises.push(getShow(serie));
});
Promise.all(promises).then((...results) => {
results.forEach(r => {
fs.write(r);
});
});
}
run();