2006-01-28, 04:14
hello all. this is my first script.
i'm having a little trouble with the search. the search url works fine in a browser, but google seems to have stopped sending me data. popular and random work fine. please check out the code and provide any feedback.
the code source was roughly based on the stupidvids script made by nishil
have fun.
-morrows
listing of googlevideo.py
i'm having a little trouble with the search. the search url works fine in a browser, but google seems to have stopped sending me data. popular and random work fine. please check out the code and provide any feedback.
the code source was roughly based on the stupidvids script made by nishil
have fun.
-morrows
listing of googlevideo.py
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 = {
'accept-language': 'en-us',
}
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.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.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.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()
#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)
videos = self.getgooglevideos(search_url)
for video in 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...")
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>') #todo: insert regular expression
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
print("getgooglevideos:" + link_data[1]) #title
video_data.append(googlevideo(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("[url]http://video.google.com/videogvp?docid="[/url] + 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