-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source.py
80 lines (62 loc) · 2.21 KB
/
data_source.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
# -*- coding: utf-8 -*-
from __future__ import division, print_function, unicode_literals
import commands
class PlayerClosed(Exception):
pass
class PlayerPaused(Exception):
pass
class SameTrack(Exception):
pass
class WMCtrlData(object):
COMMAND = "wmctrl -lx | awk '/spotify.Spotify/ {print $0}'"
PLAYER_NAME = 'Spotify'
# possible status
CLOSED = 'CLOSED'
PAUSED = 'PAUSED'
PLAYING = None
def __init__(self, lyric_source):
self.lyric_source = lyric_source
self.artist = ''
self.title = ''
self.lyrics = ''
self.updated = False
self.status = self.CLOSED
def set_status(self, status):
self.status = status
def get_info(self):
raw_output = commands.getoutput(self.COMMAND).decode('utf-8')
if raw_output == '':
raise PlayerClosed
# processing command output
_hostname = commands.getoutput('hostname')
# right now the string is something like this
# "<numbers> <number> spotify.Spotify <hostname> <author> - <track>"
# then we need to split using the hostname
output = raw_output.split(_hostname)[1].lstrip()
if output == self.PLAYER_NAME:
raise PlayerPaused
try:
res = output.split(" - ")
# in case the title of the song contains a hyphen
artist, title = res[0], " - ".join(res[1:])
except (ValueError, IndexError):
# if something goes bad, we keep the previous values
artist, title = self.artist, self.title
if (self.artist, self.title) == (artist, title):
raise SameTrack
return artist, title
def process(self):
self.updated = False
try:
current_artist, current_title = self.get_info()
except PlayerClosed:
self.set_status(self.CLOSED)
except PlayerPaused:
self.set_status(self.PAUSED)
except SameTrack:
return
else:
self.set_status(self.PLAYING)
self.lyrics = self.lyric_source(current_artist, current_title).lyric
self.artist, self.title = current_artist, current_title
self.updated = True