-
Notifications
You must be signed in to change notification settings - Fork 69
/
backend.py
222 lines (193 loc) · 6.68 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# -*- coding: utf-8 -*-
import requests
import urllib
import time
import os
import sys
import re
import webbrowser # to open link on browser
import services as s
import subprocess
if sys.platform == "win32":
import win32process
import psutil
import win32gui
elif sys.platform == "linux":
import dbus
else:
pass
# 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]
services_list3 = [s._ultimateguitar, s._cifraclub, s._songsterr]
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 load_chords():
for i in range(len(services_list3)):
urls = services_list3[i](artist, song)
if len(urls) != 0:
webbrowser.open(urls[0])
break
def getwindowtitle():
if sys.platform == "win32":
spotifypids = []
for proc in psutil.process_iter():
if proc.name() == 'Spotify.exe':
spotifypids.append(proc.pid)
def enum_window_callback(hwnd, pid):
tid, current_pid = win32process.GetWindowThreadProcessId(hwnd)
if pid == current_pid and win32gui.IsWindowVisible(hwnd):
windows.append(hwnd)
windows = []
windowname = ''
try:
for pid in spotifypids:
win32gui.EnumWindows(enum_window_callback, pid)
for item in windows:
if win32gui.GetWindowText(item) != '':
windowname = win32gui.GetWindowText(item)
raise StopIteration
except StopIteration: pass
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 = ''
try:
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")
except Exception:
pass
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
spotify = 'Spotify Lyrics'
if spotify == '':
windowname = 'Spotify'
except Exception:
pass
if windowname != 'Spotify' and windowname != 'Spotify Lyrics':
try:
windowname = "%s - %s" %(metadata['xesam:artist'][0], metadata['xesam:title'])
except Exception:
pass
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 float(version()) >= float(currentversion):
return(True)
else:
return(False)
except Exception:
return(True)
def version():
version = "1.21"
return(version)
def open_spotify():
if sys.platform == "win32":
if getwindowtitle() == "":
path = os.getenv("APPDATA") + '\Spotify\Spotify.exe'
subprocess.Popen(path)
else:
pass
elif sys.platform == "linux":
if getwindowtitle() == "":
subprocess.Popen("spotify")
else:
pass
elif sys.platform == "darwin":
# I don't have a mac so I don't know if this actually works
# If it does, please let me know, if it doesn't please fix it :)
if getwindowtitle() == "":
subprocess.Popen("open -a Spotify")
else:
pass
else:
pass
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()