-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliri.js
More file actions
209 lines (192 loc) · 7.03 KB
/
liri.js
File metadata and controls
209 lines (192 loc) · 7.03 KB
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
// reads the Keys from keys.js
var keysINeed = require('./keys.js');
// loads the dependencies needed
var Twitter = require('twitter');
var Spotify = require('node-spotify-api');
var request = require('request');
var fs = require('fs');
// retrieves user input data
function input(command, chosenInput) {
var printInput;
// checks for user input
if (chosenInput === undefined) {
if (command === 'spotify-this-song') {
// sets default if no song was chosen
chosenInput = '"The Sign" by Ace of Base';
} else if (command === 'movie-this') {
// sets default if no movie was chosen
chosenInput = 'Mr. Nobody';
}
}
// checks for command
switch (command) {
case 'my-tweets':
tweets();
break;
case 'spotify-this-song':
spotify(chosenInput);
break;
case 'movie-this':
imdb(chosenInput);
break;
case 'do-what-it-says':
random();
break;
}
// checks again for user input
if (chosenInput === undefined) {
// prints only user command
printInput = `${command}`
} else {
// prints both user command and input
printInput = `${command} ${chosenInput}`
}
// formats the printInput
printInput = `\n********************************************\n` +
`${printInput}` +
`\n********************************************\n`
// displays user input to terminal/bash and writes it to log.txt
saveData(printInput);
}
// for `node liri.js my-tweets`
// retrieves the latest 20 tweets and
// when they were created
function tweets() {
// creates a new instace of the Twitter object
var client = new Twitter(keysINeed.twitterKeys);
// sets the query parameters
var params = {
screen_name: 'escoding',
limit: 20
};
// gets the tweets as response
client.get('statuses/user_timeline', params, function(error, tweets, response) {
// checks for errors
if (!error) {
// gets each tweet
for (var i = 0; i < tweets.length; i++) {
var tweetText = tweets[i].text;
var tweetCreated = tweets[i].created_at;
// formats the data that is being displayed
var printTweets = `\n ********************************************\n` +
` Tweet: ${tweetText}\n` +
` Date: ${tweetCreated}\n` +
` ********************************************\n`
// prints each tweet to terminal and log.txt
saveData(printTweets);
}
} else {
return console.log(error);
}
});
}
// for `node liri.js spotify-this-song '<song name here>'`
// retrieves information about the song
function spotify(songTitle) {
// creates a new instace of the Spotify object
var spotify = new Spotify(keysINeed.spotifyKeys);
// sets query parameters
params = {
type: 'track',
query: songTitle,
limit: 1
}
// searches for the songs' info
spotify.search(params, function(err, data) {
// console.log(JSON.stringify(data, null, 2));
// checks for errors
if (err) {
return console.log('Error occurred: ' + err);
} else {
// gets data from the object
var dataTracksItem = data.tracks.items[0];
var name = dataTracksItem.album.artists[0].name;
var song = dataTracksItem.name;
var url = dataTracksItem.preview_url;
var album = dataTracksItem.album.name;
// formats the display
var printSong = `\n ********************************************\n` +
` Artist(s): ${name}\n` +
` The Song's name: ${song}\n` +
` Preview URL: ${url}\n` +
` Album's name: ${album}\n` +
` ********************************************\n`
// prints song to terminal and log.txt
saveData(printSong);
}
});
}
// for `node liri.js spotify-this-song '<movie name here>'`
// retrieves information about the movie
function imdb(movieTitle) {
// sets query url
var queryUrl = "http://www.omdbapi.com/?t=" + movieTitle + "&y=&plot=short&apikey=40e9cece";
// sends request to retrieve data of the movie
request(queryUrl, function(error, response, body) {
// parses data into JSON object
var dataObj = JSON.parse(body);
// If the request is successful
if (!error && response.statusCode === 200) {
// gets the movie data
var title = dataObj.Title;
var year = dataObj.Year;
var imdbRating = dataObj.imdbRating;
var rtRating;
// check for Rotten Tomatoes Ratings
if (dataObj.Ratings[1] === undefined) {
rtRating = "No Rotten Tomatoes Ratings available"
} else {
rtRating = dataObj.Ratings[1].Value;
}
var country = dataObj.Country;
var language = dataObj.Language;
var plot = dataObj.Plot;
var actors = dataObj.Actors;
// formats display
var printMovie = `\n ********************************************\n` +
` Movie Title: ${title}\n` +
` Release Year: ${year}\n` +
` IMDB Rating: ${imdbRating}\n` +
` Rotten Tomatoes Rating: ${rtRating}\n` +
` Country: ${country}\n` +
` Language: ${language}\n` +
` Plot: ${plot}\n` +
` Actors: ${actors}\n` +
` ********************************************\n`
// prints movie data
saveData(printMovie);
}
});
}
// for `node liri.js do-what-it-says`
function random() {
// reads content from random.txt
fs.readFile('random.txt', 'utf8', function(err, data) {
// checks for error
if (err) {
return console.log(err);
}
// splits content by comma. data is an array
var data = data.split(',')
// sends/sets values of data to input() function
input(data[0], data[1]);
})
}
// prints data to terminal/bash and writes data to log.txt
function saveData(log) {
// writes result data to log.txt
fs.appendFile("log.txt", log, function(err) {
// If an error was experienced
if (err) {
console.log(err);
}
// If no error is experienced
else {
// displays result data to terminal
console.log(log);
}
})
}
// input function is called,
// passes the two argv arguments entered by the user
input(process.argv[2], process.argv[3]);