-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (53 loc) · 1.56 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
/* global phantom */
var async = require('async');
var system = require('system');
var parameters = require('./lib/parameters');
var openProductsPage = require('./lib/openProductsPage');
var analysePrices = require('./lib/analysePrices');
var latestCards = require('./lib/latestCards');
var getUserCardsPrices = require('./lib/getUserCardsPrices');
var paramsObj = parameters(system.args);
// analyse the most recent JSON file if the script is called with the argument "--analyse=true"
if (paramsObj.analyse === 'true') {
var userCards = latestCards();
analysePrices(userCards);
phantom.exit();
}
/**
* Store all the cards the user is selling
*
* @type Card[]
*/
var userCards = [];
/**
* Keep count of the user's cards result page being checked
*
* @type Number
*/
var pageNum = 0;
// Keep getting the user's products while there are more pages
async.doWhilst(
/**
* Get the products from a user's page
*
* @param {Function} callback - A function which must be called once openProductsPage has completed
*/
function getProducts(callback) {
openProductsPage(userCards, callback, pageNum);
},
/**
* Check for a "next page" link to know whether to keep paginating
*
* @param {WebPage} page - The page instance that has been closed
* @returns {Boolean}
*/
function hasNext(page) {
// indexOf is faster than turning into DOM and querying for the <a> element
if (page.content.indexOf('rel="next"') !== -1) {
pageNum++;
return true;
}
return false;
},
getUserCardsPrices.bind(null, userCards)
);