-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
181 lines (171 loc) · 6.43 KB
/
index.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
180
181
/* jshint node: true, esversion: 6 */
'use strict';
const request = require('request');
const Twit = require('twit');
const config = require('./config');
const T = new Twit(config.twitConfig);
const http = require('http');
const P = config.pnetConfig;
let count = 0;
http.createServer().listen(3005);
var stream = T.stream('user');
//1st call, listens for activity on BrianAndRobot account
stream.on('tweet', function(tweet) {
//if hashtag exists, create an object to store data
//TODO: move object into seperate class
const hashtag = tweet.entities.hashtags[0];
console.log('tweet text:', tweet.text);
if (hashtag) {
const userObject = {
user: '',
hashtag: '',
dates: {
first: {
season: '',
year: ''
},
second: {
year: '',
month: ''
}
}
};
//assign the name of the person who tweeted to the userObject & the hashtag
userObject.user = tweet.user.screen_name;
userObject.hashtag = [hashtag.text];
//if there's a hashtag send the object to prepareDate
prepareDate(userObject);
} else if (!hashtag) {
console.log(`No Hashtag for ${tweet.user.screen_name}`);
}
});
//Mock the user object for debugging and pass to prepareDate()
// const userObject = {
// user: 'obiwan2',
// hashtag: 'Summer94',
// dates: {
// first: {
// season: '',
// year: ''
// },
// second: {
// year: '',
// month: ''
// }
// }
// };
// prepareDate(userObject);
// 2nd call seperates the season and year from the hashtag
function prepareDate(userObject) {
const dates = userObject.hashtag.toString();
const index = dates.search(/\d/);
userObject.dates.first.season = dates.substr(0, index);
userObject.dates.first.year = dates.substr(index);
console.log(`Step 1: Prepping date ${userObject.dates.first.season} & ${userObject.dates.first.year}`);
if (isNaN(userObject.dates.first.year)) {
console.log("Not a number.");
return;
}
buildDate(userObject);
}
// 3rd call converts season into a random month and formats year properly for pnet API
function buildDate(userObject) {
count++;
const season = userObject.dates.first.season;
const year = userObject.dates.first.year;
let months = [];
let yr;
//set months to an array of three numbers based on season input
switch (season.toLowerCase()) {
case "fall":
case "autumn":
months = ['09', '10', '11'];
break;
case "winter":
months = ['12', '01', '02'];
break;
case "spring":
months = ['03', '04', '05'];
break;
case "summer":
months = ['06', '07', '08'];
}
// Prefix two digit year with 19 or 20
if (year > 20 && year < 100) {
yr = '19' + year;
} else {
yr = '20' + year;
}
// Pick a random month within selected season
const month = months[Math.floor(Math.random() * months.length)];
userObject.dates.second.year = yr;
userObject.dates.second.month = month;
console.log(`Step 2: Building query with ${userObject.dates.second.month} & ${userObject.dates.second.year}`);
composeTweet(userObject);
}
// 4th call queries pnet API and composes the tweet.
function composeTweet(userObject) {
const year = userObject.dates.second.year;
const month = userObject.dates.second.month;
const season = userObject.dates.first.season;
// Pnet API returns all shows from specified month and year
//TODO: rewrite for phish.net API v 3
request.get({
url: `https://api.phish.net/api.js?api=2.0&method=pnet.shows.query&format=json&year=${year}&month=${month}&apikey=${P.apikey}`
},
function(err, response, data) {
if (err) {
console.log(err);
}
const jsonData = JSON.parse(data);
//if pnet API reports no shows found, run buildDate again
if (data === '{"success":0,"reason":"No Shows Found"}') {
console.log(`No shows found with ${month} & ${year}, trying again...`);
// We only want to do this 10 times, then quit
if (count < 10) {
console.log(`Count: ${count}`);
buildDate(userObject);
} else if (count >= 10) {
console.log(`Count is ${count}, terminating.`);
// TODO: probably should return some info back to the user here.
return;
}
} else {
// check to see if show exists on phishtracks.com
request.get({
url: `http://phishtracks.com/shows/${jsonData[0].showdate}`
},
function(err, response, data) {
if (err) {
console.log(err);
}
//if show is not found on phishtracks.com, run buildDate again
if (response.statusCode === 404) {
console.log("Phishtracks doesn't seem to have this show online. Getting a new show...");
buildDate(userObject);
} else {
//pick a random show out of pnet API results, get phishtracks.com link
const show = jsonData[Math.floor(Math.random() * jsonData.length)];
const phishTracksUrl = `http://phishtracks.com/shows/${show.showdate}`;
const message = `@${userObject.user} ${show.venue}, ${show.nicedate}: ${phishTracksUrl} #phish`;
console.log("Success! Sending tweet.");
console.log(`message: ${message}`);
sendTweet(message);
}
});
}
});
}
// 5th and final call, sends tweet
function sendTweet(message) {
T.post('statuses/update', { status: message }, function(err, reply) {
if (err) {
console.log('error:', err);
} else {
console.log('tweet:', reply.text);
// Set count back to 0 since tweet was successful
count = 0;
return;
}
});
}