From f5f41705f3efa9c8a71e077809761422fea8a503 Mon Sep 17 00:00:00 2001 From: 7x11x13 Date: Fri, 26 Nov 2021 10:21:57 -0500 Subject: [PATCH] Allow downloading of mobile links --- scdl/__init__.py | 2 +- scdl/scdl.py | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/scdl/__init__.py b/scdl/__init__.py index 74fdb31..7dc1d99 100644 --- a/scdl/__init__.py +++ b/scdl/__init__.py @@ -2,4 +2,4 @@ """Python Soundcloud Music Downloader.""" -__version__ = "v2.2.5" \ No newline at end of file +__version__ = "v2.3.0" \ No newline at end of file diff --git a/scdl/scdl.py b/scdl/scdl.py index 0244022..824714d 100755 --- a/scdl/scdl.py +++ b/scdl/scdl.py @@ -76,6 +76,7 @@ import sys import tempfile import time import traceback +import urllib.parse import warnings from dataclasses import asdict @@ -218,17 +219,42 @@ def main(): if arguments["me"]: # set url to profile associated with auth token arguments["-l"] = client.get_me().permalink_url + + arguments["-l"] = validate_url(arguments["-l"]) # convert arguments dict to python_args (kwargs-friendly args) for key, value in arguments.items(): key = key.strip("-").replace("-", "_") python_args[key] = value - download_url(client, **python_args) if arguments["--remove"]: remove_files() +def validate_url(url: str): + """ + If url is a valid soundcloud.com url, return it. + Otherwise, try to fix the url so that it is valid. + If it cannot be fixed, exit the program. + """ + if url.startswith("https://m.soundcloud.com") or url.startswith("http://m.soundcloud.com") or url.startswith("m.soundcloud.com"): + url = url.replace("m.", "", 1) + if url.startswith("https://www.soundcloud.com") or url.startswith("http://www.soundcloud.com") or url.startswith("www.soundcloud.com"): + url = url.replace("www.", "", 1) + if url.startswith("soundcloud.com"): + url = "https://" + url + if url.startswith("https://soundcloud.com") or url.startswith("http://soundcloud.com"): + return url + + # see if link redirects to soundcloud.com + resp = requests.get(url) + url = urllib.parse.urljoin(resp.url, urllib.parse.urlparse(resp.url).path) + if url.startswith("https://soundcloud.com") or url.startswith("http://soundcloud.com"): + return url + + logger.error("URL is not valid") + sys.exit(1) + def get_config(config_file: pathlib.Path) -> configparser.ConfigParser: """ Gets config from scdl.cfg