1

I'm working on my python script for xbmc media application to send the request to url to get the response so I can store the data in a sqlite3 database.

I want to to set up the calculate progress from 0% to 100% for the label control to see how far I'm going when I send the request to url to get the response and then to store the data in a database.

Here is the control id that i want to use:

main_loading_time_left = 4202

Here is the code:

#DOWNLOAD THE XML SOURCE HERE
url = ADDON.getSetting('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(channel TEXT, title TEXT, start_date TIMESTAMP, stop_date TIMESTAMP, description TEXT)')
   con.commit()
   con.close
   tv_elem = ElementTree.parse(StringIO.StringIO(data)).getroot()
   profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
   profilePath = profilePath + 'source.db'
   con = sqlite3.connect(profilePath)
   cur = con.cursor()
   channels = OrderedDict()

   # Get the loaded data
   for channel in tv_elem.findall('channel'):
       channel_name = channel.find('display-name').text
       for program in channel.findall('programme'):
           title = program.find('title').text
           start_time = program.get("start")
           stop_time = program.get("stop")
           cur.execute("INSERT INTO programs(channel, title, start_date, stop_date)" + " VALUES(?, ?, ?, ?)", [channel_name, title, start_time, stop_time])
           con.commit()

Does anyone know how I can use the control id main_loading_time_left to set up the calculate progress that start from 0% to 100% when I send the request to the url to get the response and then store the data into the database. Does anyone know how?

1 Answers1

0

To display progress, you should have 2 points: start, current and end. So, 0, current processed data and total data.

But you have 2 expensive operations here: http query fetch and export to sqlite.

To track progress of http query, I'd suggest to look at these questions and answers: this and this.

To track sql query progress, you should just take length of tv_elem.findall('channel') as end.

elements = tv_elem.findall('channel')
total = len(elements)
for current, channel in enumerate(elements):
    ...
    setProgressBar(main_loading_time_left, float(current)/total*100)
Community
  • 1
  • 1
dt0xff
  • 1,553
  • 1
  • 10
  • 18