forked from Senexis/Google-Weather-Frog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
151 lines (123 loc) · 5.44 KB
/
weather.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
const https = require("https");
const fs = require("fs");
const { performance } = require('perf_hooks');
const wideRegex = /(?<url>https:\/\/www\.gstatic\.com\/weather\/froggie\/l\/)(?<name>.*?)_2x(?<ext>\.png)/g;
const locationUrls = [
["https://www.google.com/search?q=weather+nieuwendijk", "nieuwendijk"],
]
const colorsRegex = /background:-webkit-linear-gradient\((?<colors>.*?)\)/g;
const androidUserAgent = {
headers: {
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0.1; A0001 Build/MHC19Q; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.80 Mobile Safari/537.36 GSA/10.28.7.21.arm"
}
};
var nextIterationDelay = 0;
var iterationDelayAmount = 3000;
function getImages(url, regex, getColors = false) {
return new Promise((resolve, reject) => {
try {
https.get(url, androidUserAgent, (response) => {
if (response.statusCode !== 200) {
reject("Non-OK status code: " + response.statusCode);
return;
}
var body = "";
response.on("data", function (chunk) {
body += chunk;
});
response.on("end", function () {
var images = [];
var match;
while (match = regex.exec(body)) {
var object = {
url: match[0],
name: match[2],
extension: match[3]
};
if (getColors) {
var color;
while (color = colorsRegex.exec(body)) {
object.css = color[0];
object.gradient = color[1];
}
}
images.push(object);
}
resolve(images);
});
}).on("error", (error) => {
reject(error);
});
} catch (error) {
reject(error);
}
});
}
function downloadFile(url, path) {
return new Promise((resolve, reject) => {
try {
var file = fs.createWriteStream(path);
https.get(url, (response) => {
if (response.statusCode !== 200) {
file.end(() => {
fs.unlinkSync(path);
});
reject("Non-OK status code: " + response.statusCode);
return;
}
response.pipe(file);
response.on('end', () => {
file.close(resolve);
});
}).on("error", (error) => {
reject(error);
});
} catch (error) {
reject(error);
}
});
}
function makeFile(data, path) {
return new Promise((resolve, reject) => {
try {
fs.writeFile(path, data, (error) => {
if (error) {
reject(error);
return;
}
resolve();
});
} catch (error) {
reject(error);
}
})
}
async function doLocationsDownload() {
locationUrls.forEach(async (item, index, array) => {
nextIterationDelay = nextIterationDelay + iterationDelayAmount;
setTimeout(async () => {
const realIndex = (index + 1).toString();
const padLength = array.length.toString().length;
const currentItemString = "[L: " + realIndex.padStart(padLength, '0') + "/" + array.length + "]";
const performanceString = "[" + Math.round(performance.now()) + " ms]";
var result = await getImages(item[0], wideRegex, true).catch((error) => console.log(currentItemString, performanceString, "Item failed:", JSON.stringify(error)));
if (result === undefined) {
return;
}
if (result.length < 1) {
console.log(currentItemString, performanceString, "Item failed, empty result:", item[0]);
return;
}
result.forEach(async (image) => {
console.log(currentItemString, performanceString, "Item:", image.name + image.extension)
var css = ".weather-frog { background: linear-gradient(" + image.gradient + "); background: -moz-linear-gradient(" + image.gradient + "); background: -ms-linear-gradient(" + image.gradient + "); background: -o-linear-gradient(" + image.gradient + "); background: -webkit-linear-gradient(" + image.gradient + "); }";
await makeFile(css, "./locations/" + item[1] + "/weather-frog.css");
await downloadFile(image.url.replace("_2x.png", "_4x.png"), "./locations/" + item[1] + "/weather-frog" + image.extension).catch((error) => console.log(currentItemString, performanceString, "Item failed:", JSON.stringify(error)));
});
console.log(currentItemString, performanceString, "Item complete.");
}, nextIterationDelay);
});
}
console.log("=== [" + new Date().toString() + "] ===");
console.log("Totals: " + locationUrls.length + " location URL(s)");
doLocationsDownload().catch((error) => console.error(error));