idk i added a bunch of stuff like a month ago
This commit is contained in:
1 parent
95c4dc96f6
commit
851d5ebb35
13 files changed
+5044
-40
No files matched your search
+67
-26
@@ -19,8 +19,16 @@ class Album:
|
||||
def from_row(cls, row: tuple[int, str, str | None, str | None]) -> Self:
|
||||
return cls(*row)
|
||||
|
||||
@dataclass
|
||||
class Artist:
|
||||
pass
|
||||
id: int
|
||||
name: str
|
||||
mbid: str | None = None
|
||||
icon_url: str | None = None
|
||||
|
||||
@classmethod
|
||||
def from_row(cls, row: tuple[int, str, str | None, str | None]) -> Self:
|
||||
return cls(*row)
|
||||
|
||||
@dataclass
|
||||
class Song:
|
||||
@@ -64,7 +72,7 @@ class Connect:
|
||||
def _load_query(path: str) -> str:
|
||||
return open(path, 'r').read()
|
||||
|
||||
def execute_query(query_path: str, qargs: list | tuple) -> list:
|
||||
def execute_query(query_path: str, *args) -> list:
|
||||
if not os.path.exists(query_path):
|
||||
logging.error(f'Query Path "{query_path}" does not exist')
|
||||
return []
|
||||
@@ -72,26 +80,40 @@ def execute_query(query_path: str, qargs: list | tuple) -> list:
|
||||
query: str = _load_query(query_path)
|
||||
|
||||
with Connect(DATABASE) as (conn, cur):
|
||||
cur.execute(query, qargs)
|
||||
cur.execute(query, [arg for arg in args])
|
||||
|
||||
return cur.fetchall()
|
||||
|
||||
def execute_insertion_query(query_path: str, *args) -> int | None:
|
||||
if not os.path.exists(query_path):
|
||||
logging.error(f'Query Path "{query_path}" does not exist')
|
||||
return None
|
||||
|
||||
query: str = _load_query(query_path)
|
||||
|
||||
with Connect(DATABASE) as (conn, cur):
|
||||
cur.execute(query, [arg for arg in args])
|
||||
|
||||
return cur.lastrowid
|
||||
|
||||
def insert_listen_event(song_id: int, user_id: int, date: int | datetime.datetime, time: int):
|
||||
pass
|
||||
|
||||
def search_song_mbid(mbid: str) -> Song | None:
|
||||
results: list = execute_query('sql/get_song_by_mbid.sql', (mbid,))
|
||||
results: list = execute_query('sql/get_song_by_mbid.sql', mbid)
|
||||
|
||||
if results:
|
||||
return Song.from_rows(results)
|
||||
|
||||
return None
|
||||
|
||||
def search_song_name(name: str) -> list[Song] | None:
|
||||
results: list = execute_query('sql/get_song_by_name.sql', (name,))
|
||||
def search_song_name(name: str) -> list[Song]:
|
||||
name = '%' + name + '%'
|
||||
|
||||
results: list = execute_query('sql/get_song_by_name.sql', name)
|
||||
|
||||
if not results:
|
||||
return None
|
||||
return []
|
||||
|
||||
songs: list[Song] = []
|
||||
|
||||
@@ -113,7 +135,7 @@ def search_song_name(name: str) -> list[Song] | None:
|
||||
return songs
|
||||
|
||||
def search_song_by_id(id: int) -> Song | None:
|
||||
results: list = execute_query('sql/get_song_by_id.sql', (id,))
|
||||
results: list = execute_query('sql/get_song_by_id.sql', id)
|
||||
|
||||
if results:
|
||||
return Song.from_rows(results)
|
||||
@@ -121,42 +143,61 @@ def search_song_by_id(id: int) -> Song | None:
|
||||
return None
|
||||
|
||||
def insert_song(id: int, name: str, length: int, album_id: int, artist_id: int, mbid: str | None = None) -> None:
|
||||
execute_query('sql/insert_song.sql', (id, name, length, album_id, artist_id, mbid))
|
||||
execute_query('sql/insert_song.sql', id, name, length, album_id, artist_id, mbid)
|
||||
|
||||
def get_last_song_id() -> int:
|
||||
return execute_query('sql/get_last_song_id.sql', [])[0][0]
|
||||
return execute_query('sql/get_last_song_id.sql')[0][0]
|
||||
|
||||
def search_album_by_name(name: str) -> list[Album] | None:
|
||||
results: list = execute_query('sql/get_album_by_name.sql', (name,))
|
||||
def search_album_by_name(name: str) -> list[Album]:
|
||||
name = '%' + name + '%'
|
||||
|
||||
results: list = execute_query('sql/get_album_by_name.sql', name)
|
||||
|
||||
if not results:
|
||||
return None
|
||||
return []
|
||||
|
||||
albums: list[Album] = []
|
||||
for row in results:
|
||||
albums.append(Album.from_row(row))
|
||||
|
||||
return albums
|
||||
|
||||
def search_album_by_mbid(mbid: str) -> Album | None:
|
||||
pass
|
||||
results: list = execute_query('sql/get_album_by_mbid.sql', mbid)
|
||||
|
||||
return Album.from_row(results[0]) if results else None
|
||||
|
||||
def search_album_by_id(id: int) -> Album | None:
|
||||
pass
|
||||
results: list = execute_query('sql/get_album_by_id.sql', id)
|
||||
|
||||
def insert_album(name: str, mbid: str | None, cover_art_url: str | None) -> None:
|
||||
pass
|
||||
return Album.from_row(results[0]) if results else None
|
||||
|
||||
def search_artist_by_name(name: str) -> list[Artist] | None:
|
||||
pass
|
||||
def insert_album(name: str, mbid: str | None = None, cover_art_url: str | None = None) -> Album:
|
||||
return search_album_by_id(execute_insertion_query('sql/insert_album.sql', name, mbid, cover_art_url))
|
||||
|
||||
def search_artist_by_name(name: str) -> list[Artist]:
|
||||
name = '%' + name + '%'
|
||||
|
||||
results: list = execute_query('sql/get_artist_by_name.sql', name)
|
||||
|
||||
if not results:
|
||||
return []
|
||||
|
||||
artists: list[Artist] = []
|
||||
for row in results:
|
||||
artists.append(Artist.from_row(row))
|
||||
|
||||
return artists
|
||||
|
||||
def search_artist_by_mbid(mbid: str) -> Artist | None:
|
||||
pass
|
||||
results: list = execute_query('sql/get_artist_by_mbid.sql', mbid)
|
||||
|
||||
return Artist.from_row(results[0]) if results else None
|
||||
|
||||
def search_artist_by_id(id: int) -> Artist | None:
|
||||
pass
|
||||
results: list = execute_query('sql/get_artist_by_id.sql', id)
|
||||
|
||||
return Artist.from_row(results[0]) if results else None
|
||||
|
||||
def insert_artist(name: str, mbid: str | None = None, icon_url: str | None = None) -> None:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
return search_artist_by_id(execute_insertion_query('sql/insert_artist.sql', name, mbid, icon_url))
|
||||
Reference in new issue
Block a user