def run(self): try: database, cursor = connectEpgDB() epg_db = EpgDb(database, cursor) kodisleep(getTimeSleep()) # Getting last update date. update_date = epg_db.getLastUpdateDate() if not doStartupUpdate() or epg_db.firstTimeRuning(): return current_time = dt.fromtimestamp(time()) update_time = strToDatetime(update_date) delta = current_time - update_time if delta.days < getUpdateFrequency(): return epg_xml = EpgXml(database, cursor, progress_bar=False) epg_xml.getXMLTV() epg_db.setUpdateDate() epg_xml.close() epg_db.close() del epg_xml del epg_db except Exception as e: log("%s %s" % (DEBUG_HEADER, e.message), LOGERROR)
def getCleanOld(self): programs = "SELECT id_program, end_date FROM programs" try: if self.cursor is None or self.database is None: return self.cursor.execute(programs) for program in self.cursor.fetchall(): program_end_date = program[1] current_time = datetime.now() program_time = strToDatetime(program_end_date) delta = current_time - program_time if delta.days >= getCleanupTreshold(): # Then delete old ones. request = "DELETE FROM programs WHERE id_program=%i" % program[ 0] self.cursor.execute(request) self.database.commit() except SqliteError as e: if DEBUG: notify(strings.CLEANUP_PROGRAMS_ERROR, e.message)
def getProgramStartDate(self, program_id): try: progr_infos = "SELECT start_date FROM programs WHERE id_program = %i" % program_id self.cursor.execute(progr_infos) start_date = self.cursor.fetchone()[0] return strToDatetime(start_date) except SqliteError: pass return None
def __getGridPortion(self): def __removeDuplicates(plist): rem = [] for i in range(len(plist)): for y in range(len(plist)): if y != i and int(plist[y]["start"]) == int( plist[i]["start"]): idx = i if int(plist[y]["end"]) > int( plist[i]["end"]) else y if not idx in rem: rem.append(idx) break remrt = [] for i in range(len(plist)): if not i in rem: remrt.append(plist[i]) return remrt grid = self.globalGrid[self.start_channel_id:self.start_channel_id + settings.getDisplayChannelsCount()] tbr = [] for channel in grid: programs = [] for program in __removeDuplicates(channel["programs"]): stop = strToDatetime(program["end"]) start = strToDatetime(program["start"]) if stop > self.start_time and start < self.stop_time: programs.append(program) tbr.append({ "db_id": channel["db_id"], "id_channel": channel["id_channel"], "display_name": channel["display_name"], "programs": programs, "logo": channel["logo"] }) return tbr
def displayChannels(self): self.reset(clear_grid=True) EPG_page = self.__getGridPortion() gridControls = [] idx = 0 for channel in EPG_page: y = self.top + self.cellHeight * idx + int((self.cellHeight / 14)) if not settings.useXMLTVSourceLogos(): pchannel = xbmcgui.ControlLabel( 16, y + 2, 180, self.cellHeight - 8, "[B]" + channel["display_name"] + "[/B]") else: logo = channel["logo"] if logo != "" and logo is not None: logo = join(settings.getChannelsLogoPath(), logo) if exists(logo): pchannel = xbmcgui.ControlImage(16, y + 2, 170, self.cellHeight - 8, logo, aspectRatio=2) else: pchannel = xbmcgui.ControlLabel( 16, y + 2, 180, self.cellHeight - 8, "[B]" + channel["display_name"] + "[/B]") else: pchannel = xbmcgui.ControlLabel( 16, y + 2, 180, self.cellHeight - 8, "[B]" + channel["display_name"] + "[/B]") gridControls.append(pchannel) self.labelControls.append(pchannel) # Program details. controls_x_grid = [] programs = channel["programs"] if len(programs) == 0: pbutton = xbmcgui.ControlButton( self.left, self.top + self.cellHeight * idx, self.right - self.left - 2, self.cellHeight - 2, PROGRAM_NO_INFOS, settings.getFocusTexture(), settings.getNoFocusTexture()) gridControls.append(pbutton) controls_x_grid.append({ "db_id": None, "desc": PROGRAM_NO_INFOS, "title": PROGRAM_NO_INFOS, "start": None, "stop": None, "control": pbutton, "cdisplay_name": channel["display_name"], "cdb_id": channel["db_id"], "logo": channel["logo"] }) for program in programs: program_start = strToDatetime(program["start"]) program_end = strToDatetime(program["end"]) deltaStart = program_start - self.start_time deltaStop = program_end - self.start_time y = self.top + self.cellHeight * idx x = self.secondsToX(deltaStart.seconds) if deltaStart.days < 0: x = self.left width = self.secondsToX(deltaStop.seconds) - x if x + width > self.right: width = self.right - x width -= 2 if width < 28: program["title"] = "" if not settings.useProgramsReminder(): focusTexture = settings.getFocusTexture() noFocusTexture = settings.getNoFocusTexture() else: # Checking if program is to be reminded. database, cursor = connectEpgDB() epgDb = EpgDb(database, cursor) remind = epgDb.hasReminder(int(program["db_id"])) epgDb.close() del database del cursor del epgDb if remind: focusTexture = settings.getReminderFocusTexture() noFocusTexture = settings.getReminderNoFocusTexture() else: focusTexture = settings.getFocusTexture() noFocusTexture = settings.getNoFocusTexture() pbutton = xbmcgui.ControlButton(x, y, width, self.cellHeight - 2, program["title"], noFocusTexture=noFocusTexture, focusTexture=focusTexture) gridControls.append(pbutton) controls_x_grid.append({ "db_id": program["db_id"], "desc": program["desc"], "title": program["title"], "start": program_start, "stop": program_end, "control": pbutton, "cdisplay_name": channel["display_name"], "cdb_id": channel["db_id"], "logo": channel["logo"] }) self.append(controls_x_grid) idx += 1 self.window.addControls(gridControls)
def __parseXMLTV(self): if not isfile(getEpgXmlFilePath()): return False self.progress_bar.create(strings.DIALOG_TITLE, strings.SFX_LONG_TIME_MSG) xmltv = minidom.parse(getEpgXmlFilePath()) channels = xmltv.getElementsByTagName('channel') programs = xmltv.getElementsByTagName('programme') plist = [] clist = [] icons_sources = [] i = 1 for channel in channels: self.progress_bar.update( int((i / float(channels.length)) * 100), "", strings.SFX_CHANNEL + ' %i/%i' % (i, channels.length)) id_channel = channel.getAttribute('id').encode('utf-8', 'ignore') display_name = channel.getElementsByTagName( 'display-name')[0].firstChild.data.encode('utf-8', 'ignore') display_name = display_name.replace(r'/', '-') display_name = display_name.replace("\\", "-") icon = "" if useXMLTVSourceLogos(): if channel.getElementsByTagName('icon').length > 0: icon = channel.getElementsByTagName( 'icon')[0].getAttribute('src').encode( 'utf-8', 'ignore') elif useTheTvDBSourceLogos(): search = TheLogoDbChannel(display_name) if search.search(): icon = search.getLogo() elif useTheTvDBSourceLogos(): search = TheLogoDbChannel(display_name) if search.search(): icon = search.getLogo() if not icon == "" and not icon is None: icons_sources.append(icon) icon = icon[icon.rfind(r"/") + 1:] if not self.epg_db.channelExists(id_channel): clist.append([id_channel, display_name, icon, '', '1']) i += 1 self.epg_db.addChannels(clist) self.epg_db.truncatePrograms() i = 1 for program in programs: self.progress_bar.update( int((i / float(programs.length)) * 100), "", strings.SFX_PROGRAM + ' %i/%i' % (i, programs.length)) id_channel = program.getAttribute('channel').encode( 'utf-8', 'ignore') start_date = program.getAttribute('start').encode( 'utf-8', 'ignore') if start_date.find(' +'): start_date = start_date[:start_date.find(" +")] end_date = program.getAttribute('stop').encode('utf-8', 'ignore') if end_date.find(' +'): end_date = end_date[:end_date.find(" +")] program_start = strToDatetime(start_date) program_end = strToDatetime(end_date) if useTimeZone(): delta = getTimeZoneDelta() operation = getTimeZoneOperation() if operation == AddonConst.TIMEZONE_ADD: program_start += delta program_end += delta else: program_start -= delta program_end -= delta ptitle = desc = "" if program.getElementsByTagName('title').length > 0: try: ptitle = program.getElementsByTagName( 'title')[0].firstChild.data ptitle = ptitle.encode('utf-8', 'ignore') except AttributeError: pass if program.getElementsByTagName('desc').length > 0: try: desc = program.getElementsByTagName( 'desc')[0].firstChild.data desc = desc.encode('utf-8', 'ignore') except AttributeError: pass if program_end + timedelta(days=1) > datetime.now(): plist.append([ id_channel, ptitle, program_start.strftime("%Y%m%d%H%M%S"), program_end.strftime("%Y%m%d%H%M%S"), desc ]) i += 1 self.epg_db.addPrograms(plist) self.progress_bar.close() if useXMLTVSourceLogos() or useTheTvDBSourceLogos(): self.downloadIcons(icons_sources) return True