Thread Rating:
  • 0 Vote(s) - 0 Average
How to refresh data on xbmcgui.WindowDialog?
#1
Hi,

I'm working on a addon. I want see current foreign exchange rates on top corner of the screen. I've brought the addon up to a point. But I cannot refresh the data automatically.

How to refresh data every 15 seconds on xbmcgui.WindowDialog?

Please help me.

Code:
# -*- coding: utf-8 -*-
import xbmc, xbmcplugin, xbmcaddon, xbmcgui
import re
import urllib2
from datetime import datetime
import time, locale, pytz
import platform,os
import json, threading

locale.setlocale(locale.LC_TIME,'')
'tr_TR.UTF-8'

sysarg=str(sys.argv[1])
ADDON_ID='script.kralex.foreign'
addon = xbmcaddon.Addon(id=ADDON_ID)


def cek(link):
   req = urllib2.Request(link)
   req.add_header('User-Agent',
                  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36')
   yanit = urllib2.urlopen(req)
   sonuc = json.load(yanit)
   yanit.close()

   return sonuc

def convert_datetime_timezone(dt, tz1, tz2):
   tz1 = pytz.timezone(tz1)
   tz2 = pytz.timezone(tz2)
   try:
       dt = datetime.strptime(dt,"%Y-%m-%dT%H:%M:%SZ")
   except:
       dt = datetime.fromtimestamp(time.mktime(time.strptime(dt, "%Y-%m-%dT%H:%M:%SZ")))
   dt = tz1.localize(dt)
   dt = dt.astimezone(tz2)
   dt = dt.strftime("%c")
   return dt

url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDTRY%22,%20%22EURTRY%22)&env=store://datatables.org/alltableswithkeys&format=json'


class MyAddon(xbmcgui.WindowDialog):
   def __init__(self):
       veri = cek(url)
       tarih = veri['query']['created']
       tarih = convert_datetime_timezone(tarih, "UTC", "Europe/Istanbul")

       dolar = []
       euro = []

       for x in veri['query']['results']['rate']:
           if x['id'] == 'USDTRY':
               dolar = x['Rate']
           if x['id'] == 'EURTRY':
               euro = x['Rate']

       self.addControl(xbmcgui.ControlImage(800,20, 500, 100, ""))
       self.list = xbmcgui.ControlList(810, 20, 500, 110, 'font12', '0xFFFFFFFF')
       self.addControl(self.list)

       self.list.addItem('Dolar:               ' + dolar +' TL\nGuncelleme:    ' + tarih)

       self.setFocus(self.list)
       xbmc.executebuiltin('Dialog.Close(10138)')

if __name__ == '__main__':
   myaddon = MyAddon()
   myaddon.doModal()
   del myaddon
Download addon
Reply
#2
You need to run a secondary thread from which you can update your Controls with new data.
Raspberry PI 2 + LibreELEC 8 (Kodi 17)
Samsung Galaxy Tab A 10.1 + Kodi 17 for Android
Reply



How to refresh data on xbmcgui.WindowDialog?00