Advertisement
Guest User

Untitled

a guest
Jul 4th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.25 KB | None | 0 0
  1. import xbmc
  2. import xbmcgui
  3. import xbmcaddon
  4. import os
  5. import threading
  6. import datetime
  7. import time
  8. import urllib2
  9. import StringIO
  10. import sqlite3
  11. import threading
  12. from sqlite3 import dbapi2 as database
  13. from xml.etree import ElementTree
  14. import xml.etree.ElementTree as ET
  15. from UserDict import DictMixin
  16.  
  17. __killthread__ = False
  18.  
  19. #get actioncodes from keyboard.xml
  20. ACTION_ENTER = 7
  21. ACTION_BACKSPACE = 110
  22.  
  23. def cSetVisible(WiNdOw,iD,V=True): WiNdOw.getControl(iD).setVisible(V)
  24. ADDON = xbmcaddon.Addon(id = 'script.tvguide')
  25.  
  26. class OrderedDict(dict, DictMixin):
  27.  
  28.     def __init__(self, *args, **kwds):
  29.         if len(args) > 1:
  30.             raise TypeError('expected at most 1 arguments, got %d' % len(args))
  31.         try:
  32.             self.__end
  33.         except AttributeError:
  34.             self.clear()
  35.         self.update(*args, **kwds)
  36.  
  37.     def clear(self):
  38.         self.__end = end = []
  39.         end += [None, end, end]         # sentinel node for doubly linked list
  40.         self.__map = {}                 # key --> [key, prev, next]
  41.         dict.clear(self)
  42.  
  43.     def __setitem__(self, key, value):
  44.         if key not in self:
  45.             end = self.__end
  46.             curr = end[1]
  47.             curr[2] = end[1] = self.__map[key] = [key, curr, end]
  48.         dict.__setitem__(self, key, value)
  49.  
  50.     def __delitem__(self, key):
  51.         dict.__delitem__(self, key)
  52.         key, prev, next = self.__map.pop(key)
  53.         prev[2] = next
  54.         next[1] = prev
  55.  
  56.     def __iter__(self):
  57.         end = self.__end
  58.         curr = end[2]
  59.         while curr is not end:
  60.             yield curr[0]
  61.             curr = curr[2]
  62.  
  63.     def __reversed__(self):
  64.         end = self.__end
  65.         curr = end[1]
  66.         while curr is not end:
  67.             yield curr[0]
  68.             curr = curr[1]
  69.  
  70.     def popitem(self, last=True):
  71.         if not self:
  72.             raise KeyError('dictionary is empty')
  73.         if last:
  74.             key = reversed(self).next()
  75.         else:
  76.             key = iter(self).next()
  77.         value = self.pop(key)
  78.         return key, value
  79.  
  80.     def __reduce__(self):
  81.         items = [[k, self[k]] for k in self]
  82.         tmp = self.__map, self.__end
  83.         del self.__map, self.__end
  84.         inst_dict = vars(self).copy()
  85.         self.__map, self.__end = tmp
  86.         if inst_dict:
  87.             return (self.__class__, (items,), inst_dict)
  88.         return self.__class__, (items,)
  89.  
  90.     def keys(self):
  91.         return list(self)
  92.  
  93.     setdefault = DictMixin.setdefault
  94.     update = DictMixin.update
  95.     pop = DictMixin.pop
  96.     values = DictMixin.values
  97.     items = DictMixin.items
  98.     iterkeys = DictMixin.iterkeys
  99.     itervalues = DictMixin.itervalues
  100.     iteritems = DictMixin.iteritems
  101.  
  102.     def __repr__(self):
  103.         if not self:
  104.             return '%s()' % (self.__class__.__name__,)
  105.         return '%s(%r)' % (self.__class__.__name__, self.items())
  106.  
  107.     def copy(self):
  108.         return self.__class__(self)
  109.  
  110.     @classmethod
  111.     def fromkeys(cls, iterable, value=None):
  112.         d = cls()
  113.         for key in iterable:
  114.             d[key] = value
  115.         return d
  116.  
  117.     def __eq__(self, other):
  118.         if isinstance(other, OrderedDict):
  119.             if len(self) != len(other):
  120.                 return False
  121.             for p, q in  zip(self.items(), other.items()):
  122.                 if p != q:
  123.                     return False
  124.             return True
  125.         return dict.__eq__(self, other)
  126.  
  127.     def __ne__(self, other):
  128.         return not self == other
  129.  
  130.  
  131.  
  132. class MyClass(xbmcgui.WindowXML):
  133.  
  134.  
  135.     def onInit(self):
  136.         pass
  137.  
  138.     def timer1_8percent(self):
  139.          for i in range(1):
  140.              time.sleep(1)
  141.              self.getControl(4202).setLabel("8%")
  142.  
  143.  
  144.      def timer1_12percent(self):
  145.          for i in range(1):
  146.              time.sleep(2)
  147.              self.getControl(4202).setLabel("12%")
  148.  
  149.  
  150.      def timer1_18percent(self):
  151.          for i in range(1):
  152.              time.sleep(3)
  153.              self.getControl(4202).setLabel("18%")
  154.  
  155.  
  156.      def timer1_24percent(self):
  157.          for i in range(1):
  158.              time.sleep(4)
  159.              self.getControl(4202).setLabel("24%")
  160.  
  161.  
  162.      def timer1_28percent(self):
  163.          for i in range(1):
  164.              time.sleep(5)
  165.              self.getControl(4202).setLabel("28%")
  166.  
  167.  
  168.      def timer1_32percent(self):
  169.          for i in range(1):
  170.              time.sleep(6)
  171.              self.getControl(4202).setLabel("32%")
  172.  
  173.  
  174.      def timer1_38percent(self):
  175.          for i in range(1):
  176.              time.sleep(7)
  177.              self.getControl(4202).setLabel("38%")
  178.  
  179.  
  180.      def timer1_42percent(self):
  181.          for i in range(1):
  182.              time.sleep(8)
  183.              self.getControl(4202).setLabel("42%")
  184.  
  185.  
  186.      def timer1_48percent(self):
  187.          for i in range(1):
  188.              time.sleep(9)
  189.              self.getControl(4202).setLabel("48%")
  190.  
  191.  
  192.      def timer1_56percent(self):
  193.          for i in range(1):
  194.              time.sleep(10)
  195.              self.getControl(4202).setLabel("56%")
  196.  
  197.  
  198.      def timer1_64percent(self):
  199.          for i in range(1):
  200.              time.sleep(11)
  201.              self.getControl(4202).setLabel("64%")
  202.  
  203.  
  204.      def timer1_72percent(self):
  205.          for i in range(1):
  206.              time.sleep(12)
  207.              self.getControl(4202).setLabel("72%")
  208.  
  209.  
  210.      def timer1_78percent(self):
  211.          for i in range(1):
  212.              time.sleep(13)
  213.              self.getControl(4202).setLabel("78%")
  214.  
  215.  
  216.      def timer1_86percent(self):
  217.          for i in range(1):
  218.              time.sleep(14)
  219.              self.getControl(4202).setLabel("86%")
  220.  
  221.  
  222.      def timer1_92percent(self):
  223.          for i in range(1):
  224.              time.sleep(15)
  225.              self.getControl(4202).setLabel("92%")
  226.  
  227.  
  228.      def timer1_96percent(self):
  229.          for i in range(1):
  230.              time.sleep(16)
  231.              self.getControl(4202).setLabel("96%")
  232.  
  233.  
  234.      def timer1_98percent(self):
  235.          for i in range(1):
  236.              time.sleep(17)
  237.              self.getControl(4202).setLabel("98%")
  238.  
  239.  
  240.      def timer1_100percent(self):
  241.          for i in range(1):
  242.              time.sleep(18)
  243.              self.getControl(4202).setLabel("100%")
  244.  
  245.  
  246.     def allchannels_timer(self):
  247.          while __killthread__ is False:
  248.              time.sleep(0.3)
  249.              self.getControl(4202).setLabel("0%")
  250.              #DOWNLOAD THE XML SOURCE HERE
  251.              url = ADDON.getSetting('allchannel.url')
  252.              req = urllib2.Request(url)
  253.              response = urllib2.urlopen(req)
  254.              data = response.read()
  255.              response.close()
  256.              profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
  257.              self.getControl(4202).setLabel("1%")
  258.              self.thread = threading.Thread(target=self.timer1_8percent)
  259.              self.thread.setDaemon(True)
  260.              self.thread.start()
  261.              self.thread = threading.Thread(target=self.timer1_12percent)
  262.              self.thread.setDaemon(True)
  263.              self.thread.start()
  264.              self.thread = threading.Thread(target=self.timer1_18percent)
  265.              self.thread.setDaemon(True)
  266.              self.thread.start()
  267.              self.thread = threading.Thread(target=self.timer1_24percent)
  268.              self.thread.setDaemon(True)
  269.              self.thread.start()
  270.              self.thread = threading.Thread(target=self.timer1_28percent)
  271.              self.thread.setDaemon(True)
  272.              self.thread.start()
  273.              self.thread = threading.Thread(target=self.timer1_32percent)
  274.              self.thread.setDaemon(True)
  275.              self.thread.start()
  276.  
  277.  
  278.              if os.path.exists(profilePath):
  279.                  profilePath = profilePath + 'source.db'
  280.                  con = database.connect(profilePath)
  281.                  cur = con.cursor()
  282.                  cur.execute('CREATE TABLE programs(channel TEXT, title TEXT, start_date TIMESTAMP, stop_date TIMESTAMP, description TEXT)')
  283.                  con.commit()
  284.                  con.close
  285.                  tv_elem = ElementTree.parse(StringIO.StringIO(data)).getroot()
  286.                  profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
  287.                  profilePath = profilePath + 'source.db'
  288.                  con = sqlite3.connect(profilePath)
  289.                  cur = con.cursor()
  290.                  channels = OrderedDict()
  291.                  self.thread = threading.Thread(target=self.timer1_38percent)
  292.                  self.thread.setDaemon(True)
  293.                  self.thread.start()
  294.                  self.thread = threading.Thread(target=self.timer1_42percent)
  295.                  self.thread.setDaemon(True)
  296.                  self.thread.start()
  297.                  self.thread = threading.Thread(target=self.timer1_48percent)
  298.                  self.thread.setDaemon(True)
  299.                  self.thread.start()
  300.                  self.thread = threading.Thread(target=self.timer1_56percent)
  301.                  self.thread.setDaemon(True)
  302.                  self.thread.start()
  303.                  self.thread = threading.Thread(target=self.timer1_64percent)
  304.                  self.thread.setDaemon(True)
  305.                  self.thread.start()
  306.                  self.thread = threading.Thread(target=self.timer1_72percent)
  307.                  self.thread.setDaemon(True)
  308.                  self.thread.start()
  309.                  self.thread = threading.Thread(target=self.timer1_78percent)
  310.                  self.thread.setDaemon(True)
  311.                  self.thread.start()
  312.                  self.thread = threading.Thread(target=self.timer1_86percent)
  313.                  self.thread.setDaemon(True)
  314.                  self.thread.start()
  315.                  self.thread = threading.Thread(target=self.timer1_92percent)
  316.                  self.thread.setDaemon(True)
  317.                  self.thread.start()
  318.                  self.thread = threading.Thread(target=self.timer1_96percent)
  319.                  self.thread.setDaemon(True)
  320.                  self.thread.start()
  321.                  self.thread = threading.Thread(target=self.timer1_98percent)
  322.                  self.thread.setDaemon(True)
  323.                  self.thread.start()
  324.                  self.thread = threading.Thread(target=self.timer1_100percent)
  325.                  self.thread.setDaemon(True)
  326.                  self.thread.start()
  327.  
  328.  
  329.                  # Get the loaded data
  330.                  for channel in tv_elem.findall('channel'):
  331.                      channel_name = channel.find('display-name').text
  332.                      for program in channel.findall('programme'):
  333.                          title = program.find('title').text
  334.                          start_time = program.get("start")
  335.                          stop_time = program.get("stop")
  336.                          cur.execute("INSERT INTO programs(channel, title, start_date, stop_date)" + " VALUES(?, ?, ?, ?)", [channel_name, title, start_time, stop_time])
  337.                          con.commit()
  338.                          con.close
  339.  
  340.                  print 'Channels store into database are now successfully!'
  341.                  program = None
  342.                  now = datetime.datetime.now()
  343.                  #strCh = '(\'' + '\',\''.join(channelMap.keys()) + '\')'
  344.                  cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel')
  345.                  getprogram_info = cur.fetchall()
  346.  
  347.                  for row in getprogram_info:
  348.                      programming = row[0], row[1], row[2], row[3]
  349.                      print programming
  350.                      #print row[0], row[1], row[2], row[3]
  351.                      #programming = row[0], row[1], row[2], row[3]
  352.                      #programming = row[0], row[1], row[2], row[3]
  353.                      #cur.close()
  354.  
  355.  
  356. def onAction(self, action):
  357.  
  358.     if action == ACTION_BACKSPACE:
  359.        __killthread__ = True
  360.        self.thread.join(0.0)
  361.        profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
  362.        database_source = profilePath + 'source.db'
  363.        write_source = profilePath + 'source.db-journal'
  364.        os.remove(write_source)
  365.        os.remove(database_source)
  366.  
  367.  
  368. if action == ACTION_ENTER:
  369.     self.thread = threading.Thread(target=self.allchannels_timer)
  370.     self.thread.setDaemon(True)
  371.     self.thread.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement