mirror of
https://github.com/alexankitty/Myrient-Search-Engine.git
synced 2026-01-15 16:33:15 -03:00
Working search if you provide your own file list
This commit is contained in:
6
lib/loadfiles.js
Normal file
6
lib/loadfiles.js
Normal file
@@ -0,0 +1,6 @@
|
||||
import { readFile } from 'fs/promises';
|
||||
|
||||
export default async function parseJsonFile(filePath) {
|
||||
let data = JSON.parse(await readFile(filePath, "utf8"));
|
||||
return data
|
||||
}
|
||||
15
lib/search.js
Normal file
15
lib/search.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import Fuse from 'fuse.js'
|
||||
|
||||
export default function fuzzySearch(fileArr, query){
|
||||
const leniency = parseInt(process.env.FUZZY_LENIENCY)
|
||||
const options = {
|
||||
findAllMatches: true,
|
||||
threshold: leniency,
|
||||
ignoreLocation: true,
|
||||
includeScore: true,
|
||||
ignoreFieldNorm: true,
|
||||
keys: ['name']
|
||||
}
|
||||
const fuse = new Fuse(fileArr, options)
|
||||
return fuse.search(query)
|
||||
}
|
||||
15
package-lock.json
generated
15
package-lock.json
generated
@@ -8,7 +8,7 @@
|
||||
"dotenv": "^16.4.5",
|
||||
"ejs": "^3.1.10",
|
||||
"express": "^4.21.1",
|
||||
"fuzzystring": "^1.0.2",
|
||||
"fuse.js": "^7.0.0",
|
||||
"jsdom": "^25.0.1",
|
||||
"node-cron": "^3.0.3",
|
||||
"node-fetch": "^3.3.2"
|
||||
@@ -601,11 +601,14 @@
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/fuzzystring": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/fuzzystring/-/fuzzystring-1.0.2.tgz",
|
||||
"integrity": "sha512-5L1DG0o2aHPXXeOfs+MBPp/+oZdt8t6GuvXYvfhxdY9n1xnwnY/nHzvUWqsrd8NSiiMaPaiseEQJDg8pwMfLBA==",
|
||||
"license": "MIT"
|
||||
"node_modules/fuse.js": {
|
||||
"version": "7.0.0",
|
||||
"resolved": "https://registry.npmjs.org/fuse.js/-/fuse.js-7.0.0.tgz",
|
||||
"integrity": "sha512-14F4hBIxqKvD4Zz/XjDc3y94mNZN6pRv3U13Udo0lNLCWRBUsrMv2xwcF/y/Z5sV6+FQW+/ow68cHpm4sunt8Q==",
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/get-intrinsic": {
|
||||
"version": "1.2.4",
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"dotenv": "^16.4.5",
|
||||
"ejs": "^3.1.10",
|
||||
"express": "^4.21.1",
|
||||
"fuzzystring": "^1.0.2",
|
||||
"fuse.js": "^7.0.0",
|
||||
"jsdom": "^25.0.1",
|
||||
"node-cron": "^3.0.3",
|
||||
"node-fetch": "^3.3.2"
|
||||
|
||||
14
server.js
14
server.js
@@ -1,6 +1,11 @@
|
||||
import getAllFiles from './lib/dirwalk.js'
|
||||
import parseJsonFile from './lib/loadfiles.js'
|
||||
import fuzzySearch from './lib/search.js'
|
||||
import 'dotenv/config'
|
||||
import express from 'express'
|
||||
|
||||
var fileList = await parseJsonFile('./filelist.json')
|
||||
|
||||
var app = express();
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
@@ -11,10 +16,15 @@ app.get('/', function(req, res) {
|
||||
})
|
||||
|
||||
app.get('/search', function(req, res) {
|
||||
let results = fuzzySearch(fileList, req.query.q)
|
||||
console.log(results)
|
||||
res.render('pages/index', {
|
||||
page: 'results'
|
||||
page: 'results',
|
||||
query: req.query.q,
|
||||
results: results
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(process.env.PORT)
|
||||
console.log(`Listening on ${process.env.PORT}.`)
|
||||
console.log(`Listening on ${process.env.PORT}.`)
|
||||
console.log(`Loaded ${fileList.length} known files.`)
|
||||
@@ -10,7 +10,7 @@ _ / / / _ /_/ /_ / _ / / __/ / / / /_
|
||||
/_/ /_/ _\__, / /_/ /_/ \___//_/ /_/\__/
|
||||
/____/
|
||||
</pre>
|
||||
<input type="text" class="w-50 form-control bg-dark text-white" name="q">
|
||||
<input type="text" class="w-50 form-control bg-dark text-white" name="q" value="<%= query %>"">
|
||||
<button type="submit" class="btn btn-secondary">Search</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -23,7 +23,29 @@ _ / / / _ /_/ /_ / _ / / __/ / / / /_
|
||||
<th>Category</th>
|
||||
<th>Size</th>
|
||||
<th>Date</th>
|
||||
<th>Match %</th>
|
||||
</tr>
|
||||
<% for (let x = 0; x < results.length; x++) { %>
|
||||
<tr>
|
||||
<td>
|
||||
<a href="<%= results[x].item.path %>">
|
||||
<%= results[x].item.name %>
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
|
||||
</td>
|
||||
<td>
|
||||
<%= results[x].item.size %>
|
||||
</td>
|
||||
<td>
|
||||
<%= results[x].item.date %>
|
||||
</td>
|
||||
<td>
|
||||
<%= 1 - results[x].score %>%
|
||||
</td>
|
||||
</tr>
|
||||
<% } %>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user