Ejemplo n.º 1
0
    def render(self, req):
        req.setHeader('Content-type', 'text/html')
        req.setHeader('charset', 'UTF-8')
        """ rendering server response """
        extraMeta = '<meta http-equiv="refresh" content="5">'
        key = None
        arg = None
        arg2 = None
        arg3 = None
        DMlist = []
        if len(req.args.keys()) >= 1:
            key = req.args.keys()[0]
            arg = req.args.get(key, None)[0]
            try:
                arg2 = req.args.get(key, None)[1]
            except Exception:
                pass
            try:
                arg3 = req.args.get(key, None)[2]
            except Exception:
                pass
            print 'Received: "%s"="%s","%s","%s"' % (key, arg, arg2, arg3)

        if key is None or arg is None:
            if None != Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                DMlist = Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.getList(
                )
        elif key == 'cmd' and arg == 'initDM':
            if None == Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                printDBG(
                    '============WebSite.py Initialize Download Manager============'
                )
                Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager = IPTVDMApi(
                    2,
                    int(config.plugins.iptvplayer.IPTVDMMaxDownloadItem.value))
                DMlist = Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.getList(
                )
        elif key == 'cmd' and arg == 'runDM':
            if None != Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.runWorkThread(
                )
                DMlist = Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.getList(
                )
        elif key == 'cmd' and arg == 'stopDM':
            if None != Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.stopWorkThread(
                )
                DMlist = Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.getList(
                )
                extraMeta = '<meta http-equiv="refresh" content="10">'
        elif key == 'cmd' and arg == 'downloadsDM':
            if None != Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                DMlist = Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.getList(
                )
        elif key == 'watchMovie' and os.path.exists(arg):
            return util.redirectTo(
                "/file?action=download&file=%s" %
                urllib.quote(arg.decode('utf8', 'ignore').encode('utf-8')),
                req)
        elif key == 'stopDownload' and arg.isdigit():
            if None != Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.stopDownloadItem(
                    int(arg))
                DMlist = Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.getList(
                )
        elif key == 'downloadAgain' and arg.isdigit():
            if None != Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.continueDownloadItem(
                    int(arg))
                DMlist = Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.getList(
                )
        elif key == 'removeMovie' and arg.isdigit():
            if None != Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.removeDownloadItem(
                    int(arg))
                DMlist = Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.getList(
                )

        elif key == 'cmd' and arg == 'arvchiveDM':
            if arg2 == 'deleteMovie' and os.path.exists(arg3):
                os.remove(arg3)
            elif arg2 == 'watchMovie' and os.path.exists(arg3):
                return util.redirectTo(
                    "/file?action=download&file=%s" % urllib.quote(
                        arg3.decode('utf8', 'ignore').encode('utf-8')), req)
            if os.path.exists(
                    config.plugins.iptvplayer.NaszaSciezka.value
            ) and None != Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                files = os.listdir(
                    config.plugins.iptvplayer.NaszaSciezka.value)
                files.sort(key=lambda x: x.lower())
                for item in files:
                    if item.startswith('.'):
                        continue  # do not list hidden items
                    if item[-4:].lower() not in ['.flv', '.mp4']: continue
                    fileName = os.path.join(
                        config.plugins.iptvplayer.NaszaSciezka.value, item)
                    skip = False
                    for item2 in Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.getList(
                    ):
                        if fileName == item2.fileName.replace('//', '/'):
                            skip = True
                            break
                    if skip: continue
                    listItem = DMItemBase(url=fileName, fileName=fileName)
                    try:
                        listItem.downloadedSize = os.path.getsize(fileName)
                    except Exception:
                        listItem.downloadedSize = 0
                    listItem.status = DMHelper.STS.DOWNLOADED
                    listItem.downloadIdx = -1
                    DMlist.append(listItem)
                if len(DMlist) == 0:
                    listItem = DMItemBase(
                        _('Nothing has been downloaded yet.'), '')
                    listItem.status = 'INFO'
                    DMlist.append(listItem)

        if len(DMlist) == 0 and arg != 'arvchiveDM':
            listItem = DMItemBase(
                _('No materials waiting in the downloader queue'), '')
            listItem.status = 'INFO'
            DMlist.append(listItem)
            extraMeta = ''
        elif len(DMlist) == 0 and arg in ['arvchiveDM', 'stopDM']:
            extraMeta = ''

        reloadScripts()
        html = '<html lang="%s">' % language.getLanguage()[:2]
        html += webParts.IncludeHEADER(extraMeta)
        html += webParts.Body().downloaderPageContent(
            Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.
            gDownloadManager, DMlist)
        return html
