Advertisement
Toumo

spotify_album_cover_downloader.py

Apr 24th, 2023 (edited)
1,488
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 1 0
  1. import os
  2. import sys
  3. import spotipy
  4. import requests
  5. import webbrowser
  6.  
  7. def clean_filename(filename):
  8.     replacer_map = {"#": "no.", "%": "percent", "&": "and", "{": "(", "}": ")", "\\": "-", "<": "(", ">": ")", "*": "X", "?": "", "!": "", "$": "", " ": "_", "/": "-", "'": "", '"': "", ":": "_-", "@": "at", "+": "plus", "|": "-", "=": "equals", "`": "",}
  9.     for symbol in replacer_map.keys():
  10.         filename = filename.replace(symbol, replacer_map[symbol])
  11.     return filename
  12.  
  13. sp = spotipy.Spotify(client_credentials_manager = spotipy.oauth2.SpotifyClientCredentials())
  14. uri = str(sys.argv[1]).split("/")[-1]
  15. album = sp.album(uri)
  16. album_title = album["name"]
  17. cover_url = album["images"][0]["url"]
  18. webbrowser.get("/usr/bin/firefox").open(cover_url)
  19.  
  20. album_filename = clean_filename(album_title)
  21. full_save_path = os.path.join("/home/tomo/Downloads/cover_art/", uri + "_" + album_filename + ".jpg")
  22. if not os.path.isfile(full_save_path):
  23.     response = requests.get(cover_url)
  24.     with open(full_save_path, "wb") as f:
  25.         f.write(response.content)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement