-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·250 lines (217 loc) · 7.07 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
var config = require("./config.json");
/* Application */
var puppeteer = require('puppeteer');
var fs = require('fs');
var request = require("request");
var rp = require('request-promise');
var cheerio = require("cheerio");
var handlebars = require("handlebars");
var checklist = {};
var supportedJams = [
"ldjam.com",
"ludumdare.com",
"globalgamejam.org",
"itch.io/jam"
];
var ldGet = require("./lib/ldGet.js");
var ldjamGet = require("./lib/ldjamGet.js");
var itchGet = require("./lib/itchGet.js");
var ggjGet = require("./lib/ggjGet.js");
var messages = {
jamFail : "jam type not supported yet, or jamUrl not set in config",
urlFail : "\nERROR: \"{{url}}\" is not recognized as a valid jam game url...\n",
urlSuccess : "\"This is a {{jamType}} system, I know this.\" - Lex Murphy"
};
var totalEntriesCompo = undefined;
var totalEntriesJam = undefined;
requestJamMetadata (config, requestGamePages, requestGamePage, generateOutputFile);
function requestJamMetadata (config, requestGamePages, requestGamePage, generateOutputFile) {
// ldjam.com
if (config.jamUrl.indexOf("ldjam.com") > -1){
var games = [];
getPageHTML(config.jamUrl, "./temp/jampage.html", function(){
var htmlString = fs.readFileSync("./temp/jampage.html");
totalEntriesJam = ldjamGet.entries(cheerio.load(htmlString), "Jam");
console.log("Jam entries", totalEntriesJam);
totalEntriesCompo = ldjamGet.entries(cheerio.load(htmlString), "Compo");
console.log("Compo entries", totalEntriesCompo);
for (var i = 0; i < config.urls.length; i++){
getPageHTML(config.urls[i], "./temp/"+ i +".html", function(){
for (var i = 0; i < config.urls.length; i++){
var body = fs.readFileSync("./temp/"+ i +".html");
games.push(buildJamGame("ldjam.com", config, i, body));
if (games.length == config.urls.length){
generateOutputFile(games, config);
}
}
});
}
});
}
// ludumdare.com
else if (config.jamUrl.indexOf("ludumdare.com") > -1){
rp(config.jamUrl + "/?action=preview&etype=open")
.then( function (htmlString) {
totalEntriesJam = ldGet.entries(cheerio.load(htmlString), "Jam");
console.log("Jam entries", totalEntriesJam);
return rp(config.jamUrl + "/?action=preview&etype=compo")
})
.then( function (htmlString) {
totalEntriesCompo = ldGet.entries(cheerio.load(htmlString), "Compo");
console.log("Compo entries", totalEntriesCompo);
}).then( function () {
requestGamePages(config, requestGamePage, generateOutputFile);
})
.catch(err => console.log);
}
// Global Game Jam
else if (config.jamUrl.indexOf("globalgamejam.org") > -1){
requestGamePages(config, requestGamePage, generateOutputFile);
}
// itch.io/jam
else if (config.jamUrl.indexOf("itch.io/jam/") > -1){
rp(config.jamUrl)
.then( function (htmlString) {
totalEntriesJam = itchGet.entries(cheerio.load(htmlString));
console.log("Jam entries: ", totalEntriesJam);
})
.then( function () {
requestGamePages(config, requestGamePage, generateOutputFile);
})
.catch(err => console.log);
}
// bad url
else {
console.log(messages.jamFail);
}
}
function requestGamePages(config, requestGamePage, generateOutputFile){
var promises = [];
for (var i = 0; i < config.urls.length; i++) {
promises.push( requestGamePage(config, i, config.urls[i], generateOutputFile) );
}
return Promise.all(promises).then((games) => {
generateOutputFile(games, config);
});
}
function requestGamePage(config, i, currentUrl) {
return new Promise( function(resolve, reject) {
request(currentUrl, function (error, response, body) {
if(error) {
console.log(error);
resolve({title: "Something went wrong..."});
}
var jamType = whatJamIsThis(config.urls[i], supportedJams);
niceTerminalOutput(i, config.urls, supportedJams);
resolve( buildJamGame(jamType, config, i, body) );
});
});
}
function generateOutputFile(games, config){
if (config.ordering === "alpha"){
games = sortArrayObjects(games, "title");
}
fs.writeFile(config.outputFile, renderFromExternalTemplate(config.template, games), function(err) {
if(err) { return console.log(err); }
console.log("\n -- complete A++ would scrape again --\n");
});
}
function buildJamGame(jamType, config, i, body){
var $ = cheerio.load(body);
var jam;
switch (jamType) {
case "ludumdare.com":
jam = ldGet;
break;
case "ldjam.com":
jam = ldjamGet;
break;
case "itch.io/jam":
jam = itchGet;
break;
case "globalgamejam.org":
jam = ggjGet;
break;
}
var game = {};
game.url = config.urls[i];
game.title = jam.title($);
game.authors = jam.authors($);
game.screenshots = jam.screenshots($);
game.description = jam.description($);
if(jamType === "ludumdare.com" || jamType === "ldjam.com") {
game.type = jam.type($);
game.ratings = jam.ratings($, totalEntriesJam, totalEntriesCompo);
game.comments = jam.commentCount($);
}
if(jamType === "itch.io/jam"){
game.cover = jam.cover($);
game.submissionTime = jam.submissionTime($);
}
if(jamType === "globalgamejam.org"){
game.featuredImage = jam.featuredImage($);
}
return game;
}
function renderFromExternalTemplate(templateFile, games){
var file = fs.readFileSync(templateFile, "utf8");
var template = handlebars.compile(file);
return template(games);
}
function sortArrayObjects(array, objProp){
return array.sort(function(a, b){
if(a[objProp] < b[objProp]) return -1;
if(a[objProp] > b[objProp]) return 1;
return 0;
});
}
function whatJamIsThis (url, supportedJams) {
return supportedJams[supportedJams.map(function (thing) {
return contains(thing, url);
}).indexOf(true)];
}
function contains(needle, haystack){
return (haystack.indexOf(needle) > -1);
}
function msgForType (url, supportedJams) {
var jamType = whatJamIsThis(url, supportedJams);
if (contains(jamType, supportedJams)){
return messages.urlSuccess.replace("{{jamType}}", jamType);
} else {
return messages.urlFail.replace("{{url}}", url);
}
}
function niceTerminalOutput(i, configUrls, supportedJams){
console.log("\n -- processing", (i + 1) + "/" + configUrls.length, "-- \n", configUrls[i], "\n", msgForType(configUrls[i], supportedJams));
}
function getPageHTML(url, fileName, callback){
checklist[fileName] = false;
(async() => {
var browser = await puppeteer.launch({ headless: true });
var page = await browser.newPage();
await page.goto(url, {});
await page.waitFor(3000);
var html = await page.content();
fs.writeFile(fileName, html, () => {
console.log("saved", fileName);
checklist[fileName] = true;
if (checkListCheck(checklist)){
if(!callback){
console.log("okay, all done!");
}else{
callback();
}
}
});
browser.close();
})();
}
function checkListCheck(list) {
var result = true;
Object.keys(list).forEach( function (key) {
if (!list[key]){
result = false;
}
});
return result;
}