From a0975ea5f2de2503b6d437f42659cd96a4ec122c Mon Sep 17 00:00:00 2001 From: vitiko98 Date: Mon, 15 Mar 2021 00:18:56 -0400 Subject: [PATCH] Add possible solution for _gen_pages --- qobuz_dl_rewrite/clients.py | 14 +++++++++++--- qobuz_dl_rewrite/core.py | 11 ++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/qobuz_dl_rewrite/clients.py b/qobuz_dl_rewrite/clients.py index 858d13c..61d7165 100644 --- a/qobuz_dl_rewrite/clients.py +++ b/qobuz_dl_rewrite/clients.py @@ -168,9 +168,17 @@ class QobuzClient(SecureClientInterface): def _gen_pages(self, epoint: str, params): page, status_code = self._api_request(epoint, params) - total = page["albums"]["total"] - limit = page["albums"]["limit"] - offset = page["albums"]["offset"] + logger.debug("Keys returned from _gen_pages: %s", ", ".join(page.keys())) + key = epoint.split("/")[0] + "s" + total = page.get(key, {}) + total = total.get("total") or total.get("items") + + if not total: + logger.debug("Nothing found from %s epoint", epoint) + return + + limit = page.get(key, {}).get("limit", 500) + offset = page.get(key, {}).get("offset", 0) params.update({"limit": limit}) yield page while (offset + limit) < total: diff --git a/qobuz_dl_rewrite/core.py b/qobuz_dl_rewrite/core.py index 27b686f..673a791 100644 --- a/qobuz_dl_rewrite/core.py +++ b/qobuz_dl_rewrite/core.py @@ -1,6 +1,7 @@ import logging import os import re + # ------- Testing ---------- from typing import Generator, Sequence, Tuple, Union @@ -87,12 +88,4 @@ class QobuzDL: raise ParsingError("Error parsing URLs from file `{filepath}`") def search(self, query: str, media_type: str, limit: int = 200) -> Generator: - results = self.client.search(query, media_type=media_type, limit=limit) - key = media_type + "s" - - # Temporal to test Deezer - iterable = results.get(key, {}).get("items") or results.get("data", []) - - return ( - MEDIA_CLASS[media_type].from_api(item, self.client) for item in iterable - ) + return self.client.search(query, media_type, limit)