implement google translate. works pretty well for url scraping
This commit is contained in:
parent
11ddb9af27
commit
56a45bc83b
24
dr.botzo.py
24
dr.botzo.py
|
@ -6,6 +6,8 @@ import os
|
|||
import random
|
||||
import re
|
||||
import sys
|
||||
from urllib2 import urlopen
|
||||
from urllib import urlencode
|
||||
|
||||
from dateutil.parser import *
|
||||
from dateutil.relativedelta import *
|
||||
|
@ -103,6 +105,28 @@ class GoogleTranslate(Module):
|
|||
def __init__(self, config, server):
|
||||
super(GoogleTranslate, self).__init__(config, server)
|
||||
|
||||
def register_handlers(self, server):
|
||||
server.add_global_handler('pubmsg', self.on_pubmsg)
|
||||
server.add_global_handler('privmsg', self.on_privmsg)
|
||||
|
||||
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||
whats = what.split(' ')
|
||||
if whats[0] == 'translate' and len(whats) >= 4:
|
||||
fromlang = whats[1]
|
||||
tolang = whats[2]
|
||||
text = ' '.join(whats[3:])
|
||||
|
||||
langpair = '%s|%s' % (fromlang, tolang)
|
||||
gt_url = 'http://ajax.googleapis.com/ajax/services/language/translate?'
|
||||
params = urlencode( (('v', 1.0), ('q', text), ('langpair', langpair),) )
|
||||
url = gt_url + params
|
||||
content = urlopen(url).read()
|
||||
start_idx = content.find('"translatedText":"')+18
|
||||
translation = content[start_idx:]
|
||||
end_idx = translation.find('"}, "')
|
||||
translation = translation[:end_idx]
|
||||
connection.privmsg(replypath, translation)
|
||||
|
||||
#####
|
||||
# sub_join_channel
|
||||
# join a channel when told to by an admin
|
||||
|
|
Loading…
Reference in New Issue