195 lines
5.1 KiB
JavaScript
195 lines
5.1 KiB
JavaScript
const axios = require('axios');
|
|
const moment = require('../utils/moment.js')
|
|
|
|
const Entry = require('../models/Entry.js')
|
|
const Collection = require('../models/Franchise.js')
|
|
const EntryId = require('../models/EntryId.js')
|
|
const EntryRating = require('../models/EntryRating.js');
|
|
const { saveEntry } = require('./entryService.js');
|
|
|
|
const tmdb_apiKey = "5323ef3dc430df54dcb9e58c37fd22bb";
|
|
|
|
// async function getMovies(max, url) {
|
|
// let list = [];
|
|
|
|
// await axios.get(url)
|
|
// .then((response) => {
|
|
// if (response.status != 200) return console.log('Error fetching the data.');
|
|
|
|
// let data = response.data.results;
|
|
// let count = 0;
|
|
// if (data != null)
|
|
// data.forEach(mv => {
|
|
// if (count < max) {
|
|
// // console.log(mv);
|
|
// let movie = {
|
|
// id: mv.id,
|
|
// title: mv.title || mv.name,
|
|
// poster: 'https://image.tmdb.org/t/p/w300' + mv.poster_path,
|
|
// year: mv.release_date != undefined ? mv.release_date.substring(0, 4) : mv.first_air_date.substring(0, 4),
|
|
// rating: mv.vote_average
|
|
|
|
// };
|
|
|
|
// list.push(movie)
|
|
|
|
// count++;
|
|
// }
|
|
// });
|
|
// })
|
|
// return list;
|
|
// }
|
|
|
|
|
|
async function fetchEntryData(tmdb_id, is_tvserie) {
|
|
// fetching the informations
|
|
let type = is_tvserie ? "tv" : "movie";
|
|
let url = `https://api.themoviedb.org/3/${type}/${tmdb_id}?api_key=${tmdb_apiKey}&language=en-US`
|
|
let data;
|
|
await axios.get(url)
|
|
.then((response) => {
|
|
if (response.status != 200) return console.log('Error fetching the data.');
|
|
data = response.data;
|
|
})
|
|
|
|
let savedEntry;
|
|
|
|
if (is_tvserie)
|
|
savedEntry = await saveEntry(data);
|
|
else
|
|
savedEntry = await saveEntry(data);
|
|
|
|
return savedEntry.get({ plain: true });
|
|
}
|
|
|
|
// async function fetchSerieData(serieId) {
|
|
// // getting the informations
|
|
// let url = `https://api.themoviedb.org/3/tv/${serieId}?api_key=${tmdb_apiKey}&language=en-US`
|
|
// let data;
|
|
// await axios.get(url)
|
|
// .then((response) => {
|
|
// if (response.status != 200) return console.log('Error fetching the data.');
|
|
// data = response.data;
|
|
// })
|
|
|
|
// // creating the object
|
|
// let serie = new MovieClass();
|
|
|
|
// serie.tmdb_id = serieId
|
|
|
|
// serie.title = data.name
|
|
// serie.release_date = data.first_air_date
|
|
// serie.runtime = data.episode_run_time[0]
|
|
// serie.overview = data.overview
|
|
// serie.country = data.production_countries[0].name
|
|
// serie.status = data.status
|
|
|
|
// serie.poster_path = data.poster_path
|
|
// serie.poster_path = data.backdrop_path
|
|
|
|
// serie.is_tvserie = true
|
|
|
|
// serie.tmdb_rating = data.vote_average
|
|
|
|
// serie.last_air_date = data.last_air_date
|
|
// serie.date_added = moment().format("YYYY-MM-DD")
|
|
|
|
// // gettings the seasons info
|
|
// if (data.seasons.length != 0)
|
|
// for (let i = 0; i < data.seasons.length; i++) {
|
|
// let s = new Season();
|
|
|
|
// s.tmdb_id = data.seasons[i].id
|
|
// s.temp_parent_tmdb_id = serieId
|
|
|
|
// s.season_number = data.seasons[i].season_number
|
|
// s.overview = data.seasons[i].overview
|
|
// s.air_date = data.seasons[i].air_date
|
|
|
|
// s.poster_path = data.seasons[i].poster_path
|
|
|
|
// serie.seasons.push(s)
|
|
// }
|
|
|
|
// // todo: getting the episodes info
|
|
|
|
// // console.log(serie);
|
|
// return serie;
|
|
// }
|
|
|
|
|
|
// async function insertMovieSerie(tmdb_id, isTvSerie) {
|
|
// try {
|
|
// await Execute(`INSERT INTO Movies
|
|
// (tmdb_id, is_in_library, is_tvserie)
|
|
// VALUES
|
|
// ('${tmdb_id}', true, ${isTvSerie});`
|
|
// );
|
|
|
|
// console.log(`Inserted into db: ${tmdb_id}`)
|
|
// } catch (e) {
|
|
// console.log(e)
|
|
// }
|
|
// }
|
|
|
|
// async function updateMovieSerieData(movie) {
|
|
// let error = false;
|
|
// try {
|
|
// await Execute(
|
|
// `UPDATE Movies SET
|
|
// tmdb_id = '${movie.tmdb_id}',
|
|
// imdb_id = '${movie.imdb_id}',
|
|
// title = '${movie.title}',
|
|
// original_title = '${movie.original_title}',
|
|
// overview = '${movie.overview}',
|
|
// release_date = '${movie.release_date}',
|
|
// runtime = ${movie.runtime},
|
|
// status = '${movie.status}',
|
|
// tmdb_rating = ${movie.tmdb_rating},
|
|
// poster_path = '${movie.poster_path}',
|
|
// banner_path = '${movie.banner_path}',
|
|
// country = '${movie.country}',
|
|
// budget = ${movie.budget},
|
|
// revenue = ${movie.revenue},
|
|
|
|
// is_tvserie = false
|
|
|
|
// WHERE id = ${movie.id};`
|
|
// )
|
|
|
|
// // todo : updating the genres
|
|
// // todo : updating the tags
|
|
// // todo : updating the actors
|
|
|
|
// if (movie.is_tvserie) {
|
|
// // todo : the seasons
|
|
// // todo : the episodes
|
|
// }
|
|
|
|
// } catch (e) {
|
|
// error = true;
|
|
// console.log(e)
|
|
// }
|
|
|
|
// if (!error) {
|
|
// console.log(`Movie/Serie Updated: ${movie.id}:${movie.title}`)
|
|
// return false;
|
|
// } else {
|
|
// return true;
|
|
// }
|
|
// }
|
|
|
|
module.exports = {
|
|
// getMovies,
|
|
|
|
fetchEntryData,
|
|
// fetchSerieData,
|
|
|
|
// insertMovieSerie,
|
|
// updateMovieSerieData,
|
|
|
|
}
|
|
|
|
|
|
|