Skip to content

Instantly share code, notes, and snippets.

@richbai90
Created September 18, 2019 22:05
Show Gist options
  • Save richbai90/28e6d526efa997b7eebbbdb4ee30a3a5 to your computer and use it in GitHub Desktop.
Save richbai90/28e6d526efa997b7eebbbdb4ee30a3a5 to your computer and use it in GitHub Desktop.
An example of how to implement a generic search object with a single API
const axios = require("axios");
const Spotify = require("node-spotify-api");
const moment = require("moment");
/**
*
* @param searchType {{'concert' | 'movie' | 'spotify'}}
* @param spotifyCredentials {Object=}
* @param spotifyCredentials.id {string}
* @param spotifyCredentials.secret {string}
*/
function Search(searchType, spotifyCredentials) {
this.searchType = searchType;
this.spotify = new Spotify(
Object.assign(
{
id: "16df60132b67415c993e57e3a36ee26c",
secret: "8f79ba8a4fe14bc0b5e27b2c255ca413"
},
spotifyCredentials
)
);
}
Search.prototype.search = function(value) {
switch (this.searchType) {
case "movie":
case "concert":
return this._axiosSearch(this._createUrl(value)).then(result =>
this._formatResults(result)
);
case "spotify":
return this._spotifySearch(value).then(result =>
this._formatSpotifyResults(result)
);
default:
throw `Invalid Search Type ${this.searchType}`;
}
};
Search.prototype._createUrl = function(param) {
const escapedParam = param.split(" ").join("+");
switch (this.searchType) {
case "concert":
return `https://rest.bandsintown.com/artists/${escapedParam}/events?app_id=codingbootcamp`;
case "movie":
return `http://www.omdbapi.com/?apikey=d373dcb1&t=${escapedParam}`;
default:
throw `Cannot format URL for search type ${this.searchType}`;
}
};
Search.prototype._axiosSearch = function(url) {
return axios
.get(url)
.then(response => response.data)
.catch(err => {
throw err;
});
};
Search.prototype._spotifySearch = function(value) {
return this.spotify.search({ type: "track", query: value }).catch(err => {
throw err;
});
};
Search.prototype._formatResults = function(results) {
switch (this.searchType) {
case "concert":
return this._formatConcertResults(results);
case "movie":
return this._formatMovieResults(results);
case "spotify":
return this._formatSpotifyResults(results);
default:
throw `Unable to format results for search type ${this.searchType}`;
}
};
Search.prototype._formatMovieResults = function(results) {
return `${results.Title}
Year: ${results.Year}
IMDB Rating: ${results.imdbRating}
Country: ${results.Country}
Language: ${results.Language}
Plot: ${results.Plot}
`;
};
Search.prototype._formatConcertResults = function(results) {
let list = "";
for (i = 0; i < results.length; i++) {
list += `${moment(results[i].datetime).format("MM/DD/YYYY")} -- ${
results[i].venue.name
} -- ${results[i].venue.city} ${results[i].venue.region}, ${
results[i].venue.country
}\n`;
}
return list;
};
Search.prototype._formatSpotifyResults = function(results) {
return results.tracks.items
.map(
track =>
`Song: ${track.name}
Artist(s): ${track.artists[0].name}
Album: ${track.album.name}
Preview: ${track.preview_url}
`
)
.join("\n");
};
let concertSearch = new Search("concert");
let movieSearch = new Search("movie");
let spotifySearch = new Search("spotify");
concertSearch.search("green day").then(results => {
console.log(results);
});
movieSearch.search("north by").then(results => {
console.log(results);
});
spotifySearch.search("brain stew").then(results => {
console.log(results);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment