-
Notifications
You must be signed in to change notification settings - Fork 9
/
liri.js
227 lines (208 loc) · 7.48 KB
/
liri.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
//Liri takes the following arguments
// * my-tweets
// * spotify-this-song
// * movie-this
// * do-what-it-says
//these add other programs to this one
require("dotenv").config();
let dataKeys = require("./keys.js");
let fs = require('fs'); //file system
let twitter = require('twitter');
let Spotify = require('node-spotify-api');
let request = require('request');
var inquirer = require('inquirer');
let space = "\n" + "\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0";
let header = "================= Extraordinary Liri found this ...==================";
// Function that writes all the data from output to the logfile
function writeToLog(data) {
fs.appendFile("log.txt", '\r\n\r\n', function(err) {
if (err) {
return console.log(err);
}
});
fs.appendFile("log.txt", (data), function(err) {
if (err) {
return console.log(err);
}
console.log(space + "log.txt was updated!");
});
}
function catName(name) {
if (!name) {
name = "Tigger";
}
console.log("My cat's name is " + name);
}
// =================================================================
// Spotify function, Spotify api
function getMeSpotify(songName) {
let spotify = new Spotify(dataKeys.spotify);
// If there is no song name, set the song to Blink 182's What's my age again
if (!songName) {
songName = "What's my age again";
}
spotify.search({ type: 'track', query: songName }, function(err, data) {
if (err) {
console.log('Error occurred: ' + err);
return;
} else {
output =
"================= LIRI FOUND THIS FOR YOU...==================" +
space + "Song Name: " + "'" + songName.toUpperCase() + "'" +
space + "Album Name: " + data.tracks.items[0].album.name +
space + "Artist Name: " + data.tracks.items[0].album.artists[0].name +
space + "URL: " + data.tracks.items[0].album.external_urls.spotify;
console.log(output);
writeToLog(output);
}
});
}
// TODO: Need to pass in year, begin date and end date
function getNEO() {
// Placeholder variables until I pass in the dates
var startDate = "1999-12-03";
var endDate = "1999-12-10";
// In case that no date is passed through (API defaults to end date + 7 days from start date)
if (startDate === undefined) {
startDate = "1996-12-01";
}
var nasa_key = process.env.NASA_KEY;
// URL for the API
var urlHit = "https://api.nasa.gov/neo/rest/v1/feed?start_date=" + startDate + "&end_date=" + endDate + "&api_key=" + nasa_key;
request(urlHit, function(error, response, body) {
if (error) {
console.log(error);
} else if (!error && response.statusCode === 200) {
var jsonData = JSON.parse(body);
var neow1 = jsonData.near_earth_objects[endDate];
output =
space + header +
space + "----------------- Data of Near Earth Objects ------------------" +
space + "Name: " + neow1[1].name +
space + "ID: " + neow1[1].id +
space + "Hazardous: " + neow1[1].is_potentially_hazardous_asteroid +
space + "Diamater min (Km): " + neow1[1].estimated_diameter.kilometers.estimated_diameter_min +
space + "Diamater max (Km): " + neow1[1].estimated_diameter.kilometers.estimated_diameter_max;
console.log(output);
writeToLog(output);
} else {
console.log(response.statusCode);
}
});
}
let getTweets = function() {
let client = new twitter(dataKeys.twitter);
let params = { screen_name: 'ednas', count: 10 };
client.get('statuses/user_timeline', params, function(err, tweets, res) {
let data = []; //empty array to hold data
data.push(space + header);
if (err) {
console.log('Error occured: ' + err);
return;
} else {
for (let i = 0; i < tweets.length; i++) {
data.push(
space + 'Created at: ' + tweets[i].created_at +
space + 'Tweet: ' + tweets[i].text + "\n"
);
}
let newData = data.join('');
console.log(newData);
writeToLog(newData);
}
});
};
let getMeMovie = function(movieName) {
if (!movieName) {
movieName = "Mr Nobody";
}
//Get your OMDb API key creds here http://www.omdbapi.com/apikey.aspx
// t = movietitle, y = year, plot is short, then the API key
const omdb = process.env.OMDB_KEY
let urlHit = "http://www.omdbapi.com/?t=" + movieName + "&y=&plot=short&apikey=" + omdb;
request(urlHit, function(err, res, body) {
if (err) {
console.log('Error occurred: ' + err);
return;
} else {
let jsonData = JSON.parse(body);
output = space + header +
space + 'Title: ' + jsonData.Title +
space + 'Year: ' + jsonData.Year +
space + 'Rated: ' + jsonData.Rated +
space + 'IMDB Rating: ' + jsonData.imdbRating +
space + 'Country: ' + jsonData.Country +
space + 'Language: ' + jsonData.Language +
space + 'Plot: ' + jsonData.Plot +
space + 'Actors: ' + jsonData.Actors +
space + 'Tomato Rating: ' + jsonData.Ratings[1].Value +
space + 'IMDb Rating: ' + jsonData.imdbRating + "\n";
console.log(output);
writeToLog(output);
}
});
};
function doWhatItSays() {
// Reads the random text file and passes it to the spotify function
fs.readFile("random.txt", "utf8", function(error, data) {
getMeSpotify(data);
});
}
let questions = [{
type: 'list',
name: 'programs',
message: 'What would you like to do?',
choices: ['Spotify', 'Movie', 'Get Latest Tweets', 'Name my cat', 'NASA info', 'Do what it says']
},
{
type: 'input',
name: 'movieChoice',
message: 'What\'s the name of the movie you would like?',
when: function(answers) {
return answers.programs == 'Movie';
}
},
{
type: 'input',
name: 'songChoice',
message: 'What\'s the name of the song you would like?',
when: function(answers) {
return answers.programs == 'Spotify';
}
},
{
type: 'input',
name: 'catName',
message: 'What would you name your cat?',
when: function(answers) {
return answers.programs == 'Name my cat';
}
}
];
inquirer
.prompt(questions)
.then(answers => {
// Depending on which program the user chose to run it will do the function for that program
switch (answers.programs) {
case 'Get Latest Tweets':
getTweets();
break;
case 'Spotify':
getMeSpotify(answers.songChoice);
break;
case 'Movie':
getMeMovie(answers.movieChoice);
break;
case 'Name my cat':
catName(answers.catName);
break;
case 'Do what it says':
doWhatItSays();
break;
case 'NASA info':
getNEO();
break;
default:
console.log('LIRI doesn\'t know that');
}
});