Advertisement
CommissarSov

Untitled

Apr 1st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. # -*- coding: UTF-8 -*-
  2. '''
  3. Searches from Google
  4. '''
  5. import json
  6. import urllib
  7. import sys
  8. reload(sys)
  9. sys.setdefaultencoding("utf-8")
  10.  
  11. def google(components): # #wiki <search term>
  12. # Returns a wiki link and the first paragraph of the page
  13.  
  14. main_page = "https://www.google.com/#q="
  15.  
  16. wlink = components.split("#google ") # notice the trailing space
  17. if 1 == len(wlink): # no search term given, the Main_Page is "displayed"
  18. response = main_page
  19. else:
  20. search_term = wlink[1].lstrip()
  21. search_term = urllib.quote_plus(search_term)
  22.  
  23. if len(search_term) < 1:
  24. response = main_page
  25. else:
  26. response = "https://www.google.com/#q=" + search_term
  27.  
  28. response = response + "\r\n" #+ get_link(response)
  29.  
  30. return response.encode("utf-8")
  31.  
  32. '''def google(searchfor, components):
  33. query = urllib.urlencode({"q": searchfor})
  34. url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s" % query
  35. search_response = urllib.urlopen(url)
  36. search_results = search_response.read()
  37. results = json.loads(search_results)
  38. data = results["responseData"]
  39. hits = data["results"]
  40. print "Top %d hits:" % len(hits)
  41. for h in hits: printh["url"]
  42. print "For more results, see %s" % data["cursor"]["moreResultsUrl"]
  43.  
  44. showsome('ermanno olmi')'''
  45.  
  46. class Handler:
  47. priority = 10500; ''' It is used to determine which handler should be checked canHandle() first.
  48. Two handlers must not have the same priority (unless priority is 0)
  49. If priority is not defined, it is considered to be 0. '''
  50.  
  51. def __init__(self, brain):
  52. self.brain = brain; # Brain is not used in this example, but it is useful if you want i.e the name of the bot
  53.  
  54. def canHandle(self, msg):
  55. return msg.command == "PRIVMSG" and msg.msg.startswith("#google")
  56.  
  57. def handle(self, msg, resp):
  58. responseText = google(msg.msg)
  59. resp.send(responseText, msg.re());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement