Skip to content

Commit

Permalink
Fix update mechanism
Browse files Browse the repository at this point in the history
which was not working on all boxes
  • Loading branch information
betacentauri committed Nov 3, 2018
1 parent 6e01751 commit 6d1b09b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
73 changes: 73 additions & 0 deletions plugin/downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## PLi Downloader.py

from twisted.web import client
from twisted.internet import reactor, defer
from urlparse import urlparse

class TelekomSportHTTPProgressDownloader(client.HTTPDownloader):
def __init__(self, url, outfile, headers=None):
client.HTTPDownloader.__init__(self, url, outfile, headers=headers, agent="Enigma2 HbbTV/1.1.1 (+PVR+RTSP+DL;OpenPLi;;;)")
self.status = self.progress_callback = self.error_callback = self.end_callback = None
self.deferred = defer.Deferred()

def noPage(self, reason):
if self.status == "304":
client.HTTPDownloader.page(self, "")
else:
client.HTTPDownloader.noPage(self, reason)
if self.error_callback:
self.error_callback(reason.getErrorMessage(), self.status)

def gotHeaders(self, headers):
if self.status == "200":
if "content-length" in headers:
self.totalbytes = int(headers["content-length"][0])
else:
self.totalbytes = 0
self.currentbytes = 0.0
return client.HTTPDownloader.gotHeaders(self, headers)

def pagePart(self, packet):
if self.status == "200":
self.currentbytes += len(packet)
if self.totalbytes and self.progress_callback:
self.progress_callback(self.currentbytes, self.totalbytes)
return client.HTTPDownloader.pagePart(self, packet)

def pageEnd(self):
ret = client.HTTPDownloader.pageEnd(self)
if self.end_callback:
self.end_callback()
return ret

class TelekomSportDownloadWithProgress:
def __init__(self, url, outputfile, contextFactory=None, *args, **kwargs):
parsed = urlparse(url)
scheme = parsed.scheme
host = parsed.hostname
port = parsed.port or (443 if scheme == 'https' else 80)
self.factory = TelekomSportHTTPProgressDownloader(url, outputfile, *args, **kwargs)
if scheme == 'https':
from twisted.internet import ssl
if contextFactory is None:
contextFactory = ssl.ClientContextFactory()
self.connection = reactor.connectSSL(host, port, self.factory, contextFactory)
else:
self.connection = reactor.connectTCP(host, port, self.factory)

def start(self):
return self.factory.deferred

def stop(self):
if self.connection:
self.factory.progress_callback = self.factory.end_callback = self.factory.error_callback = None
self.connection.disconnect()

def addProgress(self, progress_callback):
self.factory.progress_callback = progress_callback

def addEnd(self, end_callback):
self.factory.end_callback = end_callback

def addError(self, error_callback):
self.factory.error_callback = error_callback
34 changes: 24 additions & 10 deletions plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from Components.ConfigList import ConfigListScreen
from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigText, ConfigPassword, ConfigInteger, ConfigNothing, ConfigYesNo, ConfigSelection, NoSave
from Tools.BoundFunction import boundFunction
from downloader import TelekomSportDownloadWithProgress

from enigma import eTimer, eListboxPythonMultiContent, gFont, eEnv, eServiceReference, getDesktop, eConsoleAppContainer

Expand Down Expand Up @@ -1044,7 +1045,7 @@ def showTableResults(self):

class TelekomSportMainScreen(Screen):

version = 'v2.0.2'
version = 'v2.0.3'

base_url = 'https://www.telekomsport.de/api/v2/mobile'
main_page = '/navigation'
Expand Down Expand Up @@ -1114,6 +1115,7 @@ def __init__(self, session, args = None):

self.updateUrl = ''
self.updateText = ''
self.filename = ''

self['title'] = Label('')
self['subtitle'] = Label('')
Expand Down Expand Up @@ -1200,11 +1202,13 @@ def checkForUpdate(self):
self.updateText = rel['body'].encode('utf8')
for asset in rel['assets']:
if telekomsport_isDreamOS and asset['name'].endswith('.deb'):
self.updateUrl = asset['browser_download_url']
self.updateUrl = asset['browser_download_url'].encode('utf8')
self.filename = '/tmp/enigma2-plugin-extensions-telekomsport.deb'
self['buttongreen'].show()
break
elif (not telekomsport_isDreamOS) and asset['name'].endswith('.ipk'):
self.updateUrl = asset['browser_download_url']
self.updateUrl = asset['browser_download_url'].encode('utf8')
self.filename = '/tmp/enigma2-plugin-extensions-telekomsport.ipk'
self['buttongreen'].show()
break
if self.version >= rel['tag_name'] or self.updateUrl != '':
Expand All @@ -1219,14 +1223,24 @@ def update(self):

def updateConfirmed(self, answer):
if answer:
self.container = eConsoleAppContainer()
self.container.appClosed.append(self.finishedUpdate)
if telekomsport_isDreamOS:
self.container.execute('wget -O /tmp/%s %s; dpkg -i /tmp/%s' % ('enigma2-plugin-extensions-telekomsport.deb', self.updateUrl, 'enigma2-plugin-extensions-telekomsport.deb'))
else:
self.container.execute('opkg update; opkg install ' + self.updateUrl)
self.downloader = TelekomSportDownloadWithProgress(self.updateUrl, self.filename)
self.downloader.addError(self.updateFailed)
self.downloader.addEnd(self.downloadFinished)
self.downloader.start()

def downloadFinished(self):
self.downloader.stop()
self.container = eConsoleAppContainer()
self.container.appClosed.append(self.updateFinished)
if telekomsport_isDreamOS:
self.container.execute('dpkg -i ' + self.filename)
else:
self.container.execute('opkg update; opkg install ' + self.filename)

def updateFailed(self, reason, status):
self.updateFinished(1)

def finishedUpdate(self, retval):
def updateFinished(self, retval):
self['buttongreen'].hide()
self.updateUrl = ''
if retval == 0:
Expand Down

0 comments on commit 6d1b09b

Please sign in to comment.