-
Notifications
You must be signed in to change notification settings - Fork 6
/
parsePage.js
77 lines (53 loc) · 1.72 KB
/
parsePage.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
var fs = require('fs');
var cheerio = require('cheerio');
var _ = require('underscore');
var mongoose = require('mongoose');
var configuration = require('./config/config.json');
var parseHtml = function(html) {
var $ = cheerio.load(html);
var $tweets = $("#stream-items-id .tweet");
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 saveTweets = function(tweets, callback) {
var Tweet = db.model('tweet');
console.log('Saving ' + tweets.length + ' tweets');
return Tweet.create(tweets, 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 page = fs.readFileSync('turandot20140501.html').toString();
var tweets = parseHtml(page);
saveTweets(tweets, function(err) {
if (err) return console.log(err);
return console.log('done');
});
});