The searchAll
function is a main exported function that retrieves search results from multiple search engines in parallel using the multiCrawl
function, and saves the results to a JSON file in the user's Collections/searches
directory. The function takes an optional query
parameter and returns a promise that resolves to an object containing search results, with the file name constructed from the query string and the current date.
npm run import -- "meta search all"
var fs = require('fs');
var path = require('path');
var importer = require('../Core');
var multiCrawl = importer.import("multi crawl");
// http://www.exploratorium.edu/files/ronh/research/index.html
/*
Exploratorium
Search
Google
Search
alltheweb
Search
Teoma
Search
AltaVista
Search
Wisenut
Search
HotBot
Search
lii.org
Search
Northern Light
Search
Lycos
Search
Scirus
Meta:
Ask Jeeves
Search
MetaCrawler
Search
Dogpile
Search
SavvySearch
*/
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
var project = path.join(PROFILE_PATH, 'Collections/searches');
function searchAll(query = 'search engine') {
var engines = [
'https://www.google.com/search?q=' + query,
'https://www.bing.com/search?q=' + query,
'https://search.yahoo.com/search?p=' + query,
'https://www.ask.com/web?q=' + query,
'https://search.aol.com/aol/search?q=' + query,
'http://www.baidu.com/s?wd=' + query,
'https://www.wolframalpha.com/input/?i=' + query,
'https://duckduckgo.com/?q=' + query,
'https://www.yandex.com/search/?text=' + query,
'https://archive.org/search.php?query=' + query,
];
// TODO: save results
return multiCrawl(engines, 'search results json')
.then(r => {
const time = new Date();
fs.writeFileSync(path.join(project, query.replace(/[^a-z0-9]/ig, '_')
+ '-' + time.getFullYear()
+ '-' + (time.getMonth() + 1)
+ '-' + time.getDate()
+ '.json'), JSON.stringify(r, null, 4));
return r;
})
}
module.exports = searchAll;
if(typeof $ !== 'undefined') {
$.async();
searchAll()
.then(r => $.sendResult(r))
.catch(e => $.sendError(e))
}
const fs = require('fs').promises;
const path = require('path');
const multiCrawl = require('../Core').import('multi crawl');
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
const PROJECT_PATH = path.join(PROFILE_PATH, 'Collections','searches');
async function searchAll(query ='search engine') {
try {
const engines = [
'https://www.google.com/search?q=' + encodeURIComponent(query),
'https://www.bing.com/search?q=' + encodeURIComponent(query),
'https://search.yahoo.com/search?p=' + encodeURIComponent(query),
'https://www.ask.com/web?q=' + encodeURIComponent(query),
'https://search.aol.com/aol/search?q=' + encodeURIComponent(query),
'http://www.baidu.com/s?wd=' + encodeURIComponent(query),
'https://www.wolframalpha.com/input/?i=' + encodeURIComponent(query),
'https://duckduckgo.com/?q=' + encodeURIComponent(query),
'https://www.yandex.com/search/?text=' + encodeURIComponent(query),
'https://archive.org/search.php?query=' + encodeURIComponent(query),
];
const results = await multiCrawl(engines,'search results json');
const timestamp = new Date().toISOString().replace(/:/g, '-').replace('.', '-');
const filename = query.replace(/[^a-z0-9]/ig, '_') + '-' + timestamp + '.json';
const filePath = path.join(PROJECT_PATH, filename);
await fs.writeFile(filePath, JSON.stringify(results, null, 4));
return results;
} catch (error) {
throw error;
}
}
module.exports = searchAll;
if (typeof $!== 'undefined') {
$.async();
searchAll()
.then((results) => $.sendResult(results))
.catch((error) => $.sendError(error));
}
Code Breakdown
fs
(File System) modulepath
moduleCore
located at ../Core
multiCrawl
function from the Core
module using importer.import('multi crawl')
PROFILE_PATH
is set to the user's home directory using various environment variables (HOME
, HOMEPATH
, USERPROFILE
)project
is the path to a directory named Collections/searches
within the user's home directoryengines
array contains URLs for various search enginessearchAll
Functionquery
parameter with a default value of 'search engine'
multiCrawl
function to crawl the search engines in parallel and retrieve their resultsfs
and path
query
string with underscores, and appending the current date and yearCollections/searches
directorysearchAll
function is exported as a module$
object is defined, it is used to send the search results or an error message