-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.js
173 lines (145 loc) · 6.13 KB
/
scraper.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
const app = require('electron').app
const ipcRenderer = require('electron').ipcRenderer
const path = require('path')
const mapl = require('map-limit')
const jsdom = require('jsdom')
const dateFormat = require('dateformat')
onload = function () {
// webview element
const webview = document.getElementById('twitter')
// get user login from main process
var user = {}
ipcRenderer.on('get-user-info-reply', function (event, arg) {
user = arg
})
ipcRenderer.send('get-user-info', '')
// login after 3 seconds
setTimeout(function () {
var login = `
document.getElementsByClassName('js-username-field')[0].value = '${ user.username }'
document.getElementsByClassName('js-password-field')[0].value = '${ user.password }'
document.querySelectorAll('input[name="remember_me"]')[1].checked = false
document.querySelector('button[type="submit"]').click()
`
webview.executeJavaScript(login, false, function (result) {
scroll()
})
}, 3000)
// scroll to bottom 4 times @ intervals of 3 seconds
var scrollCounter = 0;
function scroll () {
setTimeout(function () {
if (scrollCounter < 4) {
var scrollTo = 'window.scrollTo(0, 999999)'
webview.executeJavaScript(scrollTo, false, function (result) {
scrollCounter++
scroll()
})
} else {
// start scraping
var getTweets = `
var array = []
var promoted = document.getElementsByClassName('promoted-tweet')
for (var tweet of promoted) {
array.push(tweet)
}
array
`
// query DOM for all promoted tweets
webview.executeJavaScript(getTweets, false, function (result) {
var ads = result
// quit if there are no promoted tweets
if (ads.length === 0) {
ipcRenderer.send('quit', '')
} else {
// iterate through promoted tweets one at a time
var tweetCounter = 0
mapl(ads, 1, function(tweet, next) {
// convert innerHTML of promoted tweet to DOM
var query = `document.getElementsByClassName('promoted-tweet')[${ tweetCounter }].innerHTML`
webview.executeJavaScript(query, false, function (result) {
jsdom.env(result, function (err, window) {
if (err) {
throw err
} else {
// extract username, url, text from HTML markup
var username = window.document.getElementsByClassName('js-action-profile-name')[1].getElementsByTagName('b')[0].textContent
var url = 'https://twitter.com' + window.document.getElementsByClassName('js-permalink')[0].href
var text = window.document.getElementsByClassName('js-tweet-text')[0].textContent
// compose timestamp (eg. 2016-12-25_09-00-05-175)
var timestamp = dateFormat(new Date(), 'yyyy-mm-dd_HH-MM-ss-l')
// compose filename
var filename = path.resolve(__dirname, `${timestamp}_${username}`)
// compose text file content
var output = `Username: ${username}\n\nURL: ${url}\n\nTweet: ${text}`
// export text file
ipcRenderer.send('save-text', {
filename: filename,
text: output
})
// get bounds of promoted tweet for screenshot
var query = `
var bound = document.getElementsByClassName('promoted-tweet')[${ tweetCounter }].getBoundingClientRect()
var rect = {
y: bound.top,
width: bound.width,
height: bound.height
}
var y = window.scrollY
var array = [rect, y]
array
`
webview.executeJavaScript(query, false, function (result) {
var ad = {
y: result[0].y,
width: result[0].width,
height: result[0].height
}
var y = {
viewport: result[1],
ad: result[1] + ad.y
}
// scroll to ad
var query = `window.scrollTo(0, ${ y.ad - 48 })`
webview.executeJavaScript(query, false, function (result) {
// get device pixel ratio for screenshot purposes
var query = 'window.devicePixelRatio'
webview.executeJavaScript(query, false, function (result) {
var pixelRatio = Number(result)
// create bound rect for screenshot
var rect = {
x: 337 * pixelRatio,
y: 48 * pixelRatio,
width: tweet.width * pixelRatio,
height: tweet.height * pixelRatio
}
// screenshot ad after 2 seconds
setTimeout(function () {
webview.capturePage(rect, function (image) {
// move onto next ad
tweetCounter++
next(null, 0)
ipcRenderer.send('save-image', {
filename: filename,
image: image.toPNG()
})
})
}, 2000)
})
})
})
}
})
})
}, function(err, results) {
// quit after all ads are saved
setTimeout(function () {
ipcRenderer.send('quit', '')
}, 5000)
})
}
})
}
}, 3000)
}
}