-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetl_episodes.js
141 lines (117 loc) · 4.07 KB
/
etl_episodes.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
const fs = require("fs");
const fsp = fs.promises;
const { readFile, mkdir, writeFile } = fsp;
const cheerio = require("cheerio");
const got = require("got");
const stream = require("stream");
const { promisify } = require("util");
const pipeline = promisify(stream.pipeline);
const rimraf = require("rimraf");
async function main() {
rimraf.sync("office_json/episodes_3");
const files = await fsp.readdir("office_html/episodes");
const all = await Promise.all(
files
.filter((file) => file && file.endsWith(".json"))
.map(async (file) => {
const filepath = `office_html/episodes/${file}`;
const json = JSON.parse((await fsp.readFile(filepath)).toString());
delete json.file;
delete json.summary;
json.season = json.season.split(" ")[1];
json.season = Number(json.season);
json.episodes = json.episode.split("/").map((i) => Number(i));
json.episodeDetails = JSON.parse(
(
await fsp.readFile(`office_json/episodes_2/${json.title}.json`)
).toString()
);
const quotesFileName = `office_json/quotes_2/${json.title
.replace(/\s/g, "_")
.replace(/é/g, "e")}_Quotes.json`;
if (fs.existsSync(quotesFileName)) {
json.quotesDetails = JSON.parse(
(await readFile(quotesFileName)).toString()
);
} else {
console.log("File not found:", quotesFileName);
}
const coldOpensJson = JSON.parse(
(
await readFile(`office_json/cold_opens/Season ${json.season}.json`)
).toString()
);
json.coldOpens = coldOpensJson.li.filter(
(c) => c.indexOf(`("${json.title}")`) > -1
);
if (!json.coldOpens.length) {
if (json.title === "Ultimatum") {
json.coldOpens = coldOpensJson.li.filter(
(c) => c.indexOf(`("The Ultimatum")`) > -1
);
}
if (json.title === "Café Disco") {
json.coldOpens = coldOpensJson.li.filter(
(c) => c.indexOf(`("Cafe Disco")`) > -1
);
}
}
json.coldOpens = json.coldOpens.map((c) =>
c.trim().replace(/\(".+"\)$/, "")
);
json.storylines = json.episodeDetails.sections.filter((details) => {
const key = ["Plot", "Summary", "Synopsis"].find(
(t) => t === details.title
);
return key != null;
});
if (json.storylines.length !== 1) {
console.log("Storylines:", json.storylines.length, "=>", json.title);
} else {
json.storylines = json.storylines[0].content.map((c) => c.text);
}
if (json.quotesDetails && json.quotesDetails.sections) {
json.quotes = json.quotesDetails.sections
.map((i) => i.content)[0]
.map((c) => c.text)
.filter((i) => !!i);
if (json.quotes.length === 0) {
delete json.quotes;
}
if (json.quotes) {
json.quotes = json.quotes
.filter((i) => !!i)
.map((quote) =>
quote.split("\n").map((quotePart) => {
let [speaker, ...contents] = quotePart.split(":");
let content = contents.join(":");
if (content) {
return { speaker, content };
}
return { content: quotePart };
})
);
}
}
delete json.episodeDetails;
delete json.quotesDetails;
if (!fs.existsSync(`office_json/episodes_3`)) {
await mkdir(`office_json/episodes_3`);
}
await writeFile(
`office_json/episodes_3/${json.season}--${json.episode.replace(
/\//g,
"-"
)}.json`,
JSON.stringify(json, null, 4)
);
return json;
})
);
await writeFile(
"office_json/episodes_concat.json",
JSON.stringify(all, null, 4)
);
console.log("Done", all[Math.floor(all.length * Math.random())]);
}
main();