-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-ticket-data.js
96 lines (80 loc) · 2.95 KB
/
get-ticket-data.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
const { writeFileSync } = require("fs");
const cheerio = require("cheerio");
const parseDuration = require("parse-duration");
const { parse, format, getTime } = require("date-fns");
const en = require("date-fns/locale/en-GB");
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
const parseDateTime = (date, time) =>
parse(`${date} ${time}`, "EEEE d MMMM HH:mm", new Date(), { locale: en });
// const formatOverviewItem = (key, value) => {
// if (value.split(",").length > 1) {
// return value.split(",").map((item) => item.trim());
// }
// if (key === "duration") return parseDuration(value);
// if (key === "actors" || key === "category") return [value.trim()];
// return value.trim();
// };
const getDataFromHeading = ($heading) => {
const titleText = $heading.text().trim();
const titleWithCertificate = titleText.match(/^(.+?)\s+- Certificate (.*)$/i);
if (!titleWithCertificate) return { title: titleText };
return {
title: titleWithCertificate[1].trim(),
certificate: titleWithCertificate[2].trim(),
};
};
const createShowFrom = ($, $performance) => {
const { title, certificate } = getDataFromHeading($performance.find("h3"));
const overview = {};
if (certificate) overview["age-restriction"] = certificate;
const restrictionLogo = $performance.find("h3 a");
if (restrictionLogo.length > 0) {
const restriction = restrictionLogo.attr("alt").trim();
overview["age-restriction"] = restriction;
}
const performances = [];
const rowPairs = [];
const $rows = $performance.find(".row");
$rows.each(function (index) {
if (index % 2 === 0)
rowPairs.push([$rows.get(index), $rows.get(index + 1)]);
});
rowPairs.shift(); // throw away the title rows
rowPairs.forEach(([dateRow, showsRow]) => {
const readableDateString = $(dateRow).text().trim();
$(showsRow)
.find("a")
.each((_, timeLink) => {
const readableTimeString = $(timeLink).text().trim();
const performanceDateTime = parseDateTime(
readableDateString,
readableTimeString
);
performances.push({
time: getTime(performanceDateTime),
bookingUrl:
"https://eu.internet-ticketing.com/sales/STRBEL/" +
$(timeLink).attr("href"),
});
});
});
return {
title,
overview,
performances,
};
};
(async () => {
console.log(`Getting ticket site information ... `);
const ticketSiteUrl = "https://eu.internet-ticketing.com/sales/STRBEL/start";
const page = await fetch(ticketSiteUrl).then((response) => response.text());
const shows = [];
const $ = cheerio.load(page);
$("#start-main-page .start-performance-box").each((_, performance) => {
const show = createShowFrom($, $(performance));
shows.push(show);
});
const dataFile = `${__dirname}/strand-ticket-data.json`;
writeFileSync(dataFile, JSON.stringify(shows, null, 4));
console.log(`✅ New data file created at ${dataFile}`);
})();