forked from Senexis/Google-Weather-Frog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.js
executable file
·106 lines (82 loc) · 4.43 KB
/
sync.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
const https = require("https");
const fs = require("fs");
const { performance } = require('perf_hooks');
var nextIterationDelay = 0;
var iterationDelayAmount = 500;
function downloadFile(url, path) {
return new Promise((resolve, reject) => {
try {
if (fs.existsSync(path)) {
if (fs.statSync(path).size > 0) {
resolve();
return;
}
}
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);
}
});
}
async function doSyncWides() {
var squareImages = fs.readdirSync('./images/square');
squareImages.shift();
squareImages.forEach((file, index, array) => {
array[index] = file.replace('_bg', '').replace('_mg', '').replace('_fg', '').replace('.png', '');
});
var uniqueSquareImages = [...new Set(squareImages)];
uniqueSquareImages.forEach(async (file, index, array) => {
nextIterationDelay = nextIterationDelay + iterationDelayAmount;
setTimeout(async () => {
const realIndex = (index + 1).toString();
const padLength = array.length.toString().length;
const currentItemString = "[S: " + realIndex.padStart(padLength, '0') + "/" + array.length + "]";
const performanceString = "[" + Math.round(performance.now()) + " ms]";
var url = "https://www.gstatic.com/weather/froggie/l/" + file;
await downloadFile(url + "_4x.png", "./images/wide/" + file + ".png").catch((error) => console.log(currentItemString, performanceString, "Item failed:", JSON.stringify(error)));
await downloadFile(url + "_c_4x.png", "./images/wide/" + file + "_c.png").catch((error) => console.log(currentItemString, performanceString, "Item failed:", JSON.stringify(error)));
await downloadFile(url + "_f_4x.png", "./images/wide/" + file + "_f.png").catch((error) => console.log(currentItemString, performanceString, "Item failed:", JSON.stringify(error)));
console.log(currentItemString, performanceString, "Item complete.");
}, nextIterationDelay);
});
}
async function doSyncSquares() {
var wideImages = fs.readdirSync('./images/wide');
wideImages.shift();
wideImages.forEach((file, index, array) => {
array[index] = file.replace('.png', '');
})
var uniqueWideImages = [...new Set(wideImages)];
uniqueWideImages.forEach(async (file, index, array) => {
nextIterationDelay = nextIterationDelay + iterationDelayAmount;
setTimeout(async () => {
const realIndex = (index + 1).toString();
const padLength = array.length.toString().length;
const currentItemString = "[W: " + realIndex.padStart(padLength, '0') + "/" + array.length + "]";
const performanceString = "[" + Math.round(performance.now()) + " ms]";
var url = "https://ssl.gstatic.com/onebox/weather/doodle/temp2/" + file;
await downloadFile(url + "_bg.png", "./images/square/" + file + "_bg.png").catch((error) => console.log(currentItemString, performanceString, "Item failed:", JSON.stringify(error)));
await downloadFile(url + "_fg.png", "./images/square/" + file + "_fg.png").catch((error) => console.log(currentItemString, performanceString, "Item failed:", JSON.stringify(error)));
await downloadFile(url + "_mg.png", "./images/square/" + file + "_mg.png").catch((error) => console.log(currentItemString, performanceString, "Item failed:", JSON.stringify(error)));
console.log(currentItemString, performanceString, "Item complete.");
}, nextIterationDelay);
})
}
console.log("=== [" + new Date().toString() + "] ===");
doSyncWides().catch((error) => console.error(error));
doSyncSquares().catch((error) => console.error(error));