<s> from kaa import bot <EOL> from kaa . utils import shortener <EOL> from kaa . wikipedia import wiki_re , wiki_search <EOL> from kaa . youtube import youtube_re , get_video_description <EOL> from urllib import quote <EOL> from HTMLParser import HTMLParser <EOL> import re <EOL> import json <EOL> import requests <EOL> URL = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def search ( query , kind = '<STR_LIT>' ) : <EOL> r = requests . get ( URL . format ( kind , quote ( query ) ) ) <EOL> data = json . loads ( r . content ) <EOL> return data <EOL> @ bot . command ( '<STR_LIT:g>' ) <EOL> @ bot . command <EOL> def google ( context ) : <EOL> data = search ( context . args ) <EOL> if not data [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> return '<STR_LIT>' <EOL> first_result = data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> url = first_result [ '<STR_LIT>' ] <EOL> wiki = re . search ( wiki_re , url ) <EOL> if wiki : <EOL> desc , url = wiki_search ( wiki . groups ( ) [ - <NUM_LIT:1> ] ) <EOL> return '<STR_LIT>' . format ( desc , url ) <EOL> youtube = re . search ( youtube_re , url ) <EOL> if youtube : <EOL> vid_id = youtube . groups ( ) [ - <NUM_LIT:1> ] <EOL> desc = get_video_description ( vid_id ) <EOL> return '<STR_LIT>' . format ( desc , url ) <EOL> title = first_result [ '<STR_LIT>' ] . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> title = HTMLParser . unescape . __func__ ( HTMLParser , title ) <EOL> ret = title + '<STR_LIT>' + shortener ( url ) <EOL> return ret <EOL> @ bot . command <EOL> def gis ( context ) : <EOL> data = search ( context . args , '<STR_LIT>' ) [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if not data : <EOL> return '<STR_LIT>' <EOL> return data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] </s>
