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,
Database,
Artist,
Album
)
from music_kraken.pages import (
EncyclopaediaMetallum
)
"""
test_db = Database("test.db")
# test_db.reset()
@ -64,3 +63,7 @@ print_song(song)
artist = song.main_artist_list[0]
artist = EncyclopaediaMetallum.fetch_artist_details(artist, flat=False)
print_artist(artist)
"""
results = EncyclopaediaMetallum.search_by_query("#a only smile")
print(results)

View File

@ -6,6 +6,7 @@ import logging
import os
from . import (
objects,
database,
pages
)
@ -37,33 +38,21 @@ logging.getLogger("musicbrainzngs").setLevel(logging.WARNING)
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
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
Database = database.Database
def get_options_from_query(query: str) -> List[MusicObject]:
def get_options_from_query(query: str) -> List[objects.MusicObject]:
options = []
for MetadataPage in pages.MetadataPages:
options.extend(MetadataPage.search_by_query(query=query))
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:
option = MetadataPage.fetch_details(option, flat=False)
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)]))
def cli():

View File

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

View File

@ -1,6 +1,7 @@
# Standard library
from typing import Optional, Union, List
from enum import Enum
from playhouse.migrate import *
# third party modules
from peewee import (
@ -12,9 +13,9 @@ from peewee import (
# own modules
from . import (
data_models,
write,
objects,
write
)
from .. import objects
class DatabaseType(Enum):
@ -76,6 +77,18 @@ class Database:
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):
"""
Connect to the database
@ -84,20 +97,38 @@ class Database:
"""
self.database = self.create_database()
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.
If a collumn has been added in a new version this adds it to old Tables,
without deleting the data in legacy databases
"""
for model in data_models.ALL_MODELS:
for field_name, field_obj in model._meta.fields.items():
# 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)
model = model.Use(self.database)
print(model._meta.fields)
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 (
TEMP_DATABASE_PATH,
@ -9,11 +9,8 @@ logger = DATABASE_LOGGER
class TempDatabase(Database):
def __init__(self, reset_on_start: bool = True) -> None:
super().__init__(TEMP_DATABASE_PATH)
if reset_on_start:
self.reset()
def __init__(self) -> None:
super().__init__(db_type=DatabaseType.SQLITE, db_name=TEMP_DATABASE_PATH)
temp_database = TempDatabase()

View File

@ -9,7 +9,7 @@ from peewee import (
Model
)
from . import objects
from .. import objects
from . import data_models
# 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
ID3Timestamp = metadata.ID3Timestamp
Source = source.Source
SourceTypes = source.SourceTypes
SourcePages = source.SourcePages
SourceAttribute = source.SourceAttribute

View File

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

View File

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

View File

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

View File

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