-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (128 loc) · 4.57 KB
/
index.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
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');
const sharp = require('sharp');
const readFileAsync = (fileName) => {
return new Promise((resolve, reject) => {
fs.readFile(fileName, (err, data) => {
if (err)
reject(err);
resolve(data);
});
});
}
const dataToJsonFile = (data) => {
fs.writeFile("data.json", data, "binary", (err) => {
if (err) throw err;
console.log("Data saved in data.json");
});
}
const getWeaponDatas = () => {
return new Promise((resolve, reject) => {
axios.get(`https://main.codm.garena.co.id/guide/weapons/assault_rifle`)
.then(req => {
const data = {};
const $ = cheerio.load(req.data);
const weaponCategoryList = $("div.tabs__content div.tab-container__content");
weaponCategoryList.each(function (_, category) {
const title = [];
const img = [];
const tCategory = $(category).find("span.title").text();
data[tCategory] = [];
const weaponList = $(category).find("div.weapon-list div.weapon-list__list div.left");
weaponList.find("div.name-cont").each(function (_, e) {
title.push($(e).text());
})
weaponList.find("div.image-cont img").each(function (_, e) {
img.push($(e).attr('src'))
})
if (title == undefined || img == undefined) {
reject("Error occured title or image");
}
for (let index = 0; index < title.length; index++) {
let isi = {
"title": title[index],
"img": img[index]
}
data[tCategory].push(isi);
}
});
if (data == undefined) {
reject("No result :(");
} else {
resolve(data);
}
});
});
};
async function fetchServerData() {
const output = await getWeaponDatas();
var result = JSON.stringify(output, null, 2);
dataToJsonFile(result);
}
async function saveImgFromJsonFile() {
const rawData = await readFileAsync("data.json");
const data = JSON.parse(rawData.toString());
for (const key in data) {
if (Object.hasOwnProperty.call(data, key)) {
const arrData = data[key];
for (const jWeapon in arrData) {
if (Object.hasOwnProperty.call(arrData, jWeapon)) {
const element = arrData[jWeapon];
const imageRaw = await axios.get(element["img"], { responseType: 'arraybuffer' });
const imageExtensionRaw = element["img"].split(".");
const imageExtension = imageExtensionRaw[imageExtensionRaw.length - 1];
const imageNameRaw = element["title"].replaceAll(" ", "-");
const imageName = imageNameRaw + "." + imageExtension;
fs.mkdir("IconHD", (err) => { });
fs.writeFile(`IconHD/${imageName}`, imageRaw.data, "binary", (err) => {
if (err) throw err;
console.log("Download: ", imageName);
});
}
}
}
}
}
async function resizeDownloadImage() {
const rawData = await readFileAsync("data.json");
const data = JSON.parse(rawData.toString());
for (const key in data) {
if (Object.hasOwnProperty.call(data, key)) {
const arrData = data[key];
for (const jWeapon in arrData) {
const element = arrData[jWeapon];
const imageExtensionRaw = element["img"].split(".");
const imageExtension = imageExtensionRaw[imageExtensionRaw.length - 1];
const imageNameRaw = element["title"].replaceAll(" ", "-");
const imageName = imageNameRaw + "." + imageExtension;
fs.mkdir("Icon", (err) => { });
sharp(`IconHD/${imageName}`)
.resize(60)
.toFile(`Icon/${imageName}`, (err, info) => {
if (err)
console.error(err);
else
console.log("Resize: ", imageName);
})
}
}
}
}
/**
* If you want to fetch data from server use :
* main();
*
* If you want to save image data from server use :
* saveImgFromJsonFile();
*
* If you want to resize all the image use :
* resizeDownloadImage();
*
*/
async function main() {
await fetchServerData();
await saveImgFromJsonFile();
await resizeDownloadImage();
}
main();