-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (85 loc) · 3.32 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
const Nightmare = require('nightmare')
const fs = require('fs')
const vo = require('vo')
if (typeof process.env.PROJECT_NAME === 'undefined')
throw 'Please provide a indiegogo project name as an environment variable PROJECT_NAME'
const INDIEGOGO_URL = `https://www.indiegogo.com/projects/${process.env.PROJECT_NAME}#/backers`
const SHOW_MORE_SELECTOR = '.campaignBackers > .campaignBackers-seeMore'
const SHOW_MORE_BUTTON_SELECTOR = '.campaignBackers-seeMore > a'
const BACKERS_COMPONENT_SELECTOR = 'campaign-backers > .campaignBackers'
const WAIT_TIME = 1000
const DEBUG = typeof process.env.DEBUG !== 'undefined'
const DEBUG_OPTIONS = {
show: true,
openDevTools: true,
waitTimeout: 90000000
}
var nightmareOptions = DEBUG ? DEBUG_OPTIONS : {}
vo(run)(function (err, result) {
if (err) throw err
console.log('Total of', result.length, 'backers')
result = result.sort(sortAmounts).reverse()
fs.writeFileSync('backers.json', JSON.stringify(result, null, 4))
process.exit(0)
})
function* run() {
console.log(`Gathering info from ${INDIEGOGO_URL}`)
var nightmare = Nightmare(nightmareOptions)
.goto(INDIEGOGO_URL)
.wait(WAIT_TIME)
.wait(SHOW_MORE_BUTTON_SELECTOR)
var moreExists = true
while (moreExists) {
var currentHeight = yield nightmare.evaluate(function () {
return document.body.scrollHeight
})
yield nightmare.scrollTo(currentHeight, 0)
.wait(WAIT_TIME)
yield nightmare
.click(SHOW_MORE_BUTTON_SELECTOR)
.wait(WAIT_TIME)
var currentHeight = yield nightmare.evaluate(function () {
return document.body.scrollHeight
})
yield nightmare.scrollTo(currentHeight, 0)
.wait(WAIT_TIME)
moreExists = yield nightmare.evaluate(SHOW_MORE_SELECTOR => {
return document.querySelectorAll(SHOW_MORE_SELECTOR).length > 1
}, SHOW_MORE_SELECTOR)
console.log('Loading more backers...')
}
console.log('All backers loaded')
var allPledges = yield nightmare.evaluate(resolveAllBackers, BACKERS_COMPONENT_SELECTOR)
yield nightmare.end()
return allPledges
}
function resolveAllBackers(BACKERS_COMPONENT_SELECTOR) {
var pledgeItemElements = document.querySelector(BACKERS_COMPONENT_SELECTOR).children
var pledgeItems = Array.prototype.map.call(pledgeItemElements, parsePledge)
return pledgeItems
function parsePledge(pledgeElement) {
var pledge = {
imgSrc: pledgeElement.querySelector('img').src,
name: pledgeElement.querySelector('.campaignBackers-pledge-backer-details-text').innerText,
url: pledgeElement.querySelector('.campaignBackers-pledge-backer-details-text').href
}
var amount = pledgeElement.querySelector('.campaignBackers-pledge-amount-bold').innerText
if (amount.match(/\$/)) {
pledge.amount = amount.replace(/[^.\d$]/g, '')
} else {
pledge.amount = amount
}
return pledge
}
}
function sortAmounts(a, b) {
return parseAmount(a) - parseAmount(b)
}
function parseAmount(backer) {
var amount = backer.amount
if (typeof amount === 'undefined' || amount === '' || amount === 'Private') {
amount = "$0"
}
var value = parseInt(amount.replace('$', ''))
return value
}