forked from meppadiyan/movieStats
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweekly.js
208 lines (194 loc) · 6.28 KB
/
weekly.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
const { parseString } = require("fast-csv");
const fs = require("fs");
const { HttpsProxyAgent } = require("https-proxy-agent");
const { orderBy, round, shuffle, startCase } = require("lodash");
const fetch = require("node-fetch");
const path = require("path");
const { launch } = require("puppeteer");
const sharp = require("sharp");
const { csvPath, executablePath, local, proxy } = require("./config/env.js");
const { toEnIn } = require("./config/misc.js");
const { sync } = require("./config/git.js");
const { moment } = require("./config/moment.js");
const { db, syncFileInfo } = require("./config/nedb.js");
const json_path = path.resolve(__dirname, "./store/data.json");
const json = fs.existsSync(json_path)
? JSON.parse(fs.readFileSync(json_path, "utf8"))
: {};
const collageMax = 6;
(async () => {
const start_date = moment("2025-02-24", ["YYYY-MM-DD"]).startOf("day");
const end_date = start_date.clone().add(7, "day").startOf("day");
await sync(csvPath); // git clone/pull
await syncFileInfo(csvPath); // sync folder/file metadata to nedb
for (const { group } of await db.find({
date: { $gte: start_date.toDate(), $lt: end_date.toDate() },
group: { $exists: true },
})) {
const $in = await db.find({ group });
await db.update(
{ id: { $in: $in.map((i) => i.id) } },
{ $set: { group } },
{ multi: true }
);
}
// aggregate
const data = {};
for (const i of await db
.find({ date: { $gte: start_date.toDate(), $lt: end_date.toDate() } })
.sort({ date: 1 })) {
const file = path.resolve(
csvPath,
`${i.name}/${i.id}.${moment(i.date).format("YYYY-MM-DD")}.csv`
);
console.log(file);
const _id = i.group || i.id;
if (data[_id])
data[_id].images = [...data[_id].images, ...(json[i.id]?.images || [])];
else
data[_id] = {
name: i.name,
images: json[i.id]?.images || [],
shows: 0,
booked: 0,
capacity: 0,
sum: 0,
};
await new Promise(async (resolve, reject) => {
const csv = [];
parseString(fs.readFileSync(file, "utf8"), { headers: true })
.on("error", reject)
.on("data", (row) => csv.push(row))
.on("end", () => {
for (const j of csv) {
const show_id = `${j.City}|${j.Name}|${j.Language}|${j["Time(IST)"]}`; // unique show id
const booked = +j.Booked.replace(/[^0-9]+/g, "");
const capacity = +j.Capacity.replace(/[^0-9]+/g, "");
const sum = booked * +j.Price.split(".")[0].replace(/[^0-9]+/g, "");
if (capacity) {
if (data[_id].show_id != show_id) {
data[_id].show_id = show_id;
data[_id].shows += 1;
}
data[_id].booked += booked;
data[_id].capacity += capacity;
data[_id].sum += sum;
}
}
resolve(true);
});
});
}
const items = orderBy(
Object.values(data).filter((i) => i.images.length),
["booked", "sum"],
["desc", "desc"]
);
console.log(
items.reduce(
(m, { name }) => (`${m} #${name}`.length < 240 ? `${m} #${name}` : m),
`#Kerala #BoxOffice ${start_date.format("MMMD")}/${end_date.format(
"MMMD"
)} ${Math.round(end_date.diff(start_date, "week", true))}Week Summary`
)
);
// image processing
for (const item of items.slice(0, collageMax)) {
for (const i of shuffle(item.images)) {
const [link, size] = i.split("|");
if (32 * 1024 < +size) {
console.log(item.name, link);
item.image = link;
item.dominant = (
await sharp(
await (
await fetch(link, {
...(proxy ? { agent: new HttpsProxyAgent(proxy) } : {}),
headers: { "user-agent": "curl/1.0" },
})
).buffer()
).stats()
).dominant;
item.bg =
"#" +
Object.values(item.dominant)
.map((e) => e.toString(16).padStart(2, 0))
.join("");
item.fg =
128 <
Math.round(
(item.dominant.r * 299 +
item.dominant.g * 587 +
item.dominant.b * 114) /
1000
)
? "black"
: "white";
break;
}
}
}
// other props
for (const item of items) {
item.booked_ = toEnIn(item.booked, "en-in", { notation: "compact" });
item.occupancy = item.booked
? `${round((item.booked / item.capacity) * 100, 2)}%`
: "";
item.gross = toEnIn(item.sum, "en-in", { notation: "compact" });
}
// html generation
const html_path = path.resolve(__dirname, "./weekly.html");
const html = fs.existsSync(html_path)
? fs.readFileSync(html_path, "utf8")
: "";
fs.writeFileSync(
`${html_path}.html`,
String.raw({ raw: html.split("$?") }, [
JSON.stringify(items.slice(0, collageMax)),
])
);
// screenshot
const browser = await launch({
args: [
"--disable-setuid-sandbox",
"--lang=en-IN,en",
"--no-sandbox",
"--window-size=1920,1080",
],
defaultViewport: { width: 1920, height: 1080 },
executablePath,
// headless: false,
});
let [page] = await browser.pages();
if (!page) page = await browser.newPage();
await page.goto(`file:///${path.resolve(__dirname, "./weekly.html.html")}`, {
waitUntil: "networkidle0",
});
await (
await page.$("#screenshot")
).screenshot({
path: path.resolve(local, "weekly.png"),
});
await browser.close();
// md generation
let text = `Kerala BoxOffice ${start_date.format(
"Wo"
)} Week Summary (${start_date.format("MMM DD")} - ${end_date.format(
"MMM DD YYYY"
)})\n\n| Movie | Shows | Occupancy↓ | Gross |\n| - | -: | -: | -: |`;
for (const item of items)
text += `\n| [${startCase(
item.name
)}](https://github.com/hedcet/boxoffice/tree/main/${item.name}) | ${toEnIn(
item.shows
)} | ${item.booked_}${item.occupancy ? `(${item.occupancy})` : ""} | ₹${
item.gross
} |`;
text += `\n\n[source](https://github.com/hedcet/boxoffice/commits/main/?since=${start_date
.clone()
.add(1, "day")
.format("YYYY-MM-DD")}&until=${end_date.format(
"YYYY-MM-DD"
)}) | last updated at ${moment().format("YYYY-MM-DDTHH:mmZ")}`;
console.log(text);
})();