-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (136 loc) · 4.46 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
149
150
151
152
const { writeFileSync } = require("fs");
const Ajv = require("ajv");
const addFormats = require("ajv-formats");
const ics = require("ics");
const getSites = require("./common/get-sites");
const {
dailyCache,
getCacheStats,
clearCacheStats,
} = require("./common/cache");
const hydrate = require("./common/hydrate");
const {
generateEventDescription,
getEventDate,
parseMinsToMs,
sanitize,
} = require("./common/utils");
const schema = require("./schema.json");
const getDuration = (show) => {
const title = show.title.toLowerCase();
const isAllNighter = !!title.match(/all[\s|-]night/i);
// Default to 90 minutes if we don't know the duration
// unless it's an all nighter, then make it 6 hours
const defaultDuration = isAllNighter ? parseMinsToMs(360) : parseMinsToMs(90);
return show.overview.duration || defaultDuration;
};
async function generateCalendar(cinema) {
const {
retrieve,
transform,
attributes: { url, location, geo },
} = require(`./cinemas/${cinema}`);
clearCacheStats();
console.log(`[🎞️ Cinema: ${cinema}]`);
process.stdout.write(` - Retriving data ... `);
let data;
try {
data = await dailyCache(cinema, () => retrieve());
console.log(`\t✅ Retrieved`);
} catch (e) {
console.log(`\t❌ Error retriving`);
throw e;
}
process.stdout.write(` - Transforming data ... `);
let shows;
try {
shows = await transform(data);
console.log(`\t✅ Transformed`);
} catch (e) {
console.log(`\t❌ Error transforming`);
throw e;
}
process.stdout.write(` - Hydrating movie data ... `);
let hydratedShows;
try {
hydratedShows = await hydrate(shows);
const hydrated = hydratedShows.filter(({ moviedb }) => !!moviedb).length;
console.log(`\t✅ Hydrated (${hydrated} of ${hydratedShows.length})`);
} catch (e) {
console.log(`\t❌ Error hydrating`);
throw e;
}
process.stdout.write(` - Validating data ... `);
const ajv = new Ajv({ allErrors: true });
addFormats(ajv);
const validate = ajv.compile(schema);
if (!validate(hydratedShows)) {
console.log(`\t❌ Error validating`);
console.log(validate.errors);
throw new Error("Error validating");
}
console.log(`\t✅ Validated`);
const dataFile = `./output/${cinema}-shows.json`;
writeFileSync(dataFile, JSON.stringify(hydratedShows, null, 4));
process.stdout.write(` - Generating calendar ... `);
let icsFormattedEvents;
try {
icsFormattedEvents = hydratedShows.reduce((events, show) => {
const duration = getDuration(show);
const showEvents = show.performances.map((performance) => ({
title: sanitize(show.title),
description: generateEventDescription(show, performance),
categories: [].concat(show.overview.categories),
start: getEventDate(performance.time),
end: getEventDate(performance.time + duration),
url,
location,
geo,
}));
return events.concat(showEvents);
}, []);
} catch (e) {
console.log(`\t❌ Error generating events`);
throw new Error("Error generating events");
}
const { error, value } = ics.createEvents(icsFormattedEvents);
if (error) {
console.log(`\t❌ Error generating ISC file`);
console.log(error);
throw new Error("Error generating ICS file");
}
console.log(`\t✅ Generated`);
const calendarFile = `./output/${cinema}-calendar.ics`;
writeFileSync(calendarFile, value);
console.log(`🗂️ Files created`);
console.log(" ");
const {
hits: { length: hit },
misses: { length: miss },
} = getCacheStats();
const percentage = Math.round((hit / (hit + miss)) * 100);
console.log(`📊 ${percentage}% cache success (${hit} hits, ${miss} misses)`);
const unhydrated = hydratedShows.filter((show) => !show.moviedb);
const unhydratedCount = unhydrated.length;
if (unhydratedCount > 0) {
const showsText = `show${unhydratedCount === 1 ? "" : "s"}`;
console.log(`🏜️ Unable to hydrate ${unhydratedCount} ${showsText}`);
console.log(` * ${unhydrated.map(({ title }) => title).join("\n * ")}`);
} else {
console.log(`🌊 All shows hydrated`);
}
}
(async function () {
const parameter = process.argv[2];
const sites = getSites();
if (parameter === "all") {
for (site of sites) {
await generateCalendar(site);
console.log("\n---\n");
}
} else if (sites.includes(parameter)) {
await generateCalendar(parameter);
} else {
throw new Error("❌ Invalid cinema site provided");
}
})();