Hi all,
I need your help, I have got a problem with my python script. I'm using XBMC media application which they are using python 2.6v which they works the same way as the other version for python.
When I'm pressing on the enter button of the keyboard, the code will keep re-fetching the same values from each XML tag which it will print 5 same values for each tag when there is one value from each tag.
Here's the code:
import xbmc
import xbmcgui
import xbmcaddon
import os
import urllib2
import StringIO
import sqlite3
from sqlite3 import dbapi2 as database
from xml.etree import ElementTree
class Channel:
def __init__(self):
self.__display_name = None
self.__icon = None
self.__programs = []
def get_display_name(self):
return self.__display_name
def get_icon(self):
return self.__icon
def get_programs(self):
return self.__programs
def set_display_name(self, value):
self.__display_name = value
def set_icon(self, value):
self.__icon = value
def set_programs(self, value):
self.__programs = value
def del_display_name(self):
del self.__display_name
def del_icon(self):
del self.__icon
def del_programs(self):
del self.__programs
display_name = property(get_display_name, set_display_name, del_display_name, "display_name's docstring")
icon = property(get_icon, set_icon, del_icon, "icon's docstring")
programs = property(get_programs, set_programs, del_programs, "programs's docstring")
class Programme:
def __init__(self):
self.__start = None
self.__stop = None
self.__title = None
self.__sub_title = None
self.__desc = None
self.__category = []
self.__credits = []
self.__icon = None
self.__episode_num = None
def get_episode_num(self):
return self.__episode_num
def set_episode_num(self, value):
self.__episode_num = value
def del_episode_num(self):
del self.__episode_num
def get_start(self):
return self.__start
def get_stop(self):
return self.__stop
def get_title(self):
return self.__title
def get_sub_title(self):
return self.__sub_title
def get_desc(self):
return self.__desc
def get_category(self):
return self.__category
def get_credits(self):
return self.__credits
def get_icon(self):
return self.__icon
def set_start(self, value):
self.__start = value
def set_stop(self, value):
self.__stop = value
def set_title(self, value):
self.__title = value
def set_sub_title(self, value):
self.__sub_title = value
def set_desc(self, value):
self.__desc = value
def set_category(self, value):
self.__category = value
def set_credits(self, value):
self.__credits = value
def set_icon(self, value):
self.__icon = value
def del_start(self):
del self.__start
def del_stop(self):
del self.__stop
def del_title(self):
del self.__title
def del_sub_title(self):
del self.__sub_title
def del_desc(self):
del self.__desc
def del_category(self):
del self.__category
def del_credits(self):
del self.__credits
def del_icon(self):
del self.__icon
start = property(get_start, set_start, del_start, "start's docstring")
stop = property(get_stop, set_stop, del_stop, "stop's docstring")
title = property(get_title, set_title, del_title, "title's docstring")
sub_title = property(get_sub_title, set_sub_title, del_sub_title, "sub_title's docstring")
desc = property(get_desc, set_desc, del_desc, "desc's docstring")
category = property(get_category, set_category, del_category, "category's docstring")
creditss = property(get_credits, set_credits, del_credits, "credits's docstring")
icon = property(get_icon, set_icon, del_icon, "icon's docstring")
episode_num = property(get_episode_num, set_episode_num, del_episode_num, "episode_num's docstring")
class Credits:
def __init__(self):
self.__type = None
self.__role = None
self.__name = None
def get_type(self):
return self.__type
def get_role(self):
return self.__role
def get_name(self):
return self.__name
def set_type(self, value):
self.__type = value
def set_role(self, value):
self.__role = value
def set_name(self, value):
self.__name = value
def del_type(self):
del self.__type
def del_role(self):
del self.__role
def del_name(self):
del self.__name
type = property(get_type, set_type, del_type, "type's docstring")
role = property(get_role, set_role, del_role, "role's docstring")
name = property(get_name, set_name, del_name, "name's docstring")
class MyClass(xbmcgui.WindowXML):
def __new__(cls):
return super(MyClass, cls).__new__(cls, 'script-tvguide-mainmenu.xml', ADDON.getAddonInfo('path'))
def onInit(self):
pass
def load_channel(self, elem):
channel = Channel()
for elem in elem.getchildren():
if elem.tag == 'display-name':
channel.set_display_name(elem.text)
elif elem.tag == 'icon':
channel.set_icon(elem.attrib['src'])
return channel
def load_programme(self, elem):
programme = Programme()
programme.set_start(elem.attrib['start'])
programme.set_stop(elem.attrib['stop'])
for elem in elem.getchildren():
if elem.tag == 'title':
programme.set_title(elem.text)
elif elem.tag == 'sub-title':
programme.set_title(elem.text)
elif elem.tag == 'desc':
programme.set_desc(elem.text)
elif elem.tag == 'category':
categories = programme.get_category()
categories.append(elem.text)
elif elem.tag == 'episode-num':
programme.set_episode_num(elem.text)
elif elem.tag == 'credits':
creditss = programme.get_credits()
creditss.append(self.load_credits(elem))
elif elem.tag == 'icon':
programme.set_icon(elem.attrib['src'])
return programme
def load_credits(self, elem):
creditss = Credits()
for elem in elem.getchildren():
if elem.tag == 'actor':
creditss.set_name(elem.text)
creditss.set_type('actor')
elif elem.tag == 'presenter':
creditss.set_name(elem.text)
creditss.set_type('presenter')
elif elem.tag == 'director':
creditss.set_name(elem.text)
creditss.set_type('director')
return credits
def onAction(self, action):
if action == ACTION_ENTER:
#OPEN THE XML SOURCE
url = ADDON.getSetting('ontv.url')
req = urllib2.Request(url)
response = urllib2.urlopen(req)
data = response.read()
response.close()
profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
if os.path.exists(profilePath):
profilePath = profilePath + 'source.db'
con = database.connect(profilePath)
cur = con.cursor()
cur.execute('CREATE TABLE programs(id TEXT, channel TEXT, title TEXT, start_date TIMESTAMP, end_date TIMESTAMP, description TEXT)')
con.commit()
con.close
tv_elem = ElementTree.parse(StringIO.StringIO(data)).getroot()
channels = {}
for elem in tv_elem.getchildren():
if elem.tag == 'channel':
channels[elem.attrib['id']] = self.load_channel(elem)
for channel_key in channels:
channel = channels[channel_key]
display_name = channel.get_display_name()
print display_name
profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
profilePath = profilePath + 'source.db'
con = database.connect(profilePath)
cur = con.cursor()
if not display_name in cur:
cur.execute("INSERT INTO programs(id, channel)" + " VALUES(?, ?)", [display_name, 0])
con.commit()
cur.close
print 'Channels store into database are now successfully!'
I guess the problem must be somewhere in this code:
for elem in tv_elem.getchildren():
if elem.tag == 'channel':
channels[elem.attrib['id']] = self.load_channel(elem)
for channel_key in channels:
channel = channels[channel_key]
display_name = channel.get_display_name()
print display_name
profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
profilePath = profilePath + 'source.db'
con = database.connect(profilePath)
cur = con.cursor()
if not display_name in cur:
cur.execute("INSERT INTO programs(id, channel)" + " VALUES(?, ?)", [display_name, 0])
con.commit()
cur.close
print 'Channels store into database are now successfully!'[/code]
Here's the xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<tv generator-info-name="www.timefor.tv/xmltv">
<channel id="www.timefor.tv/tv/162">
<display-name lang="de">ARD DE DE</display-name>
<icon src="http://static.timefor.tv/imgs/epg/logos/ard_big.png" />
</channel>
<channel id="www.timefor.tv/tv/1">
<display-name lang="dk">DR1 DK DK</display-name>
<icon src="http://static.timefor.tv/imgs/epg/logos/dr1_dk_big.png" />
</channel>
<channel id="www.timefor.tv/tv/130">
<display-name lang="no">NRK1 NO NO</display-name>
<icon src="http://static.timefor.tv/imgs/epg/logos/nrk1_big.png" />
</channel>
<channel id="www.timefor.tv/tv/135">
<display-name lang="se">SVT 1 SE SE</display-name>
<icon src="http://static.timefor.tv/imgs/epg/logos/svt1_big.png" />
</channel>
<channel id="www.timefor.tv/tv/10769">
<display-name lang="en">BBC One UK EN</display-name>
<icon src="http://static.timefor.tv/imgs/epg/logos/bbcone_big.png" />
</channel>
<channel id="www.timefor.tv/tv/10214">
<display-name lang="en">National Geographic Channel UK EN</display-name>
<icon src="http://static.timefor.tv/imgs/epg/logos/nationalgeographicchannel_fr_big.png" />
</channel>
<channel id="www.timefor.tv/tv/10847">
<display-name lang="en">Discovery Channel UK EN</display-name>
<icon src="http://static.timefor.tv/imgs/epg/logos/discoverychannel_big.png" />
</channel></tv>
And here's the logs: http://xbmclogs.com/show.php?id=144672
I want to fetch one value from each tag without re-fetching.
Do you know how I can fetch one value from each tag without re-fetching the same values over and over?