Files
Local-Booru/system_module/db.py
T
Hell13Cat 270be38d1e 0.0.1s
2026-05-26 22:53:39 +03:00

54 lines
1.5 KiB
Python

from tinydb import TinyDB, Query
import os
import json
from collections import Counter
class DB:
def __init__(self):
db_path = "datas"
if not os.path.exists(db_path):
os.makedirs(os.path.dirname(db_path), exist_ok=True)
json.dump({}, open(db_path+"/db.json", "w", encoding="utf-8"))
self.db = TinyDB(db_path+"/db.json")
self.File = Query()
def add(self, data_add):
self.db.insert(data_add)
def add_raw(self, data_add):
# self.db.insert(data_add)
pass
def get_all(self):
return self.db.all()
def get_search(self, ratings, tags):
ratings
results = self.db.search(
self.File.rating.one_of(self.ratings) &
self.File.tags.any(self.tags)
)
return results
def get_unknow(self):
results = self.db.search(self.File.tags == [])
return results
def get_autocomplete(self, tag_slise):
counter = Counter()
for record in self.db.all():
counter.update(record.get("tags", []))
results = [
{
"tag": tag,
"count": count
}
for tag, count in counter.items()
if tag.lower().startswith(prefix)
]
results.sort(key=lambda x: x["count"], reverse=True)
top_10 = results[:10]
return top_10
def edit_data(self, id_file, edit_data):
self.db.update(self.edit_data, self.File.md5 == self.id_file)