2018-11-02 19:15:33 +02:00
|
|
|
# TODO: tidy up the fucking code
|
|
|
|
import plugins
|
|
|
|
import requests
|
2018-11-09 13:43:38 +02:00
|
|
|
import time
|
2018-11-02 19:15:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
URI = 'http://fucking-great-advice.ru/api/random'
|
|
|
|
class PluginThread(plugins.PluginThreadCommon):
|
|
|
|
def __init__(self, config):
|
2018-11-09 13:43:38 +02:00
|
|
|
defaults = {'freq': 120, 'retry': 3}
|
2018-11-02 19:15:33 +02:00
|
|
|
super(PluginThread, self).__init__(config, 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(URI, timeout=2)
|
|
|
|
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)
|