/build/kartinatv/src/api/jtvepg.py

https://github.com/technic/iptvdream · Python · 95 lines · 71 code · 12 blank · 12 comment · 20 complexity · 562f7b3ad5bdc4712dfa3bf89d5b023e MD5 · raw file

  1. # Dreambox Enigma2 KartinaTV/RodnoeTV player! (by technic)
  2. #
  3. # Copyright (c) 2010 Alex Maystrenko <alexeytech@gmail.com>
  4. # web: http://techhost.dlinkddns.com/
  5. #
  6. # This is free software; you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free
  8. # Software Foundation; either version 2, or (at your option) any later
  9. # version.
  10. from datetime import datetime
  11. import urllib
  12. from . import tdSec, secTd, setSyncTime, syncTime, EpgEntry, Channel, APIException, jtvreader
  13. import os
  14. EPG_ZIP = '/tmp/jtv.zip'
  15. EPG_DIR = EPG_ZIP[:-4]+'/'
  16. urlnew = "http://www.teleguide.info/download/new3/jtv.zip"
  17. urlold = "http://www.teleguide.info/download/old/jtv.zip"
  18. deltat = 4*60*60 #Moscow time
  19. class JTVEpg:
  20. def __init__(self):
  21. self.act_url = urlnew
  22. self.lastload = syncTime()
  23. def getPiconName(self, cid):
  24. return "%s_%s" % (self.iName, self.channels[cid].name)
  25. def getFname(self, cid):
  26. f = self.channels[cid].epg_name
  27. try:
  28. if isinstance(f, unicode):
  29. f = f.decode('utf-8')
  30. f = f.encode('CP866')
  31. f = EPG_DIR + f
  32. except (UnicodeDecodeError, UnicodeEncodeError):
  33. pass
  34. return f
  35. def getChannelsEpg(self, cids):
  36. map(self.getCurrentEpg, cids)
  37. def getCurrentEpg(self, cid):
  38. fname = self.getFname(cid)
  39. try:
  40. jtv = jtvreader.current(fname, deltat)
  41. except IOError as e:
  42. if e[0] == 2:
  43. # self.trace('epg fail for %s (possible encoding problem)' % self.channels[cid].epg_name)
  44. return -1
  45. else:
  46. raise(e)
  47. lepg = [EpgEntry(x[1].encode('utf-8'), datetime.fromtimestamp(x[0]-deltat), None) for x in jtv]
  48. # print jtv
  49. if datetime.fromtimestamp(jtv[0][0]-deltat) > syncTime() and self.act_url == urlnew:
  50. self.act_url = urlold
  51. self.load_epg()
  52. self.channels[cid].pushEpgSorted(lepg)
  53. def check_epgdir(self):
  54. if not os.path.isdir(EPG_DIR):
  55. self.load_epg()
  56. def load_epg(self):
  57. try:
  58. os.mkdir(EPG_DIR)
  59. except OSError as e:
  60. if e[0] != 17:
  61. raise(e)
  62. self.trace("Loading epg %s" % self.act_url)
  63. try:
  64. urllib.urlretrieve(self.act_url, EPG_ZIP)
  65. self.lastload = syncTime()
  66. except:
  67. raise APIException("epg download failed")
  68. cmd = "unzip -q -o %s -d %s" % (EPG_ZIP, EPG_DIR)
  69. self.trace(cmd)
  70. os.system(cmd)
  71. def getDayEpg(self, cid, date):
  72. fname = self.getFname(cid)
  73. self.trace("epg for cid %s" % cid)
  74. try:
  75. jtv = jtvreader.read(fname)
  76. except IOError as e:
  77. if e[0] == 2:
  78. # self.trace('epg fail for %s (possible encoding problem)' % self.channels[cid].epg_name)
  79. return -1
  80. else:
  81. raise(e)
  82. lepg = [EpgEntry(x[1].encode('utf-8'), datetime.fromtimestamp(x[0]-deltat), None) for x in jtv]
  83. self.channels[cid].pushEpgSorted(lepg)