Eingangsschalter/sopel-bot/modules/spaceapi.py

64 lines
1.7 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
import json
import sopel.module
SPACEAPI = "/var/www/html/spaceapi.json"
2018-08-22 12:17:02 -05:00
PLACE = "ccchb"
cache = {
"open": False
}
2018-08-28 13:40:17 -05:00
# opens the spaceapi json file, loads it and interpretes the json
def get_spaceapi():
with open(SPACEAPI, 'r') as infile:
data = json.load(infile)
return data
2018-08-28 13:40:17 -05:00
# generates a status message provides the current status
def status_msg(status):
status_text = "closed"
if status:
status_text = "open"
2018-08-22 15:39:18 -05:00
return '{} is {}'.format(PLACE, status_text)
2018-08-28 13:40:17 -05:00
# writes a message to the irc channel if the status has changed
def change_status(bot, status):
status_text = "closed"
if status:
status_text = "open"
2018-08-22 15:39:18 -05:00
for ch in bot.channels:
bot.notice('{} changed to {}'.format(PLACE, status_text), ch)
2018-08-28 13:40:17 -05:00
# reads the spaceapi file and checks if the status is up to date. Updates otherwise,
# this function is called every 5 seconds
2018-08-23 05:47:10 -05:00
def check_status(bot, human=False):
data = get_spaceapi()
status = data["state"]["open"]
if status != cache["open"]:
cache["open"] = status
change_status(bot, status)
2018-08-23 05:47:10 -05:00
status_text = status_msg(status)
2018-08-22 15:39:18 -05:00
for ch in bot.channels:
channel = bot.channels[ch]
2018-08-23 05:47:10 -05:00
topic_new = status_text
2018-08-22 15:39:18 -05:00
topic_cur = status_msg(not status)
if channel.topic != None:
topic_new = channel.topic.replace(topic_cur, topic_new)
topic_cur = channel.topic
if topic_new != topic_cur:
bot.write(('TOPIC', ch), topic_new)
if human:
2018-08-23 05:47:10 -05:00
bot.reply(status_text)
@sopel.module.interval(5)
def interval_check_status(bot):
check_status(bot)
2018-08-23 06:22:00 -05:00
@sopel.module.commands('status','help')
def cmd_check_status(bot, trigger):
check_status(bot, True)