I am a total newbie to Python, but a veteran programmer. I am trying to write my first script. It will take a given URL, parse out all the anchors and display them in a list box. The user can then select a URL and press a to parse that one into the list box. Long term goal is to allow users to view videos or pictures from any web site. Right now it will parse the URL that the user enters, but when they choose a URL from the list, it doesn't parse that, just shows a blank list. I'm sure my problem has to do with the way I am creating and destroying the CGExtractor class. Any help is appreciated. Here is the code:
import urllib
import xbmc, xbmcgui
import imageviewer
from HTMLParser import HTMLParser
try: Emulating = xbmcgui.Emulating
except: Emulating = True
BaseURL = "base"
Root = "Q:\\scripts\\CG\\CG\\"
CGHrefList = []
class CGExtractor(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag=="a":
for att, val in attrs:
if val!=None:
if att=="href":
CGHrefList.append(parseURL(val))
class CG_Main(xbmcgui.Window):
def __init__(self):
if Emulating: xbmcgui.Window.__init__(self)
w = self.getWidth()
h = self.getHeight()
self.hrefList = xbmcgui.ControlList(w*.1,h*.1,w*.9,h*.9)
self.exit = xbmcgui.ControlButton(1200,1000,200,40, "Exit")
self.lblStatus = xbmcgui.ControlFadeLabel(100,1200,1000,100, "font12", "0xFFFFFFFF")
self.addControl(self.lblStatus)
self.addControl(self.hrefList)
self.addControl(self.exit)
keyboard = xbmc.Keyboard("http://")
keyboard.doModal()
self.setFocus(self.hrefList)
self.ProcessURL(keyboard.getText())
for item in CGHrefList:
self.hrefList.addItem(item)
def onControl(self, control):
print "Event Caught"
if control == self.hrefList:
item = self.hrefList.getSelectedItem().getLabel()
print "Using URL " + item
self.hrefList.reset()
print "reset list"
CGHrefList = []
print "Processing URL"
self.ProcessURL(item)
print "adding items to list"
for item2 in CGHrefList:
print "adding " + item2
self.hrefList.addItem(item2)
if control == self.exit:
self.close()
def onAction(self, Action):
if Action == 10:
self.close()
def ProcessURL(self, U):
print "processURL processing " + U
CG_Sock = urllib.urlopen(U)
CG_HTML = CG_Sock.read()
CG_Sock.close
p = CGExtractor()
p.feed(CG_HTML)
p.close
del p
def addPic(URL):
Display_Main.picList.addItem(URL)
def parseURL(URL):
if URL!=None:
if URL.startswith("http"):
pass
else:
if URL.startswith("/"):
URL = BaseURL + URL
else:
URL = BaseURL + "/" + URL
else:
URL=""
return URL
global Display_Main
Display_Main = CG_Main()
Display_Main.doModal()
del Display_Main
OK, so HTML eliminated my indenting. I hope you can still make sense of it.