From 0a0fb144c0215d336d08eace16f9a4a717a1a924 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Sat, 29 Apr 2023 11:06:54 -0400 Subject: [PATCH] Handle Mediafire link when in Javascript --- c3dbdl/c3dbdl.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/c3dbdl/c3dbdl.py b/c3dbdl/c3dbdl.py index 18e3821..387bbbb 100755 --- a/c3dbdl/c3dbdl.py +++ b/c3dbdl/c3dbdl.py @@ -286,34 +286,35 @@ def downloadFile(download_url, download_path, download_filename): def parseC3Universe(dl_link): try: p = requests.get(dl_link) - if p.status_code != 200: - raise HTTPError(dl_link, p.status_code, "", None, None) - parsed_html = BeautifulSoup(p.text, "html.parser") - download_url = ( + download_element = ( parsed_html.body.find("div", attrs={"class": "lock-head"}) .find("a") - .get("href") ) + download_url = download_element.get("href") return download_url except Exception as e: - click.echo(f"Failed parsing or retrieving HTML link: {e}") return None -def parseMediafire(dl_link): +def parseMediafirePage(dl_link): try: p = requests.get(dl_link) - if p.status_code != 200: - raise HTTPError(dl_link, p.status_code, "", None, None) - parsed_html = BeautifulSoup(p.text, "html.parser") - download_url = parsed_html.find( - "a", attrs={"id": "downloadButton", "rel": "nofollow", "aria-label": "Download file"} - ).get("href") + download_element = parsed_html.find( + "a", attrs={"aria-label": "Download file"} + ) + download_url = download_element.get("href") + return download_url + except Exception as e: + return None + +def parseMediafireJS(dl_link): + try: + p = requests.get(dl_link) + download_url = re.find(r"'(http://download[0-9]+.mediafire.com/.*)';", p.text).group(1) return download_url except Exception as e: - click.echo(f"Failed parsing or retrieving HTML link: {e}") return None def downloadSong(destination, filename, entry, dlid, dldesc): @@ -345,13 +346,16 @@ def downloadSong(destination, filename, entry, dlid, dldesc): if 'dl.c3universe.com' in dl_link['link']: download_url = parseC3Universe(dl_link["link"]) elif 'www.mediafire.com' in dl_link["link"]: - download_url = parseMediafire(dl_link["link"]) + download_url = parseMediafirePage(dl_link["link"]) + if download_url is None: + download_url = parseMediafireJS(dl_link["link"]) else: click.echo("Download URL is not valid for CLI download; skipping...") click.echo(f"URL: {dl_link['link']}") continue if download_url is None: + click.echo(f"Failed parsing or retrieving file URL from link {dl_link}, skipping...") continue download_filename = filename.format(