Implement webhook JSON checking
This commit is contained in:
parent
f620a13554
commit
3a1ddac115
18
bot.py
18
bot.py
|
@ -5,13 +5,23 @@ class RadioBot(teambot.Handler):
|
|||
def __init__(self,*args):
|
||||
super(RadioBot,self).__init__(*args)
|
||||
self.tasks = tasks.TaskPool(handler=self)
|
||||
self.tasks.add_coroutine(self.check_nowplaying,1,dict(now_playing="",dj=None))
|
||||
self.tasks.add_coroutine(self.check_nowplaying,1)
|
||||
self.channels = [x.split()[0] for x in self._bot.chanlist]
|
||||
self.tasks.run()
|
||||
def check_nowplaying(self,state,basestate):
|
||||
with open(fs.expanduser("~ben/public_html/radio/now_playing.txt")) as f:
|
||||
text = f.read().rstrip()
|
||||
print(text) #TODO: implement parsing
|
||||
if not hasattr(self._bot,"conn"):
|
||||
return {}
|
||||
bot = basestate["handler"]
|
||||
with open(fs.expanduser("~khuxkm/public_html/radiobot/now_playing.json")) as f:
|
||||
resp = json.loads(f.read().rstrip())
|
||||
if resp["dj"] is not None and not state.get("dj_live",False):
|
||||
bot.say(bot.channels[0],"{} is now live!".format(resp["dj"]))
|
||||
state["dj_live"]=True
|
||||
if resp["song"]!=state.get("song") or resp["listeners"]!=state.get("listeners"):
|
||||
bot.say(bot.channels[0],"now playing: {} ({!s} listeners{})".format(resp["song"],resp["listeners"],(", played by "+resp["dj"] if resp["dj"] is not None else "")))
|
||||
state["song"]=resp["song"]
|
||||
state["listeners"]=resp["listeners"]
|
||||
return state
|
||||
def on_pubmsg(self,channel,nick,text):
|
||||
if nick.startswith("khuxkm") and text.rstrip()=="!down":
|
||||
self.tasks.stop()
|
||||
|
|
Loading…
Reference in New Issue