forked from fr31/spotifylyrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
166 lines (141 loc) · 4.88 KB
/
backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
import urllib
import time
import os
import sys
import re
import lyrics as minilyrics
import services as s
if sys.platform == "win32":
import win32gui
elif sys.platform == "darwin":
import subprocess
else:
import subprocess
import dbus
# With Sync. Of course, there is one for now, but for the sake of
# make the code a little bit more cleaner, is declared.
services_list1 = [s._minilyrics]
# Without Sync.
services_list2 = [s._wikia, s._musixmatch, s._songmeanings, s._songlyrics, s._genius, s._versuri]
artist = ""
song = ""
url = ""
'''
current_service is used to store the current index of the list.
Useful to change the lyrics with the button "Next Lyric" if
the service returned a wrong song
'''
current_service = -1
def load_lyrics(artist, song, sync=False):
error = "Error: Could not find lyrics."
global current_service
if current_service == len(services_list2)-1: current_service = -1
if sync == True:
lyrics, url, service_name, timed = s._minilyrics(artist, song)
current_service = -1
if sync == True and lyrics == error or sync == False:
timed = False
for i in range (current_service+1, len(services_list2)):
lyrics, url, service_name = services_list2[i](artist, song)
current_service = i
if lyrics != error:
lyrics = lyrics.replace("&", "&").replace("`", "'").strip()
break
#return "Error: Could not find lyrics." if the for loop doens't find any lyrics
return(lyrics, url, service_name, timed)
def getlyrics(songname, sync=False):
global artist, song, url, current_service
artist = ""
song = ""
url = ""
current_service = -1
if songname.count(" - ") == 1:
artist, song = songname.rsplit(" - ", 1)
if songname.count(" - ") == 2:
artist, song, garbage = songname.rsplit(" - ", 2)
if " / " in song:
song, garbage = song.rsplit(" / ", 1)
song = re.sub(' \(.*?\)', '', song, flags=re.DOTALL)
return load_lyrics(artist, song, sync)
def next_lyrics():
global current_service
lyrics, url, service_name, timed = load_lyrics(artist, song)
return (lyrics, url, service_name, timed)
def getwindowtitle():
if sys.platform == "win32":
spotify = win32gui.FindWindow('SpotifyMainWindow', None)
windowname = win32gui.GetWindowText(spotify)
elif sys.platform == "darwin":
windowname = ''
try:
command = "osascript getCurrentSong.AppleScript"
windowname = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")
except Exception:
pass
else:
windowname = ''
session = dbus.SessionBus()
spotifydbus = session.get_object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
spotifyinterface = dbus.Interface(spotifydbus, "org.freedesktop.DBus.Properties")
metadata = spotifyinterface.Get("org.mpris.MediaPlayer2.Player", "Metadata")
try:
command = "xwininfo -tree -root"
windows = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")
spotify = ''
for line in windows.splitlines():
if '("spotify" "Spotify")' in line:
if " - " in line:
spotify = line
break
if spotify == '':
windowname = 'Spotify'
except Exception:
pass
if windowname != 'Spotify':
windowname = "%s - %s" %(metadata['xesam:artist'][0], metadata['xesam:title'])
if "—" in windowname:
windowname = windowname.replace("—", "-")
if "Spotify - " in windowname:
windowname = windowname.strip("Spotify - ")
return(windowname)
def versioncheck():
proxy = urllib.request.getproxies()
try:
currentversion = requests.get("https://raw.githubusercontent.com/fr31/spotifylyrics/master/currentversion", timeout=5, proxies=proxy).text
except Exception:
return(True)
try:
if version() >= float(currentversion):
return(True)
else:
return(False)
except Exception:
return(True)
def version():
version = 1.15
return(version)
def main():
if os.name == "nt":
os.system("chcp 65001")
def clear():
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
clear()
oldsongname = ""
while True:
songname = getwindowtitle()
if oldsongname != songname:
if songname != "Spotify":
oldsongname = songname
clear()
# print(songname+"\n")
lyrics, url, service_name, timed = getlyrics(songname)
# print(lyrics+"\n")
time.sleep(1)
if __name__ == '__main__':
main()