Spaces:
Paused
Paused
Update commons/queryBuilder.js
Browse files- commons/queryBuilder.js +30 -8
commons/queryBuilder.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
const
|
| 2 |
|
| 3 |
/**
|
| 4 |
-
* Builds
|
| 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 |
-
//
|
| 24 |
const genre = query.genre || 'General';
|
| 25 |
|
| 26 |
-
// Construct
|
| 27 |
-
const
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
};
|