const Catalog = (dependencies) => { const { httpClient, queryBuilder, env } = dependencies; const searchCatalog = async (request) => { const { search, genre, skip } = request; try { // Build the query using the queryBuilder const query = queryBuilder({ collection: 'feature_films', mediatype: 'movies', search: search ? `${search}*` : null, subject: genre, }); // Make the API request to Archive.org using the built query const response = await httpClient.get(env.url.search, { params: { rows: (+skip || 0) + 50, // Adjusted to fetch 50 results per page (can increase this as needed) sort: 'num_reviews desc, avg_rating desc', // Sort by number of reviews and rating fields: 'avg_rating,description,genre,identifier,title,subject', // Fetch relevant fields q: query, output: 'json', }, }); // Check if response data is valid and return catalog items if (response.data && response.data.response && response.data.response.docs) { const catalogItems = response.data.response.docs.map((doc) => ({ id: `archorg.addon:${doc.identifier}`, name: doc.title || "Untitled", description: doc.description || "No description available", poster: `https://archive.org/services/img/${doc.identifier}`, background: `https://archive.org/services/img/${doc.identifier}`, type: 'movie', genre: genre || "Unknown", // Default to "Unknown" if no genre is provided })); return { metas: catalogItems }; } else { // Return an empty catalog if response is not as expected return { metas: [] }; } } catch (error) { // Return empty catalog in case of an error and log the error for debugging console.error('Error fetching catalog:', error.message || error); return { metas: [] }; } }; return { searchCatalog, }; }; module.exports = Catalog;