forked from edbgon/steelseries-oled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.py
62 lines (44 loc) · 1.61 KB
/
spotify.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
# - coding: UTF-8 -
from device import getDevice, send_feature_report
from typing import Final
import dbus
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
from urllib.parse import urlparse
from PIL import Image, ImageFont, ImageDraw
def draw_song_info(status, title, artist):
FONT_NAME: Final = "OpenSans-Regular.ttf"
dev = getDevice()
dev.open()
im = Image.new('1', (128, 40))
draw = ImageDraw.Draw(im)
draw.rectangle([(0, 0), (128, 40)], fill=0)
font14 = ImageFont.truetype(FONT_NAME,14)
font12 = ImageFont.truetype(FONT_NAME,12)
first_line = ('|| ' if status == 'Paused' else '') + title
second_line = artist
draw.text((0, 0), first_line, font=font14, fill=255)
draw.text((0, 18), second_line, font=font12, fill=255)
draw.rectangle([(0, 38), (128, 40)], fill="#ffffff")
data = im.tobytes()
send_feature_report(dev, data)
dev.close()
def replace_image_host(image_url):
parsed = urlparse(image_url)
parsed = parsed._replace(netloc="i.scdn.co").geturl()
return parsed
def handler_cond(*args):
eventData = args[1]
status = eventData.get('PlaybackStatus')
metadata = eventData.get('Metadata')
song_title = metadata['xesam:title']
artist = ", ".join(metadata['xesam:artist'])
image = replace_image_host(metadata['mpris:artUrl'])
draw_song_info(status, song_title, artist)
DBusGMainLoop(set_as_default=True)
loop = GLib.MainLoop()
session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify",
"/org/mpris/MediaPlayer2")
spotify_bus.connect_to_signal("PropertiesChanged", handler_cond)
loop.run()