Skip to content

Commit

Permalink
fit get_type and get_id functions into one
Browse files Browse the repository at this point in the history
using both needlessly searched the url twice
  • Loading branch information
nathom committed Mar 3, 2021
1 parent c995d9c commit e45ce11
Showing 1 changed file with 9 additions and 25 deletions.
34 changes: 9 additions & 25 deletions qobuz_dl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import string
import sys
import time
from typing import Tuple

import requests
from bs4 import BeautifulSoup as bso
Expand Down Expand Up @@ -98,29 +99,13 @@ def create_dir(self, directory=None):
os.makedirs(fix, exist_ok=True)
return fix

def get_id(self, url):
return re.match(
r"https?://(?:w{0,3}|play|open)\.qobuz\.com/(?:(?:album|track"
r"|artist|playlist|label)/|[a-z]{2}-[a-z]{2}/album/-?\w+(?:-\w+)*"
r"-?/|user/library/favorites/)(\w+)",
def get_url_info(url: str) -> Tuple[str, str]:
r = re.search(
r"(?:https:\/\/(?:w{3}|open|play)\.qobuz\.com)?(?:\/[a-z]{2}-[a-z]{2})"
r"?\/(album|artist|track|playlist|label)(?:\/[-\w\d]+)?\/([\w\d]+)",
url,
).group(1)

def get_type(self, url):
if re.match(r"https?", url) is not None:
url_type = url.split("/")[3]
if url_type not in ["album", "artist", "playlist", "track", "label"]:
if url_type == "user":
url_type = url.split("/")[-1]
else:
# url is from Qobuz store
# e.g. "https://www.qobuz.com/us-en/album/..."
url_type = url.split("/")[4]
else:
# url missing base
# e.g. "/us-en/album/{artist}/{id}"
url_type = url.split("/")[2]
return url_type
)
return r.groups()

def download_from_id(self, item_id, album=True, alt_path=None):
if handle_download_id(self.downloads_db, item_id, add_id=False):
Expand Down Expand Up @@ -167,9 +152,8 @@ def handle_url(self, url):
"track": {"album": False, "func": None, "iterable_key": None},
}
try:
url_type = self.get_type(url)
url_type, item_id = self.get_info(url)
type_dict = possibles[url_type]
item_id = self.get_id(url)
except (KeyError, IndexError):
logger.info(
f'{RED}Invalid url: "{url}". Use urls from ' "https://play.qobuz.com!"
Expand Down Expand Up @@ -423,7 +407,7 @@ def download_lastfm_pl(self, playlist_url):
)

for i in track_list:
track_id = self.get_id(self.search_by_type(i, "track", 1, lucky=True)[0])
track_id = self.get_url_info(self.search_by_type(i, "track", 1, lucky=True)[0])[1]
if track_id:
self.download_from_id(track_id, False, pl_directory)

Expand Down

0 comments on commit e45ce11

Please sign in to comment.