Тестовая интеграция TMDB

This commit is contained in:
degradin 2025-05-07 13:32:41 +03:00
parent e06bd3c1e6
commit 18877a2da2

View File

@ -1,11 +1,13 @@
import axios from 'axios'; import axios from 'axios';
const IMDB_API_URL = 'https://imdb.iamidiotareyoutoo.com'; const IMDB_API_URL = 'https://imdb.iamidiotareyoutoo.com';
const TMDB_API_KEY = import.meta.env.VITE_TMDB_API_KEY;
const TMDB_BASE_URL = 'https://api.themoviedb.org/3';
export const mediaTypes = { export const mediaTypes = {
MOVIE: 'movie', MOVIE: 'movie',
TV: 'tv', TV: 'series',
ALL: 'all' // Добавлено GAME: 'game'
}; };
function isLocalStorageAvailable() { function isLocalStorageAvailable() {
@ -86,16 +88,60 @@ export async function getMediaById(id) {
} }
} }
export async function searchMedia(query) { export const searchMedia = async (query, type = 'movie') => {
try { try {
if (!query || !query.trim()) return []; const response = await fetch(
return await searchIMDb(query, mediaTypes.ALL); `${TMDB_BASE_URL}/search/${type}?api_key=${TMDB_API_KEY}&query=${encodeURIComponent(query)}&language=ru-RU`
);
if (!response.ok) {
throw new Error('Failed to fetch from TMDB');
}
const data = await response.json();
return data.results.map(item => ({
title: item.title || item.name,
description: item.overview,
poster_url: item.poster_path ? `https://image.tmdb.org/t/p/w500${item.poster_path}` : null,
release_date: item.release_date || item.first_air_date,
type: type,
tmdb_id: item.id,
rating: item.vote_average
}));
} catch (error) { } catch (error) {
console.error('Error searching media:', error); console.error('Error searching media:', error);
return []; throw error;
} }
};
export const getMediaDetails = async (tmdbId, type = 'movie') => {
try {
const response = await fetch(
`${TMDB_BASE_URL}/${type}/${tmdbId}?api_key=${TMDB_API_KEY}&language=ru-RU`
);
if (!response.ok) {
throw new Error('Failed to fetch media details');
} }
const data = await response.json();
return {
title: data.title || data.name,
description: data.overview,
poster_url: data.poster_path ? `https://image.tmdb.org/t/p/w500${data.poster_path}` : null,
release_date: data.release_date || data.first_air_date,
type: type,
tmdb_id: data.id,
rating: data.vote_average,
genres: data.genres.map(g => g.name).join(', '),
runtime: data.runtime || data.episode_run_time?.[0] || null
};
} catch (error) {
console.error('Error fetching media details:', error);
throw error;
}
};
export async function addMedia(mediaData) { export async function addMedia(mediaData) {
try { try {
if (!isLocalStorageAvailable()) { if (!isLocalStorageAvailable()) {
@ -129,3 +175,34 @@ export async function addMedia(mediaData) {
throw error; throw error;
} }
} }
export const validateMediaData = (data) => {
const errors = [];
if (!data.title?.trim()) {
errors.push('Название обязательно');
}
if (!data.type || !Object.values(mediaTypes).includes(data.type)) {
errors.push('Неверный тип медиа');
}
if (data.rating && (isNaN(data.rating) || data.rating < 0 || data.rating > 10)) {
errors.push('Рейтинг должен быть от 0 до 10');
}
return errors;
};
export const formatMediaData = (data) => {
return {
title: data.title?.trim(),
description: data.description?.trim() || '',
type: data.type,
poster_url: data.poster_url?.trim() || null,
release_date: data.release_date || null,
rating: data.rating ? parseFloat(data.rating) : null,
genres: data.genres?.trim() || '',
runtime: data.runtime ? parseInt(data.runtime) : null
};
};