Пример #1
0
def google(query, bot):
    query = urllib.quote(query.encode('utf-8'))
    try:
        if "google_key" in bot.settings:
            google_key = "&key=" + bot.settings["google_key"]
        else:
            google_key = ""
        data = misc.readUrl('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s&hl=ru%s' % (query, google_key), None, bot)
        if not data: return 'can\'t get data'
    except:
        return _("google is not available, sorry.")

    try:
    	convert = loads(data)
	results = convert['responseData']['results']
	if not results: return 'not found'

    	url = urllib.unquote(results[0]['url'])
    	title = results[0]['titleNoFormatting']
    	content = results[0]['content']
    	text = '%s\n%s\n%s' %(title, content, url)

    	text = re.sub('<b>|</b>', '', text)
    	text = re.sub('   ', '\n', text)

    	return text
    except:
    	return 'error'
Пример #2
0
def translate(from_l, to_l, text, bot):
    text = urllib.quote(text.encode("utf-8"))
    try:
        data = misc.readUrl(
            "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s%%7C%s"
            % (text, from_l, to_l),
            None,
            bot,
        )
        if not data:
            return "can't get data"
    except:
        return _("google is not available, sorry.")

    try:
        convert = loads(data)
        status = convert["responseStatus"]

        results = convert["responseData"]["translatedText"]
        if results:
            return results
    except:
        pass

    return "can't translate this shit!"
Пример #3
0
def detect_lang(text, bot):
	google_langs =\
		{ 'af':'Afrikaans','sq':'Albanian','am':'Amharic','ar':'Arabic','hy':'Armenian','az':'Azerbaijani','eu':'Basque'
		, 'be':'Belarusian','bn':'Bengali','bh':'Bihari','bg':'Bulgarian','my':'Burmese','ca':'Catalan','chr':'Cherokee'
		, 'zh':'Chinese','zh-CN':'Chinese_simplified','zh-TW':'Chinese_traditional','hr':'Croatian','cs':'Czech','da':'Danish'
		, 'dv':'Dhivehi','nl':'Dutch','en':'English','eo':'Esperanto','et':'Estonian','tl':'Filipino','fi':'Finnish'
		, 'fr':'French','gl':'Galician','ka':'Georgian','de':'German','el':'Greek','gn':'Guarani','gu':'Gujarati','iw':'Hebrew'
		, 'hi':'Hindi','hu':'Hungarian','is':'Icelandic','id':'Indonesian','iu':'Inuktitut','it':'Italian','ja':'Japanese'
		, 'kn':'Kannada','kk':'Kazakh','km':'Khmer','ko':'Korean','ku':'Kurdish','ky':'Kyrgyz','lo':'Laothian','lv':'Latvian'
		, 'lt':'Lithuanian','mk':'Macedonian','ms':'Malay','ml':'Malayalam','mt':'Maltese','mr':'Marathi','mn':'Mongolian'
		, 'ne':'Nepali','no':'Norwegian','or':'Oriya','ps':'Pashto','fa':'Persian','pl':'Polish','pt-PT':'Portuguese'
		, 'pa':'Punjabi','ro':'Romanian','ru':'Russian','sa':'Sanskrit','sr':'Serbian','sd':'Sindhi','si':'Sinhalese'
		, 'sk':'Slovak','sl':'Slovenian','es':'Spanish','sw':'Swahili','sv':'Swedish','tg':'Tajik','ta':'Tamil','tl':'Tagalog'
		, 'te':'Telugu','th':'Thai','bo':'Tibetan','tr':'Turkish','uk':'Ukrainian','ur':'Urdu','uz':'Uzbek','ug':'Uighur','vi':'Vietnamese'
		}

	if not text:
		return ", ".join(["%s (%s)" %(v, k) for k, v in google_langs.items()])

	text = urllib.quote(text.encode('utf-8'))
	data = misc.readUrl('http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=%s' %(text), None, bot)
	if not data: return 'can\'t get data'

	try:
		convert = loads(data)
		results = convert['responseData']['language']

		return '%s (%s)' %(google_langs[results], results)
	except:
		return 'can\'t detect this shit!'
Пример #4
0
def google(query, bot):
    query = urllib.quote(query.encode('utf-8'))
    try:
        if "google_key" in bot.settings:
            google_key = "&key=" + bot.settings["google_key"]
        else:
            google_key = ""
        data = misc.readUrl(
            'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s&hl=ru%s'
            % (query, google_key), None, bot)
        if not data: return 'can\'t get data'
    except:
        return _("google is not available, sorry.")

    try:
        convert = loads(data)
        results = convert['responseData']['results']
        if not results: return 'not found'

        url = urllib.unquote(results[0]['url'])
        title = results[0]['titleNoFormatting']
        content = results[0]['content']
        text = '%s\n%s\n%s' % (title, content, url)

        text = re.sub('<b>|</b>', '', text)
        text = re.sub('   ', '\n', text)
        text = text.replace(u'\ufeff', '')

        return text
    except:
        return 'error'
Пример #5
0
def translate(from_l, to_l, text, bot):
    text = urllib.quote(text.encode('utf-8'))
    try:
        data = misc.readUrl('http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&key=%s&langpair=%s%%7C%s' %(text, bot.settings["google_key"], from_l, to_l), None, bot)
        if not data: return 'can\'t get data'
    except:
        return _("google is not available, sorry.")
   
    try:
        convert = loads(data)
        status = convert['responseStatus']

        results = convert['responseData']['translatedText']
        if results: 
            return results
    except:
        pass

    return 'can\'t translate this shit!'
Пример #6
0
def translate(from_l, to_l, text, bot):
    text = urllib.quote(text.encode('utf-8'))
    try:
        data = misc.readUrl(
            'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&key=%s&langpair=%s%%7C%s'
            % (text, bot.settings["google_key"], from_l, to_l), None, bot)
        if not data: return 'can\'t get data'
    except:
        return _("google is not available, sorry.")

    try:
        convert = loads(data)
        status = convert['responseStatus']

        results = convert['responseData']['translatedText']
        if results:
            return results
    except:
        pass

    return 'can\'t translate this shit!'