From 06cc826a219ebd7f2660f35d855fdb9e8f454d69 Mon Sep 17 00:00:00 2001 From: Hellow Date: Mon, 30 Jan 2023 18:27:49 +0100 Subject: [PATCH] finished database integration and simmilar --- src/create_custom_objects.py | 50 ++-------------- src/music_kraken/database/new_database.py | 5 +- src/music_kraken/database/objects/metadata.py | 6 +- src/music_kraken/database/objects/parents.py | 10 ---- src/music_kraken/database/objects/song.py | 54 ++++++++---------- src/music_kraken/database/objects/source.py | 1 - .../pages/encyclopaedia_metallum.py | 28 ++++----- src/test.db | Bin 65536 -> 65536 bytes 8 files changed, 51 insertions(+), 103 deletions(-) diff --git a/src/create_custom_objects.py b/src/create_custom_objects.py index 7fac0a0..392610b 100644 --- a/src/create_custom_objects.py +++ b/src/create_custom_objects.py @@ -32,6 +32,7 @@ def div(msg: str = ""): cache = music_kraken.database.new_database.Database("test.db") cache.reset() + def print_song(song_: Song): print(str(song_.metadata)) print("----album--") @@ -65,14 +66,16 @@ song = Song( language=pycountry.languages.get(alpha_2="en"), label="cum productions", source_list=[ - Source(SourcePages.ENCYCLOPAEDIA_METALLUM, "https://www.metal-archives.com/albums/I%27m_in_a_Coffin/One_Final_Action/207614") + Source(SourcePages.ENCYCLOPAEDIA_METALLUM, + "https://www.metal-archives.com/albums/I%27m_in_a_Coffin/One_Final_Action/207614") ] ), main_artist_list=[ Artist( name="I'm in a coffin", source_list=[ - Source(SourcePages.ENCYCLOPAEDIA_METALLUM, "https://www.metal-archives.com/bands/I%27m_in_a_Coffin/127727") + Source(SourcePages.ENCYCLOPAEDIA_METALLUM, + "https://www.metal-archives.com/bands/I%27m_in_a_Coffin/127727") ] ), Artist(name="some_split_artist") @@ -82,8 +85,6 @@ song = Song( print_song(song) - - div() song_ref = song.reference cache.push([song]) @@ -97,44 +98,5 @@ print_song(song_from_db) # try writing metadata write_metadata(song) -exit() - # getting song by album ref -div() -song_output_list = cache.pull_songs(album_ref=album_input.reference) -print(len(song_output_list), song_output_list) -for song in song_output_list: - print(song, song.album) - -# getting album -div("album") -album_output_list = cache.pull_albums(album_ref=album_input.reference) -album_output = album_output_list[0] -print(album_output) -print(f"--tracklist-{len(album_output)}--") -for track in album_output.tracklist: - print(track.tracksort, track) -print("--artist--") -for artist in album_output.artists: - print(artist) - -# getting album by song -div() -album_output_list = cache.pull_albums(song_ref=song_ref) -print(album_output_list) -print("len of album ->", len(album_output_list[0]), album_output_list[0], sep=" | ") - -# get artist -div("artist") -artist_output = cache.pull_artists(artist_ref=artist_ref)[0] -print(artist_output) - -print("---static---") -print("albums", artist_output.main_albums) -print("main_s", artist_output.main_songs) -print("feat_s", artist_output.feature_songs) - -print("---dynamic---") -print("discography", artist_output.discography) -print("songs", artist_output.songs, artist_output.songs.tracklist) -print("features", artist_output.features, artist_output.features.tracklist) +div() \ No newline at end of file diff --git a/src/music_kraken/database/new_database.py b/src/music_kraken/database/new_database.py index 452793a..858e710 100644 --- a/src/music_kraken/database/new_database.py +++ b/src/music_kraken/database/new_database.py @@ -161,9 +161,12 @@ class Database: for source in album.source_list: source.type_enum = SourceTypes.ALBUM + source.add_song(album) self.push_source(source=source) def push_song(self, song: Song): + if song.dynamic: + return # ADDING THE DATA FOR THE SONG OBJECT """ db_field - object attribute @@ -356,6 +359,7 @@ class Database: Gets a list of sources. if source_ref is passed in the List will most likely only contain one Element if everything goes accordingly. **If neither song_ref nor source_ref are passed in it will return ALL sources** + :param artist_ref: :param song_ref: :param source_ref: :param type_str: the thing the source belongs to like eg. "song" or "album" @@ -512,7 +516,6 @@ class Database: ) if Album not in exclude_relations and song_result['album_id'] is not None: - print(dict(song_result)) album_obj = self.pull_albums(album_ref=Reference(song_result['album_id']), exclude_relations=new_exclude_relations) if len(album_obj) > 0: diff --git a/src/music_kraken/database/objects/metadata.py b/src/music_kraken/database/objects/metadata.py index caba7ad..d1ac999 100644 --- a/src/music_kraken/database/objects/metadata.py +++ b/src/music_kraken/database/objects/metadata.py @@ -5,10 +5,6 @@ import dateutil.tz from mutagen import id3 import datetime -from .parents import ( - ID3Metadata -) - class Mapping(Enum): """ @@ -253,7 +249,7 @@ class MetadataAttribute: """ class Metadata: - # its a null byte for the later concatenation of text frames + # it's a null byte for the later concatenation of text frames NULL_BYTE: str = "\x00" # this is pretty self-explanatory # the key is an enum from Mapping diff --git a/src/music_kraken/database/objects/parents.py b/src/music_kraken/database/objects/parents.py index ee2802d..6e104d8 100644 --- a/src/music_kraken/database/objects/parents.py +++ b/src/music_kraken/database/objects/parents.py @@ -64,13 +64,3 @@ class SongAttribute: self.song_ref = Reference(song_id) song_ref_id = property(fget=get_ref_song_id, fset=set_ref_song_id) - - -class ID3Metadata: - def get_metadata(self): - pass - - def get_id3_dict(self) -> dict: - return {} - - id3_dict: dict = property(fget=get_id3_dict) diff --git a/src/music_kraken/database/objects/song.py b/src/music_kraken/database/objects/song.py index e63596a..a1d5277 100644 --- a/src/music_kraken/database/objects/song.py +++ b/src/music_kraken/database/objects/song.py @@ -1,10 +1,10 @@ import os -from typing import List, Tuple, Dict -import datetime +from typing import List import pycountry +import copy from .metadata import ( - Mapping as ID3_MAPPING, + Mapping as id3Mapping, ID3Timestamp, MetadataAttribute ) @@ -15,8 +15,7 @@ from ...utils.shared import ( from .parents import ( DatabaseObject, Reference, - SongAttribute, - ID3Metadata + SongAttribute ) from .source import ( Source, @@ -83,11 +82,11 @@ class Target(DatabaseObject, SongAttribute): class Lyrics(DatabaseObject, SongAttribute, SourceAttribute, MetadataAttribute): def __init__( self, - text: str, - language: str, + text: str, + language: str, id_: str = None, source_list: List[Source] = None - ) -> None: + ) -> None: DatabaseObject.__init__(self, id_=id_) SongAttribute.__init__(self) self.text = text @@ -134,7 +133,7 @@ class Song(DatabaseObject, SourceAttribute, MetadataAttribute): self.album_name: str | None = album_name self.tracksort: int | None = tracksort self.genre: str = genre - + if source_list: self.source_list = source_list @@ -196,15 +195,14 @@ class Song(DatabaseObject, SourceAttribute, MetadataAttribute): return str(self.tracksort) return f"{self.tracksort}/{len(self.album.tracklist)}" - def get_metadata(self) -> MetadataAttribute.Metadata: metadata = MetadataAttribute.Metadata({ - ID3_MAPPING.TITLE: [self.title], - ID3_MAPPING.ISRC: [self.isrc], - ID3_MAPPING.LENGTH: [str(self.length)], - ID3_MAPPING.GENRE: [self.genre], - ID3_MAPPING.TRACKNUMBER: [self.tracksort_str] + id3Mapping.TITLE: [self.title], + id3Mapping.ISRC: [self.isrc], + id3Mapping.LENGTH: [str(self.length)], + id3Mapping.GENRE: [self.genre], + id3Mapping.TRACKNUMBER: [self.tracksort_str] }) metadata.merge_many([s.get_song_metadata() for s in self.source_list]) @@ -216,15 +214,17 @@ class Song(DatabaseObject, SourceAttribute, MetadataAttribute): return metadata def set_album(self, album): + if album is None: + return + self._album = album if self not in self._album.tracklist: - self._album.tracklist.append(self) - + flat_copy = copy.copy(self) + flat_copy.dynamic = True + self._album.tracklist.append(flat_copy) tracksort_str = property(fget=get_tracksort_str) album = property(fget=lambda self: self._album, fset=set_album) - - """ @@ -313,11 +313,11 @@ class Album(DatabaseObject, SourceAttribute, MetadataAttribute): def get_metadata(self) -> MetadataAttribute.Metadata: return MetadataAttribute.Metadata({ - ID3_MAPPING.ALBUM: [self.title], - ID3_MAPPING.COPYRIGHT: [self.copyright], - ID3_MAPPING.LANGUAGE: [self.iso_639_2_language], - ID3_MAPPING.ALBUM_ARTIST: [a.name for a in self.artists], - ID3_MAPPING.DATE: [self.date.timestamp] + id3Mapping.ALBUM: [self.title], + id3Mapping.COPYRIGHT: [self.copyright], + id3Mapping.LANGUAGE: [self.iso_639_2_language], + id3Mapping.ALBUM_ARTIST: [a.name for a in self.artists], + id3Mapping.DATE: [self.date.timestamp] }) def get_copyright(self) -> str: @@ -337,7 +337,6 @@ class Album(DatabaseObject, SourceAttribute, MetadataAttribute): tracklist = property(fget=lambda self: self._tracklist, fset=set_tracklist) - """ All objects dependent on Artist """ @@ -391,7 +390,6 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): def get_features(self) -> Album: feature_release = Album( title="features", - copyright_=self.name, album_status="dynamic", is_split=True, albumsort=666, @@ -405,7 +403,6 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): def get_songs(self) -> Album: song_release = Album( title="song collection", - copyright_=self.name, album_status="dynamic", is_split=False, albumsort=666, @@ -429,13 +426,12 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): :return: """ metadata = MetadataAttribute.Metadata({ - ID3_MAPPING.ARTIST: [self.name] + id3Mapping.ARTIST: [self.name] }) metadata.merge_many([s.get_artist_metadata() for s in self.source_list]) return metadata - discography: List[Album] = property(fget=get_discography) features: Album = property(fget=get_features) songs: Album = property(fget=get_songs) diff --git a/src/music_kraken/database/objects/source.py b/src/music_kraken/database/objects/source.py index 717a4f8..0b5e3c5 100644 --- a/src/music_kraken/database/objects/source.py +++ b/src/music_kraken/database/objects/source.py @@ -5,7 +5,6 @@ from .metadata import Mapping, MetadataAttribute from .parents import ( DatabaseObject, SongAttribute, - ID3Metadata ) diff --git a/src/music_kraken/pages/encyclopaedia_metallum.py b/src/music_kraken/pages/encyclopaedia_metallum.py index 8a80da8..d9773d2 100644 --- a/src/music_kraken/pages/encyclopaedia_metallum.py +++ b/src/music_kraken/pages/encyclopaedia_metallum.py @@ -26,7 +26,6 @@ class EncyclopaediaMetallum(Page): SOURCE_TYPE = SourcePages.ENCYCLOPAEDIA_METALLUM - @classmethod def search_by_query(cls, query: str) -> List[MusicObject]: query_obj = cls.Query(query) @@ -48,10 +47,11 @@ class EncyclopaediaMetallum(Page): @classmethod def search_for_song(cls, query: Page.Query) -> List[Song]: endpoint = "https://www.metal-archives.com/search/ajax-advanced/searching/songs/?songTitle={song}&bandName={artist}&releaseTitle={album}&lyrics=&genre=&sEcho=1&iColumns=5&sColumns=&iDisplayStart=0&iDisplayLength=200&mDataProp_0=0&mDataProp_1=1&mDataProp_2=2&mDataProp_3=3&mDataProp_4=4&_=1674550595663" - + r = cls.API_SESSION.get(endpoint.format(song=query.song_str, artist=query.artist_str, album=query.album_str)) if r.status_code != 200: - LOGGER.warning(f"code {r.status_code} at {endpoint.format(song=query.song_str, artist=query.artist_str, album=query.album_str)}") + LOGGER.warning( + f"code {r.status_code} at {endpoint.format(song=query.song_str, artist=query.artist_str, album=query.album_str)}") return [] return [cls.get_song_from_json( @@ -65,10 +65,11 @@ class EncyclopaediaMetallum(Page): @classmethod def search_for_album(cls, query: Page.Query) -> List[Album]: endpoint = "https://www.metal-archives.com/search/ajax-advanced/searching/albums/?bandName={artist}&releaseTitle={album}&releaseYearFrom=&releaseMonthFrom=&releaseYearTo=&releaseMonthTo=&country=&location=&releaseLabelName=&releaseCatalogNumber=&releaseIdentifiers=&releaseRecordingInfo=&releaseDescription=&releaseNotes=&genre=&sEcho=1&iColumns=3&sColumns=&iDisplayStart=0&iDisplayLength=200&mDataProp_0=0&mDataProp_1=1&mDataProp_2=2&_=1674563943747" - + r = cls.API_SESSION.get(endpoint.format(artist=query.artist_str, album=query.album_str)) if r.status_code != 200: - LOGGER.warning(f"code {r.status_code} at {endpoint.format(song=query.song_str, artist=query.artist_str, album=query.album_str)}") + LOGGER.warning( + f"code {r.status_code} at {endpoint.format(song=query.song_str, artist=query.artist_str, album=query.album_str)}") return [] return [cls.get_album_from_json( @@ -80,14 +81,14 @@ class EncyclopaediaMetallum(Page): @classmethod def search_for_artist(cls, query: Page.Query) -> List[Artist]: endpoint = "https://www.metal-archives.com/search/ajax-advanced/searching/bands/?bandName={artist}&genre=&country=&yearCreationFrom=&yearCreationTo=&bandNotes=&status=&themes=&location=&bandLabelName=&sEcho=1&iColumns=3&sColumns=&iDisplayStart=0&iDisplayLength=200&mDataProp_0=0&mDataProp_1=1&mDataProp_2=2&_=1674565459976" - + r = cls.API_SESSION.get(endpoint.format(artist=query.artist)) if r.status_code != 200: LOGGER.warning(f"code {r.status_code} at {endpoint.format(artist=query.artist)}") return [] return [ - cls.get_artist_from_json(html=raw_artist[0], genre=raw_artist[1], country=raw_artist[2]) + cls.get_artist_from_json(html=raw_artist[0], genre=raw_artist[1], country=raw_artist[2]) for raw_artist in r.json()['aaData'] ] @@ -105,7 +106,7 @@ class EncyclopaediaMetallum(Page): return [] return [ - cls.get_artist_from_json(html=raw_artist[0], genre=raw_artist[1], country=raw_artist[2]) + cls.get_artist_from_json(html=raw_artist[0], genre=raw_artist[1], country=raw_artist[2]) for raw_artist in r.json()['aaData'] ] @@ -138,7 +139,7 @@ class EncyclopaediaMetallum(Page): sources=[ Source(SourcePages.ENCYCLOPAEDIA_METALLUM, artist_url) ], - notes = notes + notes=notes ) @classmethod @@ -164,7 +165,8 @@ class EncyclopaediaMetallum(Page): ) @classmethod - def get_song_from_json(cls, artist_html=None, album_html=None, release_type=None, title=None, lyrics_html=None) -> Song: + def get_song_from_json(cls, artist_html=None, album_html=None, release_type=None, title=None, + lyrics_html=None) -> Song: song_id = None if lyrics_html is not None: # Edit song lyrics @@ -172,7 +174,7 @@ class EncyclopaediaMetallum(Page): anchor = soup.find('a') raw_song_id = anchor.get('id') song_id = raw_song_id.replace("lyricsLink_", "") - + return Song( id_=song_id, title=title, @@ -191,6 +193,6 @@ class EncyclopaediaMetallum(Page): break if relevant_source is None: return artist - + print(relevant_source.url) - return artist \ No newline at end of file + return artist diff --git a/src/test.db b/src/test.db index 1a30c7b5e6268a0ddd6c3669463a86f0a9c12fa6..986df99b066814814e87cf13eb161e0bf14d5872 100644 GIT binary patch delta 1931 zcma)6Uuzsy9G%(Ow7WvQ*@rfk2whF2()4!k-DTYB-wqM%b1>|}%7*|;QURuTfjLV{EF^qc4L(#AxFIuU2%*!3jjKUEywdo9 z?ndY-dVo&RU5Lj`G=4kD;}HGZ&B6bXX0A-q91PK;btTEWYat_^-Fk$AGCBLB=qz0i zF}w+7jCLZl12Sx*Ym>EjAunBSrN}aH9~Ac<3o1O;r33@0;+i@FNublm=hAJs$O_vO9Aax|(xDf?TW9oBB-%aGd-K&P9%oO3`WR71?kYYq#iE38xo zFG&R<1e(N<>2IfR294B2vxQD0h~?8+Z}&%3G$9I^XC9&#$-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