Implement webhook JSON checking

This commit is contained in:
Robert Miles 2018-11-30 22:42:04 -05:00
parent f620a13554
commit 3a1ddac115
1 changed files with 14 additions and 4 deletions

18
bot.py
View File

@ -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()