-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliri.js
135 lines (127 loc) · 3.95 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
// required libraries
require("dotenv").config();
let fs = require("fs");
let moment = require("moment");
let axios = require("axios");
const keys = require("./keys.js");
const Spotify = require("node-spotify-api");
let spotify = new Spotify(keys.spotify);
//pull in required variables
const spotifyTest = process.env.SPOTIFY_ID;
let command = process.argv[2];
let searchTerm = process.argv[3];
fs.appendFile("log.txt", `${command},`, err => {
if (err) throw err;
});
switch (command) {
case "concert-this": //bands in town
searchForBandsInTown(searchTerm);
break;
case "spotify-this-song": //spotify
spotifyThisSong(searchTerm);
break;
case "movie-this": // OMDB for movies
movieThis(searchTerm);
break;
case "do-what-it-says": // read commands from a file and excute the commands above
doRandom();
break;
}
function searchForBandsInTown(artist) {
const queryUrl = `https://rest.bandsintown.com/artists/${artist}/events?app_id=codingbootcamp`;
axios
.get(queryUrl)
.then(({ data }) => {
if (data[0].venue != undefined) {
console.log(`Event Veunue: ${data[0].venue.name}`);
console.log(`Event Location: ${data[0].venue.city}`);
const eventDateTime = moment(data[0].datetime);
console.log(
`Event Date & Time: ${eventDateTime.format("dddd, MMMM Do YYYY")}`
);
} else {
console.log("No results found.");
}
})
.catch(error => {
console.log(error);
});
}
function spotifyThisSong(song) {
spotify
.search({ type: "track", query: song })
.then(({ tracks }) => {
if (tracks.total === 0) {
errorConditionForSpotify();
} else {
console.log(`Artist: ${tracks.items[0].artists[0].name}`);
console.log(`Track: ${tracks.items[0].name}`);
console.log(`Preview URL: ${tracks.items[0].preview_url}`);
console.log(`Album: ${tracks.items[0].album.name}`);
}
})
.catch(error => {
console.log(error);
console.log(
"No Results found. Showing results for 'The Sign' by Ace of Base"
);
});
}
function errorConditionForSpotify() {
spotify
.search({ type: "track", query: "The Sign" })
.then(({ tracks }) => {
for (let i = 0; i < tracks.items.length; i++) {
if (tracks.items[i].artists[0].name === "Ace of Base") {
console.log(`Artist: ${tracks.items[i].artists[0].name}`);
console.log(`Track: ${tracks.items[i].name}`);
console.log(`Preview URL: ${tracks.items[i].preview_url}`);
console.log(`Album: ${tracks.items[i].album.name}`);
i = tracks.items.length;
}
}
})
.catch(error => {
console.log(error);
console.log("No Results found. ");
});
}
function movieThis(movie) {
axios
.get(
`http://www.omdbapi.com/?t=${movie}&y=&plot=short&tomatoes=true&apikey=trilogy`
)
.then(
({ data }) => {
//console.log(response.data);
if (data.Title != undefined) {
console.log(`Title: ${data.Title}`);
console.log(`Year: ${data.Year}`);
console.log(`imdbRating:: ${data.imdbRating}`);
console.log(`Title: ${data.Title}`);
console.log(`Country:: ${data.Country}`);
console.log(`Language:: ${data.Language}`);
console.log(`Plot: ${data.Plot}`);
console.log(`Actors: ${data.Actors}`);
console.log(`RottenTomatoes: ${data.tomatoRating}`);
} else {
movieThis("Mr. Nobody");
}
}
// if response is empty call the api again with the "default" movie
)
.catch(error => {
console.log(error);
console.log("No Results found. ");
});
}
function doRandom() {
fs.readFile("random.txt", "utf8", (error, data) => {
const dataArr = data.split(",");
spotifyThisSong(dataArr[1]);
// If the code experiences any errors it will log the error to the console.
if (error) {
return console.log(error);
}
});
}