mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-14 21:38:38 +00:00
added server side search
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
# CONFIGURATION
|
||||
JSON_DB = '/var/log/share_scan.json'
|
||||
SHARE_DIR = Path('/var/www/html/share')
|
||||
BASE_URL = 'https://czernobog.pl/share'
|
||||
|
||||
# Ensure share directory exists
|
||||
SHARE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def load_db():
|
||||
with open(JSON_DB) as f:
|
||||
return json.load(f)['entries']
|
||||
|
||||
ENTRIES = load_db()
|
||||
|
||||
def relevancy(path, keywords):
|
||||
score = 0
|
||||
low = path.lower()
|
||||
for kw in keywords:
|
||||
if kw.lower() in low:
|
||||
score += low.count(kw.lower())
|
||||
return score
|
||||
|
||||
def find_matches(count, keywords):
|
||||
scored = []
|
||||
for e in ENTRIES:
|
||||
score = relevancy(e['path'], keywords)
|
||||
if score > 0:
|
||||
scored.append((score, e['path']))
|
||||
scored.sort(reverse=True, key=lambda x: x[0])
|
||||
result = [p for _, p in scored]
|
||||
return result[:count]
|
||||
|
||||
def publish(paths):
|
||||
urls = []
|
||||
for path in paths:
|
||||
token = uuid.uuid4().hex
|
||||
link = SHARE_DIR / token
|
||||
try:
|
||||
os.symlink(path, link)
|
||||
except FileExistsError:
|
||||
pass
|
||||
urls.append(f"{BASE_URL}/{token}")
|
||||
return urls
|
||||
|
||||
Reference in New Issue
Block a user