-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (109 loc) · 4.47 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
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
'use strict';
const imgurConfig = require('./config/imgur');
const CronJob = require('cron').CronJob;
const Scrapper = require('./src/Scrapers/Scraper');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const fs = require('fs');
const imgur = require('imgur');
imgur.setCredentials(imgurConfig.login, imgurConfig.password, imgurConfig.client_id);
const sequelize = new Sequelize('twitter', 'root', 'root', {
dialect: 'mysql',
operatorsAliases: false,
logging: false
});
const Account = sequelize.import('./src/Models/Account');
const Tweet = sequelize.import('./src/Models/Tweet');
const RemovedTweet = sequelize.import('./src/Models/RemovedTweet');
Account.hasMany(Tweet, {foreignKey: 'account_id'});
const job = new CronJob('*/1 * * * *', async () => {
try {
const accounts = await Account.findAll({
where: {
next_update: {
[Op.or]: {
[Op.lt]: new Date(),
[Op.eq]: null
}
}
},
include: [Tweet]
});
if (accounts.length < 1) {
console.log('Nothing to scrape');
return false;
}
for (let key in accounts) {
const account = accounts[key];
Account.update({
next_update: new Date(new Date().getTime() + 1000 * 60 * account.update_time),
}, {
where: {
id: account.id
}
});
}
const scrapper = new Scrapper(accounts);
const result = await scrapper.get();
for (let key in result) {
const account_id = result[key].account_id;
// console.log(result[key].result.removed_items); // console.log(tweets with status 404 (removed));
for (let tweet_key in result[key].result.removed_items) { // save removed tweets
const tweet = result[key].result.removed_items[tweet_key];
const screen = __dirname + '/screenshots/' + tweet.tweet_id + '.jpeg';
imgur.uploadFile(screen)
.then(function (json) {
fs.unlink(screen, (err) => {
if (!err) {
RemovedTweet.findOrCreate({
where: {tweet_id: tweet.tweet_id},
defaults: {
content: tweet.content,
url: json.data.link,
account_id: tweet.account_id,
published_at: new Date(tweet.published_at)
}
});
Tweet.destroy({
where: {
id: tweet.id
}
});
console.log('Founded removed tweet. Id - ' + tweet.tweet_id);
}
});
})
.catch(function (err) {
console.error(err.message);
});
}
for (let tweet_key in result[key].result.items) { // save new tweets
const tweet = result[key].result.items[tweet_key];
Tweet.findOrCreate({
where: {tweet_id: tweet.tweet_id},
defaults: {
content: tweet.tweet_content,
url: tweet.tweet_url,
account_id: account_id,
published_at: new Date(tweet.tweet_published)
}
});
}
for (let tweet_key in result[key].result.old_items) { // remove old tweets
const tweet = result[key].result.old_items[tweet_key];
const screen = __dirname + '/screenshots/' + tweet.tweet_id + '.jpeg';
fs.unlink(screen, (err) => {
if (!err) console.log("Screen " + tweet.tweet_id + ".jpeg deleted");
}); // remove file of screen
Tweet.destroy({
where: {
id: tweet.id
}
});
}
}
} catch (e) {
console.log(e.message);
}
});
job.start();