Ejemplo n.º 2
0
    def run(self):
        print("doUseHostAction received: '%s'='%s'" %
              (self.key, str(self.arg)))
        if self.key == 'activeHost' and isActiveHostInitiated() == False:
            initActiveHost(self.arg)
        elif self.key == 'activeHost' and self.arg != settings.activeHost[
                'Name']:
            initActiveHost(self.arg)
        elif self.key == 'cmd' and self.arg == 'RefreshList':
            settings.retObj = settings.activeHost['Obj'].getCurrentList()
            settings.activeHost['ListType'] = 'ListForItem'
            settings.currItem = {}
        elif self.key == 'DownloadURL' and self.arg.isdigit():
            myID = int(self.arg)
            url = settings.retObj.value[myID].url
            if url != '' and IsUrlDownloadable(url):
                titleOfMovie = settings.currItem['itemTitle'].replace(
                    '/', '-').replace(':', '-').replace('*', '-').replace(
                        '?', '-').replace('"', '-').replace('<', '-').replace(
                            '>', '-').replace('|', '-')
                fullFilePath = config.plugins.iptvplayer.NaszaSciezka.value + '/' + titleOfMovie + '.mp4'
                if None == Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager:
                    printDBG(
                        '============webThreads.py Initialize Download Manager============'
                    )
                    Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager = IPTVDMApi(
                        2,
                        int(config.plugins.iptvplayer.IPTVDMMaxDownloadItem.
                            value))
                ret = Plugins.Extensions.IPTVPlayer.components.iptvplayerwidget.gDownloadManager.addToDQueue(
                    DMItem(url, fullFilePath))
                #print(ret)
        elif self.key == 'ResolveURL' and self.arg.isdigit():
            myID = int(self.arg)
            url = "NOVALIDURLS"
            linkList = []
            ret = settings.activeHost['Obj'].getResolvedURL(
                settings.retObj.value[myID].url)
            if ret.status == RetHost.OK and isinstance(ret.value, list):
                for item in ret.value:
                    if isinstance(item, CUrlItem):
                        item.urlNeedsResolve = 0  # protection from recursion
                        linkList.append(item)
                    elif isinstance(item, basestring):
                        linkList.append(CUrlItem(item, item, 0))
                    else:
                        print(
                            "selectResolvedVideoLinks: wrong resolved url type!"
                        )
                settings.retObj = RetHost(RetHost.OK, value=linkList)
            else:
                print("selectResolvedVideoLinks: wrong status or value")

        elif self.key == 'ListForItem' and self.arg.isdigit():
            myID = int(self.arg)
            settings.activeHost['selectedItemType'] = settings.retObj.value[
                myID].type
            if settings.activeHost['selectedItemType'] in ['CATEGORY']:
                settings.activeHost[
                    'Status'] += '>' + settings.retObj.value[myID].name
                settings.currItem = {}
                settings.retObj = settings.activeHost['Obj'].getListForItem(
                    myID, 0, settings.retObj.value[myID])
                settings.activeHost['PathLevel'] += 1
            elif settings.activeHost['selectedItemType'] in ['VIDEO']:
                settings.currItem['itemTitle'] = settings.retObj.value[
                    myID].name
                try:
                    links = settings.retObj.value[myID].urlItems
                except Exception as e:
                    print("ListForItem>urlItems exception:", str(e))
                    links = 'NOVALIDURLS'
                try:
                    settings.retObj = settings.activeHost[
                        'Obj'].getLinksForVideo(
                            myID, settings.retObj.value[myID]
                        )  #returns "NOT_IMPLEMENTED" when host is using curlitem
                except Exception as e:
                    print("ListForItem>getLinksForVideo exception:", str(e))
                    settings.retObj = RetHost(RetHost.NOT_IMPLEMENTED,
                                              value=[])

                if settings.retObj.status == RetHost.NOT_IMPLEMENTED and links != 'NOVALIDURLS':
                    print("getLinksForVideo not implemented, using CUrlItem")
                    tempUrls = []
                    iindex = 1
                    for link in links:
                        if link.name == '':
                            tempUrls.append(
                                CUrlItem('link %d' % iindex, link.url,
                                         link.urlNeedsResolve))
                        else:
                            tempUrls.append(
                                CUrlItem(link.name, link.url,
                                         link.urlNeedsResolve))
                        iindex += 1
                    settings.retObj = RetHost(RetHost.OK, value=tempUrls)
                elif settings.retObj.status == RetHost.NOT_IMPLEMENTED:
                    settings.retObj = RetHost(RetHost.NOT_IMPLEMENTED,
                                              value=[(CUrlItem(
                                                  "No valid urls", "fakeUrl",
                                                  0))])
        elif self.key == 'ForSearch' and None is not self.arg and self.arg != '':
            settings.retObj = settings.activeHost['Obj'].getSearchResults(
                self.arg, self.searchType)
        elif self.key == 'activeHostSearchHistory' and self.arg != '':
            initActiveHost(self.arg)
            settings.retObj = settings.activeHost['Obj'].getSearchResults(
                settings.GlobalSearchQuery, '')