vdstatus/plugins/fortune.py

38 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
FGA_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, FGA_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)
2018-11-02 19:15:33 +02:00
advice = 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:
advice = '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:
advice = 'N/A (offline)'
2018-11-09 13:44:47 +02:00
self.retry = True
2018-11-02 19:15:33 +02:00
self.status['full_text'] = advice
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)