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
    def _doGetEncodingSubtitle24Callback(self, code, data):
        encoding = data
        if 0 != code or 'unknown' in encoding:
            encoding = ''
        else:
            encoding = encoding.strip()
        
        if GetDefaultLang() == 'pl' and encoding == 'iso-8859-2':
            encoding = GetPolishSubEncoding(self.tmpData['tmpFile'])
        elif '' == encoding:
            encoding = 'utf-8'
        fileName = ''
        sts = False
        try:
            f = open(self.tmpData['tmpFile'])
            data = f.read()
            f.close()
            subItem = self.tmpData['subItem']
            try:
                data = data.decode(encoding).encode('UTF-8')
                title = self._getSubtitleNapisy24Title(subItem).replace('_', '.').replace('.srt', '').replace(' ', '.')
                match = re.search(r'[^.]', title)
                if match: title = title[match.start():]

                fileName = "{0}_{1}_0_{2}_{3}".format(title, 'pl', subItem['id'][2:], subItem['imdb'])
                fileName = self.tmpData['subDir'] + fileName + '.srt'
                try:
                    with open(fileName, 'w') as f:
                        f.write(data)
                    sts = True
                except: 
                    self.lastApiError = {'code':-999, 'message':_('write error')}
                    printExc()
            except:
                self.lastApiError = {'code':-999, 'message':_('decode error')}
                printExc()  
        except:
            self.lastApiError = {'code':-999, 'message':_('read error')}
            printExc()
        self.outerCallback(sts, fileName)
Exemple #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