this repo has no description
1#!/usr/bin/env python
2
3"""
4Usage:
5 cover-arts.py SPOTIFY_PLAYLIST_URL SAVE_INTO
6
7SAVE_INTO is always relative to where cover-arts.py is.
8"""
9
10from docopt import docopt
11from slugify import slugify
12import requests
13from helium import *
14from pathlib import Path
15import spotipy
16from spotipy.oauth2 import SpotifyClientCredentials
17from rich import print
18from dotenv import load_dotenv
19
20args = docopt(__doc__)
21playlist_url = args["SPOTIFY_PLAYLIST_URL"]
22here = Path(__file__).parent
23load_dotenv(here / ".env")
24save_into = here / args["SAVE_INTO"]
25spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
26tracks = [t["track"] for t in spotify.playlist_tracks(playlist_url)["items"]]
27
28def download_artwork(query, save_into):
29 save_as = save_into / (slugify(query) + ".png")
30 if save_as.exists():
31 print(f"Skipping {save_as}, which is already downloaded")
32 return
33 print(f"Downloading: {query}", end=" ")
34 start_chrome("https://bendodson.com/projects/itunes-artwork-finder/", headless=True)
35 link = None
36 try:
37 entity_selector = S("#entity").web_element
38 select(entity_selector, "Album")
39 write(query.replace(">", " ").replace("<", " ").replace("(", " ").replace(")", " "), into=S("#query").web_element)
40 click("Get the artwork")
41
42 wait_until(lambda: not Text("Searching...").exists())
43
44 if Text("No results found.").exists():
45 kill_browser()
46 print(f"[red][bold] not found on iTunes")
47 return
48
49 link = find_all(S("#results a"))[1].web_element.get_attribute("href") # first link is going to be to standard res. image of first result, second one (what we want) is high-res of first result.
50 except Exception:
51 print()
52 finally:
53 kill_browser()
54
55 if not link:
56 print(f"Couldn't get {query}'s artwork image URL")
57 return
58
59 print(f"-> {link} -> {save_as}")
60 image_response = requests.get(link)
61
62 if image_response.status_code == 200:
63 with open(str(save_as), "wb") as image_file:
64 image_file.write(image_response.content)
65
66for track in tracks:
67 album = track["album"]["name"]
68 artist = track["album"]["artists"][0]["name"]
69
70 download_artwork(f"{artist} {album}", save_into)