finished database integration and simmilar

This commit is contained in:
Hellow 2023-01-30 18:27:49 +01:00
parent 3ddddf2f5f
commit 06cc826a21
8 changed files with 51 additions and 103 deletions

View File

@ -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()

View File

@ -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:

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -5,7 +5,6 @@ from .metadata import Mapping, MetadataAttribute
from .parents import (
DatabaseObject,
SongAttribute,
ID3Metadata
)

View File

@ -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:
# <a href="javascript:;" id="lyricsLink_5948443" title="Toggle lyrics display" class="viewLyrics iconContainer ui-state-default"><span class="ui-icon ui-icon-script">Edit song lyrics</span></a>
@ -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
return artist

Binary file not shown.