2006-05-05, 15:14
hey,
it's been a while since i updated this script. i got most of the kinks out and got searching to work. i hope you enjoy. i also posted on xbmcscripts.com but don't know how long it will be before its available for download, so here's the raw script:
enjoy!
-morrows :joker:
it's been a while since i updated this script. i got most of the kinks out and got searching to work. i hope you enjoy. i also posted on xbmcscripts.com but don't know how long it will be before its available for download, so here's the raw script:
Quote:import urllib,urllib2 , re,os
import xbmc, xbmcgui
from string import split, replace, find
try: emulating = xbmcgui.emulating
except: emulating = false
action_move_left = 1
action_move_right = 2
action_move_up = 3
action_move_down = 4
action_page_up = 5
action_page_down = 6
action_select_item = 7
action_highlight_item = 8
action_parent_dir = 9
action_previous_menu = 10
action_show_info = 11
action_pause = 12
action_stop = 13
action_next_item = 14
action_prev_item = 15
popular_video_url = 'http://video.google.com/videopopularpage?num=100'
random_video_url = 'http://video.google.com/videorandom?num=100'
if emulating:
dirhome = 'q:\\scripts\\googlevideo\\'
else:
dirhome = os.getcwd()
dirhome = dirhome[:-1]+'\\'
strheaders = {
'http_accept_language':'en-us,en;q=0.5',
'http_connection':'keep-alive',
'http_user_agent':'mozilla/5.0 (windows; u; windows nt 5.1; en-us; rv:1.8.0.1) gecko/20060111 firefox/1.5.0.1',
'http_accept':'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
'http_accept_encoding':'gzip,deflate',
'http_accept_charset':'iso-8859-1,utf-8;q=0.7,*;q=0.7',
'request_method':'get',
}
strbuffer = none
class googlevideo:
def (self, title, docid):
self.title = title
self.docid = docid
class googlevideos(xbmcgui.window):
def (self):
if emulating: xbmcgui.window.(self)
self.scalex = ( float(self.getwidth()) / float(720) )
self.scaley = ( float(self.getheight()) / float(480) )
# stretch background image to fit window
#self.addcontrol(xbmcgui.controlimage(0, 0, self.getwidth(), self.getheight(), dirhome + "\\images\\googlebackground.jpg"))
# leave a 100 pixel top and 200 pixel left 20 pixel right and 20 pixel bottom
self.videolist = xbmcgui.controllist(200,100, (self.getwidth() - 220), (self.getheight()-120))
#self.addcontrol(xbmcgui.controlimage(5, 5, 142, 51, dirhome + "\\images\\googlevideologo.gif"))
intbottomoffset = 100
intspacingoffset = 40
self.btnpopular = xbmcgui.controlbutton(50, (self.getheight() - (intbottomoffset + (3 * intspacingoffset))), 150, 30, "popular 100")
self.btnrandom = xbmcgui.controlbutton(50, (self.getheight() - (intbottomoffset + (2 * intspacingoffset))), 150, 30, "random 100")
self.btnsearch = xbmcgui.controlbutton(50, (self.getheight() - (intbottomoffset + intspacingoffset)), 150, 30, "google search")
self.btnquit = xbmcgui.controlbutton(50, (self.getheight() - intbottomoffset), 150, 30, "quit")
#add all the buttons
self.addcontrol(self.btnpopular)
self.addcontrol(self.btnrandom)
self.addcontrol(self.btnsearch)
self.addcontrol(self.btnquit)
#add description text box
self.descriptiontxt = xbmcgui.controltextbox(150, 560, 50,300, "font14", "ffffffffff")
self.addcontrol(self.descriptiontxt)
#add the videolistbox
self.addcontrol(self.videolist)
#make the listbox visible and set focus on the popular video link
self.videolist.setvisible(1)
self.setfocus(self.btnpopular)
#set the control navigation directions
self.btnpopular.controlup(self.btnquit)
self.btnpopular.controldown(self.btnrandom)
self.btnpopular.controlright(self.videolist)
self.btnpopular.controlleft(self.videolist)
self.btnrandom.controlup(self.btnpopular)
self.btnrandom.controldown(self.btnsearch)
self.btnrandom.controlright(self.videolist)
self.btnrandom.controlleft(self.videolist)
self.btnsearch.controlup(self.btnrandom)
self.btnsearch.controldown(self.btnquit)
self.btnsearch.controlright(self.videolist)
self.btnsearch.controlleft(self.videolist)
self.btnquit.controlup(self.btnsearch)
self.btnquit.controldown(self.btnpopular)
self.btnquit.controlright(self.videolist)
self.btnquit.controlleft(self.videolist)
self.videolist.controlleft(self.btnpopular)
#start off by loading the popular 100 list
self.videos = []
self.videos = self.getgooglevideos(popular_video_url)
#load the video list into the navigation list
for video in self.videos:
self.videolist.additem(video.title)
self.setfocus(self.videolist)
def onaction(self, action):
#handle controller button press events
print("onaction")
if action == action_previous_menu:
print("onaction:back button")
self.close()
def oncontrol(self, control):
#handle on-screen gui events
print("oncontrol")
#clicked the popular 100 button
if control == self.btnpopular:
print("oncontrol:getpopular")
#clear the list
self.videolist.reset()
self.videos = []
self.videos = self.getgooglevideos(popular_video_url)
for video in self.videos:
self.videolist.additem(video.title)
#clicked the random 100 button
if control == self.btnrandom:
print("oncontrol:getrandom")
#clear the list
self.videolist.reset()
self.videos = []
self.videos = self.getgooglevideos(random_video_url)
for video in self.videos:
self.videolist.additem(video.title)
#clicked the search button
if control == self.btnsearch:
print("oncontrol:googlesearch")
#display keyboard entry
keyboard = xbmc.keyboard('')
keyboard.domodal()
#if not cancel
if (keyboard.isconfirmed()):
#clear the list
self.videolist.reset()
self.videos = []
#build a search url
tmpstring = urllib.quote_plus(keyboard.gettext())
search_url = 'http://video.google.com/videosearch?q=' + tmpstring + "&so=0"
print("oncontrol:googlesearch:" + search_url)
self.videos = self.getgooglevideos(search_url)
i = 0
while i <= 6: #gets the first 90 responses
search_url = 'http://video.google.com/videosearch?q=' + tmpstring + "&page=" + str(i) + "&so=0"
print("oncontrol:googlesearch:" + search_url)
tmpvideos = []
tmpvideos = self.getgooglevideos(search_url)
for video in tmpvideos:
self.videos.append(video)
i = i + 1
for video in self.videos:
self.videolist.additem(video.title)
#clicked the quit button
if control == self.btnquit:
print("oncontrol:quit")
#exit the application
self.close()
#clicked a video in the list box
if control == self.videolist:
print("oncontrol:launchvideo")
#get the video url
video_docid = self.videos[self.videolist.getselectedposition()].docid
video_url = self.getvideourl(video_docid)
#launch xbmc video player
if video_url != 'none':
print("oncontrol:launchvideo:" + video_url)
print("launching video player...")
if emulating:
#do nothing
print("emulation mode - no player will launch")
else:
xbmc.player().play(video_url)
else:
#todo: add a message box here.
print("sorry no video available.")
def getgooglevideos(self, url):
print("getgooglevideos")
html_data = self.gethtmldata(url)
video_data = []
#select video links with a regular expression
#googlevideolinkre = re.compile(r'<div class="thumbtitle"> <a href="videoplay\?docid=(?p<url>[^"\'<>]+)">(?p<title>.+?)</a>')
googlevideolinkre = re.compile(r'<div class="thumbtitle"> <a href="videoplay\?docid=(?p<url>[^"\'<>]+)"?[^>]+>(?p<title>.+?)</a>')
video_links = googlevideolinkre.findall(html_data)
#iterate through matches
for link_data in video_links:
#open links to get stream url's
print("getgooglevideos:" + link_data[0]) #docid
tagre = re.compile('<.+?>')
print("getgooglevideos:" + tagre.sub('', link_data[1])) #title
video_data.append(googlevideo(tagre.sub('', link_data[1]), link_data[0]))
return video_data
def getvideourl(self, docid):
print("getvideourl")
#get the video url from the docid
html_data = self.gethtmldata("http://video.google.com/videogvp?docid=" + docid)
#get the url with a regular expression
videourl_re = re.compile("url:(?p<url>[^\"\'<>]+)docid:")
videourl = videourl_re.search(html_data)
if videourl != 'none':
url = videourl.group('url')
print("getvideourl:" + url)
else:
url = videourl
return url
def gethtmldata(self, url):
request = urllib2.request(url, strbuffer, strheaders)
print("gethtmldata")
try:
tmpresponse = urllib2.urlopen(request)
except urllib2.httperror, error:
print("gethtmldata:error retrieving data : " + error)
if not emulating:
tmpdialog = xbmcgui.dialog()
tmpdialog.ok("network error", error)
return error
html = tmpresponse.read()
tmpresponse.close()
return html
#run application here.
myapplication = googlevideos()
myapplication.domodal()
del myapplication
enjoy!
-morrows :joker: