Added file's index

This commit is contained in:
Fabio Scotto di Santolo
2024-10-29 17:02:35 +01:00
parent dd01ddb5cf
commit 64bc429357
6 changed files with 161 additions and 39 deletions

42
service/index.py Normal file
View File

@@ -0,0 +1,42 @@
import hashlib
import logging
import sqlite3
from contextlib import closing
from models.track import TrackInfo
from service import __INDEX_PATH
logger = logging.getLogger(__name__)
def __fingerprint(file: str) -> str:
with open(file, "rb", buffering=0) as f:
return hashlib.file_digest(f, 'sha256').hexdigest()
def duplicated(path: str) -> bool:
try:
with closing(sqlite3.connect(database=__INDEX_PATH)) as conn:
with closing(conn.cursor()) as cursor:
rows = cursor.execute("select 1 from songs where fingerprint = ?", (__fingerprint(path),)).fetchall()
return True if len(rows) > 0 else False
except sqlite3.OperationalError as e:
logger.error("Database error:", e)
def add_track(track_info: TrackInfo, path: str) -> bool:
if duplicated(path):
logger.warning(f"File {path} is duplicated")
return False
try:
with closing(sqlite3.connect(database=__INDEX_PATH)) as conn:
with closing(conn.cursor()) as cursor:
cursor.execute("insert into songs values (?, ?, ?, ?, ?, ?)",
(track_info.artist, track_info.title, track_info.album.name, track_info.album.released,
path, __fingerprint(path)))
conn.commit()
except sqlite3.OperationalError as e:
logger.error("Database error:", e)
return True