/
twitter.js
78 lines (62 loc) 路 2.06 KB
/
twitter.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
var Twitter = require('twitter')
var fs = require('fs')
var say = require('say')
const tweet_file = "tweets.txt"
var client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
bearer_token: process.env.TWITTER_BEARER_TOKEN
})
var params = {screen_name: 'realDonaldTrump', count: '101'}
client.get('statuses/user_timeline', params, function(error, tweets, response) {
if (!error) {
for (var i in tweets) {
fs.appendFile(tweet_file, replace_word(tweets[i].text.toLowerCase()) + '\n', function(error) {})
}
} else {
console.log(error)
}
})
// Word Replacement
function replace_word(string) {
string = replace_links(string)
string = replace_mentions(string)
string = append_haha(string)
string = replace_amp(string)
string = replace_bracets(string)
string = replace_abe_bulbasaur(string)
string = replace_administration(string)
string = replace_democrats_charmander(string)
return string
}
function replace_links(string) {
return string.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
}
function replace_mentions(string) {
return string.replace(/@([a-z\d_]+)/ig, 'unicorn')
}
function replace_amp(string) {
return string.replace(/(?:&)/ig, 'and')
}
function replace_bracets(string) {
return string.replace(/[\(\)']+/g,'')
}
function replace_abe_bulbasaur(string) {
return string.replace(/(?:abe)/ig, 'bulbasaur')
}
function replace_administration(string) {
return string.replace(/(?:administration)/ig, 'pokiedecks')
}
function replace_democrats_charmander(string) {
return string.replace(/(?:democrats)/ig, 'charmander')
}
function append_haha(string) {
var haha = ' wall wall wall wall wall wall wall'
var search = 'wall'
console.log(string.indexOf(search))
var end_index = string.indexOf(search) + search.length
if (end_index === -1 || end_index === search.length - 1) {
return string
}
return [string.slice(0, end_index), haha, string.slice(end_index)].join('');
}