-
Notifications
You must be signed in to change notification settings - Fork 0
/
distill.js
57 lines (54 loc) · 1.72 KB
/
distill.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
var moment = require("moment");
var locationBlacklist = [
"47.603604,-122.337055"
];
var distill = function(tweet) {
if (!tweet.user) return null;
var timestamp = moment(tweet.created_at, "ddd MMM DD HH:mm:ss ZZ YYYY").toDate();
var parsed = {
name: tweet.user.name,
handle: tweet.user.screen_name,
timestamp: timestamp.getTime(),
date: timestamp,
tweet: tweet.extended_tweet ? tweet.extended_tweet.full_text : tweet.full_text ? tweet.full_text : tweet.text,
id: tweet.id_str,
avatar: tweet.user.profile_image_url,
latlng: null
}
var entities = tweet.entities;
var extended = tweet.extended_entities || {};
if (entities.hashtags) {
parsed.tags = entities.hashtags.map(function(t) { return t.text });
}
if (entities.urls) {
parsed.urls = entities.urls.map(function(entity) { return { short: entity.url, long: entity.expanded_url }});
}
if (extended.media || entities.media) {
parsed.media = (extended.media || entities.media).map(function(e) {
parsed.tweet = parsed.tweet.replace(e.url, "");
return {
text: e.url,
url: e.media_url,
video: e.video_info ?
e.video_info.variants
.filter(v => v.content_type == "video/mp4")
.sort((a, b) => b.bitrate - a.bitrate)
.shift().url
: false
}
});
}
if (entities.mentions) {
parsed.mentions = entities.mentions.map(function(e) {
return e.screen_name
});
}
if (tweet.coordinates && tweet.coordinates.type == "Point") {
parsed.latlng = tweet.coordinates.coordinates.reverse();
if (locationBlacklist.indexOf(parsed.latlng.join(",")) > -1) {
parsed.latlng = null;
}
}
return parsed;
};
module.exports = distill;