Spaces:
Paused
Paused
Update use-cases/catalog.js
Browse files- use-cases/catalog.js +34 -17
use-cases/catalog.js
CHANGED
|
@@ -1,39 +1,56 @@
|
|
| 1 |
const Catalog = (dependencies) => {
|
| 2 |
-
const { httpClient, queryBuilder, env } = dependencies
|
| 3 |
-
|
| 4 |
-
// https://archive.org/advancedsearch.php?q=collection%3A%28feature_films%29+AND+mediatype%3A%28movies%29&fl%5B%5D=avg_rating&fl%5B%5D=description&fl%5B%5D=genre&fl%5B%5D=headerImage&fl%5B%5D=identifier&fl%5B%5D=publicdate&fl%5B%5D=rights&fl%5B%5D=subject&fl%5B%5D=title&sort%5B%5D=&sort%5B%5D=&sort%5B%5D=&rows=50&page=1&output=json
|
| 5 |
|
| 6 |
const searchCatalog = async (request) => {
|
| 7 |
-
const { search, genre, skip } = request
|
| 8 |
|
| 9 |
try {
|
|
|
|
| 10 |
const query = queryBuilder({
|
| 11 |
collection: 'feature_films',
|
| 12 |
mediatype: 'movies',
|
| 13 |
search: search ? `${search}*` : null,
|
| 14 |
subject: genre,
|
| 15 |
-
})
|
| 16 |
|
|
|
|
| 17 |
const response = await httpClient.get(env.url.search, {
|
| 18 |
params: {
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
fields:
|
| 23 |
-
'avg_rating,creator,date,description,genre,identifier,language,subject,title,type',
|
| 24 |
q: query,
|
|
|
|
| 25 |
},
|
| 26 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
} catch (error) {
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
-
}
|
| 33 |
|
| 34 |
return {
|
| 35 |
searchCatalog,
|
| 36 |
-
}
|
| 37 |
-
}
|
| 38 |
|
| 39 |
-
module.exports = Catalog
|
|
|
|
| 1 |
const Catalog = (dependencies) => {
|
| 2 |
+
const { httpClient, queryBuilder, env } = dependencies;
|
|
|
|
|
|
|
| 3 |
|
| 4 |
const searchCatalog = async (request) => {
|
| 5 |
+
const { search, genre, skip } = request;
|
| 6 |
|
| 7 |
try {
|
| 8 |
+
// Build the query using the queryBuilder
|
| 9 |
const query = queryBuilder({
|
| 10 |
collection: 'feature_films',
|
| 11 |
mediatype: 'movies',
|
| 12 |
search: search ? `${search}*` : null,
|
| 13 |
subject: genre,
|
| 14 |
+
});
|
| 15 |
|
| 16 |
+
// Make the API request to Archive.org using the built query
|
| 17 |
const response = await httpClient.get(env.url.search, {
|
| 18 |
params: {
|
| 19 |
+
rows: (+skip || 0) + 50, // Adjusted to fetch 50 results per page (can increase this as needed)
|
| 20 |
+
sort: 'num_reviews desc, avg_rating desc', // Sort by number of reviews and rating
|
| 21 |
+
fields: 'avg_rating,description,genre,identifier,title,subject', // Fetch relevant fields
|
|
|
|
|
|
|
| 22 |
q: query,
|
| 23 |
+
output: 'json',
|
| 24 |
},
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
// Check if response data is valid and return catalog items
|
| 28 |
+
if (response.data && response.data.response && response.data.response.docs) {
|
| 29 |
+
const catalogItems = response.data.response.docs.map((doc) => ({
|
| 30 |
+
id: `archorg.addon:${doc.identifier}`,
|
| 31 |
+
name: doc.title || "Untitled",
|
| 32 |
+
description: doc.description || "No description available",
|
| 33 |
+
poster: `https://archive.org/services/img/${doc.identifier}`,
|
| 34 |
+
background: `https://archive.org/services/img/${doc.identifier}`,
|
| 35 |
+
type: 'movie',
|
| 36 |
+
genre: genre || "Unknown", // Default to "Unknown" if no genre is provided
|
| 37 |
+
}));
|
| 38 |
|
| 39 |
+
return { metas: catalogItems };
|
| 40 |
+
} else {
|
| 41 |
+
// Return an empty catalog if response is not as expected
|
| 42 |
+
return { metas: [] };
|
| 43 |
+
}
|
| 44 |
} catch (error) {
|
| 45 |
+
// Return empty catalog in case of an error and log the error for debugging
|
| 46 |
+
console.error('Error fetching catalog:', error.message || error);
|
| 47 |
+
return { metas: [] };
|
| 48 |
}
|
| 49 |
+
};
|
| 50 |
|
| 51 |
return {
|
| 52 |
searchCatalog,
|
| 53 |
+
};
|
| 54 |
+
};
|
| 55 |
|
| 56 |
+
module.exports = Catalog;
|