tried fixing shit

This commit is contained in:
Hellow2 2023-03-02 07:59:53 +01:00
parent ad07da7a49
commit 4c041ef7ff
11 changed files with 63 additions and 43 deletions

View File

@ -1,15 +1,14 @@
from music_kraken import ( from music_kraken.objects import (
Song, Song,
Database,
Artist,
Album
) )
from music_kraken.pages import ( from music_kraken.pages import (
EncyclopaediaMetallum EncyclopaediaMetallum
) )
"""
test_db = Database("test.db") test_db = Database("test.db")
# test_db.reset() # test_db.reset()
@ -64,3 +63,7 @@ print_song(song)
artist = song.main_artist_list[0] artist = song.main_artist_list[0]
artist = EncyclopaediaMetallum.fetch_artist_details(artist, flat=False) artist = EncyclopaediaMetallum.fetch_artist_details(artist, flat=False)
print_artist(artist) print_artist(artist)
"""
results = EncyclopaediaMetallum.search_by_query("#a only smile")
print(results)

View File

@ -6,6 +6,7 @@ import logging
import os import os
from . import ( from . import (
objects,
database, database,
pages pages
) )
@ -37,33 +38,21 @@ logging.getLogger("musicbrainzngs").setLevel(logging.WARNING)
musicbrainzngs.set_useragent("metadata receiver", "0.1", "https://github.com/HeIIow2/music-downloader") musicbrainzngs.set_useragent("metadata receiver", "0.1", "https://github.com/HeIIow2/music-downloader")
# define the most important values and function for import in the __init__ file # define the most important values and function for import in the __init__ file
Song = database.Song
Artist = database.Artist
Source = database.Source
SourceTypes = database.SourceTypes
SourcePages = database.SourcePages
Target = database.Target
Lyrics = database.Lyrics
Album = database.Album
MusicObject = database.MusicObject
ID3Timestamp = database.ID3Timestamp
cache = database.cache cache = database.cache
Database = database.Database Database = database.Database
def get_options_from_query(query: str) -> List[MusicObject]: def get_options_from_query(query: str) -> List[objects.MusicObject]:
options = [] options = []
for MetadataPage in pages.MetadataPages: for MetadataPage in pages.MetadataPages:
options.extend(MetadataPage.search_by_query(query=query)) options.extend(MetadataPage.search_by_query(query=query))
return options return options
def get_options_from_option(option: MusicObject) -> List[MusicObject]: def get_options_from_option(option: objects.MusicObject) -> List[objects.MusicObject]:
for MetadataPage in pages.MetadataPages: for MetadataPage in pages.MetadataPages:
option = MetadataPage.fetch_details(option, flat=False) option = MetadataPage.fetch_details(option, flat=False)
return option.get_options() return option.get_options()
def print_options(options: List[MusicObject]): def print_options(options: List[objects.MusicObject]):
print("\n".join([f"{str(j).zfill(2)}: {i.get_option_string()}" for j, i in enumerate(options)])) print("\n".join([f"{str(j).zfill(2)}: {i.get_option_string()}" for j, i in enumerate(options)]))
def cli(): def cli():

View File

@ -108,8 +108,8 @@ class Source(BaseModel):
url: str = CharField() url: str = CharField()
content_type: str = CharField() content_type: str = CharField()
content_id: int = IntegerField() content_id: int = CharField()
content: ForeignKeyField = ForeignKeyField('self', backref='content_items', null=True) # content: ForeignKeyField = ForeignKeyField('self', backref='content_items', null=True)
@property @property
def content_object(self) -> Union[Song, Album, Artist]: def content_object(self) -> Union[Song, Album, Artist]:

View File

