2025-01-03 19:46:10 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
|
|
|
|
|
|
class TMDBClient {
|
|
|
|
|
constructor(accessToken) {
|
2025-01-03 20:22:04 +00:00
|
|
|
if (!accessToken) {
|
|
|
|
|
throw new Error('TMDB access token is required');
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-03 19:46:10 +00:00
|
|
|
this.client = axios.create({
|
|
|
|
|
baseURL: 'https://api.themoviedb.org/3',
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
|
|
|
'Accept': 'application/json'
|
2025-01-03 20:08:13 +00:00
|
|
|
},
|
2025-01-03 20:22:04 +00:00
|
|
|
timeout: 10000
|
2025-01-03 19:46:10 +00:00
|
|
|
});
|
2025-01-03 20:08:13 +00:00
|
|
|
|
|
|
|
|
this.client.interceptors.response.use(
|
|
|
|
|
response => response,
|
|
|
|
|
error => {
|
|
|
|
|
console.error('TMDB API Error:', {
|
|
|
|
|
status: error.response?.status,
|
|
|
|
|
data: error.response?.data,
|
|
|
|
|
message: error.message
|
|
|
|
|
});
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-01-03 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async makeRequest(method, endpoint, params = {}) {
|
|
|
|
|
try {
|
2025-01-04 13:13:46 +00:00
|
|
|
// Убедимся, что параметры запроса корректны
|
|
|
|
|
const requestParams = {
|
|
|
|
|
...params,
|
|
|
|
|
language: 'ru-RU',
|
|
|
|
|
region: 'RU'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log('TMDB Request:', {
|
|
|
|
|
method,
|
|
|
|
|
endpoint,
|
|
|
|
|
params: requestParams
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-03 20:08:13 +00:00
|
|
|
const response = await this.client({
|
2025-01-03 19:46:10 +00:00
|
|
|
method,
|
|
|
|
|
url: endpoint,
|
2025-01-04 13:13:46 +00:00
|
|
|
params: requestParams
|
2025-01-03 19:46:10 +00:00
|
|
|
});
|
2025-01-04 13:13:46 +00:00
|
|
|
|
2025-01-04 12:54:12 +00:00
|
|
|
console.log('TMDB Response:', {
|
|
|
|
|
endpoint,
|
2025-01-04 13:13:46 +00:00
|
|
|
requestParams,
|
2025-01-04 12:54:12 +00:00
|
|
|
status: response.status,
|
|
|
|
|
page: response.data.page,
|
|
|
|
|
totalPages: response.data.total_pages,
|
|
|
|
|
resultsCount: response.data.results?.length
|
|
|
|
|
});
|
2025-01-04 13:13:46 +00:00
|
|
|
|
2025-01-03 20:08:13 +00:00
|
|
|
return response;
|
2025-01-03 19:46:10 +00:00
|
|
|
} catch (error) {
|
2025-01-04 12:54:12 +00:00
|
|
|
console.error('TMDB Error:', {
|
|
|
|
|
endpoint,
|
|
|
|
|
params,
|
|
|
|
|
error: error.message,
|
|
|
|
|
response: error.response?.data
|
|
|
|
|
});
|
2025-01-03 20:08:13 +00:00
|
|
|
if (error.response) {
|
|
|
|
|
throw new Error(`TMDB API Error: ${error.response.data.status_message || error.message}`);
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`Network Error: ${error.message}`);
|
2025-01-03 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getImageURL(path, size = 'original') {
|
|
|
|
|
if (!path) return null;
|
|
|
|
|
return `https://image.tmdb.org/t/p/${size}${path}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async searchMovies(query, page = 1) {
|
2025-01-04 12:54:12 +00:00
|
|
|
const pageNum = parseInt(page, 10) || 1;
|
2025-01-04 13:13:46 +00:00
|
|
|
console.log('Searching movies:', { query, page: pageNum });
|
|
|
|
|
|
2025-01-03 20:08:13 +00:00
|
|
|
const response = await this.makeRequest('GET', '/search/movie', {
|
2025-01-03 19:46:10 +00:00
|
|
|
query,
|
2025-01-04 12:54:12 +00:00
|
|
|
page: pageNum,
|
2025-01-03 19:46:10 +00:00
|
|
|
include_adult: false
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-03 20:08:13 +00:00
|
|
|
const data = response.data;
|
2025-01-03 20:22:04 +00:00
|
|
|
data.results = data.results
|
|
|
|
|
.filter(movie => movie.poster_path && movie.overview && movie.vote_average > 0)
|
|
|
|
|
.map(movie => ({
|
|
|
|
|
...movie,
|
|
|
|
|
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
|
|
|
|
backdrop_path: this.getImageURL(movie.backdrop_path, 'original')
|
|
|
|
|
}));
|
2025-01-03 19:46:10 +00:00
|
|
|
|
2025-01-04 12:54:12 +00:00
|
|
|
return data;
|
2025-01-03 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getMovie(id) {
|
2025-01-03 20:08:13 +00:00
|
|
|
const response = await this.makeRequest('GET', `/movie/${id}`);
|
|
|
|
|
const movie = response.data;
|
2025-01-03 19:46:10 +00:00
|
|
|
return {
|
|
|
|
|
...movie,
|
|
|
|
|
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
2025-01-03 20:08:13 +00:00
|
|
|
backdrop_path: this.getImageURL(movie.backdrop_path, 'original')
|
2025-01-03 19:46:10 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getPopularMovies(page = 1) {
|
2025-01-04 12:54:12 +00:00
|
|
|
const pageNum = parseInt(page, 10) || 1;
|
2025-01-04 13:13:46 +00:00
|
|
|
console.log('Getting popular movies:', { page: pageNum });
|
|
|
|
|
|
|
|
|
|
const response = await this.makeRequest('GET', '/movie/popular', {
|
|
|
|
|
page: pageNum
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log('Popular movies response:', {
|
|
|
|
|
requestedPage: pageNum,
|
|
|
|
|
returnedPage: response.data.page,
|
|
|
|
|
totalPages: response.data.total_pages,
|
|
|
|
|
resultsCount: response.data.results.length
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-03 20:08:13 +00:00
|
|
|
const data = response.data;
|
2025-01-03 19:46:10 +00:00
|
|
|
data.results = data.results.map(movie => ({
|
|
|
|
|
...movie,
|
|
|
|
|
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
2025-01-03 20:08:13 +00:00
|
|
|
backdrop_path: this.getImageURL(movie.backdrop_path, 'original')
|
2025-01-03 19:46:10 +00:00
|
|
|
}));
|
2025-01-04 13:13:46 +00:00
|
|
|
|
2025-01-04 12:54:12 +00:00
|
|
|
return data;
|
2025-01-03 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getTopRatedMovies(page = 1) {
|
2025-01-04 12:54:12 +00:00
|
|
|
const pageNum = parseInt(page, 10) || 1;
|
2025-01-04 13:13:46 +00:00
|
|
|
const response = await this.makeRequest('GET', '/movie/top_rated', {
|
|
|
|
|
page: pageNum
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-03 20:08:13 +00:00
|
|
|
const data = response.data;
|
2025-01-03 19:46:10 +00:00
|
|
|
data.results = data.results.map(movie => ({
|
|
|
|
|
...movie,
|
|
|
|
|
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
2025-01-03 20:08:13 +00:00
|
|
|
backdrop_path: this.getImageURL(movie.backdrop_path, 'original')
|
2025-01-03 19:46:10 +00:00
|
|
|
}));
|
2025-01-04 13:13:46 +00:00
|
|
|
|
2025-01-04 12:54:12 +00:00
|
|
|
return data;
|
2025-01-03 20:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getUpcomingMovies(page = 1) {
|
2025-01-04 12:54:12 +00:00
|
|
|
const pageNum = parseInt(page, 10) || 1;
|
2025-01-04 13:13:46 +00:00
|
|
|
const response = await this.makeRequest('GET', '/movie/upcoming', {
|
|
|
|
|
page: pageNum
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-03 20:22:04 +00:00
|
|
|
const data = response.data;
|
|
|
|
|
data.results = data.results.map(movie => ({
|
|
|
|
|
...movie,
|
|
|
|
|
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
|
|
|
|
backdrop_path: this.getImageURL(movie.backdrop_path, 'original')
|
|
|
|
|
}));
|
2025-01-04 13:13:46 +00:00
|
|
|
|
2025-01-04 12:54:12 +00:00
|
|
|
return data;
|
2025-01-03 20:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getMovieExternalIDs(id) {
|
|
|
|
|
const response = await this.makeRequest('GET', `/movie/${id}/external_ids`);
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = TMDBClient;
|