示例#1
0
    def loadSubtitles(self, filePath, encoding='utf-8', fps=0):
        printDBG("OpenSubOrg.loadSubtitles filePath[%s]" % filePath)
        # try load subtitles using C-library
        try:
            if IsSubtitlesParserExtensionCanBeUsed():
                try:
                    if fps <= 0:
                        filename, file_extension = os_path.splitext(filePath)
                        tmp = CParsingHelper.getSearchGroups(
                            filename.upper() + '_', '_FPS([0-9.]+)_')[0]
                        if '' != tmp: fps = float(tmp)
                except Exception:
                    printExc()

                from Plugins.Extensions.IPTVPlayer.libs.iptvsubparser import _subparser as subparser
                with codecs.open(filePath, 'r', encoding, 'replace') as fp:
                    subText = fp.read().encode('utf-8')
                # if in subtitles will be line {1}{1}f_fps
                # for example {1}{1}23.976 and we set microsecperframe = 0
                # then microsecperframe will be calculated as follow: llroundf(1000000.f / f_fps)
                if fps > 0:
                    microsecperframe = int(1000000.0 / fps)
                else:
                    microsecperframe = 0
                # calc end time if needed - optional, default True
                setEndTime = True
                # characters per second - optional, default 12, can not be set to 0
                CPS = 12
                # words per minute - optional, default 138, can not be set to 0
                WPM = 138
                # remove format tags, like <i> - optional, default True
                removeTags = True
                subsObj = subparser.parse(subText, microsecperframe,
                                          removeTags, setEndTime, CPS, WPM)
                if 'type' in subsObj:
                    self.subAtoms = subsObj['list']
                    # Workaround start
                    try:
                        printDBG(
                            'Workaround for subtitles from Das Erste: %s' %
                            self.subAtoms[0]['start'])
                        if len(self.subAtoms
                               ) and self.subAtoms[0]['start'] >= 36000000:
                            for idx in range(len(self.subAtoms)):
                                for key in ['start', 'end']:
                                    if key not in self.subAtoms[idx]:
                                        continue
                                    if self.subAtoms[idx][key] >= 36000000:
                                        self.subAtoms[idx][key] -= 36000000
                    except Exception:
                        printExc()
                    # workaround end
                    self._fillPailsOfAtoms()
                    return True
                else:
                    return False
        except Exception:
            printExc()
        return self._loadSubtitles(filePath, encoding)
    def downloadSubtitleFile(self, cItem):
        printDBG("NapiProjektProvider.downloadSubtitleFile")
        retData = {}
        title = cItem['title']
        lang = cItem.get('lang', 'pl')
        subId = cItem['sub_id']
        imdbid = cItem['imdbid']
        fps = cItem.get('fps', 0)

        post_data = {
            "mode": "32770",
            "client": "pynapi",
            "client_ver": "0.1",
            "VideoFileInfoID": subId
        }

        url = self.getFullUrl('api/api-napiprojekt3.php')
        sts, data = self.cm.getPage(url, self.defaultParams, post_data)
        if not sts: return retData

        fps = self.cm.ph.getDataBeetwenMarkers(data, '<fps>', '</fps>',
                                               False)[1]
        try:
            fps = float(fps.strip())
        except Exception:
            fps = 0

        post_data = {
            "downloaded_subtitles_id": subId,
            "mode": "1",
            "client": "pynapi",
            "client_ver": "0.1",
            "downloaded_subtitles_lang": lang.upper(),
            "downloaded_subtitles_txt": "1"
        }

        url = self.getFullUrl('api/api-napiprojekt3.php')
        sts, data = self.cm.getPage(url, self.defaultParams, post_data)
        if not sts: return retData

        data = self.cm.ph.getDataBeetwenMarkers(data, '<content><![CDATA[',
                                                ']]></content>', False)[1]
        try:
            data = base64.b64decode(data)
            if IsSubtitlesParserExtensionCanBeUsed():
                from Plugins.Extensions.IPTVPlayer.libs.iptvsubparser import _subparser as subparser
                subsObj = subparser.parse(data, 0, False, False)
                typeExtMap = {
                    'microdvd': 'sub',
                    'subrip': 'srt',
                    'subviewer': 'sub',
                    'ssa1': 'ssa',
                    'ssa2-4': 'ssa',
                    'ass': 'ssa',
                    'vplayer': 'txt',
                    'sami': 'smi',
                    'mpl2': 'mpl',
                    'aqt': 'aqt',
                    'pjs': 'pjs',
                    'mpsub': 'sub',
                    'jacosub': 'jss',
                    'psb': 'psb',
                    'realtext': 'rt',
                    'dks': 'dks',
                    'subviewer1': 'sub',
                    'text/vtt': 'vtt',
                    'sbv': 'sbv'
                }
                ext = typeExtMap.get(subsObj['type'], '')
                if ext == '':
                    SetIPTVPlayerLastHostError(
                        _('Unknown subtitle parser for format "%s".') %
                        subsObj['type'])
                    return retData
                tmpFile = GetTmpDir(self.TMP_FILE_NAME)
                if not self.writeFile(tmpFile, data):
                    return retData
                fileName = self._getFileName(title, lang, subId, imdbid, fps,
                                             ext)
                fileName = GetSubtitlesDir(fileName)
                if not self.converFileToUtf8(tmpFile, fileName):
                    rm(tmpFile)
                    return retData
                retData = {
                    'title': title,
                    'path': fileName,
                    'lang': lang,
                    'imdbid': imdbid,
                    'sub_id': subId
                }
        except Exception:
            printExc()
            return retData

        return retData