Menu

[r188]: / plugins / google-videos / main.py  Maximize  Restore  History

Download this file

208 lines (198 with data), 11.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# -*- 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.")