2022-09-14 19:04:02 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Download tracklists from djtracklists.com."""
|
|
|
|
|
|
|
|
import argparse
|
2022-09-23 18:50:16 +02:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional
|
2022-09-14 19:04:02 +02:00
|
|
|
|
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
2022-09-23 18:50:16 +02:00
|
|
|
@dataclass
|
|
|
|
class Track:
|
|
|
|
title: str
|
|
|
|
artists: list[str]
|
|
|
|
mix: Optional[str]
|
|
|
|
mix_artists: Optional[list[str]]
|
|
|
|
timestamp: str
|
|
|
|
|
|
|
|
|
2022-09-14 19:04:02 +02:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-s", "--series", help="download this series")
|
|
|
|
parser.add_argument("-t", "--tracklist", help="download this tracklist")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if tracklist_url := args.tracklist:
|
2022-09-23 18:50:16 +02:00
|
|
|
tracklist = download_tracklist(tracklist_url)
|
|
|
|
name = tracklist_url.rstrip("/").rsplit("/", maxsplit=1)[-1]
|
|
|
|
file_name = Path.cwd() / (name + ".txt")
|
|
|
|
save_tracklist(tracklist, file_name)
|
2022-09-14 19:04:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_track_row(css_class: str) -> bool:
|
2022-09-23 18:50:16 +02:00
|
|
|
return css_class in ("on", "off")
|
2022-09-14 19:04:02 +02:00
|
|
|
|
|
|
|
|
2022-09-23 18:50:16 +02:00
|
|
|
def download_tracklist(url: str) -> list[Track]:
|
|
|
|
response = requests.get(url, timeout=10)
|
2022-09-14 19:04:02 +02:00
|
|
|
response.raise_for_status()
|
|
|
|
soup = BeautifulSoup(response.text, "html.parser")
|
2022-09-23 18:50:16 +02:00
|
|
|
tracklist = []
|
2022-09-14 19:04:02 +02:00
|
|
|
for row in soup.find_all("div", class_=is_track_row):
|
2022-09-23 18:50:16 +02:00
|
|
|
artists = []
|
|
|
|
mix_artists = []
|
2022-09-14 19:04:02 +02:00
|
|
|
try:
|
2022-09-23 18:50:16 +02:00
|
|
|
title = row.find("a", class_="track").string
|
|
|
|
mix = row.find("a", class_="release").string
|
2022-09-14 19:04:02 +02:00
|
|
|
except AttributeError:
|
2022-09-23 18:50:16 +02:00
|
|
|
title = row.find("b").string
|
|
|
|
mix = None
|
|
|
|
timestamp = row.find("span", class_="index_time").string
|
2022-09-14 19:04:02 +02:00
|
|
|
for artist in row.find_all("a", class_="artist"):
|
|
|
|
prev_tag = artist.previous_sibling.string
|
|
|
|
if getattr(prev_tag, "string", "").strip() == "remixed by":
|
2022-09-23 18:50:16 +02:00
|
|
|
mix_artists.append(artist.string)
|
2022-09-14 19:04:02 +02:00
|
|
|
else:
|
2022-09-23 18:50:16 +02:00
|
|
|
artists.append(artist.string)
|
|
|
|
tracklist.append(
|
|
|
|
Track(
|
|
|
|
title=title,
|
|
|
|
mix=mix,
|
|
|
|
artists=artists,
|
|
|
|
mix_artists=mix_artists or None,
|
|
|
|
timestamp=timestamp,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return tracklist
|
|
|
|
|
|
|
|
|
|
|
|
def save_tracklist(tracklist: list[Track], file_name: Path):
|
|
|
|
try:
|
|
|
|
with open(file_name, "wt", encoding="utf8") as file:
|
|
|
|
for track in tracklist:
|
|
|
|
artists = " & ".join(track.artists)
|
|
|
|
line = f"{track.timestamp} — {artists} — {track.title}"
|
|
|
|
if track.mix:
|
|
|
|
line += f" ({track.mix})"
|
|
|
|
if track.mix_artists:
|
|
|
|
mix_artists = " & ".join(track.mix_artists)
|
|
|
|
line += f" remixed by {mix_artists}"
|
|
|
|
file.write(line + "\n")
|
|
|
|
except OSError as exc:
|
|
|
|
print(f"Can't save tracklist: {exc}")
|
2022-09-14 19:04:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|