forked from GumballCrash/GumBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquotes.py
76 lines (68 loc) · 2.77 KB
/
quotes.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# GumBot quotes module
# requires config keys:
# - url
# - http-user
# - http-pass
import requests
import re
import traceback
def encode_utf8_to_iso88591(utf8_text):
# Borrowed from http://jamesmurty.com/2011/12/30/python-code-utf8-to-latin1/
"""
Encode and return the given UTF-8 text as ISO-8859-1 (latin1) with
unsupported characters replaced by '?', except for common special
characters like smart quotes and symbols that we handle as well as we
can.
For example, the copyright symbol => '(c)' etc.
If the given value is not a string it is returned unchanged.
References:
en.wikipedia.org/wiki/Quotation_mark_glyphs#Quotation_marks_in_Unicode
en.wikipedia.org/wiki/Copyright_symbol
en.wikipedia.org/wiki/Registered_trademark_symbol
en.wikipedia.org/wiki/Sound_recording_copyright_symbol
en.wikipedia.org/wiki/Service_mark_symbol
en.wikipedia.org/wiki/Trademark_symbol
"""
if not isinstance(utf8_text, basestring):
return utf8_text
# Replace "smart" and other single-quote like things
utf8_text = re.sub(
u'[\u02bc\u2018\u2019\u201a\u201b\u2039\u203a\u300c\u300d\x91\x92]',
"'", utf8_text)
# Replace "smart" and other double-quote like things
utf8_text = re.sub(
u'[\u00ab\u00bb\u201c\u201d\u201e\u201f\u300e\u300f\x93\x94]',
'"', utf8_text)
# Replace copyright symbol
utf8_text = re.sub(u'[\u00a9\u24b8\u24d2]', '(c)', utf8_text)
# Replace registered trademark symbol
utf8_text = re.sub(u'[\u00ae\u24c7]', '(r)', utf8_text)
# Replace sound recording copyright symbol
utf8_text = re.sub(u'[\u2117\u24c5\u24df]', '(p)', utf8_text)
# Replace service mark symbol
utf8_text = re.sub(u'[\u2120]', '(sm)', utf8_text)
# Replace trademark symbol
utf8_text = re.sub(u'[\u2122]', '(tm)', utf8_text)
# Replace/clobber any remaining UTF-8 characters that aren't in ISO-8859-1
return utf8_text.encode('ISO-8859-1', 'replace')
def get_quote():
try:
r = requests.get(config['url'], auth=(config['http-user'], config['http-pass']), timeout=3)
if r.status_code != 200:
raise Exception()
# FUCK CHARACTER ENCODINGS.
quote = encode_utf8_to_iso88591(r.content.decode('ISO-8859-1'))
return quote
except Exception, e:
traceback.print_exc()
return 'error getting quote :( (%s)' % e
def irc_msg(source, target, msg):
if msg == '!quote' and target in config['channels']:
quote = get_quote()
ircbot.say(target, quote)
usemap[target].SendMessage(quote)
def skype_msg(sourceDisplay, sourceHandle, target, msg):
if msg == '!quote' and usemap[target] in config['channels']:
quote = get_quote()
ircbot.say(usemap[target], quote)
target.SendMessage(quote)