fix (god i hope fix) unicode in Twitter

also remove some unnecessary unicode conversions right before printing?
commented out until i'm convinced there was no need for it.

protip: clearest description i've read to date:
http://stackoverflow.com/questions/368805/python-unicodedecodeerror-am-i-misunderstanding-encode#370199
This commit is contained in:
Brian S. Stephan 2010-12-16 10:36:50 -06:00
parent 37a677946d
commit 1bacfe047e
2 changed files with 14 additions and 11 deletions

View File

@ -50,9 +50,10 @@ class DrBotServerConnection(irclib.ServerConnection):
splitpos = text.rfind(' ', 0, splitspace) splitpos = text.rfind(' ', 0, splitspace)
splittext = text[0:splitpos] + ' ' + splitter splittext = text[0:splitpos] + ' ' + splitter
text = splitter + ' ' + text[splitpos+1:] text = splitter + ' ' + text[splitpos+1:]
try: # unnecessary?
splittext = unicode(splittext).encode('utf-8') #try:
except UnicodeDecodeError: pass # splittext = unicode(splittext).encode('utf-8')
#except UnicodeDecodeError: pass
self.send_raw("PRIVMSG %s :%s" % (target, splittext)) self.send_raw("PRIVMSG %s :%s" % (target, splittext))
times = times + 1 times = times + 1
@ -60,14 +61,16 @@ class DrBotServerConnection(irclib.ServerConnection):
return return
# done splitting # done splitting
try: # unnecessary?
text = unicode(text).encode('utf-8') #try:
except UnicodeDecodeError: pass # text = unicode(text).encode('utf-8')
#except UnicodeDecodeError: pass
self.send_raw("PRIVMSG %s :%s" % (target, text)) self.send_raw("PRIVMSG %s :%s" % (target, text))
else: else:
try: # unnecessary?
text = unicode(text).encode('utf-8') #try:
except UnicodeDecodeError: pass # text = unicode(text).encode('utf-8')
#except UnicodeDecodeError: pass
self.send_raw("PRIVMSG %s :%s" % (target, text)) self.send_raw("PRIVMSG %s :%s" % (target, text))
class DrBotIRC(irclib.IRC): class DrBotIRC(irclib.IRC):

View File

@ -84,7 +84,7 @@ class Twitter(Module):
if whats[1] == 'status' and len(whats) == 3: if whats[1] == 'status' and len(whats) == 3:
try: try:
tweet = self.twit.GetStatus(whats[2]) tweet = self.twit.GetStatus(whats[2])
return self.reply(connection, replypath, tweet.user.name + ': ' + tweet.text) return self.reply(connection, replypath, tweet.user.name.encode('utf-8', 'ignore') + ': ' + tweet.text.encode('utf-8', 'ignore'))
except twitter.TwitterError as e: except twitter.TwitterError as e:
return self.reply(connection, replypath, 'Couldn\'t obtain status: ' + str(e)) return self.reply(connection, replypath, 'Couldn\'t obtain status: ' + str(e))
elif whats[1] == 'user' and len(whats) == 3: elif whats[1] == 'user' and len(whats) == 3:
@ -95,7 +95,7 @@ class Twitter(Module):
tweets = self.twit.GetUserTimeline(screen_name=whats[2], count=30, include_rts=False) tweets = self.twit.GetUserTimeline(screen_name=whats[2], count=30, include_rts=False)
if tweets: if tweets:
tweet = tweets[0] tweet = tweets[0]
return self.reply(connection, replypath, tweet.user.name + ': ' + tweet.text) return self.reply(connection, replypath, tweet.user.name.encode('utf-8', 'ignore') + ': ' + tweet.text.encode('utf-8', 'ignore'))
except twitter.TwitterError as e: except twitter.TwitterError as e:
return self.reply(connection, replypath, 'Couldn\'t obtain status: ' + str(e)) return self.reply(connection, replypath, 'Couldn\'t obtain status: ' + str(e))