mirror of
https://github.com/Matewoo/spotify-lyric-downloader.git
synced 2026-11-18 13:52:50 +01:00
Update downloader.py
This commit is contained in:
+46
-223
@@ -1,14 +1,16 @@
|
|||||||
import os, re, sys, spotipy, itertools, json, requests, base64, linecache, webbrowser, bs4, time, pyautogui, pyperclip, urllib3, pprint, datetime, shutil, platform, subprocess
|
import os, re, sys, spotipy, itertools, json, requests, base64, linecache, bs4, time, pyautogui, pyperclip, urllib3, pprint, datetime, shutil, platform, subprocess
|
||||||
import spotipy.util as util
|
import spotipy.util as util
|
||||||
import urllib.request
|
import requests
|
||||||
from os import path
|
from os import path
|
||||||
from spotipy.oauth2 import SpotifyOAuth
|
from spotipy.oauth2 import SpotifyOAuth
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from pathlib import Path #using this module to solve difference path syntax between Mac OS and Windows
|
||||||
|
|
||||||
# MAKE SURE THESE ARE CORRECT
|
# MAKE SURE THESE ARE CORRECT
|
||||||
CLIENT_ID = '(spotify API client ID here)'
|
CLIENT_ID = '(spotify API client ID here)'
|
||||||
CLIENT_SECRET = '(spotify API client SECRET here)'
|
CLIENT_SECRET = '(spotify API client SECRET here)'
|
||||||
REDIRECT_URI = '(spotify API REDIRECT URI here)'
|
REDIRECT_URI = '(spotify API REDIRECT URI here)'
|
||||||
|
authorization='(spotify authorization here)'
|
||||||
# MAKE SURE THESE ARE CORRECT
|
# MAKE SURE THESE ARE CORRECT
|
||||||
|
|
||||||
os.environ['SPOTIPY_CLIENT_ID'] = CLIENT_ID
|
os.environ['SPOTIPY_CLIENT_ID'] = CLIENT_ID
|
||||||
@@ -19,47 +21,6 @@ scope = "user-read-currently-playing"
|
|||||||
sp = spotipy.Spotify(auth_manager = SpotifyOAuth(scope = scope))
|
sp = spotipy.Spotify(auth_manager = SpotifyOAuth(scope = scope))
|
||||||
current_track = sp.current_user_playing_track()
|
current_track = sp.current_user_playing_track()
|
||||||
|
|
||||||
# saves currently playing song info to txt file
|
|
||||||
result = json.dumps(current_track)
|
|
||||||
z = open("currentsong.txt", "w")
|
|
||||||
z.write(result)
|
|
||||||
z.close()
|
|
||||||
|
|
||||||
with open('currentsong.txt', 'r') as handle:
|
|
||||||
parsed = json.load(handle)
|
|
||||||
parsed2 = (json.dumps(parsed, indent=0, sort_keys=True))
|
|
||||||
|
|
||||||
with open('currentsong.txt', 'w') as file: # line breaks
|
|
||||||
file.write(parsed2)
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
#reading for short line removal
|
|
||||||
with open('currentsong.txt', 'r') as f:
|
|
||||||
lines = f.readlines()
|
|
||||||
# Removes region codes by removing all short lines
|
|
||||||
filtered_lines = [line for line in lines if len(line) > 10]
|
|
||||||
# writing out file w no 2-character country codes
|
|
||||||
#with open('filtered.txt', 'w') as f:
|
|
||||||
# for line in filtered_lines:
|
|
||||||
# f.write(line)
|
|
||||||
|
|
||||||
database = "currentsong.txt"
|
|
||||||
def search_string_in_file(database, string_to_search):
|
|
||||||
# Search for the given string in file and return lines containing that string,
|
|
||||||
# along with line numbers
|
|
||||||
line_number = 0
|
|
||||||
list_of_results = []
|
|
||||||
# Open the file in read only mode
|
|
||||||
with open(database, 'r') as read_obj:
|
|
||||||
# Read all lines in the file one by one
|
|
||||||
for line in read_obj:
|
|
||||||
# For each line, check if line contains the string
|
|
||||||
line_number += 1
|
|
||||||
if string_to_search in line:
|
|
||||||
# If yes, then add the line number & line as a tuple in the list
|
|
||||||
list_of_results.append((line_number, line.rstrip()))
|
|
||||||
# Return list of tuples containing line numbers and lines where string is found
|
|
||||||
return list_of_results
|
|
||||||
|
|
||||||
def replace_line(database, line_num, text):
|
def replace_line(database, line_num, text):
|
||||||
lines = open(database, 'r').readlines()
|
lines = open(database, 'r').readlines()
|
||||||
@@ -77,162 +38,19 @@ def open_file(path):
|
|||||||
else:
|
else:
|
||||||
subprocess.Popen(["xdg-open", path])
|
subprocess.Popen(["xdg-open", path])
|
||||||
|
|
||||||
#i dont know how to make tables so this finds text and finds the line below which is the string needed
|
#see if song is playing
|
||||||
song_uri_link = search_string_in_file(database, 'spotify:track:')
|
if current_track == None:
|
||||||
song_name = search_string_in_file(database, '"preview_url": "https://p.scdn.co/mp3-preview/')
|
print("No song detected, make sure you're actively playing a song!")
|
||||||
cover_link = search_string_in_file(database,'"width": 640')
|
os._exit(0)
|
||||||
release_date = search_string_in_file(database,'"release_date":')
|
song_uri_link = current_track.get("item").get("uri").replace('spotify:track:','')
|
||||||
album_name = search_string_in_file(database,'"release_date":')
|
song_name = current_track.get("item").get("name")
|
||||||
artist_name = search_string_in_file(database,'"artists": [')
|
cover_link = current_track.get("item").get("album").get("images")[0].get("url")
|
||||||
track_number = search_string_in_file(database,'"track_number":')
|
release_date = current_track.get("item").get("album").get("release_date")
|
||||||
album_uri_link = search_string_in_file(database, 'spotify:album:')
|
album_name = current_track.get("item").get("album").get("name")
|
||||||
|
artist_name = current_track.get("item").get("album").get("artists")[0].get("name")
|
||||||
|
track_number = current_track.get("item").get("track_number")
|
||||||
|
album_uri_link = current_track.get("item").get("album").get("uri")
|
||||||
|
|
||||||
#song name
|
|
||||||
for elem in song_name:
|
|
||||||
song_name = elem[0] - 3
|
|
||||||
f = open("currentsong.txt","r")
|
|
||||||
lines = f.readlines()
|
|
||||||
# logic for finding song name, and assigning it to some variable
|
|
||||||
x = lines[song_name]
|
|
||||||
x = x.replace('"name": "', '')
|
|
||||||
x = x.replace('\\u00e9', 'é')
|
|
||||||
x = x.replace('\\u00e0', 'à')
|
|
||||||
x = x.replace('\\u2019', "'")
|
|
||||||
x = x.replace('\\u0027', '\'')
|
|
||||||
x = x.replace('\\u2022', '·')
|
|
||||||
x = x.replace('?', '')
|
|
||||||
x = x.replace('"', '')
|
|
||||||
x = x.replace('/', '-')
|
|
||||||
x = x.replace('|', '-')
|
|
||||||
x = x.replace('\\', '')
|
|
||||||
x = x.replace(':', ' =')
|
|
||||||
x = x.replace(';', '-')
|
|
||||||
size = len(x)
|
|
||||||
minus_quote_and_comma = x[:size - 2]
|
|
||||||
song_name = minus_quote_and_comma
|
|
||||||
|
|
||||||
#cover URL
|
|
||||||
for elem in cover_link:
|
|
||||||
cover_link = elem[0] - 2
|
|
||||||
f = open("currentsong.txt","r")
|
|
||||||
lines = f.readlines()
|
|
||||||
# logic for finding song name, and assigning it to some variable
|
|
||||||
x = lines[cover_link]
|
|
||||||
x = x.replace('"url": "https:', 'https:')
|
|
||||||
size = len(x)
|
|
||||||
minus_quote_and_comma = x[:size - 3]
|
|
||||||
cover_link = minus_quote_and_comma
|
|
||||||
|
|
||||||
# Song URI
|
|
||||||
for elem in song_uri_link:
|
|
||||||
song_uri_link = elem[0] - 1
|
|
||||||
f = open("currentsong.txt","r")
|
|
||||||
lines = f.readlines()
|
|
||||||
# logic for finding song name, and assigning it to some variable
|
|
||||||
x = lines[song_uri_link]
|
|
||||||
x = x.replace('"uri": "spotify:track:', '')
|
|
||||||
size = len(x)
|
|
||||||
minus_quote = x[:size - 2]
|
|
||||||
song_uri_link = minus_quote
|
|
||||||
|
|
||||||
# Album URI
|
|
||||||
for elem in album_uri_link:
|
|
||||||
album_uri_link = elem[0] - 1
|
|
||||||
f = open("currentsong.txt","r")
|
|
||||||
lines = f.readlines()
|
|
||||||
# logic for finding song name, and assigning it to some variable
|
|
||||||
x = lines[album_uri_link]
|
|
||||||
x = x.replace('"uri": "spotify:album:', 'spotify:album:')
|
|
||||||
size = len(x)
|
|
||||||
minus_quote = x[:size - 2]
|
|
||||||
album_uri_link = minus_quote
|
|
||||||
|
|
||||||
# Track Number
|
|
||||||
for elem in track_number:
|
|
||||||
track_number = elem[0] - 1
|
|
||||||
f = open("currentsong.txt","r")
|
|
||||||
lines = f.readlines()
|
|
||||||
# logic for finding song name, and assigning it to some variable
|
|
||||||
x = lines[track_number]
|
|
||||||
x = x.replace('"track_number": ', '')
|
|
||||||
size = len(x)
|
|
||||||
minus_quote = x[:size - 2]
|
|
||||||
track_number = minus_quote
|
|
||||||
|
|
||||||
# Release Date
|
|
||||||
for elem in release_date:
|
|
||||||
release_date = elem[0] - 1
|
|
||||||
f = open("currentsong.txt","r")
|
|
||||||
lines = f.readlines()
|
|
||||||
# logic for finding song name, and assigning it to some variable
|
|
||||||
x = lines[release_date]
|
|
||||||
x = x.replace('\\u00e9', 'é')
|
|
||||||
x = x.replace('\\u00e0', 'à')
|
|
||||||
x = x.replace('\\u2019', "'")
|
|
||||||
x = x.replace('\\u0027', '\'')
|
|
||||||
x = x.replace('\\u2022', '·')
|
|
||||||
x = x.replace('"name": "', '')
|
|
||||||
x = x.replace('?', '')
|
|
||||||
x = x.replace('"', '')
|
|
||||||
x = x.replace('/', '')
|
|
||||||
x = x.replace('|', '-')
|
|
||||||
x = x.replace('\\', '')
|
|
||||||
x = x.replace(':', ' -')
|
|
||||||
x = x.replace(';', '-')
|
|
||||||
x = x.replace('release_date - ', '')
|
|
||||||
size = len(x)
|
|
||||||
minus_quote_and_comma = x[:size - 2]
|
|
||||||
release_date = minus_quote_and_comma
|
|
||||||
|
|
||||||
# Album Name
|
|
||||||
for elem in album_name:
|
|
||||||
album_name = elem[0] - 2
|
|
||||||
f = open("currentsong.txt","r")
|
|
||||||
lines = f.readlines()
|
|
||||||
# logic for finding song name, and assigning it to some variable
|
|
||||||
x = lines[album_name]
|
|
||||||
x = x.replace('\\u00e9', 'é')
|
|
||||||
x = x.replace('\\u00e0', 'à')
|
|
||||||
x = x.replace('\\u2019', "'")
|
|
||||||
x = x.replace('\\u0027', '\'')
|
|
||||||
x = x.replace('\\u2022', '·')
|
|
||||||
x = x.replace('"name": "', '')
|
|
||||||
x = x.replace('?', '')
|
|
||||||
x = x.replace('"', '')
|
|
||||||
x = x.replace('/', '')
|
|
||||||
x = x.replace('|', '-')
|
|
||||||
x = x.replace('\\', '')
|
|
||||||
x = x.replace(':)', ' =)')
|
|
||||||
x = x.replace(': ', ' - ')
|
|
||||||
x = x.replace(';', '-')
|
|
||||||
x = x.replace(',\n', '')
|
|
||||||
album_name = x
|
|
||||||
|
|
||||||
# Artist Name
|
|
||||||
for elem in artist_name:
|
|
||||||
artist_name = elem[0] + 6
|
|
||||||
f = open("currentsong.txt","r")
|
|
||||||
lines = f.readlines()
|
|
||||||
# logic for finding song name, and assigning it to some variable
|
|
||||||
x = lines[artist_name]
|
|
||||||
x = x.replace('\\u00e9', 'é')
|
|
||||||
x = x.replace('\\u00e0', 'à')
|
|
||||||
x = x.replace('\\u2019', "'")
|
|
||||||
x = x.replace('\\u0027', '\'')
|
|
||||||
x = x.replace('\\u2022', '·')
|
|
||||||
x = x.replace('"name": "', '')
|
|
||||||
x = x.replace('?', '')
|
|
||||||
x = x.replace('"', '')
|
|
||||||
x = x.replace('/', '')
|
|
||||||
x = x.replace('|', '-')
|
|
||||||
x = x.replace('\\', '')
|
|
||||||
x = x.replace(':', ' -')
|
|
||||||
x = x.replace(';', '-')
|
|
||||||
x = x.replace(':)', ' =)')
|
|
||||||
size = len(x)
|
|
||||||
minus_quote_and_comma = x[:size - 2]
|
|
||||||
artist_name = minus_quote_and_comma
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
# generate lyrics link
|
# generate lyrics link
|
||||||
original_link = cover_link
|
original_link = cover_link
|
||||||
@@ -244,13 +62,6 @@ link_start = "https://spclient.wg.spotify.com/color-lyrics/v2/track/"
|
|||||||
lyrics_url = (link_start + song_uri_link + "/image/" + cover_link + "?format=json&vocalRemoval=false&market=from_token")
|
lyrics_url = (link_start + song_uri_link + "/image/" + cover_link + "?format=json&vocalRemoval=false&market=from_token")
|
||||||
lyrics_url_no_access = (link_start + song_uri_link + "/image/" + cover_link)
|
lyrics_url_no_access = (link_start + song_uri_link + "/image/" + cover_link)
|
||||||
|
|
||||||
#see if song is playing
|
|
||||||
filesize = os.path.getsize("currentsong.txt")
|
|
||||||
if filesize == 4:
|
|
||||||
print("No song detected, make sure you're actively playing a song!")
|
|
||||||
os.remove("currentsong.txt")
|
|
||||||
quit()
|
|
||||||
else:
|
|
||||||
print("Success! Song detected! \n")
|
print("Success! Song detected! \n")
|
||||||
print("Song: ", song_name)
|
print("Song: ", song_name)
|
||||||
print("Album name:", album_name)
|
print("Album name:", album_name)
|
||||||
@@ -339,9 +150,9 @@ artist_name = artist_name
|
|||||||
albumdir = albumdir
|
albumdir = albumdir
|
||||||
song = song
|
song = song
|
||||||
cover = lyrics_url
|
cover = lyrics_url
|
||||||
originallyricsfile = (host_folder + "\\output.lrc")
|
originallyricsfile = (Path(host_folder)/"output.lrc")
|
||||||
movedlyricsfile = (albumdir + "\\" + track_number + ". " + song + ".lrc")
|
movedlyricsfile = (Path(albumdir)/(str(track_number) + ". " + str(song) + ".lrc"))
|
||||||
movedcoverjpg = (albumdir + "\\" + "cover.jpg")
|
movedcoverjpg = (Path(albumdir)/"cover.jpg")
|
||||||
|
|
||||||
#checks if cover exists, if not it downloads
|
#checks if cover exists, if not it downloads
|
||||||
os.chdir(albumdir)
|
os.chdir(albumdir)
|
||||||
@@ -351,9 +162,9 @@ try:
|
|||||||
f.close()
|
f.close()
|
||||||
except IOError:
|
except IOError:
|
||||||
print("No cover.jpg detected, downloading now")
|
print("No cover.jpg detected, downloading now")
|
||||||
cover = original_link
|
cover =requests.get(original_link).content
|
||||||
f = open(movedcoverjpg,'wb')
|
f = open(movedcoverjpg,'wb')
|
||||||
f.write(urllib.request.urlopen(cover).read())
|
f.write(cover)
|
||||||
f.close()
|
f.close()
|
||||||
print("Cover downloaded!")
|
print("Cover downloaded!")
|
||||||
|
|
||||||
@@ -370,19 +181,31 @@ try:
|
|||||||
except IOError:
|
except IOError:
|
||||||
print("No lyric detected, downloading now")
|
print("No lyric detected, downloading now")
|
||||||
|
|
||||||
|
headers = {
|
||||||
link_for_cookie = "https://open.spotify.com/lyrics"
|
'Host': 'spclient.wg.spotify.com',
|
||||||
lyrics = webbrowser.open(link_for_cookie, new=0, autoraise=True)
|
'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
|
||||||
time.sleep(10)
|
'accept-language': 'en',
|
||||||
lyrics = webbrowser.open(lyrics_url, new=0, autoraise=True)
|
'sec-ch-ua-mobile': '?0',
|
||||||
time.sleep(1.2)
|
'app-platform': 'WebPlayer',
|
||||||
pyautogui.hotkey('ctrl', 'a')
|
'authorization': authorization,
|
||||||
pyautogui.hotkey('ctrl', 'c')
|
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
|
||||||
time.sleep(0.1)
|
'accept': 'application/json',
|
||||||
pyautogui.hotkey('ctrl', 'w')
|
'spotify-app-version': '1.1.98.597.g7f2ab0d4',
|
||||||
pyautogui.hotkey('ctrl', 'w')
|
'sec-ch-ua-platform': '"macOS"',
|
||||||
print("Tab closed and contents copied! \n")
|
'origin': 'https://open.spotify.com',
|
||||||
lyricdata = pyperclip.paste()
|
'sec-fetch-site': 'same-site',
|
||||||
|
'sec-fetch-mode': 'cors',
|
||||||
|
'sec-fetch-dest': 'empty',
|
||||||
|
'referer': 'https://open.spotify.com/',
|
||||||
|
}
|
||||||
|
params = {
|
||||||
|
'format': 'json',
|
||||||
|
'vocalRemoval': 'false',
|
||||||
|
'market': 'from_token',
|
||||||
|
}
|
||||||
|
response = requests.get(lyrics_url, params=params, headers=headers)
|
||||||
|
lyricdata=response.text
|
||||||
|
#print(lyricdata)
|
||||||
z = open("lyrics.txt", "w", encoding="utf-8")
|
z = open("lyrics.txt", "w", encoding="utf-8")
|
||||||
z.write(lyricdata)
|
z.write(lyricdata)
|
||||||
z.close()
|
z.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user