-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhal.js
297 lines (256 loc) · 7.41 KB
/
hal.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Author: Alex P
* Project Name: liri-node-app hal.js
* Version: 1
* Date: 09.18.17
* URL: github.com/itsokayitsofficial/liri-node-app
*/
// Variables - require
var keys = require("./keys.js");
var fs = require("fs");
var request = require("request");
var twitter = require("twitter");
var spotify = require("node-spotify-api");
var chalk = require("chalk");
// Variables - global
var tweetKey = keys.twitterKeys;
var spotKey = keys.spotifyKeys;
var input = process.argv;
var calc = parseFloat;
var op = input[2];
var mod = input[3];
var hal = chalk.bold('Hal: ');
// Variable - Initilize Twitter
var twitter = new twitter({
consumer_key: tweetKey.consumer_key,
consumer_secret: tweetKey.consumer_secret,
access_token_key: tweetKey.access_token_key,
access_token_secret: tweetKey.access_token_secret
});
// Variable - Initilize Spotify
var spotify = new spotify({
id: spotKey.client_id,
secret: spotKey.client_secret
});
// Run - logData()
// turn on logData
logData('', 'white', true);
// tell logData what to log
logData('- - - - - - - - - - - - - - - - - - - - - - - - -- - - ', '', true);
logData('Command: node hal.js ' + op + ' ' + mod, 'white', true);
logData('');
// Switch - to handle input
switch (op) {
case 'tweets':
getTweets();
break;
case 'song':
getSong();
break;
case 'movie':
getMovie();
break;
case 'hal':
halChoice();
break;
case '-info':
halInfo();
break;
case '-clear':
clearLogs();
break;
case '-print':
printLogs();
break;
default:
logData(hal + 'Input not understood. Type "node hal.js -info" for more information.', 'red');
};
// Function - Get tweets from Twitter
function getTweets() {
// Variable - request.params()
var params = {
screen_name: 'okayitsofficial'
};
// Get - Twitter API
twitter.get('statuses/user_timeline', params, function (error, tweets, repsonse) {
// If - no errors
if (!error) {
logData(hal + 'Here are your most recent tweets.', 'red');
logData('');
for (var i = 0; i < tweets.length; i++) {
logData('Tweeted on ' + tweets[i].created_at, 'blue');
logData(' "' + tweets[i].text + '"', 'white');
logData('');
}
} else {
console.log(error);
}
});
};
// Function - Get Spotify song info
function getSong() {
// Variable - local
var songName = '';
var query = '';
// For - Capture complete title
for (var i = 3; i < input.length; i++) {
// Build a string of song title
var songName = input[i];
}
// If - Checks if song has been entechalk.red
if (songName === '') {
// Variable - searches for What's My Age Again
var params = {
type: 'track',
query: "the sign"
};
// Log
logData(hal + 'I see you could not choose a song to search for.', 'red');
logData(hal + "Might I suggest some of Scandinavia's finest?", 'red');
}
// Else - If query is populated
else {
// Variable - applies input to search
var params = {
type: 'track',
query: songName
};
// Log
logData(hal + 'Searching...', 'red');
}
// Search - Spotify API
spotify.search(params, function (error, data) {
// Variables - local
var output = data.tracks.items;
// If - no errors
if (!error) {
// Log - hal
logData(hal + 'Here are some songs that match your search keyword.', 'red');
// For - captures output
for (var i = 0; i < output.length; i++) {
// Return - returns feedback in place of null value
var name = output[i].artists;
var artist = [];
// For - captures mutliple artists into array
for (var a = 0; a < name.length; a++) {
var multi = name[a].name;
artist.push(' ' + multi);
}
// Log - data
logData('');
logData((i + 1 + ') ') + output[i].name, 'white');
logData(' Artist(s):' + artist);
logData(' Album: ' + output[i].album.name);
logData(' Preview: ' + output[i].preview_url)
}
// Else - if errors
} else {
console.log(error);
}
});
};
// Function - Get OMDB movie info
function getMovie() {
// Variable - local
var movieName = '';
// For - Capture complete title
for (var i = 3; i < input.length; i++) {
// Build a string of movie title
var movieName = input[i];
}
// If - Checks if movie has been entechalk.red
if (movieName === '') {
// Variable - searchs for Mr. Nobody if not
movieName = 'mr nobody';
// Log
logData(hal + 'I see you could not choose a movie to search for.', 'red');
logData(hal + 'Might I suggest an early 2010s bomb starring a post-op Courtney Cox?', 'red');
}
// Else - If movieName is populated
else {
// Log
logData(hal + 'Searching...', 'red');
}
// Variable - API ref
var queryUrl = "http://www.omdbapi.com/?t=" + movieName + "&apikey=40e9cece";
// Request API - Get movie info
request(queryUrl, function (error, response, body) {
var omdb = JSON.parse(body);
// If - no errors
if (!error && response.statusCode === 200) {
if (omdb.Response === "False") {
logData(hal + 'I could not find the movie you searched for. Please try another title.', 'red');
} else {
logData(hal + 'Movie found!', 'red');
logData('');
logData(omdb.Title + ' (' + omdb.Year + ')', 'white');
logData(omdb.Actors, 'white');
logData('----------------');
logData('IMDB: ' + omdb.imdbRating + ' - Rotten Tomatoes: ' + omdb.Ratings[1].Value);
logData('Country(s): ' + omdb.Country + ' - Language(s): ' + omdb.Language);
logData('');
logData(omdb.Plot);
logData('Rated: ' + omdb.Rated);
logData('');
}
} else {
console.log(error);
}
});
};
// Function - Random choice from random.txt
function halChoice() {
fs.readFile('random.txt', 'utf8', function (error, data) {
if (!error) {
var array = data.split(",");
getSong(array[2]);
} else {
console.log(error)
}
});
};
// Function - Log input
function logData(log, color, halt) {
// File - logoData logs...data
fs.appendFileSync('log.txt', log + '\r\n');
// If - on/off for logData
if (!halt) {
if (color !== undefined) {
console.log(chalk[color](log));
} else {
console.log(log);
}
};
};
// Function - Settings for keywords
function halInfo() {
console.log('');
console.log('To run ' + chalk.red.bold('Hal'));
console.log('----------------');
console.log('Type in Terminal: node hal.js <keyword>');
console.log('');
console.log('Keywords');
console.log('----------------');
console.log('tweets shows last 20 tweets and when they were created');
console.log("song '<song name>' shows song name, artist(s), album, and preview URL");
console.log("movie '<movie name>' shows movie title, year made,, main actors, IMDB and Rotten Tomatoes rating, country and language, plot, and rating");
console.log("hal Hal's choice");
console.log('');
};
// Function - Random choice from random.txt
function printLogs() {
fs.readFile('log.txt', 'utf8', function (error, data) {
if (!error) {
console.log(data);
} else {
console.log(error)
}
});
};
// Function - Clear log file
function clearLogs() {
// File - clear logData from log.txt
fs.truncate('log.txt', 0, function () {
console.log(chalk.red(hal + 'Logs have been scrubbed.'));
});
};