-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyrics.py
83 lines (63 loc) · 2.28 KB
/
lyrics.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
from dbus.mainloop.glib import DBusGMainLoop
import mpris2
import lyricsgenius
from os import path
import gi.repository.GLib
def get_player():
uris = list(mpris2.get_players_uri())
if len(uris) == 0:
raise Exception("No Mpris2 player detected. Are you sure you're running Spotify?")
if len(uris) == 1:
match = uris[0]
else:
match = None
for u in uris:
if u.split('.')[-1] == "spotify":
match = u
break
if match is None:
print("Mpris2 players were found be not spotify. Please select a player:")
for i, u in enumerate(uris):
print("[%d] %s" % (i, u))
while match is None:
inp = input("Enter number: ")
try:
inp = int(inp)
except Exception as e:
print("%s is not a integer." % inp)
if inp < 0 or inp >= len(uris):
print("%d is not a valid option." % inp)
else:
match = uris[inp]
return mpris2.Player(dbus_interface_info={'dbus_uri': match}), match
def getLyrics(genius, title, artist):
song = genius.search_song(title, artist)
if song:
return song.lyrics
else:
return None
def start_loop():
DBusGMainLoop(set_as_default=True)
player, uri = get_player()
with open(path.join("genius_token.txt"), "r") as f:
genius = lyricsgenius.Genius(f.readline()[:-1])
current_title = player.Metadata["xesam:title"]
current_artist = player.Metadata["xesam:artist"][0]
print(getLyrics(genius, player.Metadata["xesam:title"], player.Metadata["xesam:artist"][0]))
def handler(self, *args, **kargs):
nonlocal current_title
nonlocal current_artist
title = args[0]["Metadata"]["xesam:title"]
artist = args[0]["Metadata"]["xesam:artist"][0]
if title != current_title or artist != current_artist:
print()
print("=" * 40)
lyrics = getLyrics(genius, title, artist)
current_title = title
current_artist = artist
print()
print(lyrics)
player.PropertiesChanged = handler
mloop = gi.repository.GLib.MainLoop()
mloop.run()
start_loop()