This commit is contained in:
Hellow2 2023-05-23 13:59:24 +02:00
parent da0fa2c8dc
commit ab7f414125
9 changed files with 94 additions and 26 deletions

View File

@ -1,8 +1,8 @@
from typing import Optional, Tuple, Type, Set, Union, List
from . import page_attributes
from ..abstract import Page
from ...objects import Song, Album, Artist, Label, Source
from ..pages import Page
from ..objects import Song, Album, Artist, Label, Source
MusicObject = Union[Song, Album, Artist, Label]

View File

@ -2,8 +2,8 @@ from collections import defaultdict
from typing import Tuple, List, Dict, Type
from . import page_attributes
from ..abstract import Page
from ...objects import Options, DatabaseObject, Source
from ..pages import Page
from ..objects import Options, DatabaseObject, Source
class MultiPageOptions:

View File

@ -1,9 +1,8 @@
from typing import Tuple, Type, Dict
from ...utils.enums.source import SourcePages
from ..abstract import Page
from ..encyclopaedia_metallum import EncyclopaediaMetallum
from ..musify import Musify
from ..utils.enums.source import SourcePages
from ..pages import Page, EncyclopaediaMetallum, Musify
NAME_PAGE_MAP: Dict[str, Type[Page]] = dict()
PAGE_NAME_MAP: Dict[Type[Page], str] = dict()

View File

@ -1,12 +1,12 @@
from typing import Tuple, List, Set, Type, Optional
from typing import Tuple, List, Set, Type, Optional, Dict
from . import page_attributes
from .download import Download
from .multiple_options import MultiPageOptions
from ..abstract import Page
from ..support_classes.download_result import DownloadResult
from ...objects import DatabaseObject, Source
from ...utils.enums.source import SourcePages
from ..pages.abstract import Page
from ..utils.support_classes import DownloadResult, Query
from ..objects import DatabaseObject, Source, Artist, Song, Album
from ..utils.enums.source import SourcePages
class Search(Download):
@ -52,6 +52,24 @@ class Search(Download):
self._current_option = self._option_history[-1]
return self._option_history[-1]
def _process_parsed(self, key_text: Dict[str, str], query: str) -> Query:
song = None if not "t" in key_text else Song(title=key_text["t"], dynamic=True)
album = None if not "r" in key_text else Album(title=key_text["r"], dynamic=True)
artist = None if not "a" in key_text else Artist(name=key_text["a"], dynamic=True)
if song is not None:
song.album_collection.append(album)
song.main_artist_collection.append(artist)
return Query(raw_query=query, music_object=song)
if album is not None:
album.artist_collection.append(artist)
return Query(raw_query=query, music_object=album)
if artist is not None:
return Query(raw_query=query, music_object=artist)
def search(self, query: str):
"""
@ -65,9 +83,54 @@ class Search(Download):
doesn't set derived_from thus,
can't download right after
"""
special_characters = "#\\"
query = query + " "
key_text = {}
skip_next = False
escape_next = False
new_text = ""
latest_key: str = None
for i in range(len(query) - 1):
currenct_char = query[i]
next_char = query[i+1]
if skip_next:
skip_next = False
continue
if escape_next:
new_text += currenct_char
escape_next = False
# escaping
if currenct_char == "\\":
if next_char in special_characters:
escape_next = True
continue
if currenct_char == "#":
if latest_key is not None:
key_text[latest_key]
latest_key = next_char
skip_next = True
continue
new_text += currenct_char
if latest_key is not None:
key_text[latest_key] = new_text
parsed_query: Query = self._process_parsed(key_text, query)
for page in self.pages:
self._current_option[page] = page._raw_search(query=query)
for search in parsed_query.default_search:
self._current_option[page].extend(page._raw_search(query=search))
def choose_page(self, page: Type[Page]):
"""

View File

@ -1,4 +1,5 @@
from ..utils.enums import album
from .option import Options
from . import (
song,
metadata,
@ -28,5 +29,4 @@ Album = song.Album
FormattedText = formatted_text.FormattedText
Options = option.Options
Collection = collection.Collection

View File

@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, List
from typing import TYPE_CHECKING, List, Iterable
if TYPE_CHECKING:
from .parents import DatabaseObject
@ -14,6 +14,13 @@ class Options:
def __iter__(self):
for database_object in self._data:
yield database_object
def append(self, element: 'DatabaseObject'):
self._data.append(element)
def extend(self, iterable: Iterable['DatabaseObject']):
for element in iterable:
self.append(element)
def get_next_options(self, index: int) -> 'Options':
if index >= len(self._data):

View File

@ -1,5 +1,3 @@
from .encyclopaedia_metallum import EncyclopaediaMetallum
from .musify import Musify
EncyclopaediaMetallum = EncyclopaediaMetallum
Musify = Musify
from .abstract import Page

View File

@ -9,6 +9,7 @@ from ..utils.shared import ENCYCLOPAEDIA_METALLUM_LOGGER
from .abstract import Page
from ..utils.enums.source import SourcePages
from ..utils.enums.album import AlbumType
from ..utils.support_classes import Query
from ..objects import (
Lyrics,
Artist,
@ -51,7 +52,7 @@ class EncyclopaediaMetallum(Page):
return cls.advanced_search(query_obj)
@classmethod
def advanced_search(cls, query: Page.Query) -> Options:
def advanced_search(cls, query: Query) -> Options:
if query.song is not None:
return Options(cls.search_for_song(query=query))
if query.album is not None:
@ -61,7 +62,7 @@ class EncyclopaediaMetallum(Page):
return Options
@classmethod
def search_for_song(cls, query: Page.Query) -> List[Song]:
def search_for_song(cls, query: 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&_" \
@ -82,7 +83,7 @@ class EncyclopaediaMetallum(Page):
) for raw_song in r.json()['aaData']]
@classmethod
def search_for_album(cls, query: Page.Query) -> List[Album]:
def search_for_album(cls, query: 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" \
@ -100,7 +101,7 @@ class EncyclopaediaMetallum(Page):
) for raw_album in r.json()['aaData']]
@classmethod
def search_for_artist(cls, query: Page.Query) -> List[Artist]:
def search_for_artist(cls, query: 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" \
@ -122,7 +123,7 @@ class EncyclopaediaMetallum(Page):
]
@classmethod
def simple_search(cls, query: Page.Query) -> List[Artist]:
def simple_search(cls, query: Query) -> List[Artist]:
"""
Searches the default endpoint from metal archives, which intern searches only
for bands, but it is the default, thus I am rolling with it

View File

@ -25,7 +25,7 @@ from ..objects import (
)
from ..utils.shared import MUSIFY_LOGGER
from ..utils import string_processing, shared
from ..utils.support_classes import DownloadResult
from ..utils.support_classes import DownloadResult, Query
"""
https://musify.club/artist/ghost-bath-280348?_pjax=#bodyContent
@ -120,7 +120,7 @@ class Musify(Page):
return cls.plaintext_search(cls.get_plaintext_query(query_obj))
@classmethod
def get_plaintext_query(cls, query: Page.Query) -> str:
def get_plaintext_query(cls, query: Query) -> str:
if query.album is None:
return f"{query.artist or '*'} - {query.song or '*'}"
return f"{query.artist or '*'} - {query.album or '*'} - {query.song or '*'}"