|
3 | 3 | from cloudbot import hook |
4 | 4 |
|
5 | 5 | API_URL = "http://www.rrrather.com/botapi" |
| 6 | +FILTERED_TAGS = ('rape', 'gross', 'sex') |
| 7 | + |
| 8 | + |
| 9 | +def get_wyr(headers): |
| 10 | + r = requests.get(url=API_URL, headers=headers) |
| 11 | + data = r.json() |
| 12 | + |
| 13 | + # clean up text |
| 14 | + data['title'] = data['title'].strip().capitalize().rstrip('.?,') |
| 15 | + data['choicea'] = data['choicea'].strip().lower().rstrip('.?,!').lstrip('.') |
| 16 | + data['choiceb'] = data['choiceb'].strip().lower().rstrip('.?,!').lstrip('.') |
| 17 | + |
| 18 | + if data['tags']: |
| 19 | + data['tags'] = data['tags'].lower().split(',') |
| 20 | + else: |
| 21 | + data['tags'] = [] |
| 22 | + |
| 23 | + return data |
6 | 24 |
|
7 | 25 |
|
8 | 26 | @hook.command("wyr", "wouldyourather") |
9 | 27 | def wyr(bot): |
10 | 28 | headers = {"User-Agent": bot.user_agent} |
11 | 29 |
|
12 | | - r = requests.get(url=API_URL, headers=headers) |
13 | | - data = r.json() |
| 30 | + while True: |
| 31 | + data = get_wyr(headers) |
14 | 32 |
|
15 | | - # clean up text |
16 | | - title = data['title'].strip().capitalize().rstrip('.?,') |
17 | | - choice1 = data['choicea'].strip().lower().rstrip('.?,!').lstrip('.') |
18 | | - choice2 = data['choiceb'].strip().lower().rstrip('.?,!').lstrip('.') |
19 | | - link = data['link'] |
| 33 | + if [i for i in FILTERED_TAGS if i in data['tags']]: |
| 34 | + continue |
| 35 | + else: |
| 36 | + break |
20 | 37 |
|
21 | 38 | # get a list of all the words in the answers |
22 | | - text = choice1.split() + choice2.split() |
| 39 | + text = data['choicea'].split() + data['choiceb'].split() |
23 | 40 | text = [word for word in text if word != "a"] |
24 | 41 |
|
25 | | - title_text = title.split() |
26 | | - |
27 | | - print(str(title_text)) |
28 | | - print(str(text)) |
| 42 | + title_text = data['title'].split() |
29 | 43 |
|
30 | 44 | dupl_count = 0 |
31 | 45 |
|
32 | 46 | for word in title_text: |
33 | 47 | dupl_count += text.count(word) |
34 | 48 |
|
35 | | - print(str(dupl_count / len(text))) |
36 | | - |
37 | | - title = title.replace(" u ", " you ") |
| 49 | + data['title'] = data['title'].replace(" u ", " you ") |
38 | 50 |
|
39 | 51 | # detect if the answers are also in the question |
40 | 52 | # if so, replace question with a generic one |
41 | 53 | if dupl_count / len(text) >= 0.6: |
42 | | - title = "Would you rather" |
| 54 | + data['title'] = "Would you rather" |
43 | 55 |
|
44 | | - return "{}... {} \x02OR\x02 {}? - {}".format(title, choice1, choice2, link) |
| 56 | + return "{title}... {choicea} \x02OR\x02 {choiceb}? - {link}".format(**data) |
0 commit comments