Advertisement
banjax

Untitled

May 12th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import random
  2. from util import hook, http, text
  3.  
  4.  
  5. def api_get(kind, query):
  6. """Use the RESTful Google Search API"""
  7. url = 'http://ajax.googleapis.com/ajax/services/search/%s?' \
  8. 'v=1.0&safe=moderate'
  9. return http.get_json(url % kind, q=query)
  10.  
  11.  
  12. @hook.command('y')
  13. @hook.command
  14. def youtube(inp):
  15. """google <query> -- Returns first google search result for <query>."""
  16.  
  17. parsed = api_get('web', inp)
  18. if not 200 <= parsed['responseStatus'] < 300:
  19. raise IOError('error searching for pages: {}: {}'.format(parsed['responseStatus'], ''))
  20. if not parsed['responseData']['results']:
  21. return 'No results found.'
  22.  
  23. result = parsed['responseData']['results'][0]
  24.  
  25. title = http.unescape(result['titleNoFormatting'])
  26. title = text.truncate_str(title, 60)
  27. content = http.unescape(result['content'])
  28.  
  29. if not content:
  30. content = "No description available."
  31. else:
  32. content = http.html.fromstring(content).text_content()
  33. content = text.truncate_str(content, 150)
  34.  
  35. return u'{} -- \x02{}\x02: "{}"'.format(result['unescapedUrl'], title, content)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement