def convertSubtitles(self, code=127, encoding=""):
        encoding = MapUcharEncoding(encoding)
        if 0 != code or 'unknown' in encoding:
            encoding = 'utf-8'
        else:
            encoding = encoding.strip()
        try:
            with codecs.open(self.downloader.getFullFileName(), 'r', encoding,
                             'replace') as fp:
                subText = fp.read().encode('utf-8').strip()

            ext = self.currItem.get('format', '')
            if ext == '':
                ext = self.currItem.get('url',
                                        '').split('?')[-1].split('.')[-1]
            filePath = '{0}_{1}_{2}'.format(self.params['movie_title'],
                                            self.currItem.get('title', ''),
                                            self.currItem.get('lang', ''))
            filePath = RemoveDisallowedFilenameChars(filePath)
            filePath += '.' + ext

            with open(GetSubtitlesDir(filePath), 'w') as fp:
                fp.write(subText)

            self.downloadedSubFilePath = GetSubtitlesDir(filePath)
            self.showButtons(['green'])
            tmpList = self.params.get('sub_list', [])
            if len(tmpList) == 1:
                self.acceptSub()
        except Exception:
            printExc()
            self["console"].setText(_('Subtitles conversion to UTF-8 failed.'))
Пример #2
0
 def converFileToUtf8(self, inFile, outFile, lang=''):
     printDBG('CBaseSubProviderClass.converFileToUtf8 inFile[%s] outFile[%s]' % (inFile, outFile))
     # detect encoding
     encoding = ''
     cmd = '%s "%s"' % (GetUchardetPath(), inFile)
     ret = self.iptv_execute(cmd)
     if ret['sts'] and 0 == ret['code']:
         encoding = MapUcharEncoding(ret['data'])
         if 0 != ret['code'] or 'unknown' in encoding:
             encoding = ''
         else: encoding = encoding.strip()
     
     if lang == '':
         lang = GetDefaultLang() 
     
     if lang == 'pl' and encoding == 'iso-8859-2':
         encoding = GetPolishSubEncoding(tmpFile)
     elif '' == encoding:
         encoding = 'utf-8'
         
     # convert file to UTF-8
     try:
         with open(inFile) as f:
             data = f.read()
         try:
             data = data.decode(encoding).encode('UTF-8')
             if self.writeFile(outFile, data):
                 return True
         except Exception:
             printExc()
             SetIPTVPlayerLastHostError(_('Failed to convert the file "%s" to UTF-8.') % inFile)
     except Exception:
         printExc()
         SetIPTVPlayerLastHostError(_('Failed to open the file "%s".') % inFile)
     return False
Пример #3
0
    def downloadSubtitleFile(self, cItem):
        printDBG("Napisy24plProvider.downloadSubtitleFile")
        retData = {}
        title = cItem['title']
        lang = cItem['lang']
        subId = cItem['sub_id']
        imdbid = cItem['imdbid']
        fileName = self._getFileName(title, lang, subId, imdbid)
        fileName = GetSubtitlesDir(fileName)

        url = 'http://napisy24.pl/download?napisId={0}&typ=sru'.format(subId)
        tmpFile = GetTmpDir(self.TMP_FILE_NAME)
        tmpFileZip = tmpFile + '.zip'

        urlParams = dict(self.defaultParams)
        urlParams['max_data_size'] = self.getMaxFileSize()

        sts, data = self.getPage(url, urlParams)
        if not sts:
            SetIPTVPlayerLastHostError(_('Failed to download subtitle.'))
            return retData

        try:
            with open(tmpFileZip, 'w') as f:
                f.write(data)
        except Exception:
            printExc()
            SetIPTVPlayerLastHostError(
                _('Failed to write file "%s".') % tmpFileZip)
            return retData

        printDBG(">>")
        printDBG(tmpFile)
        printDBG(tmpFileZip)
        printDBG(fileName)
        printDBG("<<")

        def __cleanFiles(all=False):
            if all: rm(fileName)
            rm(tmpFile)
            rm(tmpFileZip)

        cmd = "unzip -po '{0}' -x Napisy24.pl.url > '{1}' 2>/dev/null".format(
            tmpFileZip, tmpFile)
        ret = self.iptv_execute(cmd)
        if not ret['sts'] or 0 != ret['code']:
            __cleanFiles()
            message = _('Unzip error code[%s].') % ret['code']
            if str(ret['code']) == str(127):
                message += '\n' + _(
                    'It seems that unzip utility is not installed.')
            elif str(ret['code']) == str(9):
                message += '\n' + _('Wrong format of zip archive.')
            SetIPTVPlayerLastHostError(message)
            return retData

        # detect encoding
        cmd = '%s "%s"' % (config.plugins.iptvplayer.uchardetpath.value,
                           tmpFile)
        ret = self.iptv_execute(cmd)
        if ret['sts'] and 0 == ret['code']:
            encoding = MapUcharEncoding(ret['data'])
            if 0 != ret['code'] or 'unknown' in encoding:
                encoding = ''
            else:
                encoding = encoding.strip()
        else:
            encoding = ''

        if GetDefaultLang() == 'pl' and encoding == 'iso-8859-2':
            encoding = GetPolishSubEncoding(tmpFile)
        elif '' == encoding:
            encoding = 'utf-8'

        # convert file to UTF-8
        try:
            with open(tmpFile) as f:
                data = f.read()
            try:
                data = data.decode(encoding).encode('UTF-8')
                try:
                    with open(fileName, 'w') as f:
                        f.write(data)
                    retData = {
                        'title': title,
                        'path': fileName,
                        'lang': lang,
                        'imdbid': imdbid,
                        'sub_id': subId
                    }
                except Exception:
                    printExc()
                    SetIPTVPlayerLastHostError(
                        _('Failed to write the file "%s".') % fileName)
            except Exception:
                printExc()
                SetIPTVPlayerLastHostError(
                    _('Failed to convert the file "%s" to UTF-8.') % tmpFile)
        except Exception:
            printExc()
            SetIPTVPlayerLastHostError(
                _('Failed to open the file "%s".') % tmpFile)

        __cleanFiles()
        return retData
