-
Notifications
You must be signed in to change notification settings - Fork 6
/
crawler.js
167 lines (122 loc) · 3.98 KB
/
crawler.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
var phantom = require('phantom');
var cheerio = require('cheerio');
var async = require('async');
var mongoose = require('mongoose');
var _ = require("underscore");
var querystring = require("querystring");
var fs = require('fs');
var configuration = require('./config/config.json');
var idx = parseInt(fs.readFileSync('./status.idx'));
var saveTweets = function(tweets, callback) {
var Tweet = db.model('tweet');
console.log('Saving ' + tweets.length + ' tweets');
return Tweet.create(tweets, callback);
};
var parseHtml = function(html) {
var $ = cheerio.load(html);
var $tweets = $("#stream-items-id .tweet");
console.log('Retrieved ' + $tweets.length + ' tweets');
var tweets = _.map($tweets, function(element) {
var $tweet = $(element);
// Retrieving the id
var id = $tweet.attr('data-tweet-id');
// Retrieving the text
var text = $tweet.find('.tweet-text').text();
// Retrieving the account
var $account = $tweet.find('.account-group');
var userId = $account.attr('data-user-id');
// Retrieving the account
var $timestamp = $tweet.find('._timestamp');
var timestamp = $timestamp.attr('data-time');
// Retrieving the geolocalization
var $geo = $tweet.find('.ProfileTweet-geo');
var geo;
if ($geo.length !== 0) {
geo = $geo.attr('data-original-title') || $geo.attr('title') || $geo.find('.u-hiddenVisually').text();
}
return {
tweetId: id,
text: text,
userId: userId,
timestamp: timestamp,
location: geo
};
});
return tweets;
};
var queryWeb = function(url, callback) {
console.log('querying the web');
phantom.create(function(ph) {
console.log('Initalizing phantom');
ph.createPage(function(page) {
console.log('Creating the page');
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.open(url, function(status) {
console.log("Opened page? ", status);
var interval = setInterval(function() {
page.evaluate(function() {
window.document.body.scrollTop = window.document.body.scrollTop + 10000;
var count = $("#stream-items-id .tweet").length;
console.log(count);
var endTag = $('.stream-end');
var end = false;
if (endTag) {
end = (endTag.css('display') !== 'none');
}
var html = document.body.innerHTML;
return {
count: count,
html: html,
end: end
};
}, function(result) {
if (result.end) {
console.log('Finished scrolling');
clearInterval(interval);
console.log('Exiting from phantom');
ph.exit();
var tweets = parseHtml(result.html);
return callback(null, tweets);
} else {
console.log('Need to go on');
}
});
}, 1500); // Number of milliseconds to wait between scrolls
});
});
});
};
var startScraping = function(dates, callback) {
var url = 'https://twitter.com/search?';
var query = {
f: 'realtime',
q: 'Aleksandr Antonenko since:' + dates.since + ' until:' + dates.until,
src: 'sprv'
};
url = url + querystring.stringify(query);
console.log('Scraping the url');
console.log(url);
var steps = [_.partial(queryWeb, url), saveTweets];
return async.waterfall(steps, function(err) {
if (err) console.log(err);
console.log('done single iteration');
return updateStatus(callback);
});
};
var updateStatus = function(callback) {
idx++;
return fs.writeFile('status.idx', idx, callback);
};
console.log('Connecting to the database');
mongoose.connect(configuration.db);
var db = mongoose.connection;
db.once('open', function() {
var Tweet = require('./models/tweet.js');
var dates = require('./dates3.json');
dates.splice(0, idx);
async.eachSeries(dates, startScraping, function() {
console.log('done all');
});
});