forked from maxpowa/inumuta-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shindan.py
56 lines (50 loc) · 1.76 KB
/
shindan.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
shindan.py - Be fucking annoying, all the time!
Copyright 2014 Max Gurela
Licensed under the Eiffel Forum License 2.
"""
from willie import web
from willie.module import commands
import translate
from bs4 import BeautifulSoup
import re
import json
@commands('shindan')
def shindan(bot, trigger):
"""
.shindan <id> [name] - Do the shindanmaker thing! Will automatically translate japanese shindans to english. (Waifu id: 215100 | Teh_Colt's Drama Gen id: 490953)
"""
if not trigger.group(3) or not trigger.group(3).isdigit() or int(trigger.group(3).strip()) < 2000:
bot.say('You must specify a shindanmaker ID (Waifu id: 215100 | Teh_Colt\'s Drama Gen id: 490953)')
return
name = trigger.nick
if (trigger.group(4)):
name = trigger.group(4)
data = web.urlencode({'u': name, 'from': ''}).encode('ascii')
url = follow_redirects('http://en.shindanmaker.com/'+trigger.group(3).strip())
try:
soup = get_soup(web.post(url, data))
shindan = soup.find(attrs={'class':re.compile("result")})
if 'en' in url:
bot.say(shindan.text.strip())
else:
msg, in_lang = translate.translate(shindan.text.strip())
if in_lang == 'ja':
in_lang = 'Japanese'
bot.say('%s (Translated from %s)' % (msg, in_lang))
except Exception as e:
bot.say('418 I\'m a teapot')
def follow_redirects(url):
"""
Follow HTTP 3xx redirects, and return the actual URL. Return None if
there's a problem.
"""
try:
connection = web.get_urllib_object(url, 60)
url = connection.geturl() or url
connection.close()
except:
return None
return url
def get_soup(raw):
return BeautifulSoup(raw, 'lxml')