# -*- coding: utf-8 -*-
import sys, os, gettext, platform
import pluginsystem, configsystem
import search
import utils as u
reload(sys)
#Locale support
try:
LOCALE_DIR = "plugins" + os.path.sep + "google" + os.path.sep + "locale"
t = gettext.translation("main", LOCALE_DIR)
_ = t.ugettext
except:
_ = u.fake_locale
#Global vars
lastgooglesearchedstring = ""
lastgooglepage = 0
capabilities = [_("!google"), _("!google-more"), _("!google-config"), _("?google"), _("?google-more"), _("?google-config")]
d_resultsize = {_("Small (4 results)") : 'small', _("Large (8 results)") : 'large'}
d_resultfilter = {_("No filter") : 'off', _("Moderate filter") : 'moderate', _("Active filter") : 'active'}
config_struct = (
((_("Google")),
(
("resultsize", ("combo", (_("Number of results shown per page")), d_resultsize)),
("resultfilter", ("combo", (_("Type of filter to be used when searching")), d_resultfilter))
)
),
((_("Appearance")),
(
("plaintextcolor", ("color", (_("Color of plain text")))),
("linkcolor", ("color", (_("Color of links")))),
("numberofresultcolor", ("color", (_("Color of result number")))),
("currentpagecolor", ("color", (_("Color of current page number")))),
("totalpagescolor", ("color", (_("Color of total pages number")))),
("totalresultscolor", ("color", (_("Color of total results number")))),
("timecolor", ("color", (_("Color of time needed for the search"))))
)
)
)
config_prefs = {
'resultsize' : 'small',
'resultfilter' : 'off',
'plaintextcolor' : '#C3C3C3',
'linkcolor' : '#1862C2',
'numberofresultcolor' : '#FF0000',
'currentpagecolor' : '#C05800',
'totalpagescolor' : '#C05800',
'totalresultscolor' : '#00C000',
'timecolor' : '#04C1BE'
}
#Init event
@pluginsystem.register("init")
def init(*args, **kwargs):
global config_prefs
config_prefs = configsystem.load_config("google", config_prefs)
#Reload-config event to make a specific plugin reload it's config
@pluginsystem.register("reload_config")
def reload_config(*args, **kwargs):
global config_prefs
if "plugin" in kwargs:
if kwargs["plugin"] == "google":
config_prefs = configsystem.load_config("google", config_prefs)
else:
print _("WARNING from Google plugin: reload_config event was called with inappropiate number of arguments! Please check code.")
#Triggered after return/enter hit
@pluginsystem.register("analyze_command")
def analyze_command(*args, **kwargs):
global lastgooglesearchedstring, lastgooglepage
if "data" in kwargs and "obj" in kwargs:
cmd = kwargs["data"]
console = kwargs["obj"] #save the console from which we got the call
#Check config to see what kind of result length should we request.
resultsize = config_prefs['resultsize']
if resultsize == 'small':
intresultsize = 4
else:
intresultsize = 8
#Check config to see what kind of filter should we use.
resultfilter = config_prefs['resultfilter']
if cmd.startswith(_("!google")) and cmd[len(_("!google")):][0].isspace():
searchstring = cmd[len(_("!google"))+1:]
#Change last page to 1 if search string is not the same as the last search string, and change last searched string
if not lastgooglesearchedstring == searchstring:
lastgooglepage = 0
lastgooglesearchedstring = searchstring
#Make the search
results = search.websearch(searchstring, lastgooglepage, resultsize, resultfilter)
if results == None:
console.printString("<font color='" + config_prefs['plaintextcolor'] + "'>" + _("No results to show or an error occurred.") + "</font>", False, False)
return
tmpstr = "<font color='" + config_prefs['plaintextcolor'] + "'>Page <b>"
tmpstr += "<font color='" + config_prefs['currentpagecolor'] + "'>%(currentpage)s</font></b> of <b>"
tmpstr += "<font color='" + config_prefs['totalpagescolor'] + "'>%(totalpages)s</font></b></font><br>"
output = _((tmpstr) % {"currentpage":str(results.page+1), "totalpages":str(int(results.resultcount)/len(results.results))})
tmpstr = "<font color='" + config_prefs['plaintextcolor'] + "'>Found <b>"
tmpstr += "<font color='" + config_prefs['totalresultscolor'] + "'>%(results)s</font></b> results in <b>"
tmpstr += "<font color='" + config_prefs['timecolor'] + "'>%(seconds)s</font></b> seconds"
output += _((tmpstr) % {"results":str(results.resultcount), "seconds":str(results.responseTime)}) + "</font><br>"
i = 1
for result in results.results:
output += str("<font color='" + config_prefs['plaintextcolor'] + "'><font color='" + config_prefs['numberofresultcolor'] + "'>" + str(results.page*intresultsize+i))
output += ". </font><b>" + result.title + "</b> - " + result.description + " - </font><a href='" + result.url + "'>"
output += "<font color='" + config_prefs['linkcolor'] + "'>" + result.url + "</a></font><br>"
if i < len(results.results):
output += "<br>"
i += 1
console.printString(output, False, False)
elif cmd == _("!google-more"):
#Increase last page before doing the search
lastgooglepage = lastgooglepage + 1
#Make the search
results = search.websearch(lastgooglesearchedstring, lastgooglepage*intresultsize-1, resultsize, resultfilter)
if results == None:
console.printString("<font color='" + config_prefs['plaintextcolor'] + "'>" + _("No results to show or an error occurred.") + "</font>", False, False)
return
tmpstr = "<font color='" + config_prefs['plaintextcolor'] + "'>Page <b>"
tmpstr += "<font color='" + config_prefs['currentpagecolor'] + "'>%(currentpage)s</font></b> of <b>"
tmpstr += "<font color='" + config_prefs['totalpagescolor'] + "'>%(totalpages)s</font></b></font><br>"
output = _((tmpstr) % {"currentpage":str(lastgooglepage+1), "totalpages":str(int(results.resultcount)/len(results.results))})
tmpstr = "<font color='" + config_prefs['plaintextcolor'] + "'>Found <b>"
tmpstr += "<font color='" + config_prefs['totalresultscolor'] + "'>%(results)s</font></b> results in <b>"
tmpstr += "<font color='" + config_prefs['timecolor'] + "'>%(seconds)s</font></b> seconds"
output += _((tmpstr) % {"results":str(results.resultcount), "seconds":str(results.responseTime)}) + "</font><br>"
i = 1
for result in results.results:
output += "<font color='" + config_prefs['plaintextcolor'] + "'><font color='" + config_prefs['numberofresultcolor'] + "'>" + str(lastgooglepage*intresultsize+i)
output += ". </font><b>" + result.title + "</b> - " + result.description + " - </font><a href='" + result.url + "'>"
output += "<font color='" + config_prefs['linkcolor'] + "'>" + result.url + "</a></font><br>"
if i < len(results.results):
output += "<br>"
i += 1
console.printString(output, False, False)
elif cmd == _("!google-config"):
configsystem.config_dialog("google", config_struct, config_prefs, console)
elif cmd == _("?google"):
output = "<font color='" + config_prefs['plaintextcolor'] + "'>"
output += _("The command !google will search a string in Google and it will print the results in this console. Example of usage: !google where can I get jake?")
output += "</font>"
console.printString(output, False, False)
elif cmd == _("?google-more"):
output = "<font color='" + config_prefs['plaintextcolor'] + "'>"
output += _("The command !google-more will search for more results in Google based on the last made search.")
output += "</font>"
console.printString(output, False, False)
elif cmd == _("?google-config"):
output = "<font color='" + config_prefs['plaintextcolor'] + "'>"
output += _("The command !google-config will show up a config dialog which will let you configure the behavior of this plugin (number of results shown per page, search filter, text color...)")
output += "</font>"
console.printString(output, False, False)
else:
print _("WARNING from Google plugin: analyze_command event was called with inappropiate number of arguments! Please check code.")
#Send all available commands to the plugin system
@pluginsystem.register("get_capabilities")
def get_capabilities(*args, **kwargs):
if "cap" in kwargs:
kwargs["cap"].extend(capabilities)
else:
print _("WARNING from Google plugin: get_capabilities event was called with inappropiate number of arguments! Please check code.")