-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.js
68 lines (66 loc) · 2.87 KB
/
agent.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
const {getTwitterCounts, getTwitterUserFollowers} = require("./app/utils/utils");
module.exports = agent => {
setInterval(async () => {
let appData = await agent.mysql.get('app');
let chainData = await agent.mysql.get('chainData');
let allTwitters = await appData.select('twitter_counts');
let requestIDs = [];
let count = 0;
for (const t of allTwitters) {
requestIDs.push(t.tid);
count++;
if (count === 50) { // api required up to 100 comma-separated Tweet IDs.
try {
let results = await getTwitterCounts(requestIDs);
for (const tid of results.keys()) {
await appData.update('twitter_counts', results.get(tid), {where: {tid: tid}});
}
} catch (e) {
agent.logger.error(`update twitter_counts failed, err ${e}`)
}
requestIDs = [];
count = 0;
}
}
try {
let results = await getTwitterCounts(requestIDs);
for (const tid of results.keys()) {
await appData.update('twitter_counts', results.get(tid), {where: {tid: tid}});
}
} catch (e) {
agent.logger.error(`update failed, err ${e}`)
}
let allDaoTwitters = await chainData.query(`select twitter
from collection
where dao_create_block > 0
and !ISNULL(twitter)
and twitter != ''`);
let requestUsernames = [];
count = 0;
let totalAudiences = 0;
for (const userName of allDaoTwitters) {
const atIndex = userName.twitter.indexOf("@");
requestUsernames.push(userName.twitter.substring(atIndex + 1));
count++;
if (count === 50) { // api required up to 100 comma-separated Tweet Usernames.
try {
totalAudiences += await getTwitterUserFollowers(requestUsernames);
requestUsernames = [];
count = 0;
} catch (e) {
agent.logger.error(`get total audience failed, err ${e}`)
}
}
}
let totalDaos = allDaoTwitters.length;
let totalProposals = await appData.query(`select count(*) as total_proposals
from proposal`);
totalProposals = totalProposals[0].total_proposals;
await appData.insert('statistics', {
time: Date.now(),
total_daos: totalDaos,
total_proposals: totalProposals,
total_audiences: totalAudiences
});
}, 5 * 60 * 1000);
};