2018-08-21 18:57:27 -05:00
|
|
|
#!/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"
|
2018-08-21 18:57:27 -05:00
|
|
|
|
|
|
|
cache = {
|
|
|
|
"open": False
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_spaceapi():
|
|
|
|
with open(SPACEAPI, 'r') as infile:
|
|
|
|
data = json.load(infile)
|
|
|
|
return data
|
|
|
|
|
|
|
|
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-21 18:57:27 -05:00
|
|
|
|
|
|
|
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-21 18:57:27 -05:00
|
|
|
|
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-21 18:57:27 -05:00
|
|
|
|
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)
|
2018-08-21 18:57:27 -05:00
|
|
|
|
|
|
|
if human:
|
2018-08-23 05:47:10 -05:00
|
|
|
bot.reply(status_text)
|
2018-08-21 18:57:27 -05:00
|
|
|
|
|
|
|
@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')
|
2018-08-21 18:57:27 -05:00
|
|
|
def cmd_check_status(bot, trigger):
|
|
|
|
check_status(bot, True)
|