def nextImport(self):
self.closeReader()
if not self.sources:
self.closeImport()
return
self.source = self.sources.pop()
print("[EPGImport] nextImport, source=", self.source.description, file=log)
self.fetchUrl(self.source.url)
def fetchUrl(self, filename):
if filename.startswith('http:') or filename.startswith('https:') or filename.startswith('ftp:'):
self.do_download(filename, self.afterDownload, self.downloadFail)
else:
self.afterDownload(None, filename, deleteFile=False)
def do_download(self, sourcefile, afterDownload, downloadFail):
path = bigStorage(9000000, '/tmp', '/media/cf', '/media/mmc', '/media/usb', '/media/hdd')
filename = os.path.join(path, 'epgimport')
ext = os.path.splitext(sourcefile)[1]
# Keep sensible extension, in particular the compression type
if ext and len(ext) < 6:
filename += ext
sourcefile = sourcefile.encode('utf-8')
sslcf = SNIFactory(sourcefile) if sourcefile.decode().startswith('https:') else None
print("[EPGImport] Downloading: " + sourcefile.decode() + " to local path: " + filename, file=log)
if self.source.nocheck == 1:
print("[EPGImport] Not cheching the server since nocheck is set for it: " + sourcefile.decode(), file=log)
downloadPage(sourcefile, filename, timeout=90, contextFactory=sslcf).addCallbacks(afterDownload, downloadFail, callbackArgs=(filename, True))
return filename
else:
if self.checkValidServer(sourcefile) == 1:
downloadPage(sourcefile, filename, timeout=90, contextFactory=sslcf).addCallbacks(afterDownload, downloadFail, callbackArgs=(filename, True))
return filename
else:
self.downloadFail("checkValidServer reject the server")
def afterDownload(self, result, filename, deleteFile=False):
print("[EPGImport] afterDownload", filename, file=log)
if not os.path.exists(filename):
self.downloadFail("File not exists")
return
try:
if not os.path.getsize(filename):
raise Exception("File is empty")
except Exception as e:
self.downloadFail(e)
return
if self.source.parser == 'epg.dat':
if twisted.python.runtime.platform.supportsThreads():
print("[EPGImport] Using twisted thread for DAT file", file=log)
threads.deferToThread(self.readEpgDatFile, filename, deleteFile).addCallback(lambda ignore: self.nextImport())
else:
self.readEpgDatFile(filename, deleteFile)
return
if filename.endswith('.gz'):
self.fd = gzip.open(filename, 'rb')
try:
# read a bit to make sure it's a gzip file
self.fd.read(10)
self.fd.seek(0, 0)
except Exception as e:
print("[EPGImport] File downloaded is not a valid gzip file", filename, file=log)
self.downloadFail(e)
return
elif filename.endswith('.xz') or filename.endswith('.lzma'):
try:
import lzma
except ImportError:
from backports import lzma
self.fd = lzma.open(filename, 'rb')
try:
# read a bit to make sure it's an xz file
self.fd.read(10)
self.fd.seek(0, 0)
except Exception as e:
print("[EPGImport] File downloaded is not a valid xz file", filename, file=log)
self.downloadFail(e)
return
else:
self.fd = open(filename, 'rb')
if deleteFile and self.source.parser != 'epg.dat':
try:
print("[EPGImport] unlink", filename, file=log)
os.unlink(filename)
except Exception as e:
print("[EPGImport] warning: Could not remove '%s' intermediate" % filename, e, file=log)
self.channelFiles = self.source.channels.downloadables()
if not self.channelFiles:
self.afterChannelDownload(None, None)
else:
filename = random.choice(self.channelFiles)
self.channelFiles.remove(filename)
self.do_download(filename, self.afterChannelDownload, self.channelDownloadFail)
def afterChannelDownload(self, result, filename, deleteFile=True):
print("[EPGImport] afterChannelDownload", filename, file=log)
if filename:
try:
if not os.path.getsize(filename):
raise Exception("File is empty")
except Exception as e:
self.channelDownloadFail(e)
return
if twisted.python.runtime.platform.supportsThreads():
print("[EPGImport] Using twisted thread", file=log)
threads.deferToThread(self.doThreadRead, filename).addCallback(lambda ignore: self.nextImport())
deleteFile = False # Thread will delete it
else:
self.iterator = self.createIterator(filename)
reactor.addReader(self)
if deleteFile and filename:
try:
os.unlink(filename)
except Exception as e:
print("[EPGImport] warning: Could not remove '%s' intermediate" % filename, e, file=log)
def afterDownload(self, result, filename, deleteFile=False):
result What is this?
Edited by Dimitrij, Today, 12:15.