This repository has been archived by the owner on Nov 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcross.js
115 lines (94 loc) · 3.29 KB
/
cross.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
const https = require("https");
const fs = require("fs");
const { performance } = require('perf_hooks');
var nextIterationDelay = 0;
var iterationDelayAmount = 500;
const weatherTypes = [
'01-sunny',
'02-mostly-sunny',
'03-partly-cloudy-day',
'04-mostly-cloudy-day',
'05-clear',
'06-mostly-clear',
'07-partly-cloudy-night',
'08-mostly-cloudy-night',
'09-cloudy',
'10-drizzle',
'11-rain',
'12-heavy-rain',
'13-flurries',
'15-snow-showers-snow',
'16-blowing-snow',
'17-heavy-snow-blizzard',
'19-mixed-rain-hail-rain-sleet',
'20-rain-snow-wintry-mix',
'22-iso-thunderstorms',
'22-iso-thunderstorm',
'24-strong-thunderstorms',
'25-breezy-windy',
'26-haze-fog-dust-smoke'
]
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 doSyncTypes() {
var images = fs.readdirSync('./images/wide');
images.shift();
images.forEach((file, index, array) => {
var currentItem = array[index];
currentItem = currentItem.replace('.png', '');
weatherTypes.forEach((type) => {
currentItem = currentItem.replace(type, '');
});
currentItem = currentItem.replace(/^-+|-+$/g, '');
array[index] = currentItem;
});
var unique = [...new Set(images)];
var compiledList = [];
unique.forEach(image => {
weatherTypes.forEach(type => {
compiledList.push(type + '-' + image);
});
});
compiledList.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)));
console.log(currentItemString, performanceString, "Item complete.");
}, nextIterationDelay);
});
}
console.log("=== [" + new Date().toString() + "] ===");
doSyncTypes().catch((error) => console.error(error));