-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
132 lines (108 loc) · 3.61 KB
/
app.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
/**
* Variables n shit
*/
const { exec } = require("child_process");
const fs = require('fs');
const cheerio = require('cheerio');
const request = require('request');
const nihongo = require('nihongo');
const searchUrl = "https://eu.finalfantasyxiv.com/lodestone/playguide/db/item/?patch=&db_search_category=item&category2=&q={ITEM_NAME}";
const itemUrl = "https://eu.finalfantasyxiv.com";
// ---------------------------------------
// Arguments
// ---------------------------------------
let [start, size, force] = process.argv.slice(2);
start = parseInt(start);
size = parseInt(size);
let end = start + size;
// ---------------------------------------
// Functions
// ---------------------------------------
const getPageContents = (url, callback) => {
url = encodeURI(url);
exec(`php get_page.php "${url}"`, (error, stdout, stderr) => {
callback(stdout);
});
};
const getItemIconUrl = (url, callback) => {
// parse search page
getPageContents(url, html => {
let $ = cheerio.load(html);
let pageUrl = itemUrl + $('.db-table__txt--detail_link').attr('href');
// parse item page
getPageContents(pageUrl, html => {
let $ = cheerio.load(html);
let iconUrl = $('.db-view__item__icon__item_image').attr('src');
callback(iconUrl);
});
})
};
// ---------------------------------------
// Exec
// ---------------------------------------
// Make dir folder if it doesn't exist
fs.mkdirSync('img/', { recursive: true });
// grab ItemData.json file
console.log("Reading ItemData.json file");
var item_data = JSON.parse(fs.readFileSync('ItemData.json', 'utf8'));
// remove entries we don't care about
console.log(`Reducing the item list to: ${start} and ${end}`);
item_data = item_data.splice(start, (end - start));
// set total
const total = item_data.length;
console.log(item_data);
console.log(`${item_data.length} entries`);
if (item_data.length <= 0) {
console.log("Nothing to download for this range.");
return;
}
// begin downloading icons
let current = 0;
const doshit = () => {
current++;
if (item_data.length <= 0) {
console.log("Finished all icons!!!");
return;
}
// we always get 0 because we remove entries as we work through them
let item = item_data[0];
let url = searchUrl.replace('{ITEM_NAME}', item.name_en);
let filename = 'img/' + `${item.name_en} Icon.png`.replace(/\//g, "-");
console.log(`- (${current}/${total}) ${item.name_en}`);
// skip already downloaded ones
if (!force && fs.existsSync(filename)) {
item_data.splice(0, 1);
doshit();
return;
}
// check if the item name is Japanese
if (nihongo.isJapanese(item.name_en)) {
console.error(`---> Error: Above entry is Japanese, skipping...`);
item_data.splice(0, 1);
doshit();
return;
}
// get the item icon url
getItemIconUrl(url, itemIconUrl => {
// download and save the icon
request.get({
url: itemIconUrl,
encoding: 'binary'
}, function (err, response, body) {
if (!body) {
console.error(`---> Error: Failed to get image for the above item, icon will not be saved`);
item_data.splice(0, 1);
doshit();
return;
}
fs.writeFile(filename, body, 'binary', (error) => {
if (error) {
console.error(err);
}
item_data.splice(0, 1);
doshit();
});
});
});
};
doshit();