def tr(ircbot, context): """Translates a phrase, with an optional language hint.""" input, output, phrase = context.groups() phrase = phrase.encode('utf-8') if (len(phrase) > 350) and (not context.admin): return ircbot.reply('Phrase must be under 350 characters.') input = input or detect(phrase) if not input: err = 'Unable to guess your crazy moon language, sorry.' return ircbot.reply(err) input = input.encode('utf-8') output = (output or 'en').encode('utf-8') if input != output: msg = translate(phrase, input, output) if isinstance(msg, str): msg = msg.decode('utf-8') if msg: msg = web.decode(msg) # msg.replace(''', "'") msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output) else: msg = 'The %s to %s translation failed, sorry!' % (input, output) ircbot.reply(msg) else: ircbot.reply('Language guessing failed, so try suggesting one!')
def c(ircbot, input): """Google calculator.""" if not input.group(2): return ircbot.reply("Nothing to calculate.") q = input.group(2).encode('utf-8') q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5 q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0 uri = 'http://www.google.com/ig/calculator?q=' bytes = web.get(uri + web.urllib.quote(q)) parts = bytes.split('",') answer = [p for p in parts if p.startswith('rhs: "')][0][6:] if answer: answer = answer.decode('unicode-escape') answer = ''.join(chr(ord(c)) for c in answer) answer = answer.decode('utf-8') answer = answer.replace(u'\xc2\xa0', ',') answer = answer.replace('<sup>', '^(') answer = answer.replace('</sup>', ')') answer = web.decode(answer) ircbot.say(answer) else: ircbot.say('Sorry, no result.')