mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-04-17 15:43:28 +02:00
add tv_shows table, refactor code to make to more readable and remove the .env file for postgres from the repo
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
.idea
|
||||
venv
|
||||
MediaManager.iml
|
||||
MediaManager.iml
|
||||
MediaManager/res
|
||||
MediaManager/res/.env
|
||||
@@ -1,3 +0,0 @@
|
||||
DB_USERNAME=MediaManager
|
||||
DB_PASSWORD=MediaManager
|
||||
DB_NAME=MediaManager
|
||||
@@ -1,13 +1,11 @@
|
||||
import logging
|
||||
import os
|
||||
from logging import getLogger
|
||||
|
||||
import psycopg
|
||||
from psycopg.rows import dict_row
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
log.debug("servas")
|
||||
|
||||
class PgDatabase():
|
||||
"""PostgreSQL Database context manager using psycopg"""
|
||||
@@ -25,6 +23,7 @@ class PgDatabase():
|
||||
dbname=os.getenv("DB_NAME"),
|
||||
row_factory=dict_row
|
||||
)
|
||||
|
||||
def __enter__(self):
|
||||
self.connection = self.connect_to_database()
|
||||
return self
|
||||
@@ -32,15 +31,18 @@ class PgDatabase():
|
||||
def __exit__(self, exception_type, exc_val, traceback):
|
||||
self.connection.close()
|
||||
|
||||
|
||||
def init_db():
|
||||
log.info("Initializing database")
|
||||
from database import user
|
||||
user.init_db()
|
||||
|
||||
from database import tv, user
|
||||
user.init_table()
|
||||
tv.init_table()
|
||||
|
||||
log.info("Tables initialized successfully")
|
||||
|
||||
init_db()
|
||||
|
||||
def drop_tables() -> None:
|
||||
with PgDatabase() as db:
|
||||
db.connection.execute("DROP TABLE IF EXISTS users CASCADE;")
|
||||
log.info("User Table dropped")
|
||||
|
||||
|
||||
69
MediaManager/src/database/tv.py
Normal file
69
MediaManager/src/database/tv.py
Normal file
@@ -0,0 +1,69 @@
|
||||
from typing import Literal
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from database import PgDatabase, log
|
||||
|
||||
|
||||
# NOTE: use tmdbsimple for api calls
|
||||
|
||||
class Show(BaseModel):
|
||||
id: UUID = uuid4()
|
||||
external_id: int
|
||||
indexer: Literal["tmdb"]
|
||||
name: str
|
||||
number_of_episodes: int
|
||||
number_of_seasons: int
|
||||
origin_country: list[str]
|
||||
original_language: str
|
||||
status: str
|
||||
first_air_date: str
|
||||
|
||||
def save_show(show: Show) -> None:
|
||||
with PgDatabase() as db:
|
||||
db.connection.execute("""
|
||||
INSERT INTO tv_shows (
|
||||
id,
|
||||
external_id,
|
||||
indexer,
|
||||
name,
|
||||
number_of_episodes,
|
||||
number_of_seasons,
|
||||
origin_country,
|
||||
original_language,
|
||||
status,
|
||||
first_air_date
|
||||
)VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
|
||||
""",
|
||||
(show.id,
|
||||
show.external_id,
|
||||
show.indexer,
|
||||
show.name,
|
||||
show.number_of_episodes,
|
||||
show.number_of_seasons,
|
||||
show.origin_country,
|
||||
show.original_language,
|
||||
show.status,
|
||||
show.first_air_date
|
||||
)
|
||||
)
|
||||
log.info("added show: "+show.__str__())
|
||||
|
||||
|
||||
def init_table():
|
||||
with PgDatabase() as db:
|
||||
db.connection.execute("""
|
||||
CREATE TABLE IF NOT EXISTS tv_shows (
|
||||
id UUID PRIMARY KEY,
|
||||
external_id NUMERIC,
|
||||
indexer TEXT,
|
||||
name TEXT,
|
||||
number_of_episodes INTEGER,
|
||||
number_of_seasons INTEGER,
|
||||
origin_country TEXT[],
|
||||
original_language TEXT,
|
||||
status TEXT,
|
||||
first_air_date TEXT
|
||||
);""")
|
||||
log.info("tv_shows Table initialized successfully")
|
||||
@@ -2,7 +2,6 @@ from uuid import uuid4
|
||||
|
||||
import psycopg
|
||||
from pydantic import BaseModel
|
||||
from pydantic import UUID4
|
||||
|
||||
from database import PgDatabase, log
|
||||
|
||||
@@ -71,12 +70,14 @@ def get_user(email: str = None, uid: str = None) -> UserInternal | None:
|
||||
|
||||
if result is None:
|
||||
return None
|
||||
user = UserInternal(id=result["id"].__str__(), name=result["name"], lastname=result["lastname"], email=result["email"],
|
||||
user = UserInternal(id=result["id"].__str__(), name=result["name"], lastname=result["lastname"],
|
||||
email=result["email"],
|
||||
hashed_password=result["hashed_password"])
|
||||
log.debug(f"Retrieved User succesfully: {user.model_dump()} ")
|
||||
return user
|
||||
|
||||
def init_db():
|
||||
|
||||
def init_table():
|
||||
with PgDatabase() as db:
|
||||
db.connection.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
@@ -87,4 +88,4 @@ def init_db():
|
||||
hashed_password TEXT NOT NULL
|
||||
);
|
||||
""")
|
||||
log.info("User Table initialized successfully")
|
||||
log.info("users Table initialized successfully")
|
||||
|
||||
Reference in New Issue
Block a user