Check database when searching

Signed-off-by: nathom <nathanthomas707@gmail.com>
This commit is contained in:
nathom 2021-07-11 11:31:29 -07:00
parent 2b68b96204
commit 4abe96825f
2 changed files with 25 additions and 3 deletions

View file

@ -532,6 +532,7 @@ class MusicDL(list):
source: str,
query: str,
media_type: str = "album",
check_db: bool = False,
limit: int = 200,
) -> Generator:
"""Universal search.
@ -559,9 +560,17 @@ class MusicDL(list):
else page["albums"]["items"]
)
for i, item in enumerate(tracklist):
if item_id := item["id"] in self.db:
click.secho(
f"ID {item_id} already logged in database. Skipping.",
fg="magenta",
)
continue
yield MEDIA_CLASS[ # type: ignore
media_type if media_type != "featured" else "album"
].from_api(item, client)
if i > limit:
return
else:
@ -611,7 +620,11 @@ class MusicDL(list):
return ret
def interactive_search(
self, query: str, source: str = "qobuz", media_type: str = "album"
self,
query: str,
source: str = "qobuz",
media_type: str = "album",
limit: int = 50,
):
"""Show an interactive menu that contains search results.
@ -622,7 +635,7 @@ class MusicDL(list):
:param media_type:
:type media_type: str
"""
results = tuple(self.search(source, query, media_type, limit=50))
results = tuple(self.search(source, query, media_type, limit=limit))
def title(res):
if isinstance(res[1], Album):

View file

@ -73,7 +73,16 @@ class Database:
return bool(conn.execute(command, tuple(items.values())).fetchone()[0])
def __contains__(self, keys: dict) -> bool:
return self.contains(**keys)
if isinstance(keys, dict):
return self.contains(**keys)
if isinstance(keys, str) and len(self.structure) == 1:
only_key = tuple(self.structure.keys())[0]
query = {only_key: keys}
logger.debug("Searching for %s in database", query)
return self.contains(**query)
raise TypeError(keys)
def add(self, items: List[str]):
"""Add a row to the table.