Spaces:
Paused
Paused
Update commons/queryBuilder.js
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465const axios = require('axios');/*** Builds and executes a Lucene query for Archive.org with correct field parameters.*/module.exports = async (args) => {const query = {};console.log("Incoming arguments for queryBuilder:", args);if (args.extra) {Object.keys(args.extra).forEach((key) => {let value = args.extra[key];if (typeof value !== 'string') value = String(value);if (value && value.trim().length > 0) {query[key] = value;}});}console.log("Query constructed:", query);// Define genre with fallbackconst genre = query.genre || 'General';// Construct Lucene queryconst luceneQuery = `(title:${genre}^100 OR subject:${genre}^25 OR description:${genre}^15) AND mediatype:movies AND !collection:(podcasts OR radio OR uspto)`;// Archive.org Search API URLconst searchUrl = 'https://archive.org/advancedsearch.php';// Set request parameters without fl to avoid invalid request errorconst params = {q: luceneQuery,rows: 50,output: 'json',page: 1,};try {// Fetch response and ensure async resolutionconst response = await axios.get(searchUrl, { params });const data = response.data;// Process the response for Stremioconst catalogItems = data.response.docs.map((doc) => ({id: `archorg.addon:${doc.identifier}`, // Prefixing with idPrefixesname: doc.title || "Untitled",description: doc.description || "No description available.",poster: `https://archive.org/services/img/${doc.identifier}`, // Poster imagebackground: `https://archive.org/services/img/${doc.identifier}`, // Background imagetype: 'movie',genre: genre}));console.log("Formatted Catalog Items for Stremio:", catalogItems);// Return structured response for Stremioreturn { metas: catalogItems };} catch (error) {console.error("Error fetching data from Archive.org:", error.message);throw error;}};12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970const axios = require('axios');// Dependency setupconst dependencies = {httpClient: axios,queryBuilder: (params) => {const queryParts = [];for (const key in params) {if (params[key]) {queryParts.push(`${key}:${params[key]}`);}}return queryParts.join(' AND ');},env: {url: {search: 'https://archive.org/advancedsearch.php',},},};// Catalog functionconst Catalog = (dependencies) => {const { httpClient, queryBuilder, env } = dependencies;const searchCatalog = async (request) => {const { search, genre, skip } = request;try {// Build the query using the queryBuilderconst query = queryBuilder({collection: 'feature_films',mediatype: 'movies',search: search ? `${search}*` : null,subject: genre,});// Make the API requestconst response = await httpClient.get(env.url.search, {params: {rows: (+skip || 0) + 50,sort: 'num_reviews desc,avg_rating desc',fields: 'avg_rating,description,genre,identifier,title,subject',q: query,output: 'json',},});// Format the results for Stremioconst 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,}));return { metas: catalogItems };} catch (error) {console.error("Error in searchCatalog:", error.message);return { metas: [] };}};return { searchCatalog };};module.exports = Catalog(dependencies);
commons/queryBuilder.js+59-54