implemented genre in the cli

This commit is contained in:
Hellow 2023-04-04 22:30:14 +02:00
parent d500057315
commit 62b0102020
2 changed files with 39 additions and 11 deletions

View File

@ -2,11 +2,11 @@ import gc
import musicbrainzngs import musicbrainzngs
import logging import logging
import re import re
import os
from . import objects, pages from . import objects, pages
from .utils.shared import MUSIC_DIR, NOT_A_GENRE, MODIFY_GC, get_random_message from .utils.shared import MUSIC_DIR, NOT_A_GENRE, MODIFY_GC, get_random_message
if MODIFY_GC: if MODIFY_GC:
""" """
At the start I modify the garbage collector to run a bit fewer times. At the start I modify the garbage collector to run a bit fewer times.
@ -40,7 +40,32 @@ EXIT_COMMANDS = {
def cli(): 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 _search:
:param query: :param query:
@ -51,19 +76,19 @@ def cli():
if parsed in EXIT_COMMANDS: if parsed in EXIT_COMMANDS:
return True return True
if parsed == ".": if parsed == ".":
return False return False
if parsed == "..": if parsed == "..":
_search.goto_previous() _search.goto_previous()
return False return False
if parsed.isdigit(): if parsed.isdigit():
_search.choose_index(int(parsed)) _search.choose_index(int(parsed))
return False return False
if parsed in DOWNLOAD_COMMANDS: if parsed in DOWNLOAD_COMMANDS:
r = _search.download_chosen() r = _search.download_chosen(genre=genre)
print() print()
print(r) print(r)
@ -76,20 +101,23 @@ def cli():
if not _search.search_url(url.string): if not _search.search_url(url.string):
print("The given url couldn't be found.") print("The given url couldn't be found.")
return False return False
page = _search.get_page_from_query(parsed) page = _search.get_page_from_query(parsed)
if page is not None: if page is not None:
_search.choose_page(page) _search.choose_page(page)
return False return False
# if everything else is not valid search # if everything else is not valid search
_search.search(query) _search.search(query)
return False return False
genre = get_genre()
print(f"Selected: {genre}\n")
search = pages.Search() search = pages.Search()
while True: while True:
if next_search(search, input(">> ")): if next_search(search, input(">> "), genre):
break break
print(search) print(search)

View File

@ -130,7 +130,7 @@ class Search(Download):
return True return True
def download_chosen(self) -> DownloadResult: def download_chosen(self, genre: str = None, **kwargs) -> DownloadResult:
if self._current_option._derive_from is None: if self._current_option._derive_from is None:
return DownloadResult(error_message="No option has been chosen yet.") 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) page = self._get_page_from_source(source=source)
if page in self.audio_pages: 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}.") return DownloadResult(error_message=f"Didn't find a source for {self._current_option._derive_from.option_string}.")