228 lines
6.6 KiB
JavaScript
228 lines
6.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} tmdb_id - The TMDB ID of the person
|
|
* @returns {Promise<Object|null>} - The person data or null if there's an error
|
|
*/
|
|
async function fetchPersonDetails(tmdb_id) {
|
|
try {
|
|
let url = `${TMDB_APIURL}person/${tmdb_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;
|
|
}
|
|
}
|
|
|
|
async function fetchTrending(type, page = 1) {
|
|
try {
|
|
let url = `${TMDB_APIURL}trending/${type}/week?api_key=${TMDB_APIKEY}&language=en-US&page=${page}`;
|
|
logToFile(`Fetching Trending ${type} From: ${url}`);
|
|
const response = await axios.get(url);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.log(`Error fetching trending ${type}.`, error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function fetchPopular(type, page = 1) {
|
|
try {
|
|
let url = `${TMDB_APIURL}${type}/popular?api_key=${TMDB_APIKEY}&language=en-US&page=${page}`;
|
|
logToFile(`Fetching Popular ${type} From: ${url}`);
|
|
const response = await axios.get(url);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.log(`Error fetching popular ${type}.`, error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function searchMulti(query, page = 1) {
|
|
try {
|
|
let url = `${TMDB_APIURL}search/multi?api_key=${TMDB_APIKEY}&language=en-US&query=${encodeURIComponent(query)}&page=${page}`;
|
|
logToFile(`Searching TMDB for: ${query}`);
|
|
const response = await axios.get(url);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.log('Error searching TMDB.', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function searchPeople(query, page = 1) {
|
|
try {
|
|
let url = `${TMDB_APIURL}search/person?api_key=${TMDB_APIKEY}&language=en-US&query=${encodeURIComponent(query)}&page=${page}`;
|
|
logToFile(`Searching TMDB People for: ${query}`);
|
|
const response = await axios.get(url);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.log('Error searching TMDB people.', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function fetchPopularPeople(page = 1) {
|
|
try {
|
|
let url = `${TMDB_APIURL}person/popular?api_key=${TMDB_APIKEY}&language=en-US&page=${page}`;
|
|
logToFile(`Fetching Popular People From: ${url}`);
|
|
const response = await axios.get(url);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.log('Error fetching popular people.', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
fetchEntryData,
|
|
fetchSeasonData,
|
|
fetchEntryMedia,
|
|
fetchCollectionData,
|
|
fetchEntryCredits,
|
|
fetchPersonDetails,
|
|
fetchTrending,
|
|
fetchPopular,
|
|
searchMulti,
|
|
searchPeople,
|
|
fetchPopularPeople,
|
|
}
|
|
|
|
|
|
|