2023-01-12 15:25:50 +00:00
|
|
|
from enum import Enum
|
2023-01-25 13:14:15 +00:00
|
|
|
from typing import List, Dict
|
2023-01-12 15:25:50 +00:00
|
|
|
|
2023-01-30 13:41:02 +00:00
|
|
|
from .metadata import Mapping, MetadataAttribute
|
2023-01-12 15:25:50 +00:00
|
|
|
from .parents import (
|
|
|
|
DatabaseObject,
|
2023-01-12 16:14:21 +00:00
|
|
|
SongAttribute,
|
2023-01-12 15:25:50 +00:00
|
|
|
)
|
|
|
|
|
2023-01-20 22:05:15 +00:00
|
|
|
|
|
|
|
class SourceTypes(Enum):
|
2023-01-16 14:37:04 +00:00
|
|
|
SONG = "song"
|
|
|
|
ALBUM = "album"
|
|
|
|
ARTIST = "artist"
|
|
|
|
LYRICS = "lyrics"
|
|
|
|
|
2023-01-20 22:05:15 +00:00
|
|
|
|
|
|
|
class SourcePages(Enum):
|
2023-01-12 15:25:50 +00:00
|
|
|
YOUTUBE = "youtube"
|
2023-01-20 22:05:15 +00:00
|
|
|
MUSIFY = "musify"
|
2023-01-16 14:37:04 +00:00
|
|
|
GENIUS = "genius"
|
|
|
|
MUSICBRAINZ = "musicbrainz"
|
|
|
|
ENCYCLOPAEDIA_METALLUM = "encyclopaedia metallum"
|
2023-01-31 23:07:13 +00:00
|
|
|
BANDCAMP = "bandcamp"
|
|
|
|
DEEZER = "deezer"
|
|
|
|
SPOTIFY = "spotify"
|
|
|
|
|
|
|
|
# This has nothing to do with audio, but bands can be here
|
|
|
|
INSTAGRAM = "instagram"
|
|
|
|
FACEBOOK = "facebook"
|
|
|
|
TWITTER = "twitter" # I will use nitter though lol
|
2023-01-12 15:25:50 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_homepage(cls, attribute) -> str:
|
|
|
|
homepage_map = {
|
2023-01-20 22:05:15 +00:00
|
|
|
cls.YOUTUBE: "https://www.youtube.com/",
|
|
|
|
cls.MUSIFY: "https://musify.club/",
|
|
|
|
cls.MUSICBRAINZ: "https://musicbrainz.org/",
|
2023-01-16 14:37:04 +00:00
|
|
|
cls.ENCYCLOPAEDIA_METALLUM: "https://www.metal-archives.com/",
|
2023-01-31 23:07:13 +00:00
|
|
|
cls.GENIUS: "https://genius.com/",
|
|
|
|
cls.BANDCAMP: "https://bandcamp.com/",
|
|
|
|
cls.DEEZER: "https://www.deezer.com/",
|
|
|
|
cls.INSTAGRAM: "https://www.instagram.com/",
|
|
|
|
cls.FACEBOOK: "https://www.facebook.com/",
|
|
|
|
cls.SPOTIFY: "https://open.spotify.com/",
|
|
|
|
cls.TWITTER: "https://twitter.com/"
|
2023-01-12 15:25:50 +00:00
|
|
|
}
|
|
|
|
return homepage_map[attribute]
|
|
|
|
|
|
|
|
|
2023-01-30 13:41:02 +00:00
|
|
|
class Source(DatabaseObject, SongAttribute, MetadataAttribute):
|
2023-01-12 15:25:50 +00:00
|
|
|
"""
|
|
|
|
create somehow like that
|
|
|
|
```python
|
|
|
|
# url won't be a valid one due to it being just an example
|
|
|
|
Source(src="youtube", url="https://youtu.be/dfnsdajlhkjhsd")
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
2023-01-20 22:05:15 +00:00
|
|
|
def __init__(self, page_enum, url: str, id_: str = None, type_enum=None) -> None:
|
2023-01-12 15:25:50 +00:00
|
|
|
DatabaseObject.__init__(self, id_=id_)
|
|
|
|
SongAttribute.__init__(self)
|
|
|
|
|
2023-01-20 09:56:40 +00:00
|
|
|
self.type_enum = type_enum
|
2023-01-20 22:05:15 +00:00
|
|
|
self.page_enum = page_enum
|
|
|
|
|
2023-01-12 15:25:50 +00:00
|
|
|
self.url = url
|
|
|
|
|
2023-02-01 08:10:05 +00:00
|
|
|
@classmethod
|
|
|
|
def match_url(cls, url: str):
|
|
|
|
"""
|
|
|
|
this shouldn't be used, unlesse you are not certain what the source is for
|
|
|
|
the reason is that it is more inefficient
|
|
|
|
"""
|
|
|
|
if url.startswith("https://www.youtube"):
|
|
|
|
return cls(SourcePages.YOUTUBE, url)
|
|
|
|
|
|
|
|
if url.startswith("https://www.deezer"):
|
|
|
|
return cls(SourcePages.DEEZER, url)
|
|
|
|
|
|
|
|
if url.startswith("https://open.spotify.com"):
|
|
|
|
return cls(SourcePages.SPOTIFY, url)
|
|
|
|
|
|
|
|
if "bandcamp" in url:
|
|
|
|
return cls(SourcePages.BANDCAMP, url)
|
|
|
|
|
|
|
|
if url.startswith("https://www.metal-archives.com/"):
|
|
|
|
return cls(SourcePages.ENCYCLOPAEDIA_METALLUM, url)
|
|
|
|
|
|
|
|
# the less important once
|
|
|
|
if url.startswith("https://www.facebook"):
|
|
|
|
return cls(SourcePages.FACEBOOK, url)
|
|
|
|
|
|
|
|
if url.startswith("https://www.instagram"):
|
|
|
|
return cls(SourcePages.INSTAGRAM, url)
|
|
|
|
|
|
|
|
if url.startswith("https://twitter"):
|
|
|
|
return cls(SourcePages.TWITTER, url)
|
|
|
|
|
2023-01-30 13:41:02 +00:00
|
|
|
def get_song_metadata(self) -> MetadataAttribute.Metadata:
|
|
|
|
return MetadataAttribute.Metadata({
|
|
|
|
Mapping.FILE_WEBPAGE_URL: [self.url],
|
|
|
|
Mapping.SOURCE_WEBPAGE_URL: [self.homepage]
|
|
|
|
})
|
|
|
|
|
|
|
|
def get_artist_metadata(self) -> MetadataAttribute.Metadata:
|
|
|
|
return MetadataAttribute.Metadata({
|
|
|
|
Mapping.ARTIST_WEBPAGE_URL: [self.url]
|
|
|
|
})
|
|
|
|
|
|
|
|
def get_metadata(self) -> MetadataAttribute.Metadata:
|
2023-01-20 22:05:15 +00:00
|
|
|
if self.type_enum == SourceTypes.SONG:
|
2023-01-30 13:41:02 +00:00
|
|
|
return self.get_song_metadata()
|
2023-01-20 22:05:15 +00:00
|
|
|
|
|
|
|
if self.type_enum == SourceTypes.ARTIST:
|
2023-01-30 13:41:02 +00:00
|
|
|
return self.get_artist_metadata()
|
2023-01-20 10:01:18 +00:00
|
|
|
|
2023-01-30 13:41:02 +00:00
|
|
|
return super().get_metadata()
|
2023-01-12 16:14:21 +00:00
|
|
|
|
2023-01-12 15:25:50 +00:00
|
|
|
def __str__(self):
|
2023-02-01 08:10:05 +00:00
|
|
|
return self.__repr__()
|
2023-01-12 15:25:50 +00:00
|
|
|
|
2023-01-30 13:41:02 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"Src({self.page_enum.value}: {self.url})"
|
|
|
|
|
2023-01-20 22:05:15 +00:00
|
|
|
page_str = property(fget=lambda self: self.page_enum.value)
|
2023-01-20 09:56:40 +00:00
|
|
|
type_str = property(fget=lambda self: self.type_enum.value)
|
2023-01-20 22:05:15 +00:00
|
|
|
homepage = property(fget=lambda self: SourcePages.get_homepage(self.page_enum))
|
2023-01-25 13:14:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SourceAttribute:
|
|
|
|
"""
|
|
|
|
This is a class that is meant to be inherited from.
|
|
|
|
it adds the source_list attribute to a class
|
|
|
|
"""
|
2023-02-25 21:16:32 +00:00
|
|
|
_source_dict: Dict[SourcePages, List[Source]]
|
2023-01-31 23:07:13 +00:00
|
|
|
source_url_map: Dict[str, Source]
|
2023-01-31 12:18:52 +00:00
|
|
|
|
2023-01-31 12:28:27 +00:00
|
|
|
def __new__(cls, **kwargs):
|
2023-01-31 12:18:52 +00:00
|
|
|
new = object.__new__(cls)
|
|
|
|
new._source_dict = {page_enum: list() for page_enum in SourcePages}
|
2023-01-31 23:07:13 +00:00
|
|
|
new.source_url_map = dict()
|
2023-01-31 12:18:52 +00:00
|
|
|
return new
|
2023-01-25 13:14:15 +00:00
|
|
|
|
2023-01-31 23:07:13 +00:00
|
|
|
def match_source_with_url(self, url: str) -> bool:
|
|
|
|
"""
|
|
|
|
this function returns true, if a source with this url exists,
|
|
|
|
else it returns false
|
|
|
|
:param url:
|
|
|
|
:return source_with_url_exists:
|
|
|
|
"""
|
|
|
|
return url in self.source_url_map
|
|
|
|
|
|
|
|
def match_source(self, source: Source) -> bool:
|
|
|
|
return self.match_source_with_url(source.url)
|
|
|
|
|
2023-01-25 13:14:15 +00:00
|
|
|
def add_source(self, source: Source):
|
|
|
|
"""
|
|
|
|
adds a new Source to the sources
|
|
|
|
"""
|
2023-01-31 23:07:13 +00:00
|
|
|
if self.match_source(source):
|
|
|
|
return
|
|
|
|
self.source_url_map[source.url] = source
|
2023-01-27 11:56:59 +00:00
|
|
|
self._source_dict[source.page_enum].append(source)
|
2023-01-25 13:14:15 +00:00
|
|
|
|
|
|
|
def get_sources_from_page(self, page_enum) -> List[Source]:
|
|
|
|
"""
|
|
|
|
getting the sources for a specific page like
|
|
|
|
youtube or musify
|
|
|
|
"""
|
|
|
|
return self._source_dict[page_enum]
|
|
|
|
|
|
|
|
def get_source_list(self) -> List[Source]:
|
|
|
|
"""
|
|
|
|
gets all sources
|
|
|
|
"""
|
2023-01-30 13:41:02 +00:00
|
|
|
return [item for _, page_list in self._source_dict.items() for item in page_list]
|
2023-01-27 11:56:59 +00:00
|
|
|
|
|
|
|
def set_source_list(self, source_list: List[Source]):
|
|
|
|
self._source_dict = {page_enum: list() for page_enum in SourcePages}
|
|
|
|
|
|
|
|
for source in source_list:
|
|
|
|
self.add_source(source)
|
2023-01-25 13:14:15 +00:00
|
|
|
|
2023-01-30 13:41:02 +00:00
|
|
|
def get_source_dict(self) -> Dict[object, List[Source]]:
|
2023-01-25 13:14:15 +00:00
|
|
|
"""
|
|
|
|
gets a dictionary of all Sources,
|
|
|
|
where the key is a page enum,
|
|
|
|
and the value is a List with all sources of according page
|
|
|
|
"""
|
|
|
|
return self._source_dict
|
|
|
|
|
2023-01-27 11:56:59 +00:00
|
|
|
source_list: List[Source] = property(fget=get_source_list, fset=set_source_list)
|
2023-01-30 13:41:02 +00:00
|
|
|
source_dict: Dict[object, List[Source]] = property(fget=get_source_dict)
|