mikmc commited on
Commit
0a37b1a
·
verified ·
1 Parent(s): 6a9d0b8

Update commons/queryBuilder.js

Browse files
Files changed (1) hide show
  1. commons/queryBuilder.js +30 -8
commons/queryBuilder.js CHANGED
@@ -1,9 +1,9 @@
1
- const { convert } = require('lucene-query-generator');
2
 
3
  /**
4
- * Builds a complex Lucene query formatted for Archive.org based on arguments.
5
  */
6
- module.exports = (args) => {
7
  const query = {};
8
 
9
  console.log("Incoming arguments for queryBuilder:", args);
@@ -20,13 +20,35 @@ module.exports = (args) => {
20
 
21
  console.log("Query constructed:", query);
22
 
23
- // Check for genre or fallback to a default if not provided
24
  const genre = query.genre || 'General';
25
 
26
- // Construct query focused only on supported fields
27
- const formattedQuery = `((title:${genre}^100 OR subject:${genre}^25 OR description:${genre}^15) AND mediatype:movies AND !collection:(podcasts OR radio OR uspto))`;
28
 
29
- console.log("Formatted Lucene Query:", formattedQuery);
 
30
 
31
- return formattedQuery;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  };
 
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);
 
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, including fl and removing callback
33
+ const params = {
34
+ q: luceneQuery,
35
+ fl: 'name,title,description', // Specify necessary fields only
36
+ rows: 50,
37
+ output: 'json',
38
+ page: 1,
39
+ };
40
+
41
+ try {
42
+ // Fetch response
43
+ const response = await axios.get(searchUrl, { params });
44
+ const data = response.data;
45
+
46
+ // Process JSON response
47
+ console.log("API Response:", data);
48
+ return data.response.docs; // Returns only the documents section
49
+
50
+ } catch (error) {
51
+ console.error("Error fetching data from Archive.org:", error.message);
52
+ throw error; // Handle this higher up or log for debugging
53
+ }
54
  };