Added cli tool for adding new search engines/providers

This commit is contained in:
unknown 2021-08-06 16:16:13 +02:00
parent 1699146f79
commit 683c948f6c
6 changed files with 96 additions and 23 deletions

8
.prettierrc Normal file
View file

@ -0,0 +1,8 @@
{
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"arrowParens": "always",
"printWidth": 80,
"trailingComma": "es5"
}

3
api.js
View file

@ -9,8 +9,7 @@ api.use(express.static(join(__dirname, 'public')));
api.use('/uploads', express.static(join(__dirname, 'data/uploads'))); api.use('/uploads', express.static(join(__dirname, 'data/uploads')));
api.get(/^\/(?!api)/, (req, res) => { api.get(/^\/(?!api)/, (req, res) => {
res.sendFile(join(__dirname, 'public/index.html')); res.sendFile(join(__dirname, 'public/index.html'));
}) });
// Body parser // Body parser
api.use(express.json()); api.use(express.json());

View file

@ -12113,6 +12113,12 @@
"resolved": "https://registry.npmjs.org/prepend-http/-/prepend-http-1.0.4.tgz", "resolved": "https://registry.npmjs.org/prepend-http/-/prepend-http-1.0.4.tgz",
"integrity": "sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw=" "integrity": "sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw="
}, },
"prettier": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz",
"integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==",
"dev": true
},
"pretty-bytes": { "pretty-bytes": {
"version": "5.6.0", "version": "5.6.0",
"resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.6.0.tgz", "resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.6.0.tgz",

View file

@ -54,5 +54,8 @@
"last 1 firefox version", "last 1 firefox version",
"last 1 safari version" "last 1 safari version"
] ]
},
"devDependencies": {
"prettier": "^2.3.2"
} }
} }

View file

@ -1,9 +1,9 @@
{ {
"queries": [ "queries": [
{ {
"name": "Google", "name": "Disroot",
"prefix": "g", "prefix": "ds",
"template": "https://www.google.com/search?q=" "template": "http://search.disroot.org/search?q="
}, },
{ {
"name": "DuckDuckGo", "name": "DuckDuckGo",
@ -11,19 +11,9 @@
"template": "https://duckduckgo.com/?q=" "template": "https://duckduckgo.com/?q="
}, },
{ {
"name": "Disroot", "name": "Google",
"prefix": "ds", "prefix": "g",
"template": "http://search.disroot.org/search?q=" "template": "https://www.google.com/search?q="
},
{
"name": "YouTube",
"prefix": "yt",
"template": "https://www.youtube.com/results?search_query="
},
{
"name": "Reddit",
"prefix": "r",
"template": "https://www.reddit.com/search?q="
}, },
{ {
"name": "IMDb", "name": "IMDb",
@ -31,14 +21,24 @@
"template": "https://www.imdb.com/find?q=" "template": "https://www.imdb.com/find?q="
}, },
{ {
"name": "The Movie Database", "name": "Reddit",
"prefix": "mv", "prefix": "r",
"template": "https://www.themoviedb.org/search?query=" "template": "https://www.reddit.com/search?q="
}, },
{ {
"name": "Spotify", "name": "Spotify",
"prefix": "sp", "prefix": "sp",
"template": "https://open.spotify.com/search/" "template": "https://open.spotify.com/search/"
},
{
"name": "The Movie Database",
"prefix": "mv",
"template": "https://www.themoviedb.org/search?query="
},
{
"name": "YouTube",
"prefix": "yt",
"template": "https://www.youtube.com/results?search_query="
} }
] ]
} }

View file

@ -0,0 +1,57 @@
const queries = require('../../src/utility/searchQueries.json');
const fs = require('fs');
const prettier = require('prettier');
/**
* @description CLI tool for adding new search engines/providers. It will ensure that prefix is unique and that all entries are sorted alphabetically
* @argumens name prefix template
* @example node cli-searchQueries.js "DuckDuckGo" "d" "https://duckduckgo.com/?q="
*/
// Get arguments
const args = process.argv.slice(2);
// Check arguments
if (args.length < 3) {
return console.log('Missing arguments');
} else if (args.length > 3) {
return console.log('Too many arguments provided');
}
// Construct new query object
const newQuery = {
name: args[0],
prefix: args[1],
template: args[2],
};
// Get old queries
let rawQueries = queries.queries;
let parsedQueries = '';
// Check if prefix is unique
const isUnique = !rawQueries.find((query) => query.prefix == newQuery.prefix);
if (!isUnique) {
return console.log('Prefix already exists');
}
// Add new query
rawQueries.push(newQuery);
// Sort alphabetically
rawQueries = rawQueries.sort((a, b) => {
const _a = a.name.toLowerCase();
const _b = b.name.toLowerCase();
if (_a < _b) return -1;
if (_a > _b) return 1;
return 0;
});
// Format JSON
parsedQueries = JSON.stringify(queries);
parsedQueries = prettier.format(parsedQueries, { parser: 'json' });
// Save file
fs.writeFileSync('../../src/utility/searchQueries.json', parsedQueries);