@ -1,6 +1,7 @@
# Standard library # Standard library
from typing import Optional, Union, List from typing import Optional, Union, List
from enum import Enum from enum import Enum
from playhouse.migrate import *
# third party modules # third party modules
from peewee import ( from peewee import (
@ -12,9 +13,9 @@ from peewee import (
# own modules # own modules
from . import ( from . import (
data_models, data_models,
write, write
objects,
) )
from .. import objects
class DatabaseType(Enum): class DatabaseType(Enum):
@ -76,6 +77,18 @@ class Database:
raise ValueError("Invalid database type specified.") raise ValueError("Invalid database type specified.")
@property
def migrator(self) -> SchemaMigrator:
if self.db_type == DatabaseType.SQLITE:
return SqliteMigrator(self.database)
if self.db_type == DatabaseType.MYSQL:
return MySQLMigrator(self.database)
if self.db_type == DatabaseType.POSTGRESQL:
return PostgresqlMigrator(self.database)
def initialize_database(self): def initialize_database(self):
""" """
Connect to the database Connect to the database
@ -84,20 +97,38 @@ class Database:
""" """
self.database = self.create_database() self.database = self.create_database()
self.database.connect() self.database.connect()
migrator = self.migrator
for model in data_models.ALL_MODELS:
model = model.Use(self.database)
if self.database.table_exists(model):
migration_operations = [
migrator.add_column(
"some field", field[0], field[1]
)
for field in model._meta.fields.items()
]
migrate(*migration_operations)
else:
self.database.create_tables([model], safe=True)
self.database.create_tables(data_models.ALL_MODELS, safe=True) #self.database.create_tables([model.Use(self.database) for model in data_models.ALL_MODELS], safe=True)
""" """
upgrade old databases. upgrade old databases.
If a collumn has been added in a new version this adds it to old Tables, If a collumn has been added in a new version this adds it to old Tables,
without deleting the data in legacy databases without deleting the data in legacy databases
""" """
for model in data_models.ALL_MODELS: for model in data_models.ALL_MODELS:
for field_name, field_obj in model._meta.fields.items(): model = model.Use(self.database)
# check if the field exists in the database
if not self.database.table_column_exists(model._meta.db_table, field_name):
# add the missing column to the table
self.database.add_column(model._meta.db_table, field_name, field_obj) print(model._meta.fields)
def push(self, database_object: objects.MusicObject): def push(self, database_object: objects.MusicObject):
""" """

View File

@ -1,4 +1,4 @@
from .old_database import Database from .database import Database, DatabaseType
from ..utils.shared import ( from ..utils.shared import (
TEMP_DATABASE_PATH, TEMP_DATABASE_PATH,
@ -9,11 +9,8 @@ logger = DATABASE_LOGGER
class TempDatabase(Database): class TempDatabase(Database):
def __init__(self, reset_on_start: bool = True) -> None: def __init__(self) -> None:
super().__init__(TEMP_DATABASE_PATH) super().__init__(db_type=DatabaseType.SQLITE, db_name=TEMP_DATABASE_PATH)
if reset_on_start:
self.reset()
temp_database = TempDatabase() temp_database = TempDatabase()

View File

@ -9,7 +9,7 @@ from peewee import (
Model Model
) )
from . import objects from .. import objects
from . import data_models from . import data_models
# just a Type for type hintung. You can't do anything with it. # just a Type for type hintung. You can't do anything with it.

View File

@ -12,6 +12,7 @@ MusicObject = parents.DatabaseObject
ID3Mapping = metadata.Mapping ID3Mapping = metadata.Mapping
ID3Timestamp = metadata.ID3Timestamp ID3Timestamp = metadata.ID3Timestamp
Source = source.Source
SourceTypes = source.SourceTypes SourceTypes = source.SourceTypes
SourcePages = source.SourcePages SourcePages = source.SourcePages
SourceAttribute = source.SourceAttribute SourceAttribute = source.SourceAttribute

View File

@ -1,7 +1,7 @@
from typing import List from typing import List
from .source import SourceAttribute from .source import SourceAttribute
from src.music_kraken.utils import string_processing from ..utils import string_processing
class Collection: class Collection:

View File

@ -1,7 +1,7 @@
from typing import Optional from typing import Optional
import uuid import uuid
from src.music_kraken.utils.shared import ( from ..utils.shared import (
SONG_LOGGER as LOGGER SONG_LOGGER as LOGGER
) )

View File

@ -7,7 +7,7 @@ from .metadata import (
ID3Timestamp, ID3Timestamp,
MetadataAttribute MetadataAttribute
) )
from src.music_kraken.utils.shared import ( from ..utils.shared import (
MUSIC_DIR, MUSIC_DIR,
DATABASE_LOGGER as LOGGER DATABASE_LOGGER as LOGGER
) )

View File

@ -3,8 +3,7 @@ from typing import List, Dict
from .metadata import Mapping, MetadataAttribute from .metadata import Mapping, MetadataAttribute
from .parents import ( from .parents import (
DatabaseObject, DatabaseObject
SongAttribute,
) )
@ -48,7 +47,7 @@ class SourcePages(Enum):
return homepage_map[attribute] return homepage_map[attribute]
class Source(DatabaseObject, SongAttribute, MetadataAttribute): class Source(DatabaseObject, MetadataAttribute):
""" """
create somehow like that create somehow like that
```python ```python