nas-tools/app/db/media_db.py
2023-02-13 12:52:00 +08:00

127 lines
4.6 KiB
Python

import os
import threading
import time
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.pool import QueuePool
from app.db.models import BaseMedia, MEDIASYNCITEMS, MEDIASYNCSTATISTIC
from app.utils import ExceptionUtils
from config import Config
lock = threading.Lock()
_Engine = create_engine(
f"sqlite:///{os.path.join(Config().get_config_path(), 'media.db')}?check_same_thread=False",
echo=False,
poolclass=QueuePool,
pool_pre_ping=True,
pool_size=50,
pool_recycle=60 * 10,
max_overflow=0
)
_Session = scoped_session(sessionmaker(bind=_Engine,
autoflush=True,
autocommit=False))
class MediaDb:
@property
def session(self):
return _Session()
@staticmethod
def init_db():
with lock:
BaseMedia.metadata.create_all(_Engine)
def insert(self, server_type, iteminfo):
if not server_type or not iteminfo:
return False
try:
self.session.query(MEDIASYNCITEMS).filter(MEDIASYNCITEMS.SERVER == server_type,
MEDIASYNCITEMS.ITEM_ID == iteminfo.get("id")).delete()
self.session.flush()
self.session.add(MEDIASYNCITEMS(
SERVER=server_type,
LIBRARY=iteminfo.get("library"),
ITEM_ID=iteminfo.get("id"),
ITEM_TYPE=iteminfo.get("type"),
TITLE=iteminfo.get("title"),
ORGIN_TITLE=iteminfo.get("originalTitle"),
YEAR=iteminfo.get("year"),
TMDBID=iteminfo.get("tmdbid"),
IMDBID=iteminfo.get("imdbid"),
PATH=iteminfo.get("path")
))
self.session.commit()
return True
except Exception as e:
ExceptionUtils.exception_traceback(e)
self.session.rollback()
return False
def empty(self, server_type=None, library=None):
try:
if server_type and library:
self.session.query(MEDIASYNCITEMS).filter(MEDIASYNCITEMS.SERVER == server_type,
MEDIASYNCITEMS.LIBRARY == library).delete()
else:
self.session.query(MEDIASYNCITEMS).delete()
self.session.commit()
return True
except Exception as e:
ExceptionUtils.exception_traceback(e)
self.session.rollback()
return False
def statistics(self, server_type, total_count, movie_count, tv_count):
if not server_type:
return False
try:
self.session.query(MEDIASYNCSTATISTIC).filter(MEDIASYNCSTATISTIC.SERVER == server_type).delete()
self.session.flush()
self.session.add(MEDIASYNCSTATISTIC(
SERVER=server_type,
TOTAL_COUNT=total_count,
MOVIE_COUNT=movie_count,
TV_COUNT=tv_count,
UPDATE_TIME=time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(time.time()))
))
self.session.commit()
return True
except Exception as e:
ExceptionUtils.exception_traceback(e)
self.session.rollback()
return False
def exists(self, server_type, title, year, tmdbid):
if not server_type or not title:
return False
if tmdbid:
count = self.session.query(MEDIASYNCITEMS).filter(MEDIASYNCITEMS.TMDBID == str(tmdbid)).count()
if count:
return True
if year:
items = self.session.query(MEDIASYNCITEMS).filter(MEDIASYNCITEMS.SERVER == server_type,
MEDIASYNCITEMS.TITLE == title,
MEDIASYNCITEMS.YEAR == str(year)).all()
else:
items = self.session.query(MEDIASYNCITEMS).filter(MEDIASYNCITEMS.SERVER == server_type,
MEDIASYNCITEMS.TITLE == title).all()
if items:
if tmdbid:
for item in items:
if not item.TMDBID or item.TMDBID == str(tmdbid):
return True
return False
else:
return True
else:
return False
def get_statistics(self, server_type):
if not server_type:
return None
return self.session.query(MEDIASYNCSTATISTIC).filter(MEDIASYNCSTATISTIC.SERVER == server_type).first()