Spaces:
Paused
Paused
Update src/api/tmdb.js
Browse files- src/api/tmdb.js +13 -102
src/api/tmdb.js
CHANGED
|
@@ -28,56 +28,14 @@ const makeRequest = (url, tmdbApiKey = null) => {
|
|
| 28 |
});
|
| 29 |
};
|
| 30 |
|
| 31 |
-
const
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
reject(err);
|
| 40 |
-
} else {
|
| 41 |
-
resolve(row);
|
| 42 |
-
}
|
| 43 |
-
}
|
| 44 |
-
);
|
| 45 |
-
});
|
| 46 |
-
|
| 47 |
-
if (cachedEntry) {
|
| 48 |
-
log.debug(`Cached Entry: Page ${cachedEntry.page}, Skip ${cachedEntry.skip}`);
|
| 49 |
-
|
| 50 |
-
const nextPage = Math.ceil((skip + 1) / 20);
|
| 51 |
-
log.debug(`Determined Page to Serve: ${nextPage}`);
|
| 52 |
-
return nextPage;
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
const lastEntry = await new Promise((resolve, reject) => {
|
| 56 |
-
catalogDb.get(
|
| 57 |
-
"SELECT page, skip FROM cache WHERE provider_id = ? AND type = ? AND sortBy = ? AND ageRange = ? ORDER BY skip DESC LIMIT 1",
|
| 58 |
-
[providerId, type, sortBy, ageRange],
|
| 59 |
-
(err, row) => {
|
| 60 |
-
if (err) {
|
| 61 |
-
reject(err);
|
| 62 |
-
} else {
|
| 63 |
-
resolve(row);
|
| 64 |
-
}
|
| 65 |
-
}
|
| 66 |
-
);
|
| 67 |
-
});
|
| 68 |
-
|
| 69 |
-
log.debug(`Last Entry in Cache: ${lastEntry ? `Page ${lastEntry.page}, Skip ${lastEntry.skip}` : 'None'}`);
|
| 70 |
-
|
| 71 |
-
if (lastEntry) {
|
| 72 |
-
return lastEntry.page + 1;
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
log.debug('Default to Page 1');
|
| 76 |
-
return 1;
|
| 77 |
-
} catch (error) {
|
| 78 |
-
log.error('Error in determinePageFromSkip:', error);
|
| 79 |
-
return 1;
|
| 80 |
-
}
|
| 81 |
};
|
| 82 |
|
| 83 |
const fetchData = async (endpoint, params = {}, tmdbApiKey = null, providerId = null, ageRange = null) => {
|
|
@@ -92,7 +50,7 @@ const fetchData = async (endpoint, params = {}, tmdbApiKey = null, providerId =
|
|
| 92 |
const queryParamsWithPage = {
|
| 93 |
...queryParams,
|
| 94 |
page,
|
| 95 |
-
language: params.language,
|
| 96 |
type,
|
| 97 |
};
|
| 98 |
|
|
@@ -127,66 +85,18 @@ const fetchData = async (endpoint, params = {}, tmdbApiKey = null, providerId =
|
|
| 127 |
return data;
|
| 128 |
};
|
| 129 |
|
| 130 |
-
const fetchStreamingLinks = async (tmdbId, type, tmdbApiKey, region = 'US') => {
|
| 131 |
-
try {
|
| 132 |
-
const endpoint = `/${type}/${tmdbId}/watch/providers`;
|
| 133 |
-
const url = `${TMDB_BASE_URL}${endpoint}`;
|
| 134 |
-
const cachedData = await getCache(url);
|
| 135 |
-
|
| 136 |
-
if (cachedData) return cachedData;
|
| 137 |
-
|
| 138 |
-
const data = await makeRequest(url, tmdbApiKey);
|
| 139 |
-
const providers = data.results?.[region]?.flatrate || [];
|
| 140 |
-
await setCache(url, providers);
|
| 141 |
-
|
| 142 |
-
return providers;
|
| 143 |
-
} catch (error) {
|
| 144 |
-
log.error(`Failed to fetch streaming links for ${type} ID ${tmdbId}: ${error.message}`);
|
| 145 |
-
return [];
|
| 146 |
-
}
|
| 147 |
-
};
|
| 148 |
-
|
| 149 |
-
const discoverContentWithStreamingLinks = async (params, tmdbApiKey) => {
|
| 150 |
-
const { type, region, language, skip, sort_by, genre, year, rating } = params;
|
| 151 |
-
|
| 152 |
-
const content = await fetchData(
|
| 153 |
-
`/discover/${type}`,
|
| 154 |
-
{
|
| 155 |
-
sort_by,
|
| 156 |
-
with_genres: genre,
|
| 157 |
-
region,
|
| 158 |
-
language,
|
| 159 |
-
skip,
|
| 160 |
-
year,
|
| 161 |
-
rating,
|
| 162 |
-
},
|
| 163 |
-
tmdbApiKey
|
| 164 |
-
);
|
| 165 |
-
|
| 166 |
-
for (const item of content.results) {
|
| 167 |
-
item.streamingLinks = await fetchStreamingLinks(
|
| 168 |
-
item.id,
|
| 169 |
-
type,
|
| 170 |
-
tmdbApiKey,
|
| 171 |
-
region
|
| 172 |
-
);
|
| 173 |
-
}
|
| 174 |
-
|
| 175 |
-
return content;
|
| 176 |
-
};
|
| 177 |
-
|
| 178 |
const fetchGenres = async (type, language, tmdbApiKey) => {
|
| 179 |
const mediaType = type === 'series' ? 'tv' : 'movie';
|
| 180 |
const endpoint = `/genre/${mediaType}/list`;
|
| 181 |
|
| 182 |
try {
|
| 183 |
const params = {
|
| 184 |
-
language,
|
| 185 |
api_key: tmdbApiKey
|
| 186 |
};
|
| 187 |
|
| 188 |
const genresData = await fetchData(endpoint, params, tmdbApiKey);
|
| 189 |
-
log.debug(`Genres retrieved for ${type} (${language})`);
|
| 190 |
return genresData.genres;
|
| 191 |
} catch (error) {
|
| 192 |
log.error(`Error fetching genres from TMDB: ${error.message}`);
|
|
@@ -214,5 +124,6 @@ module.exports = {
|
|
| 214 |
fetchStreamingLinks,
|
| 215 |
discoverContentWithStreamingLinks,
|
| 216 |
fetchGenres,
|
| 217 |
-
fetchAndStoreGenres
|
|
|
|
| 218 |
};
|
|
|
|
| 28 |
});
|
| 29 |
};
|
| 30 |
|
| 31 |
+
const checkGenresExistForLanguage = async (language) => {
|
| 32 |
+
return new Promise((resolve, reject) => {
|
| 33 |
+
genresDb.get(
|
| 34 |
+
`SELECT 1 FROM genres WHERE language = ? LIMIT 1`,
|
| 35 |
+
[language],
|
| 36 |
+
(err, row) => (err ? reject(err) : resolve(!!row))
|
| 37 |
+
);
|
| 38 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
};
|
| 40 |
|
| 41 |
const fetchData = async (endpoint, params = {}, tmdbApiKey = null, providerId = null, ageRange = null) => {
|
|
|
|
| 50 |
const queryParamsWithPage = {
|
| 51 |
...queryParams,
|
| 52 |
page,
|
| 53 |
+
language: params.language || 'en', // Default language to 'en'
|
| 54 |
type,
|
| 55 |
};
|
| 56 |
|
|
|
|
| 85 |
return data;
|
| 86 |
};
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
const fetchGenres = async (type, language, tmdbApiKey) => {
|
| 89 |
const mediaType = type === 'series' ? 'tv' : 'movie';
|
| 90 |
const endpoint = `/genre/${mediaType}/list`;
|
| 91 |
|
| 92 |
try {
|
| 93 |
const params = {
|
| 94 |
+
language: language || 'en', // Default to 'en'
|
| 95 |
api_key: tmdbApiKey
|
| 96 |
};
|
| 97 |
|
| 98 |
const genresData = await fetchData(endpoint, params, tmdbApiKey);
|
| 99 |
+
log.debug(`Genres retrieved for ${type} (${language || 'en'})`);
|
| 100 |
return genresData.genres;
|
| 101 |
} catch (error) {
|
| 102 |
log.error(`Error fetching genres from TMDB: ${error.message}`);
|
|
|
|
| 124 |
fetchStreamingLinks,
|
| 125 |
discoverContentWithStreamingLinks,
|
| 126 |
fetchGenres,
|
| 127 |
+
fetchAndStoreGenres,
|
| 128 |
+
checkGenresExistForLanguage
|
| 129 |
};
|