123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- import os
- from os import path
-
- from enigma import eTimer, getDesktop
- from Components.ActionMap import ActionMap
- from Components.FileList import FileList
- from Components.Sources.List import List
- from Components.Sources.StaticText import StaticText
- from Components.Task import Task, Job, job_manager
- from Screens.Screen import Screen
- from Tools.Directories import fileExists
-
- from content.Downloader import DownloadWithProgress, DownloadWithProgressFragmented,get_header
-
- def timer_callback(timer,callback):
- if "callback" in dir(timer):
- timer.callback.append(callback)
- else:
- timer_conn = timer.timeout.connect(callback)
-
- ###########################################################################################
- class DownloadJob(Job):
- def __init__(self, url, outputfile, title, downloadStop,headers=None,stream_type="http"):
- Job.__init__(self, title)
- DownloadTask(self, url, outputfile, title, downloadStop,headers,stream_type)
-
-
- class DownloadTask(Task):
- def __init__(self, job, url, outputfile, title, downloadStop,headers={},stream_type="http"):
- Task.__init__(self, job, _('Downloading'))
- self.job = job
- self.title = title
- self.url = url
- self.outputfile = outputfile
- self.headers = headers
- self.stream_type = stream_type
- self.downloadStop = downloadStop
- self.job.currentbytes = 0
- self.job.totalbytes = -1
-
- def run(self, callback):
- self.callback = callback
- if self.stream_type == "hls":
- self.download = DownloadWithProgressFragmented(self.url, self.outputfile,self.headers)
- else:
- self.download = DownloadWithProgress(self.url, self.outputfile, self.headers)
- self.download.addProgress(self.downloadProgress)
- self.download.start().addCallback(self.downloadFinished).addErrback(self.downloadFailed)
-
- def downloadProgress(self, currentbytes, totalbytes):
- self.job.currentbytes = currentbytes
- self.job.totalbytes = totalbytes
- progress = self.job.currentbytes/float(self.job.totalbytes) * 100
- self.setProgress(progress)
-
- def downloadFinished(self, result):
- Task.processFinished(self, 0)
- self.setProgress(self.end)
- self.downloadStop("Success - " + self.title)
-
- def downloadFailed(self, failure_instance=None, error_message=''):
- print '[PlayStream] Video download failed'
- if error_message == '' and failure_instance is not None:
- error_message = failure_instance.getErrorMessage()
- print '[PlayStream]', str(error_message)
- Task.processFinished(self, 1)
- self.downloadStop("Failed - "+ self.title)
-
- class HLSDownloadJob(Job):
- def __init__(self, url, destfile, title,downloadStop,headers=None):
- Job.__init__(self, title)
- AddHLSProcessTask(self, url, destfile, title, downloadStop,headers)
-
-
- ######################################################################################################
- class AddHLSProcessTask(Task):
- def __init__(self, job, url, destfile, title,downloadStop,headers=None):
- Task.__init__(self, job, title)
- self.job = job
- self.title = title
- cmdline = '/usr/bin/gst-launch-1.0 "%s" ! hlsdemux ! filesink location="%s"'%(url,destfile)
- self.setCmdline(cmdline)
- self.url = url
- self.destfile = destfile
- self.downloadStop = downloadStop
- self.job.currentbytes = 0
- self.job.totalbytes = 0
- #self.setProgress(100)
- self.ProgressTimer = eTimer()
- timer_callback(self.ProgressTimer, self.ProgressUpdate)
-
- def ProgressUpdate(self):
- if not fileExists(self.destfile, 'r'):
- return
- self.job.currentbytes = path.getsize(self.destfile)
- progress = self.job.currentbytes/float(self.job.totalbytes) * 100 if self.job.totalbytes else 0
- self.setProgress(progress)
- #self.setProgress(int((path.getsize(self.destfile)/float(self.totalbytes))*100))
- self.ProgressTimer.start(5000, True)
-
- def prepare(self):
- self.job.totalbytes = -1 # TODO getsize(self.url)
- self.ProgressTimer.start(5000, True)
-
- def afterRun(self):
- #self.setProgress(100)
- self.ProgressTimer.stop()
- self.downloadStop(self.title)
-
-
- class VideoDownloadList(Screen):
- screenWidth = getDesktop(0).size().width()
- if screenWidth and screenWidth == 1920:
- skin = """<screen position="center,center" size="945,555">
- <widget source="list" render="Listbox" position="center,45" size="900,405" \
- scrollbarMode="showOnDemand" >
- <convert type="TemplatedMultiContent" >
- {"template": [
- MultiContentEntryText(pos=(15,1), size=(465,33), \
- font=0, flags=RT_HALIGN_LEFT, text=1), # Title
- MultiContentEntryText(pos=(345,1), size=(225,33), \
- font=0, flags=RT_HALIGN_RIGHT, text=2), # State
- MultiContentEntryProgress(pos=(585,6), size=(150,33), \
- percent=-3), # Progress
- MultiContentEntryText(pos=(750,1), size=(120,33), \
- font=0, flags=RT_HALIGN_LEFT, text=4), # Percentage
- ],
- "fonts": [gFont("Regular",30)],
- "itemHeight": 45}
- </convert>
- </widget>
- <ePixmap position="center,484" size="210,60" pixmap="skin_default/buttons/red.png" \
- transparent="1" alphatest="on" />
- <widget source="key_red" render="Label" position="center,485" zPosition="2" \
- size="210,60" valign="center" halign="center" font="Regular;33" transparent="1" />
- </screen>"""
- else:
- skin = """<screen position="center,center" size="630,370">
- <widget source="list" render="Listbox" position="center,30" size="600,270" \
- scrollbarMode="showOnDemand" >
- <convert type="TemplatedMultiContent" >
- {"template": [
- MultiContentEntryText(pos=(10,1), size=(210,22), \
- font=0, flags=RT_HALIGN_LEFT, text=1), # Title
- MultiContentEntryText(pos=(230,1), size=(150,22), \
- font=0, flags=RT_HALIGN_RIGHT, text=2), # State
- MultiContentEntryProgress(pos=(390,4), size=(100,22), \
- percent=-3), # Progress
- MultiContentEntryText(pos=(500,1), size=(80,22), \
- font=0, flags=RT_HALIGN_LEFT, text=4), # Percentage
- ],
- "fonts": [gFont("Regular",20)],
- "itemHeight": 30}
- </convert>
- </widget>
- <ePixmap position="center,323" size="140,40" pixmap="skin_default/buttons/red.png" \
- transparent="1" alphatest="on" />
- <widget source="key_red" render="Label" position="center,328" zPosition="2" \
- size="140,30" valign="center" halign="center" font="Regular;22" transparent="1" />
- </screen>"""
-
- def __init__(self, session):
- Screen.__init__(self, session)
- self['key_red'] = StaticText(_('Exit'))
- self['list'] = List([])
- self['actions'] = ActionMap(['SetupActions', 'ColorActions'],
- {
- 'cancel': self.close,
- 'ok': self.ok,
- 'red': self.close,
- 'blue': self.abort
- }, -2)
- self.onLayoutFinish.append(self.layoutFinished)
- self.onClose.append(self.cleanVariables)
- self.progressTimer = eTimer()
- timer_callback(self.progressTimer,self.updateDownloadList)
-
- def layoutFinished(self):
- self.setTitle(_('Active video downloads'))
- self.updateDownloadList()
-
- def cleanVariables(self):
- del self.progressTimer
-
- def updateDownloadList(self):
- self.progressTimer.stop()
- downloadList = []
- for job in job_manager.getPendingJobs():
- progress = job.progress / float(job.end) * 100
- currentbytes = job.currentbytes if "currentbytes" in dir(job) else 0
- downloadList.append((job, job.name, job.getStatustext(),
- int(progress), "%.1fM"%(currentbytes/1024.0/1024.0) ))
- self['list'].updateList(downloadList)
- if downloadList:
- self.progressTimer.startLongTimer(2)
-
- def ok(self):
- current = self['list'].getCurrent()
- if current:
- from Screens.TaskView import JobView
- self.session.open(JobView, current[0])
-
- def abort(self):
- current = self['list'].getCurrent()
- job = current[0]
- if job.status == job.NOT_STARTED:
- job_manager.active_jobs.remove(job)
- self.close(False)
- elif job.status == job.IN_PROGRESS:
- job.cancel()
- else:
- self.close(False)
-
-
- class VideoDirBrowser(Screen):
- def __init__(self, session, downloadDir):
- Screen.__init__(self, session)
- self.skinName = ['VideoDirBrowser', 'FileBrowser']
- self['key_red'] = StaticText(_('Cancel'))
- self['key_green'] = StaticText(_('Use'))
- if not os.path.exists(downloadDir):
- downloadDir = '/'
- self.filelist = FileList(downloadDir, showFiles = False)
- self['filelist'] = self.filelist
- self['FilelistActions'] = ActionMap(['SetupActions', 'ColorActions'],
- {
- 'cancel': self.cancel,
- 'red': self.cancel,
- 'ok': self.ok,
- 'green': self.use
- }, -2)
- self.onLayoutFinish.append(self.layoutFinished)
-
- def layoutFinished(self):
- self.setTitle(_('Please select the download directory'))
-
- def ok(self):
- if self.filelist.canDescent():
- self.filelist.descent()
-
- def use(self):
- currentDir = self['filelist'].getCurrentDirectory()
- dirName = self['filelist'].getFilename()
- if currentDir is None or \
- (self.filelist.canDescent() and dirName and len(dirName) > len(currentDir)):
- self.close(dirName)
-
- def cancel(self):
- self.close(False)
|