|
1 | 1 | import axios from 'axios' |
2 | 2 | import parseTorrent from 'parse-torrent' |
3 | 3 | import ptt from 'parse-torrent-title' |
| 4 | +import { tmdbSettingsHost } from 'utils/Hosts' |
4 | 5 |
|
5 | | -export const getMoviePosters = (movieName, language = 'en') => { |
6 | | - const url = `${window.location.protocol}//api.themoviedb.org/3/search/multi` |
7 | | - const imgHost = `${window.location.protocol}//${language === 'ru' ? 'imagetmdb.com' : 'image.tmdb.org'}` |
| 6 | +// Cache for TMDB settings to avoid repeated API calls |
| 7 | +let tmdbSettingsCache = null |
| 8 | + |
| 9 | +// Clear TMDB settings cache - call this when settings are updated |
| 10 | +export const clearTMDBCache = () => { |
| 11 | + tmdbSettingsCache = null |
| 12 | +} |
| 13 | + |
| 14 | +// Fetch TMDB settings from backend |
| 15 | +const getTMDBSettings = async () => { |
| 16 | + if (tmdbSettingsCache) { |
| 17 | + return tmdbSettingsCache |
| 18 | + } |
| 19 | + |
| 20 | + try { |
| 21 | + const { data } = await axios.get(tmdbSettingsHost()) |
| 22 | + tmdbSettingsCache = data |
| 23 | + return data |
| 24 | + } catch (error) { |
| 25 | + console.error('Error fetching TMDB settings:', error) |
| 26 | + // Return default values if API fails |
| 27 | + return { |
| 28 | + APIKey: process.env.REACT_APP_TMDB_API_KEY || '', |
| 29 | + APIURL: 'https://api.themoviedb.org/3', |
| 30 | + ImageURL: 'https://image.tmdb.org', |
| 31 | + ImageURLRu: 'https://imagetmdb.com', |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export const getMoviePosters = async (movieName, language = 'en') => { |
| 37 | + const settings = await getTMDBSettings() |
| 38 | + |
| 39 | + // If no API key is configured, return null |
| 40 | + if (!settings.APIKey) { |
| 41 | + return null |
| 42 | + } |
| 43 | + |
| 44 | + // Build API URL - automatically append /3/search/multi |
| 45 | + let apiURL = settings.APIURL.replace(/^https?:\/\//, '').replace(/\/$/, '') |
| 46 | + |
| 47 | + // If URL doesn't already contain the full path, add /3/search/multi |
| 48 | + if (!apiURL.includes('/3/search/multi')) { |
| 49 | + // Remove any partial paths that might exist |
| 50 | + apiURL = apiURL.replace(/\/3.*$/, '').replace(/\/search.*$/, '') |
| 51 | + apiURL = `${apiURL}/3/search/multi` |
| 52 | + } |
| 53 | + |
| 54 | + const url = `${window.location.protocol}//${apiURL}` |
| 55 | + |
| 56 | + // Build image URL - strip protocol and trailing slash |
| 57 | + const imgHost = `${window.location.protocol}//${ |
| 58 | + language === 'ru' |
| 59 | + ? settings.ImageURLRu.replace(/^https?:\/\//, '').replace(/\/$/, '') |
| 60 | + : settings.ImageURL.replace(/^https?:\/\//, '').replace(/\/$/, '') |
| 61 | + }` |
8 | 62 |
|
9 | 63 | return axios |
10 | 64 | .get(url, { |
11 | 65 | params: { |
12 | | - api_key: process.env.REACT_APP_TMDB_API_KEY, |
| 66 | + api_key: settings.APIKey, |
13 | 67 | language, |
14 | 68 | include_image_language: `${language},null,en`, |
15 | 69 | query: movieName, |
|
0 commit comments