plugins/disabled/google_plugin.py
changeset 0 93b25987d3e5
child 17 069f7fd5545d
equal deleted inserted replaced
-1:000000000000 0:93b25987d3e5
       
     1 #$ neutron_plugin 01
       
     2 
       
     3 import google
       
     4 
       
     5 def google_remove_html(text):
       
     6 	nobold = text.replace('<b>', '').replace('</b>', '')
       
     7 	nobreaks = nobold.replace('<br>', ' ')
       
     8 	noescape = nobreaks.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('&quot;', '"')
       
     9 	return noescape
       
    10 
       
    11 def google_search(query):
       
    12 	data = google.doGoogleSearch(query)
       
    13 	try:
       
    14 		first = data.results[0]
       
    15 		url = first.URL
       
    16 		title = google_remove_html(first.title)
       
    17 		if first.summary:
       
    18 			summary = google_remove_html(first.summary)
       
    19 		else:
       
    20 			summary = google_remove_html(first.snippet)
       
    21 		searchtime = str(round(data.meta.searchTime, 3))
       
    22 		total = str(data.meta.estimatedTotalResultsCount)
       
    23 		return url + ' - ' + title + ' - ' + summary + ' (' + searchtime + 'sec) (' + total + ' sites)'
       
    24 	except:
       
    25 		return 'No Results'
       
    26 
       
    27 def handler_google_google(type, source, parameters):
       
    28 	results = google_search(parameters)
       
    29 	smsg(type, source, results)
       
    30 
       
    31 def handler_google_spell(type, source, parameters):
       
    32 	correction = google.doSpellingSuggestion(parameters)
       
    33 	if not correction:
       
    34 		correction = 'No Suggestion'
       
    35 	smsg(type, source, correction)
       
    36 
       
    37 def handler_google_jepsearch(type, source, parameters):
       
    38 	results = google_search(parameters + ' site:www.xmpp.org "/exensions/xep-"')
       
    39 	smsg(type, source, results)
       
    40 
       
    41 register_command_handler(handler_google_google, '!google', 0, 'Looks up search terms on google.', '!google <query>', ['!google "mike mintz"'])
       
    42 register_command_handler(handler_google_spell, '!spell', 0, 'Returns a spelling suggestion from google.', '!spell <query>', ['!spell "pithon nutron"'])
       
    43 register_command_handler(handler_google_jepsearch, '!jepsearch', 0, 'Searches google for a JEP.', '!jep <query>', ['!google "jep-0001"'])