mikmc commited on
Commit
76042b9
·
verified ·
1 Parent(s): b3196c8

Update commons/queryBuilder.js

Browse files
Files changed (1) hide show
  1. commons/queryBuilder.js +59 -54
commons/queryBuilder.js CHANGED
@@ -1,65 +1,70 @@
1
  const axios = require('axios');
2
 
3
- /**
4
- * Builds and executes a Lucene query for Archive.org with correct field parameters.
5
- */
6
- module.exports = async (args) => {
7
- const query = {};
8
-
9
- console.log("Incoming arguments for queryBuilder:", args);
10
-
11
- if (args.extra) {
12
- Object.keys(args.extra).forEach((key) => {
13
- let value = args.extra[key];
14
- if (typeof value !== 'string') value = String(value);
15
- if (value && value.trim().length > 0) {
16
- query[key] = value;
17
  }
18
- });
19
- }
20
-
21
- console.log("Query constructed:", query);
 
 
 
 
 
22
 
23
- // Define genre with fallback
24
- const genre = query.genre || 'General';
 
25
 
26
- // Construct Lucene query
27
- const luceneQuery = `(title:${genre}^100 OR subject:${genre}^25 OR description:${genre}^15) AND mediatype:movies AND !collection:(podcasts OR radio OR uspto)`;
28
 
29
- // Archive.org Search API URL
30
- const searchUrl = 'https://archive.org/advancedsearch.php';
 
 
 
 
 
 
31
 
32
- // Set request parameters without fl to avoid invalid request error
33
- const params = {
34
- q: luceneQuery,
35
- rows: 50,
36
- output: 'json',
37
- page: 1,
38
- };
 
 
 
39
 
40
- try {
41
- // Fetch response and ensure async resolution
42
- const response = await axios.get(searchUrl, { params });
43
- const data = response.data;
44
-
45
- // Process the response for Stremio
46
- const catalogItems = data.response.docs.map((doc) => ({
47
- id: `archorg.addon:${doc.identifier}`, // Prefixing with idPrefixes
48
- name: doc.title || "Untitled",
49
- description: doc.description || "No description available.",
50
- poster: `https://archive.org/services/img/${doc.identifier}`, // Poster image
51
- background: `https://archive.org/services/img/${doc.identifier}`, // Background image
52
- type: 'movie',
53
- genre: genre
54
- }));
55
 
56
- console.log("Formatted Catalog Items for Stremio:", catalogItems);
57
-
58
- // Return structured response for Stremio
59
- return { metas: catalogItems };
 
 
60
 
61
- } catch (error) {
62
- console.error("Error fetching data from Archive.org:", error.message);
63
- throw error;
64
- }
65
  };
 
 
 
1
  const axios = require('axios');
2
 
3
+ // Dependency setup
4
+ const dependencies = {
5
+ httpClient: axios,
6
+ queryBuilder: (params) => {
7
+ const queryParts = [];
8
+ for (const key in params) {
9
+ if (params[key]) {
10
+ queryParts.push(`${key}:${params[key]}`);
 
 
 
 
 
 
11
  }
12
+ }
13
+ return queryParts.join(' AND ');
14
+ },
15
+ env: {
16
+ url: {
17
+ search: 'https://archive.org/advancedsearch.php',
18
+ },
19
+ },
20
+ };
21
 
22
+ // Catalog function
23
+ const Catalog = (dependencies) => {
24
+ const { httpClient, queryBuilder, env } = dependencies;
25
 
26
+ const searchCatalog = async (request) => {
27
+ const { search, genre, skip } = request;
28
 
29
+ try {
30
+ // Build the query using the queryBuilder
31
+ const query = queryBuilder({
32
+ collection: 'feature_films',
33
+ mediatype: 'movies',
34
+ search: search ? `${search}*` : null,
35
+ subject: genre,
36
+ });
37
 
38
+ // Make the API request
39
+ const response = await httpClient.get(env.url.search, {
40
+ params: {
41
+ rows: (+skip || 0) + 50,
42
+ sort: 'num_reviews desc,avg_rating desc',
43
+ fields: 'avg_rating,description,genre,identifier,title,subject',
44
+ q: query,
45
+ output: 'json',
46
+ },
47
+ });
48
 
49
+ // Format the results for Stremio
50
+ const catalogItems = response.data.response.docs.map((doc) => ({
51
+ id: `archorg.addon:${doc.identifier}`,
52
+ name: doc.title || "Untitled",
53
+ description: doc.description || "No description available.",
54
+ poster: `https://archive.org/services/img/${doc.identifier}`,
55
+ background: `https://archive.org/services/img/${doc.identifier}`,
56
+ type: 'movie',
57
+ genre: genre,
58
+ }));
 
 
 
 
 
59
 
60
+ return { metas: catalogItems };
61
+ } catch (error) {
62
+ console.error("Error in searchCatalog:", error.message);
63
+ return { metas: [] };
64
+ }
65
+ };
66
 
67
+ return { searchCatalog };
 
 
 
68
  };
69
+
70
+ module.exports = Catalog(dependencies);