vdstatus/plugins/fortune.py

39 lines
1 KiB
Python
Raw Normal View History

2018-11-02 19:15:33 +02:00
import plugins
import requests
2018-11-09 13:43:38 +02:00
import time
2018-11-02 19:15:33 +02:00
FORTUNE_DEFAULTS = {
'uri': 'http://fucking-great-advice.ru/api/random',
'freq': 120, 'retry': 3
}
2018-11-02 19:15:33 +02:00
class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config):
super(PluginThread, self).__init__(config, FORTUNE_DEFAULTS)
2018-11-09 13:43:38 +02:00
self.retry = False
2018-11-02 19:15:33 +02:00
def main(self):
try:
req = requests.get(self.conf['uri'], timeout=2)
fortune = req.json()['text'] if req.status_code == 200 else 'N/A'
2018-11-09 13:44:47 +02:00
self.retry = False
2018-11-02 19:15:33 +02:00
except requests.exceptions.Timeout:
fortune = 'N/A (timeout)'
2018-11-09 13:44:47 +02:00
self.retry = True
2018-11-02 19:15:33 +02:00
except requests.exceptions.ConnectionError:
fortune = 'N/A (offline)'
2018-11-09 13:44:47 +02:00
self.retry = True
self.format_status(fortune)
2018-11-09 13:43:38 +02:00
def run(self):
while True:
self.main()
if self.retry:
sleep_time = self.conf['retry']
else:
sleep_time = self.conf['freq']
time.sleep(sleep_time)