-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomfacts.py
35 lines (29 loc) · 1.03 KB
/
randomfacts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Random Facts"""
import urllib.request
import json
import os
from errbot import BotPlugin, botcmd
language = os.getenv('language', 'en')
class Randomfacts(BotPlugin):
"""Random Facts"""
@botcmd
def random(self, msg, args):
"""Random Fact from https://uselessfacts.jsph.pl/random.json"""
return self.randomfacts_send(msg, random=True)
@botcmd
def random_today(self, msg, args):
"""Today Random Fact of the day"""
return self.randomfacts_send(msg, random=False)
def randomfacts_send(self, msg, random):
"""Random Fact"""
# apodapi.herokuapp.com/api/?date=2001-07-12
if random:
path = '/random.json'
else:
path = '/today.json'
url = 'https://uselessfacts.jsph.pl/' + path + '?language=' + language
page = urllib.request.Request(url)
response = json.loads(urllib.request.urlopen(page).read().decode('utf-8'))
if 'text' in response:
return response['text']
return 'No fact returned'