From 7743ff1b9e5170000a48da6863c6b6c1ec0c701f Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Mon, 12 Jun 2023 17:40:54 +0200 Subject: [PATCH] continued download --- documentation/shell.md | 57 ++++++++++++++++++++ src/music_kraken/cli/download/shell.py | 46 +++++++++++++++- src/music_kraken/connection/connection.py | 1 + src/music_kraken/download/page_attributes.py | 7 ++- src/music_kraken/pages/abstract.py | 2 +- 5 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 documentation/shell.md diff --git a/documentation/shell.md b/documentation/shell.md new file mode 100644 index 0000000..de72743 --- /dev/null +++ b/documentation/shell.md @@ -0,0 +1,57 @@ +# Shell + +## Searching + +```mkshell +> s: {querry or url} + +# examples +> s: https://musify.club/release/some-random-release-183028492 +> s: r: #a an Artist #r some random Release +``` + +Searches for an url, or an query + +### Query Syntax + +``` +#a {artist} #r {release} #t {track} +``` + +You can escape stuff like `#` doing this: `\#` + +## Downloading + +To download something, you either need a direct link, or you need to have already searched for options + +```mkshell +> d: {option ids or direct url} + +# examples +> d: 0, 3, 4 +> d: 1 +> d: https://musify.club/release/some-random-release-183028492 +``` + +## Misc + +### Exit + +```mkshell +> q +> quit +> exit +> abort +``` + +### Current Options + +```mkshell +> . +``` + +### Previous Options + +``` +> .. +``` diff --git a/src/music_kraken/cli/download/shell.py b/src/music_kraken/cli/download/shell.py index 8cb627a..adb7bfb 100644 --- a/src/music_kraken/cli/download/shell.py +++ b/src/music_kraken/cli/download/shell.py @@ -5,7 +5,7 @@ import re from ...utils.shared import MUSIC_DIR, NOT_A_GENRE_REGEX from ...utils.regex import URL_PATTERN from ...utils.string_processing import fit_to_file_system -from ...utils.support_classes import Query +from ...utils.support_classes import Query, DownloadResult from ...download.results import Results, SearchResults, Option, PageResults from ...download.page_attributes import Pages from ...pages import Page @@ -211,8 +211,15 @@ class Shell: if artist is not None: return Query(raw_query=query, music_object=artist) + + return Query(raw_query=query) def search(self, query: str): + if re.match(URL_PATTERN, query) is not None: + self.set_current_options(*self.pages.fetch_url(re.match(URL_PATTERN, query))) + self.print_current_options() + return + special_characters = "#\\" query = query + " " @@ -266,7 +273,7 @@ class Shell: try: page, music_object = self.current_results.get_music_object_by_index(index) - except IndexError: + except KeyError: print() print(f"The option {index} doesn't exist.") print() @@ -280,6 +287,41 @@ class Shell: def download(self, download_str: str, download_all: bool = False) -> bool: + to_download: List[DatabaseObject] = [] + + if re.match(URL_PATTERN, download_str) is not None: + _, music_objects = self.pages.fetch_url(re.match(URL_PATTERN, download_str)) + to_download.append(music_objects) + + else: + index: str + for index in download_str.split(", "): + if not index.strip().isdigit(): + print() + print(f"Every download thingie has to be an index, not {index}.") + print() + return False + + for index in download_str.split(", "): + to_download.append(self.current_results.get_music_object_by_index(int(index))[1]) + + print() + print("Downloading:") + for download_object in to_download: + print(download_object.option_string) + print() + + _result_map: Dict[DatabaseObject, DownloadResult] = dict() + + for database_object in to_download: + r = self.pages.download(music_object=database_object, genre=self.genre, download_all=download_all) + _result_map[database_object] = r + + for music_object, result in _result_map.items(): + print() + print(music_object.option_string) + print(result) + return False def process_input(self, input_str: str) -> bool: diff --git a/src/music_kraken/connection/connection.py b/src/music_kraken/connection/connection.py index 70c77ea..02eba18 100644 --- a/src/music_kraken/connection/connection.py +++ b/src/music_kraken/connection/connection.py @@ -131,6 +131,7 @@ class Connection: accepted_response_code=accepted_response_code, url=url, timeout=timeout, + headers=headers, **kwargs ) diff --git a/src/music_kraken/download/page_attributes.py b/src/music_kraken/download/page_attributes.py index bb392f8..11598d0 100644 --- a/src/music_kraken/download/page_attributes.py +++ b/src/music_kraken/download/page_attributes.py @@ -80,11 +80,11 @@ class Pages: audio_pages = self._audio_pages_set.intersection(_page_types) for download_page in audio_pages: - return self._page_instances[download_page].download(genre=genre, download_all=download_all) + return self._page_instances[download_page].download(music_object=music_object, genre=genre, download_all=download_all) return DownloadResult(error_message=f"No audio source has been found for {music_object}.") - def fetch_url(self, url: str, stop_at_level: int = 2) -> DatabaseObject: + def fetch_url(self, url: str, stop_at_level: int = 2) -> Tuple[Type[Page], DatabaseObject]: source = Source.match_url(url, SourcePages.MANUAL) if source is None: @@ -92,5 +92,4 @@ class Pages: _actual_page = self._source_to_page[source.page_enum] - - return self._page_instances[_actual_page].fetch_object_from_source(source=source, stop_at_level=stop_at_level) \ No newline at end of file + return _actual_page, self._page_instances[_actual_page].fetch_object_from_source(source=source, stop_at_level=stop_at_level) \ No newline at end of file diff --git a/src/music_kraken/pages/abstract.py b/src/music_kraken/pages/abstract.py index 2895cf0..d2c72b5 100644 --- a/src/music_kraken/pages/abstract.py +++ b/src/music_kraken/pages/abstract.py @@ -268,7 +268,7 @@ class Page(): nonlocal naming_objects for collection_name in naming_music_object.UPWARDS_COLLECTION_ATTRIBUTES: - collection: Collection = getattr(self, collection_name) + collection: Collection = getattr(naming_music_object, collection_name) if collection.empty(): continue