-
Notifications
You must be signed in to change notification settings - Fork 0
/
whatyearisit.js
110 lines (99 loc) · 3.47 KB
/
whatyearisit.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
var Twit = require('twit');
var T = new Twit(require('./config.js'));
var fs = require('fs');
var mkdirp = require('mkdirp');
function tweetContainsKeywords(tweet) {
var keywords = ["still", "yet", "why"];
var normalizedText = tweet.text.toLowerCase();
var found = false;
keywords.forEach(function (word) {
if( normalizedText.indexOf(word + " ") !== -1 ) {
found = true;
}
})
return found;
}
function isReply(t) {
return (
t.in_reply_to_status_id //twitter recognizes this as a reply
|| t.text.charAt(0) === '@' //this is a manual reply?
|| t.text.charAt(1) === '@' //this is a .@ which isn't technically a reply but I still don't want
);
}
function isRetweet(t) {
return (
t.retweeted_status //twitter recognizes this as a retweet
|| t.retweeted //this account has already retweeted this tweet
|| t.text.substring(0, 2).toLowerCase() === "rt" //this tweet starts with RT
);
}
function run() {
var theYear = new Date().getFullYear();
var theMonth = new Date().getMonth();
var query = '%22it%27s%20' + theYear + "%22";
var logObject = {};
T.get('search/tweets', { q: query, count: 100, lang: 'en' }, function(err, data, response) {
if(err) {
console.log(err);
} else {
var theTweet = null;
for (var i = data.statuses.length - 1; i >= 0; i--) {
var t = data.statuses[i];
//filter out retweets, replies, and tweets that don't contain special keywords
if(isRetweet(t) || isReply(t) || !tweetContainsKeywords(t)) {
continue;
} else {
theTweet = t;
break;
}
}
//didn't find anything, stop here
if(!theTweet) { return; }
//create an object that will be added to the logs
logObject.tweetID = theTweet.id_str;
logObject.text = theTweet.text;
logObject.user = theTweet.user.screen_name;
//retweet the tweet
T.post('statuses/retweet/:id', { id: theTweet.id_str }, function (err, data, response) {
if(err) {
console.log("error tweeting:" + err);
} else {
console.log("successfully tweeted!");
console.log("tweet text::");
console.log(theTweet.text);
console.log("---------");
}
})
//log it to the filesystem
var filepath = __dirname + '/WhatYearIsIt-LOGS/'+theYear+'/';
var filename = theMonth+'.json';
fs.readFile(filepath + filename, function(err, data){
if(err) {
//file or directory doesn't exist
mkdirp(filepath, function(err) {
if(err) console.log("error making file:" + err);
var writeArray = JSON.stringify([logObject]);
fs.writeFile(filepath + filename, writeArray, function (err) {
if(err) console.log(err);
});
})
} else {
var readArray = JSON.parse(data);
readArray.push(logObject);
fs.writeFile(filepath + filename, JSON.stringify(readArray), function (err) {
if(err) console.log(err);
});
}
});
}
})
}
module.exports = {
run: run
}
//tests
// console.log( tweetContainsKeywords( {text: "It's 2015 why is racism still a thing"} ) );
// console.log( isReply( {text: "@robertvinluan hey"} ));
// console.log( isReply( {text: ".@robertvinluan hey"} ));
// console.log( isRetweet( {text: "RT @robertvinluan hey"} ));
// run();