Compare commits

..

4 Commits

View File

@ -153,8 +153,6 @@ def fetchSongData(entries):
for link_entry in download_links:
link = link_entry.get("href")
description = link_entry.get_text().strip()
if "c3universe.com" not in link:
continue
messages.append(f"Found download link: {link} ({description})")
dl_links.append(
{
@ -252,66 +250,7 @@ def buildDatabase(pages, concurrency):
return found_songs
def downloadSong(destination, filename, entry, dlid, dldesc):
click.echo(
f"""> Downloading song "{entry['artist']} - {entry['title']}" by {entry['author']}..."""
)
if dlid is None:
dl_links = entry["dl_links"]
else:
try:
dl_links = [entry["dl_links"][dlid - 1]]
except Exception:
click.echo(f"Invalid download link ID {dlid}.")
return
if dldesc is not None:
new_dl_links = list()
for link in dl_links:
if dldesc in link["description"]:
new_dl_links.append(link)
dl_links = new_dl_links
if not dl_links:
click.echo(f'No download link matching description "{dldesc}" found.')
return
for dl_link in dl_links:
try:
p = requests.get(dl_link["link"])
if p.status_code != 200:
raise HTTPError(dl_link["link"], p.status_code, "", None, None)
parsed_html = BeautifulSoup(p.text, "html.parser")
download_url = (
parsed_html.body.find("div", attrs={"class": "lock-head"})
.find("a")
.get("href")
)
except Exception as e:
click.echo(f"Failed parsing or retrieving HTML link: {e}")
continue
download_filename = filename.format(
genre=entry["genre"],
artist=entry["artist"],
album=entry["album"],
title=entry["title"],
year=entry["year"],
author=entry["author"],
orig_name=download_url.split("/")[-1],
)
download_filename = f"{destination}/{download_filename}"
download_path = "/".join(f"{download_filename}".split("/")[0:-1])
click.echo(
f"""Downloading file "{dl_link['description']}" from {download_url}..."""
)
if os.path.exists(download_filename):
click.echo(f"File exists at {download_filename}")
continue
def downloadFile(download_url, download_path, download_filename):
attempts = 1
p = None
try:
@ -342,8 +281,98 @@ def downloadSong(destination, filename, entry, dlid, dldesc):
click.echo(f"Successfully downloaded to {download_filename}")
except Exception as e:
click.echo(f"Download attempt failed: {e}")
return None
def parseC3Universe(dl_link):
try:
p = requests.get(dl_link)
parsed_html = BeautifulSoup(p.text, "html.parser")
download_element = (
parsed_html.body.find("div", attrs={"class": "lock-head"})
.find("a")
)
download_url = download_element.get("href")
return download_url
except Exception as e:
click.echo(f"Failed parsing or retrieving file URL from link {dl_link}: {e}")
return None
def parseMediafire(dl_link):
try:
p = requests.get(dl_link)
parsed_html = BeautifulSoup(p.text, "html.parser")
download_element = parsed_html.find(
"a", attrs={"aria-label": "Download file"}
)
if download_element is not None:
download_url = download_element.get("href")
else:
download_url = re.search(r"'(http[s]*://download[0-9]+.mediafire.com/.*)';", p.text).group(1)
return download_url
except Exception as e:
click.echo(f"Failed parsing or retrieving file URL from link {dl_link}: {e}")
return None
def downloadSong(destination, filename, entry, dlid, dldesc):
click.echo(
f"""> Downloading song "{entry['artist']} - {entry['title']}" by {entry['author']}..."""
)
if dlid is None:
dl_links = entry["dl_links"]
else:
try:
dl_links = [entry["dl_links"][dlid - 1]]
except Exception:
click.echo(f"Invalid download link ID {dlid}.")
return
if dldesc is not None:
new_dl_links = list()
for link in dl_links:
if dldesc in link["description"]:
new_dl_links.append(link)
dl_links = new_dl_links
if not dl_links:
click.echo(f'No download link matching description "{dldesc}" found.')
return
for dl_link in dl_links:
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"])
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"No valid download URL found, skipping...")
continue
download_filename = filename.format(
genre=entry["genre"],
artist=entry["artist"],
album=entry["album"],
title=entry["title"],
year=entry["year"],
author=entry["author"],
orig_name=download_url.split("/")[-1],
)
download_filename = f"{destination}/{download_filename}"
download_path = "/".join(f"{download_filename}".split("/")[0:-1])
click.echo(
f"""Downloading file "{dl_link['description']}" from {download_url}..."""
)
if os.path.exists(download_filename):
click.echo(f"File exists at {download_filename}")
continue
downloadFile(download_url, download_path, download_filename)
@click.command(name="build", short_help="Build the local database.")
@click.option(
@ -594,7 +623,13 @@ def download(_filters, _id, _desc, _limit, _file_structure):
for information_filter in song_information_filters:
filter_field = information_filter[0].lower()
filter_value = information_filter[1].lower()
if re.match("^~", filter_value):
if re.match("^!", filter_value):
filter_value = filter_value.replace("!", "")
if filter_value in song[filter_field].lower():
pending_information_filters.append(False)
else:
pending_information_filters.append(True)
elif re.match("^~", filter_value):
filter_value = filter_value.replace("~", "")
if filter_value in song[filter_field].lower():
pending_information_filters.append(True)
@ -694,6 +729,13 @@ def search(_filters):
For example, to match all songs with "Word" in their titles:
--filter title ~word
A filter can be negated by adding an exclamation mark ("!") to the beginning of the
"<value>". Note that "!" must be escaped or single-quoted under BASH.
\b
For example, to match all songs except those by Yes as their artist:
--filter artist '!Yes'
Instrument filters allow selection of the presence of instruments. If an instrument
fitler is given, only songs which contain parts for the given instrument(s) will be
shown.
@ -750,7 +792,13 @@ def search(_filters):
for information_filter in song_information_filters:
filter_field = information_filter[0].lower()
filter_value = information_filter[1].lower()
if re.match("^~", filter_value):
if re.match("^!", filter_value):
filter_value = filter_value.replace("!", "")
if filter_value in song[filter_field].lower():
pending_information_filters.append(False)
else:
pending_information_filters.append(True)
elif re.match("^~", filter_value):
filter_value = filter_value.replace("~", "")
if filter_value in song[filter_field].lower():
pending_information_filters.append(True)