# -*- coding: utf-8 -*-
import sys, os, gettext, platform, utils
reload(sys)
#Pluginsystem support
import pluginsystem
#Configsystem support
import configsystem
#Google support
import search
#UTF-8 support
sys.setdefaultencoding('utf-8')
#Locale support
try:
LOCALE_DIR = "plugins" + os.path.sep + "google-videos" + os.path.sep + "locale"
t = gettext.translation("main", LOCALE_DIR)
_ = t.ugettext
except:
_ = utils.fake_locale
#Global vars
lastgooglevideosearchedstring = ""
lastgooglevideospage = 0
capabilities = [_("!google-videos"), _("!google-videos-more"), _("!google-videos-config"), _("?google-videos"), _("?google-videos-more"), _("?google-videos-config")]
config = (
((_("Google Videos")),
(
("resultsize", ("list", 0, (_("Number of results shown per page")), ((_("Small (4 results)")), (_("Large (8 results)"))))),
("resultfilter", ("list", 0, (_("Type of filter to be used when searching")), ((_("No filter")), (_("Moderate filter")), (_("Active filter"))))),
)
),
((_("Appearance")),
(
("plaintextcolor", ("color", "#C3C3C3", (_("Color of plain text")))),
("linkcolor", ("color", "#1862C2", (_("Color of links")))),
("titlecolor", ("color", "#C3C3C3", (_("Color of title")))),
("descriptioncolor", ("color", "#C3C3C3", (_("Color of description")))),
("numberofresultcolor", ("color", "#FF0000", (_("Color of result number")))),
("currentpagecolor", ("color", "#C05800", (_("Color of current page number")))),
("totalpagescolor", ("color", "#C05800", (_("Color of total pages number")))),
("totalresultscolor", ("color", "#00C000", (_("Color of total results number")))),
("timecolor", ("color", "#00C0C0", (_("Color of time needed for the search")))),
("videodurationcolor", ("color", "#AC47CD", (_("Color of the video duration text")))),
)
),
(3)
)
#Init event
@pluginsystem.register("init")
def init(*args, **kwargs):
global config
tmp_config = configsystem.load_config("google-videos", config)
if not tmp_config == None:
config = tmp_config
#Reload-config event to make a specific plugin reload it's config
@pluginsystem.register("reload_config")
def reload_config(*args, **kwargs):
global config
if "plugin" in kwargs:
if kwargs["plugin"] == "google-videos":
tmp_config = configsystem.load_config("google-videos", config)
if not tmp_config == None:
config = tmp_config
else:
print _("WARNING from Google-videos 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 lastgooglevideosearchedstring, lastgooglevideospage, config
if "data" in kwargs and "obj" in kwargs:
qcmd = kwargs["data"] #in case we need the QString instead of...
cmd = str(kwargs["data"]) #...python string
console = kwargs["obj"] #save the console from which we got the call
if cmd.startswith(_("!google-videos")) and qcmd.at(len(_("!google-videos"))).isSpace():
searchstring = cmd[len(_("!google-videos"))+1:]
#Check config to see what kind of result length should we request.
if configsystem.value(config, 'resultsize') == 0:
resultsize = "small"
intresultsize = 4
else:
resultsize = "large"
intresultsize = 8
#Check config to see what kind of filter should we use.
if configsystem.value(config, 'resultfilter') == 0:
resultfilter = "off"
elif configsystem.value(config, 'resultfilter') == 1:
resultfilter = "moderate"
else:
resultfilter = "active"
#Change last page to 1 if search string is not the same as the last search string, and change last searched string
if not lastgooglevideosearchedstring == searchstring:
lastgooglevideospage = 1
lastgooglevideosearchedstring = searchstring
#Make the search
gresults = search.videosearch(searchstring, lastgooglevideospage, resultsize, resultfilter)
if gresults == None:
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("No results to show or an error occurred.") + "</font>", False, False)
return
results = gresults.results
tmpstr = "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>Page <b>"
tmpstr += "<font color='" + configsystem.value(config, 'currentpagecolor') + "'>%(currentpage)s</font></b> of <b>"
tmpstr += "<font color='" + configsystem.value(config, 'totalpagescolor') + "'>%(totalpages)s</font></b></font><br>"
output = _((tmpstr) % {"currentpage":str(gresults.page+1), "totalpages":str(int(gresults.resultcount)/len(gresults.results))})
tmpstr = "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>Found <b>"
tmpstr += "<font color='" + configsystem.value(config, 'totalresultscolor') + "'>%(results)s</font></b> results in <b>"
tmpstr += "<font color='" + configsystem.value(config, 'timecolor') + "'>%(seconds)s</font></b> seconds"
output += _((tmpstr) % {"results":str(gresults.resultcount), "seconds":str(gresults.responseTime)}) + "</font><br>"
output += "<table>"
i = 1
for result in results:
output += "<tr>"
output += " <td valign=middle width=15><font color='" + configsystem.value(config, 'numberofresultcolor') + "'>" + str(gresults.page*intresultsize+i) + ".</font></td>"
output += " <td valign=middle width=100><a href='" + result.url + "'><img src='" + result.thumbnail + "' border=0 align='left'></a></td>"
output += " <td valign=middle>"
output += " <font color='" + configsystem.value(config, 'videodurationcolor') + "'>" + str(result.duration) + "</font>"
output += " <font color='" + configsystem.value(config, 'plaintextcolor') + "'> - </font>"
output += " <font color='" + configsystem.value(config, 'titlecolor') + "'>" + result.title + "</font>"
output += " <font color='" + configsystem.value(config, 'plaintextcolor') + "'> - </font>"
output += " <font color='" + configsystem.value(config, 'descriptioncolor') + "'>" + result.description + "</font>"
output += " </td>"
output += "</tr>"
i += 1
output += "</table>"
console.printString(output, False, False)
elif cmd == _("!google-videos-more"):
#Increase last page before doing the search
lastgooglevideospage = lastgooglevideospage + 1
#Check config to see what kind of result length should we request.
if configsystem.value(config, 'resultsize') == 0:
resultsize = "small"
intresultsize = 4
else:
resultsize = "large"
intresultsize = 8
#Check config to see what kind of filter should we use.
if configsystem.value(config, 'resultfilter') == 0:
resultfilter = "off"
elif configsystem.value(config, 'resultfilter') == 1:
resultfilter = "moderate"
else:
resultfilter = "active"
#Make the search
gresults = search.videosearch(lastgooglevideosearchedstring, lastgooglevideospage*intresultsize-1, resultsize, resultfilter)
if gresults == None:
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("No more results to show or an error occurred.") + "</font>", False, False)
return
if gresults == None:
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("No more results to show or an error occurred.") + "</font>", False, False)
results = gresults.results
tmpstr = "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>Page <b>"
tmpstr += "<font color='" + configsystem.value(config, 'currentpagecolor') + "'>%(currentpage)s</font></b> of <b>"
tmpstr += "<font color='" + configsystem.value(config, 'totalpagescolor') + "'>%(totalpages)s</font></b></font><br>"
output = _((tmpstr) % {"currentpage":str(lastgooglevideospage+1), "totalpages":str(int(gresults.resultcount)/len(gresults.results))})
tmpstr = "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>Found <b>"
tmpstr += "<font color='" + configsystem.value(config, 'totalresultscolor') + "'>%(results)s</font></b> results in <b>"
tmpstr += "<font color='" + configsystem.value(config, 'timecolor') + "'>%(seconds)s</font></b> seconds"
output += _((tmpstr) % {"results":str(gresults.resultcount), "seconds":str(gresults.responseTime)}) + "</font><br>"
output += "<table>"
i = 1
for result in results:
output += "<tr>"
output += " <td valign=middle width=15><font color='" + configsystem.value(config, 'numberofresultcolor') + "'>" + str(lastgooglevideospage*intresultsize+i) + ".</font></td>"
output += " <td valign=middle width=200><a href='" + result.url + "'><img src='" + result.thumbnail + "' border=0 align='left'></a></td>"
output += " <td valign=middle>"
output += " <font color='" + configsystem.value(config, 'videodurationcolor') + "'>" + str(result.duration) + "</font>"
output += " <font color='" + configsystem.value(config, 'plaintextcolor') + "'> - </font>"
output += " <font color='" + configsystem.value(config, 'titlecolor') + "'>" + result.title + "</font>"
output += " <font color='" + configsystem.value(config, 'plaintextcolor') + "'> - </font>"
output += " <font color='" + configsystem.value(config, 'descriptioncolor') + "'>" + result.description + "</font>"
output += " </td>"
output += "</tr>"
i += 1
output += "</table>"
console.printString(output, False, False)
elif cmd == _("!google-videos-config"):
configsystem.config_dialog(config, "google-videos", console)
elif cmd == _("?google-videos"):
output = "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>"
output += _("The command !google-videos will search a string in Google and it will print the results in this console. Example of usage: !google-videos mustang gt500 vs nissan skyline")
output += "</font>"
console.printString(output, False, False)
elif cmd == _("?google-videos-more"):
output = "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>"
output += _("The command !google-videos-more will search for more results in Google based on the last made search.")
output += "</font>"
console.printString(output, False, False)
elif cmd == _("?google-videos-config"):
output = "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>"
output += _("The command !google-videos-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-videos 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-videos plugin: get_capabilities event was called with inappropiate number of arguments! Please check code.")