Пример #4
0
    def downloadSubtitleFile(self, cItem):
        printDBG("downloadSubtitleFile")
        retData = {}
        title = cItem['title']
        lang = cItem['lang']
        subId = cItem.get('sub_id', '0')
        imdbid = cItem['imdbid']
        fileName = self._getFileName(title, lang, subId, imdbid)
        fileName = GetSubtitlesDir(fileName)

        tmpFile = GetTmpDir(self.TMP_FILE_NAME)

        urlParams = dict(self.defaultParams)
        sts, data = self.cm.getPage(cItem['url'], urlParams)
        if not sts:
            SetIPTVPlayerLastHostError(_('Failed to page with subtitle link.'))
            return retData

        url = self.cm.ph.getSearchGroups(
            data, '''<meta[^>]+?refresh[^>]+?(https?://[^"^']+?)['"]''')[0]
        urlParams['max_data_size'] = self.getMaxFileSize()
        sts, data = self.cm.getPage(url, urlParams)
        if not sts:
            SetIPTVPlayerLastHostError(_('Failed to download subtitle.'))
            return retData

        try:
            with open(tmpFile, 'w') as f:
                f.write(data)
        except Exception:
            printExc()
            SetIPTVPlayerLastHostError(
                _('Failed to write file "%s".') % tmpFile)
            return retData

        printDBG(">>")
        printDBG(tmpFile)
        printDBG(fileName)
        printDBG("<<")

        def __cleanFiles(all=False):
            if all: rm(fileName)
            rm(tmpFile)

        # detect encoding
        cmd = '%s "%s"' % (config.plugins.iptvplayer.uchardetpath.value,
                           tmpFile)
        ret = self.iptv_execute(cmd)
        if ret['sts'] and 0 == ret['code']:
            encoding = MapUcharEncoding(ret['data'])
            if 0 != ret['code'] or 'unknown' in encoding:
                encoding = ''
            else:
                encoding = encoding.strip()
        else:
            encoding = ''

        # convert file to UTF-8
        try:
            with open(tmpFile) as f:
                data = f.read()
            try:
                data = data.decode(encoding).encode('UTF-8')
                try:
                    with open(fileName, 'w') as f:
                        f.write(data)
                    retData = {
                        'title': title,
                        'path': fileName,
                        'lang': lang,
                        'imdbid': imdbid,
                        'sub_id': subId
                    }
                except Exception:
                    printExc()
                    SetIPTVPlayerLastHostError(
                        _('Failed to write the file "%s".') % fileName)
            except Exception:
                printExc()
                SetIPTVPlayerLastHostError(
                    _('Failed to convert the file "%s" to UTF-8.') % tmpFile)
        except Exception:
            printExc()
            SetIPTVPlayerLastHostError(
                _('Failed to open the file "%s".') % tmpFile)

        __cleanFiles()
        return retData