-
Notifications
You must be signed in to change notification settings - Fork 2
/
metRunner.js
299 lines (202 loc) · 6.4 KB
/
metRunner.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* code to call all objects in scrapi, and run the callback function
usage
var metRunner = require("metRunner");
metRunner = metRunner.getMetRunner({objectCallback: objectCallback,
filterCallback : filterCallback,
finishedCallback : finishedCallback});
metRunner.run();
objectCallback = function to run for each individual objet page callback
- input : json array of object data from scrapi call
`
filtercallback = function, returns true or false, to process or skip the object
- input : json array of object data from scrapi call
finishedCallback : method to run when done.
*/
var $ = require("jquery");
var fs = require("./node_modules/node-fs/lib/fs");
var crypto = require('crypto');
if(typeof module !== 'undefined'){
// if we're running server-side, this stuff needs to happen.
module.exports.getMetRunner = getMetRunner;
var $ = require('jquery');
var jQuery = $;
}else{
// we're running client-side, and need a different way to include the necessary code.
console.log("some sort of problem creating module");
}
function getMetRunner(options){
return new metRunner(options);
}
var metRunner = function(options){
this.keepGettingPages = true;
this.numObjects = options.numObjects;
this.startpage = options.startpage;
this.endpage = options.endpage;
this.pendingCalls = 0;
this.fromCache = 0;
this.notFromCache = 0;
this.totalUrlsCalled = 0;
this.totalWrittenToCache = 0;
this.baseUrl = "http://scrapi.org/";
this.objectCallback = options.objectCallback;
this.filterCallback = options.filterCallback;
this.finishedCallback = options.finishedCallback;
}
metRunner.prototype.run = function(){
this.runOnAllMetObjects();
}
metRunner.prototype.runOnAllMetObjects = function(){
// iterate over all objects called from /ids,
var page = this.startpage;
this.getMetObjectsPage(page);
}
metRunner.prototype.getMetObjectsPage = function(pageNumber){
var url = this.baseUrl + "ids?page=" + pageNumber + "&images=true";
console.log("***" + url);
var realthis = this;
// console.log("calling objectlist url " + url);
this.cacheProxy(url, function(idList){realthis.processMetList(idList)});
var realthis = this;
pageNumber++;
if(this.keepGettingPages && pageNumber < this.endpage){
/*
if(this.pendingCalls > 3000){
console.log(this.pendingCalls + " too many pending, waiting");
setTimeout(function(){realthis.getMetObjectsPage(pageNumber);}, 1000);
}else{
*/
setTimeout(function(){realthis.getMetObjectsPage(pageNumber);}, 1000);
// }
}
}
metRunner.prototype.wait = function(){
realthis = this;
if(this.pendingCalls > 3000){
console.log(this.pendingCalls + " too many pending, waiting");
setTimeout(function(){realthis.wait();},1000);
}
}
metRunner.prototype.processMetList = function(idsList){
// this.wait();
console.log("idslist ");
console.log(idsList.collection.items);
if(idsList.collection.items.length == 0){
console.log("no objects on this page");
this.keepGettingPages = false;
return;
}
var realthis = this;
$(idsList.collection.items).each(function(key, value){
var objectUrl = value.href;
console.log("calling object url " + objectUrl);
realthis.cacheProxy(objectUrl, function(objectJson){realthis.processMetObject(objectJson)});
});
}
metRunner.prototype.processMetObject = function (objectJson){
// call the callback
if(this.filterCallback(objectJson) == false){
return;
}
this.objectCallback(objectJson);
}
metRunner.prototype.getFromCache = function(url){
// couch or filesystem?
var path = this.urlToCacheHash(url);
if(fs.existsSync(path)){
console.log(path);
this.fromCache++;
// get data file and return;
var data = fs.readFileSync(path, {encoding: "utf-8"});
console.log("got data from cache");
console.log(data);
if (data.trim() == ""){return false;}
data = JSON.parse(data);
console.log(data);
return data;
}
// console.log("no data at " + path);
this.notFromCache++;
return false;
}
metRunner.prototype.storeToCache = function(url, data){
// couch or filesystem
var dir = this.urlToDirPath(url);
if(!fs.existsSync(dir)){
// console.log("dir " + dir + " not found");
fs.mkdirSync(dir, 0777, true);
}
var filepath = this.urlToCacheHash(url);
fs.writeFile(filepath, JSON.stringify(data, null));
this.totalWrittenToCache++;
}
metRunner.prototype.urlToCacheHash = function(url){
console.log("cacheHash URL here" + url);
var hash = crypto.createHash('md5').update(url).digest("hex");
var path = this.urlToDirPath(url)+"/"+hash+".json";
return path;
}
metRunner.prototype.urlToDirPath = function(url){
var hash = crypto.createHash('md5').update(url).digest("hex");
// console.log("hash of " + url + " is " + hash);
var d1 = hash.substring(0,2);
var d2 = hash.substring(2,4);
var d3 = hash.substring(4,6);
var path = "cache/"+d1+"/"+d2+"/"+d3;
return path;
}
metRunner.prototype.urlToKey = function(url){
}
metRunner.prototype.cacheProxy = function (url, callback) {
// if use the url as key, try to get the object from cache.
// if not in cache, load from url, then save in cache.
// call callback when data retreived.
console.log("in cacheproxy, pending calls is " + this.pendingCalls);
var realthis = this;
// check in cache
var data = this.getFromCache(url);
this.totalUrlsCalled++;
console.log("from cache: " + this.fromCache + " not: " +this.notFromCache + " total : " + this.totalUrlsCalled);
if(data){
// console.log("got from cache");
callback(data);
if(realthis.pendingCalls == 0){
realthis.finishedCallback();
}
return;
}
// not bothering with scraping right now...
// return;
/*
if(this.pendingCalls > 3000){
// console.log(this.pendingCalls + " too many pending, waiting");
setTimeout(function(){realthis.cacheProxy(url, callback);}, 1000);
}
*/
this.pendingCalls++;
console.log("not in cache, getting data from url " +url);
$.ajax({
url : url,
error : function(retdata){
console.log("failure");
console.log(retdata);
realthis.pendingCalls--;
},
success : function (retdata){
// console.log("|"+retdata+"|");
realthis.pendingCalls--;
if(retdata == ''){
console.log("no results");
return true;
}
// console.log("parsing");
// console.log(retdata);
// retdata = JSON.parse(retdata);
// console.log("writing to cache: " + realthis.totalWrittenToCache);
realthis.storeToCache(url, retdata);
callback(retdata);
if(realthis.pendingCalls == 0){
realthis.finishedCallback();
}
}
});
}