Skip to content

Instantly share code, notes, and snippets.

@m3hari
Created November 29, 2017 07:14
Show Gist options
  • Save m3hari/f4cf9442527e90d7d7f38b3b57948c27 to your computer and use it in GitHub Desktop.
Save m3hari/f4cf9442527e90d7d7f38b3b57948c27 to your computer and use it in GitHub Desktop.
function addTvShowToDb(tmdbId) {
return new Promise(async (resolve, reject) => {
try {
const res = await getTv(tmdbId);
const tv = {
tmdb_id: res.id,
overview: res.overview,
genres: res.genres,
seasons: res.seasons,
created_by: res.created_by,
title: res.name || res.original_name,
poster_path: res.poster_path,
backdrop_path: res.backdrop_path,
first_air_date: res.first_air_date,
vote_average: res.vote_average,
status: res.status
};
if (res.videos && res.videos.results && res.videos.results.length > 0) {
tv.videos = res.videos.results;
tv.trailer_key = res.videos.results[0].key;
}
if (tv.seasons) {
const seasons = await getSessonDetails(tv.tmdb_id, tv.seasons);
tv.seasons = seasons.map(season => {
return {
id: season.id,
air_date: season.air_date,
poster_path: season.poster_path,
overview: season.overview,
season_number: season.season_number,
name: season.name
};
});
const addedTv = await TvShow.create(tv);
const episodes = seasons.map(season => {
return season.episodes;
})
.reduce((prev, curr) => { return prev.concat(curr) }, [])
.map(episode => {
return {
tvId: tv.tmdb_id,
air_date: episode.air_date,
episode_number: episode.episode_number,
title: episode.name,
overview: episode.overview,
tmdb_id: episode.id,
season_number: episode.season_number,
still_path: episode.still_path,
vote_average: episode.vote_average,
}
});
await addEpisodes(episodes);
resolve(addedTv);
}
else {
throw makerError(Util.UNKNOWN_ERROR);
}
} catch (err) {
reject(err);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment