-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliri.js
More file actions
149 lines (126 loc) · 3.51 KB
/
Copy pathliri.js
File metadata and controls
149 lines (126 loc) · 3.51 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
/*
Config
*/
require("dotenv").config();
var fs = require("fs");
var Spotify = require('node-spotify-api');
var keys = require("./keys.js");
var axios = require("axios");
var moment = require('moment');
var spotify = new Spotify(keys.spotify);
var type = process.argv[2];
var value = process.argv[3];
/*
Handling system files
*/
function readFile(file) {
fs.readFile(file, "utf8", function (error, data) {
if (error) {
return console.log(error);
}
var dataArray = data.split(",");
processCommand(dataArray[0], dataArray[1]);
}
)
}
function appendFile(file, text){
fs.appendFile(file, text, function(err) {
if (err) {
console.log(err);
} else {
console.log("Content Added");
}
});
}
/*
Handling user input
*/
processCommand(type, value);
function processCommand(cmnd, value) {
var executed = cmnd + " " + value+"\n";
appendFile("log.txt", executed);
switch (cmnd) {
case "concert-this":
getConcertInfo(value);
break;
case "spotify-this-song":
getSpotifyInfo(value);
break;
case "movie-this":
getMovieInfo(value);
break;
case "do-what-it-says":
readFile("random.txt");
break;
}
}
/*
Connections
*/
function getConcertInfo(value) {
axios.get("https://rest.bandsintown.com/artists/" + value + "/events?app_id=codingbootcamp")
.then(function (response) {
printBandData(response.data, value)
})
.catch(function (error) {
console.log(error);
});
}
function getSpotifyInfo(valueToExecute) {
if (valueToExecute == undefined) {
valueToExecute = "The Sign - Ace of base";
}
spotify.search(
{
type: 'track', query: valueToExecute
}, function (err, data) {
if (err) {
return console.log('Error occurred: ' + err);
}
printSpotifyData(data);
});
}
function getMovieInfo(valueToExecute) {
if (valueToExecute == undefined) {
valueToExecute = "Mr. Nobody";
}
axios.get("http://www.omdbapi.com/?apikey=trilogy&t=" + valueToExecute)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
}
/*
Printing data
*/
function printBandData(data, searchValue) {
for (let i = 0; i < data.length; i++) {
var search = searchValue;
var name = data[i].venue.name;
var location = data[i].venue.city + ", " + data[i].venue.country;
var date = moment(data[i].datetime).format("MM-DD-YYYY");
console.log("Search: " + search
+ "\nName: " + name
+ "\nLocation: " + location
+ "\nDate: " + date);
console.log("**************************");
}
}
function printSpotifyData(data) {
for (let i = 0; i < data.tracks.items.length; i++) {
var artist = data.tracks.items[i].artists[0].name;
var song = data.tracks.items[i].name;
var album = data.tracks.items[i].album.name;
var preview = data.tracks.items[i].preview_url;
if (preview == null) {
preview = "No preview available."
}
console.log("Artist: " + artist
+ "\nSong: " + song
+ "\nAlbum: " + album
+ "\nPreview: " + preview);
console.log("**************************");
}
}