-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteps.js
179 lines (159 loc) · 6.55 KB
/
steps.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
174
175
176
177
178
179
// imports
var persistenceService = require('./persistence.js');
var twitterService = require('./twitter.js');
// static class definition
var steps = {
errorHandler: function (err) {
console.error(err);
},
retrieveTweetsMaxAndSinceId: Promise.all([persistenceService.getTweetsMaxId(), persistenceService.getTweetsSinceId()]),
calcTweetsMaxAndSinceId: function (args) {
var _max_id = args[0];
var _since_id = args[1];
if (_max_id) {
max_id = _max_id;
since_id = undefined;
} else {
max_id = undefined;
since_id = _since_id;
}
console.log('tweets', max_id, since_id);
return [max_id, since_id];
},
retrieveTweets: function (args) {
var max_id = args[0];
var since_id = args[1];
return twitterService.getTweets('#pixel_dailies @Pixel_Dailies ', max_id, since_id);
},
filterSearchResult: function (data) {
return new Promise(function (res) {
var result = {};
var parsedMaxId = /.*?max_id=(.*?)&.*?/.exec(data.search_metadata.next_results);
var parsedSinceId = /.*?since_id=(.*?)&.*?/.exec(data.search_metadata.refresh_url);
result.meta = {
max_id: (parsedMaxId && parsedMaxId.length > 1 ? parsedMaxId[1] : undefined),
since_id: (parsedSinceId && parsedSinceId.length > 1 ? parsedSinceId[1] : undefined)
};
result.topics = data.statuses.filter(function (v) {
// ignore RTs
if (v.text.substr(0, 3) === "RT ") {
return false;
}
// only tweets by pixel_dailies can contain topics
return v.user.screen_name.toLowerCase() == 'pixel_dailies';
})/*.map(function (v) {
return {
text: v.text,
creation_date: v.created_at,
hashtags: (v.entities.hashtags ? v.entities.hashtags.map(function (v) { return v.text }) : []),
mentions: (v.entities.user_mentions ? v.entities.user_mentions.map(function (v) { return v.screen_name }) : []),
medias: (v.entities.media ? v.entities.media.map(function (v) { return { url: v.media_url, type: v.type } }) : []),
id: v.id,
user_id: v.user.screen_name,
user_name: v.user.name
};
})*/;
result.tweets = data.statuses.filter(function (v) {
// ignore RTs
if (v.text.substr(0, 3) === "RT ") {
return false;
}
// remove all without media
return !(!v.entities.media || v.entities.media.length === 0);
}).map(function (v) {
return {
text: v.text,
creation_date: v.created_at,
hashtags: (v.entities.hashtags ? v.entities.hashtags.map(function (v) {
return v.text
}) : []),
mentions: (v.entities.user_mentions ? v.entities.user_mentions.map(function (v) {
return v.screen_name
}) : []),
medias: (v.entities.media ? v.entities.media.map(function (v) {
return {url: v.media_url, type: v.type}
}) : []),
id: v.id,
user_id: v.user.screen_name,
user_name: v.user.name
};
});
res(result);
});
},
logTweetResult: function (tweets) {
console.log("Got", tweets.tweets.length, "new", "tweets.");
return tweets;
},
retrieveTopicsMaxAndSinceId: Promise.all([persistenceService.getTopicsMaxId(), persistenceService.getTopicsSinceId()]),
calcTopicMaxAndSinceId: function (args) {
var _max_id = args[0];
var _since_id = args[1];
if (_max_id) {
max_id = _max_id;
since_id = undefined;
} else {
max_id = undefined;
since_id = _since_id;
}
console.log('topics', max_id, since_id);
return [max_id, since_id];
},
retrieveTopics: function (args) {
var max_id = args[0];
var since_id = args[1];
return twitterService.getTweets('from:Pixel_Dailies ', max_id, since_id)
},
filterTopics: function (data) {
return new Promise(function (res) {
var result = {};
var parsedMaxId = /.*?max_id=(.*?)&.*?/.exec(data.search_metadata.next_results);
var parsedSinceId = /.*?since_id=(.*?)&.*?/.exec(data.search_metadata.refresh_url);
result.meta = {
max_id: (parsedMaxId && parsedMaxId.length > 1 ? parsedMaxId[1] : undefined),
since_id: (parsedSinceId && parsedSinceId.length > 1 ? parsedSinceId[1] : undefined)
};
result.topics = data.statuses.filter(function (v) {
// ignore RTs
if (v.text.substr(0, 3) === "RT ") {
return false;
}
// only tweets by pixel_dailies can contain topics
return v.user.screen_name.toLowerCase() == 'pixel_dailies';
}).map(function (v) {
return {
text: v.text,
creation_date: v.created_at,
hashtags: (v.entities.hashtags ? v.entities.hashtags.map(function (v) {
return v.text
}) : []),
id: v.id
};
});
res(result);
});
},
logTopicResult: function (topics) {
console.log("Got", topics.topics.length, "new", "topics.");
return topics;
},
persistTweetsAndTopics: function (args) {
var tweets = args[0];
var topics = args[1];
return Promise.all([
persistenceService.setTweetsMaxId(tweets.meta.max_id),
persistenceService.setTweetsSinceId(tweets.meta.since_id),
persistenceService.setTopicsMaxId(topics.meta.max_id),
persistenceService.setTopicsSinceId(topics.meta.since_id),
persistenceService.getTweets().then((oldTweets) => persistenceService.setTweets(oldTweets.concat(tweets.tweets))),
persistenceService.getTopics().then((oldTopics) => persistenceService.setTopics(oldTopics.concat(topics.topics)))
]);
},
logProcessResults: function (args) {
var tweets = args[4];
var topics = args[5];
console.log("Collection has", tweets.length, "tweets and", topics.length, "topics at all.");
}
};
// export
module.exports = steps;