-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnowPlayingChecker.py
49 lines (42 loc) · 1.4 KB
/
nowPlayingChecker.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
# now playing checker
import constants
import logging
import requests
import time
import threading
import traceback
import wx
interval = 15
class NowPlayingChecker(threading.Thread):
def __init__(self, url, onChange, onExit):
super().__init__(daemon=True)
self.url = url
self.onChange = onChange
self.onExit = onExit
self.log = logging.getLogger("%s.%s" % (constants.LOG_PREFIX, "nowPlayingChecker"))
self.log.debug("Nowplaying checker created. URL: %s" % self.url)
self.running = False
self.oldData = {}
def run(self):
self.running = True
while self.running:
self.log.debug("Getting data...")
try:
req = requests.get(self.url)
if req.status_code != 200:
self.log.error("Failed to get data: %s" % req.text)
continue
data = req.json()
data = data["now_playing"]
if data != self.oldData:
self.log.debug("New data")
wx.CallAfter(self.onChange, data)
self.oldData = data
except Exception as e:
self.log.error(traceback.format_exc())
continue
time.sleep(interval)
def exit(self):
self.log.debug("exitting...")
self.running = False
wx.CallAfter(self.onExit)