Files
Showy/backend/services/tmdbService.js
T

163 lines
4.6 KiB
JavaScript

const axios = require('axios');
const { logToFile } = require('./logService');
const configService = require('./configService');
const TMDB_APIURL = configService.getTmdbApiUrl();
const TMDB_APIKEY = configService.getTmdbApiKey();
async function fetchEntryData(tmdb_id, is_tvserie) {
try {
let type = is_tvserie ? "tv" : "movie";
let url = `${TMDB_APIURL}${type}/${tmdb_id}?api_key=${TMDB_APIKEY}&language=en-US`
logToFile(`Fetching Entry Data From: ${url}`)
let data;
await axios.get(url)
.then((response) => {
if (response.status != 200) {
logToFile(`Error Fetching Data From: ${url}`)
return console.log('Error fetching the data.')
};
data = response.data;
})
return data;
} catch (error) {
console.log('Error fetching the entry data.', error)
return null;
}
}
async function fetchSeasonData(tmdbTvId, seasonNumber) {
// fetching the informations
try {
let url = `${TMDB_APIURL}tv/${tmdbTvId}/season/${seasonNumber}?api_key=${TMDB_APIKEY}&language=en-US`
logToFile(`Fetching Season Data From: ${url}`)
let data;
await axios.get(url)
.then((response) => {
if (response.status != 200) {
logToFile(`Error Fetching Data From: ${url}`)
return console.log('Error fetching the data.')
};
data = response.data;
})
return data;
} catch (error) {
console.log('Error fetching the data.')
return null;
}
}
async function fetchEntryMedia(tmdb_id, is_tvserie) {
try {
let type = is_tvserie ? "tv" : "movie";
let url = `${TMDB_APIURL}${type}/${tmdb_id}/images?api_key=${TMDB_APIKEY}`
logToFile(`Fetching Entry Media Data From: ${url}`)
let data;
await axios.get(url)
.then((response) => {
if (response.status != 200) {
logToFile(`Error Fetching Data From: ${url}`)
return console.log('Error fetching the data.')
};
data = response.data;
})
return data;
} catch (error) {
console.log('Error fetching the entry media.', error)
return null;
}
}
async function fetchCollectionData(collectionId) {
try {
let url = `${TMDB_APIURL}collection/${collectionId}?api_key=${TMDB_APIKEY}&language=en-US`
logToFile(`Fetching Collection Data From: ${url}`)
let data;
await axios.get(url)
.then((response) => {
if (response.status != 200) return console.log('Error fetching collection data from tmdb:', response);
data = response.data;
})
return data;
} catch (error) {
if (response.status != 200) {
logToFile(`Error Fetching Data From: ${url}`)
return console.log('Error fetching the data.')
};
return null;
}
}
/**
* Fetches credits (cast and crew) for a movie or TV show
* @param {number} tmdb_id - The TMDB ID of the movie or TV show
* @param {boolean} is_tvserie - Whether the entry is a TV series or not
* @returns {Promise<Object|null>} - The credits data or null if there's an error
*/
async function fetchEntryCredits(tmdb_id, is_tvserie) {
try {
let type = is_tvserie ? "tv" : "movie";
let url = `${TMDB_APIURL}${type}/${tmdb_id}/credits?api_key=${TMDB_APIKEY}&language=en-US`;
logToFile(`Fetching Entry Credits From: ${url}`);
let data;
await axios.get(url)
.then((response) => {
if (response.status != 200) {
logToFile(`Error Fetching Credits From: ${url}`);
return console.log('Error fetching the credits data.');
};
data = response.data;
});
return data;
} catch (error) {
console.log('Error fetching the entry credits.', error);
return null;
}
}
/**
* Fetches detailed information about a person
* @param {number} person_id - The TMDB ID of the person
* @returns {Promise<Object|null>} - The person data or null if there's an error
*/
async function fetchPersonDetails(person_id) {
try {
let url = `${TMDB_APIURL}person/${person_id}?api_key=${TMDB_APIKEY}&language=en-US&append_to_response=external_ids`;
logToFile(`Fetching Person Details From: ${url}`);
let data;
await axios.get(url)
.then((response) => {
if (response.status != 200) {
logToFile(`Error Fetching Person Details From: ${url}`);
return console.log('Error fetching the person data.');
};
data = response.data;
});
return data;
} catch (error) {
console.log('Error fetching the person details.', error);
return null;
}
}
module.exports = {
fetchEntryData,
fetchSeasonData,
fetchEntryMedia,
fetchCollectionData,
fetchEntryCredits,
fetchPersonDetails,
}