From 62b010202098c53136d8b67b98d8cf8c90d21294 Mon Sep 17 00:00:00 2001 From: Hellow Date: Tue, 4 Apr 2023 22:30:14 +0200 Subject: [PATCH] implemented genre in the cli --- src/music_kraken/__init__.py | 46 +++++++++++++++---- .../pages/download_center/search.py | 4 +- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/music_kraken/__init__.py b/src/music_kraken/__init__.py index d043b19..a4e1176 100644 --- a/src/music_kraken/__init__.py +++ b/src/music_kraken/__init__.py @@ -2,11 +2,11 @@ import gc import musicbrainzngs import logging import re +import os from . import objects, pages from .utils.shared import MUSIC_DIR, NOT_A_GENRE, MODIFY_GC, get_random_message - if MODIFY_GC: """ At the start I modify the garbage collector to run a bit fewer times. @@ -40,7 +40,32 @@ EXIT_COMMANDS = { def cli(): - def next_search(_search: pages.Search, query: str) -> bool: + def get_genre(): + def get_existing_genre(): + valid_directories = [] + for elem in os.listdir(MUSIC_DIR): + if elem not in NOT_A_GENRE: + valid_directories.append(elem) + + return valid_directories + + existing_genres = get_existing_genre() + print("Available genres:") + for i, genre_option in enumerate(existing_genres): + print(f"{i}: {genre_option}") + + genre = input("Input the ID for an existing genre or text for a new one: ") + + if genre.isdigit(): + genre_id = int(genre) + if genre_id >= len(existing_genres): + logging.warning("An invalid genre id has been given") + return get_genre() + return existing_genres[genre_id] + + return genre + + def next_search(_search: pages.Search, query: str, genre: str) -> bool: """ :param _search: :param query: @@ -51,19 +76,19 @@ def cli(): if parsed in EXIT_COMMANDS: return True - + if parsed == ".": return False if parsed == "..": _search.goto_previous() return False - + if parsed.isdigit(): _search.choose_index(int(parsed)) return False - + if parsed in DOWNLOAD_COMMANDS: - r = _search.download_chosen() + r = _search.download_chosen(genre=genre) print() print(r) @@ -76,20 +101,23 @@ def cli(): if not _search.search_url(url.string): print("The given url couldn't be found.") return False - + page = _search.get_page_from_query(parsed) if page is not None: _search.choose_page(page) return False - + # if everything else is not valid search _search.search(query) return False + genre = get_genre() + print(f"Selected: {genre}\n") + search = pages.Search() while True: - if next_search(search, input(">> ")): + if next_search(search, input(">> "), genre): break print(search) diff --git a/src/music_kraken/pages/download_center/search.py b/src/music_kraken/pages/download_center/search.py index ba97137..34daa45 100644 --- a/src/music_kraken/pages/download_center/search.py +++ b/src/music_kraken/pages/download_center/search.py @@ -130,7 +130,7 @@ class Search(Download): return True - def download_chosen(self) -> DownloadResult: + def download_chosen(self, genre: str = None, **kwargs) -> DownloadResult: if self._current_option._derive_from is None: return DownloadResult(error_message="No option has been chosen yet.") @@ -139,7 +139,7 @@ class Search(Download): page = self._get_page_from_source(source=source) if page in self.audio_pages: - return page.download(music_object=self._current_option._derive_from) + return page.download(music_object=self._current_option._derive_from, genre=None, **kwargs) return DownloadResult(error_message=f"Didn't find a source for {self._current_option._derive_from.option_string}.")