def getXMLTV(self): if self.epg_db.isDBInitOk(): if isfile(getEpgXmlFilePath()): remove(getEpgXmlFilePath()) if getXMLTVSourceType() == AddonConst.XMLTV_SOURCE_LOCAL: self.__getLocalXmltv(getXMLTVURLLocal()) elif getXMLTVSourceType() == AddonConst.XMLTV_SOURCE_URL: self.__getUrlXmltv(getXMLTVURLRemote()) else: notify(strings.XMLTV_LOAD_ERROR) return return self.__parseXMLTV()
def __getLocalXmltv(self, local_file): if not isfile(local_file): notify(strings.XMLTV_FILE_NOT_FOUND) else: if not isXMLTVCompressed() and not local_file.endswith(".xml"): notify(strings.BAD_XMLTV_FILE_TYPE) elif not isXMLTVCompressed(): # Moving to userdata if not getXMLTVSourceType() == AddonConst.XMLTV_SOURCE_URL: copyfile(local_file, getEpgXmlFilePath()) else: # Uncompress and moving to userdata self.__uncompressAndMove(local_file)
def __getUrlXmltv(self, url_file_source): import ssl ssl._create_default_https_context = ssl._create_unverified_context dest_file = getEpgXmlFilePath() try: self.progress_bar.create(strings.SFX_EPG_UPDATE_HEADER, strings.SFX_DOWNLOADING_MSG) download = urlopen(url_file_source, timeout=30) if isfile(dest_file): remove(dest_file) tsf = open(dest_file, "w") tsf.write(download.read()) tsf.close() del tsf self.progress_bar.close() self.__getLocalXmltv(dest_file) except HTTPError as e: if e.code == 304: notify(strings.HTTP_UNCHANGED_REMOTE_FILE, e.reason) elif e.code == 301: notify(strings.HTTP_MOVED_PERMANENTLY, e.reason) elif e.code == 400: notify(strings.HTTP_BAD_REQUEST, e.reason) elif e.code in [401, 403]: notify(strings.HTTP_UNAUTHORIZED, e.reason) elif e.code == 404: notify(strings.HTTP_NOT_FOUND, e.reason) elif e.code == 500: notify(strings.HTTP_INTERNAL_SERVER_ERROR, e.reason) elif e.code == 502: notify(strings.HTTP_BAD_GATEWAY, e.reason) elif e.code == 503: notify(strings.HTTP_SERVER_OVERLOADED, e.reason) elif e.code == 504: notify(strings.HTTP_REQUEST_TIMEOUT, e.reason) else: notify(strings.HTTP_UNHANDLED_ERROR, e.reason) self.progress_bar.close() return
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
if len(paths) <= 0: notify(strings.ARCHIVE_NO_XMLTV_FOUND) rmtree(dest) return else: zfile = None for ptest in paths: xmltest = minidom.parse(ptest) channels = True if xmltest.getElementsByTagName( 'channel').length > 0 else False programs = True if xmltest.getElementsByTagName( 'programme').length > 0 else False if channels and programs: rename(ptest, getEpgXmlFilePath()) rmtree(dest) break else: notify(strings.ARCHIVE_UNSUPPORTED_FORMAT) ''' Parse the xmltv file and return the result. ''' def __parseXMLTV(self): if not isfile(getEpgXmlFilePath()): return False self.progress_bar.create(strings.DIALOG_TITLE,