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
|
|
|
|
|
|
|
|
2019-07-05 18:24:46 +03:00
|
|
|
FORTUNE_DEFAULTS = {
|
2019-03-28 12:21:15 +02:00
|
|
|
'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):
|
2019-07-05 18:24:46 +03:00
|
|
|
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:
|
2019-03-28 12:21:15 +02:00
|
|
|
req = requests.get(self.conf['uri'], timeout=2)
|
2019-07-05 18:24:46 +03:00
|
|
|
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:
|
2019-07-05 18:24:46 +03:00
|
|
|
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:
|
2019-07-05 18:24:46 +03:00
|
|
|
fortune = 'N/A (offline)'
|
2018-11-09 13:44:47 +02:00
|
|
|
self.retry = True
|
2019-07-08 14:19:05 +03:00
|
|
|
|
2019-07-05 18:24:46 +03:00
|
|
|
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)
|