4 Replies - 920 Views - Last Post: 09 March 2014 - 03:54 PM Rate Topic: -----

#1 mark103   User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 107
  • Joined: 13-April 08

problem with loops

Posted 07 March 2014 - 12:04 PM

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!'




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?

Is This A Good Question/Topic? 0
  • +

Replies To: problem with loops

#2 mark103   User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 107
  • Joined: 13-April 08

Re: problem with loops

Posted 08 March 2014 - 12:26 PM

24 hours and no one is reply, does anyone know?????
Was This Post Helpful? 0
  • +
  • -

#3 MentalFloss   User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: problem with loops

Posted 08 March 2014 - 04:18 PM

Quote

24 hours and no one is reply, does anyone know?????


Your problem is very specialized and requires a lot of dependencies. You might be waiting a very long time. What has changed in 24 hours? Have you made any adjustments at all or did you expect to just put everything on hold while you wait for a solution?

EDIT:

Are you aware that your indentation is bad at line 306?

def onAction(self, action):

if action == ACTION_ENTER:
    #OPEN THE XML SOURCE



It also gets weird at line 327:

       for elem in tv_elem.getchildren():
        if elem.tag == 'channel':
           channels[elem.attrib['id']] = self.load_channel(elem)



Furthermore, what exactly is this about? (line 240):

class MyClass(xbmcgui.WindowXML):

     def __new__(cls):
         return super(MyClass, cls).__new__(cls, 'script-tvguide-mainmenu.xml', ADDON.getAddonInfo('path'))



I can't answer these questions for myself because I cannot run this just yet, but I am looking at it.

EDIT:

OK. Line 262 is inside load_channel() and is never called. What's that about?:

     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


This post has been edited by MentalFloss: 08 March 2014 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 mark103   User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 107
  • Joined: 13-April 08

Re: problem with loops

Posted 09 March 2014 - 02:05 PM

Sorry for the confusion, what I'm trying to do is to dict the channels everytime when an item is inserted.

However, I'm using this code:

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




Here is what it print it out:

20:58:02 T:6548  NOTICE: BBC One UK EN
20:58:02 T:6548  NOTICE: SVT 1 SE SE
20:58:02 T:6548  NOTICE: National Geographic Channel UK EN
20:58:02 T:6548  NOTICE: NRK1 NO NO
20:58:02 T:6548  NOTICE: Discovery Channel UK EN
20:58:02 T:6548  NOTICE: ARD DE DE
20:58:02 T:6548  NOTICE: DR1 DK DK


Here is 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>



I want to print them in the same alphabet order as the XML file. Can you please help?

Thanks!!!

This post has been edited by mark103: 09 March 2014 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 MentalFloss   User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: problem with loops

Posted 09 March 2014 - 03:54 PM

Well, with a dictionary, there's no concept of order so it doesn't know that you want to output it the same way it was added in. However, there is the http://docs.python.o...ons.OrderedDict which will remember. Try switching your data over to using that instead. It should give you what you want.

EDIT:
Apparently named URLs don't work. Oh well...

This post has been edited by MentalFloss: 09 March 2014 - 03:55 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1