-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathliri.js
164 lines (137 loc) · 5.42 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
require("dotenv").config();
//vars
var keys = require("./keys.js");
var fs = require("fs");
var request = require("request");
var Twitter = require("twitter");
//var Spotify = require('spotify-web-api-node');
var Spotify = require('node-spotify-api');
//creates log.txt file
var filename = './log.txt';
//NPM module used to write output to console and log.txt simulatneously
var log = require('simple-node-logger').createSimpleFileLogger(filename);
log.setLevel('all');
//argv[2] chooses users actions; argv[3] is input parameter, ie; movie title
var userCommand = process.argv[2];
var secondCommand = process.argv[3];
//concatenate multiple words in 2nd user argument
for (var i = 4; i < process.argv.length; i++) {
secondCommand += '+' + process.argv[i];
}
// Fetch Spotify Keys
var spotify = new Spotify(keys.spotify);
// Writes to the log.txt file
var getArtistNames = function (artist) {
return artist.name;
};
// Function for running a Spotify search - Command is spotify-this-song
var getSpotify = function (songName) {
if (songName === undefined) {
songName = "What's my age again";
}
spotify.search(
{
type: "track",
query: userCommand
},
function (err, data) {
if (err) {
console.log("Error occurred: " + err);
return;
}
var songs = data.tracks.items;
for (var i = 0; i < songs.length; i++) {
console.log(i);
console.log("artist(s): " + songs[i].artists.map(getArtistNames));
console.log("song name: " + songs[i].name);
console.log("preview song: " + songs[i].preview_url);
console.log("album: " + songs[i].album.name);
console.log("-----------------------------------");
}
}
);
};
//Switch command
function mySwitch(userCommand) {
//choose which statement (userCommand) to switch to and execute
switch (userCommand) {
case "my-tweets":
getTweets();
break;
case "spotify-this-song":
getSpotify();
break;
case "movie-this":
getMovie();
break;
case "do-what-it-says":
doWhat();
break;
}
//Twitter - command: my-tweets
function getTweets() {
//Fetch Twitter Keys
var client = new Twitter(keys.twitter);
//Set my account to pull Tweets from
var screenName = { screen_name: 'captnwalker' };
//GET tweets
client.get('statuses/user_timeline', screenName, function (error, tweets, response) {
//throw error
if (error) throw error;
//Loop and Log first 20 tweets
for (var i = 0; i < tweets.length; i++) {
var date = tweets[i].created_at;
logOutput("@captnwalker: " + tweets[i].text + " Created At: " + date.substring(0, 19));
//seperator
logOutput("-----------------------");
}
});
}
//OMDB Movie - command: movie-this
function getMovie() {
// OMDB Movie - this MOVIE base code is from class files, I have modified for more data and assigned parse.body to a Var
var movieName = secondCommand;
// Then run a request to the OMDB API with the movie specified
var queryUrl = "http://www.omdbapi.com/?t=" + movieName + "&y=&plot=short&tomatoes=true&apikey=trilogy";
request(queryUrl, function (error, response, body) {
// If the request is successful = 200
if (!error && response.statusCode === 200) {
var body = JSON.parse(body);
//Simultaneously output to console and log.txt via NPM simple-node-logger
logOutput('================ Movie Info ================');
logOutput("Title: " + body.Title);
logOutput("Release Year: " + body.Year);
logOutput("IMdB Rating: " + body.imdbRating);
logOutput("Country: " + body.Country);
logOutput("Language: " + body.Language);
logOutput("Plot: " + body.Plot);
logOutput("Actors: " + body.Actors);
logOutput("Rotten Tomatoes Rating: " + body.Ratings[2].Value);
logOutput("Rotten Tomatoes URL: " + body.tomatoURL);
logOutput('==================THE END=================');
} else {
//else - throw error
console.log("Error occurred.")
}
//Response if user does not type in a movie title
if (movieName === "Mr. Nobody") {
console.log("-----------------------");
console.log("If you haven't watched 'Mr. Nobody,' then you should: http://www.imdb.com/title/tt0485947/");
console.log("It's on Netflix!");
}
});
}
//Function for command do-what-it-says; reads and splits random.txt file
//command: do-what-it-says
function doWhat() {
//Read random.txt file
fs.readFile("random.txt", "utf8", function (error, data) {
if (!error);
console.log(data.toString());
//split text with comma delimiter
var cmds = data.toString().split(',');
});
}
} //Closes mySwitch func - Everything except the call must be within this scope
//Call mySwitch function
mySwitch(userCommand);