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.'))
def downloadSubtitleFile(self, cItem): printDBG("SubsceneComProvider.downloadSubtitleFile") retData = {} title = cItem['title'] lang = cItem['lang'] subId = cItem['sub_id'] imdbid = cItem['imdbid'] inFilePath = cItem['file_path'] ext = cItem.get('ext', 'srt') fps = cItem.get('fps', 0) outFileName = self._getFileName(title, lang, subId, imdbid, fps, ext) outFileName = GetSubtitlesDir(outFileName) printDBG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") printDBG(inFilePath) printDBG(outFileName) printDBG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") if self.converFileToUtf8(inFilePath, outFileName, lang): retData = { 'title': title, 'path': outFileName, 'lang': lang, 'imdbid': imdbid, 'sub_id': subId, 'fps': fps } return retData
def downloadSubtitleFile(self, cItem): printDBG("OpenSubOrgProvider.downloadSubtitleFile") retData = {} title = cItem['title'] fileName = cItem['file_name'] url = cItem['url'] lang = cItem['lang'] encoding = cItem['encoding'] imdbid = cItem['imdbid'] urlParams = dict(self.defaultParams) urlParams['return_data'] = False try: fileSize = self.getMaxFileSize() sts, response = self.cm.getPage(url, urlParams) data = response.read(fileSize) response.close() except Exception: printExc() sts = False if not sts: SetIPTVPlayerLastHostError(_('Failed to download subtitle.')) return retData try: buf = StringIO(data) f = gzip.GzipFile(fileobj=buf) data = f.read() except Exception: printExc() SetIPTVPlayerLastHostError(_('Failed to gzip.')) return retData try: data = data.decode(encoding).encode('UTF-8') except Exception: printExc() SetIPTVPlayerLastHostError(_('Failed to decode to UTF-8.')) return retData fileName = GetSubtitlesDir(fileName) printDBG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") printDBG(fileName) printDBG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") try: with open(fileName, 'w') as f: f.write(data) retData = { 'title': title, 'path': fileName, 'lang': lang, 'imdbid': imdbid } except Exception: SetIPTVPlayerLastHostError( _('Failed to write file "%s".') % fileName) printExc() return retData
def downloadAndUnpack(self, url, params={}, post_data=None, unpackToSubDir=False): data, fileName = self.downloadFileData(url, params, post_data) if data == None: return None ext = fileName.rsplit('.', 1)[-1].lower() printDBG("fileName[%s] ext[%s]" % (fileName, ext)) if ext not in ['zip', 'rar']: SetIPTVPlayerLastHostError(_('Unknown file extension "%s".') % ext) return None tmpFile = GetTmpDir( self.TMP_FILE_NAME ) tmpArchFile = tmpFile + '.' + ext tmpDIR = '' if unpackToSubDir: dirName = fileName.rsplit('.', 1)[0].split('filename=', 1)[-1] if dirName != '': tmpDIR = GetSubtitlesDir(dirName) if tmpDIR == '': tmpDIR = GetTmpDir(self.TMP_DIR_NAME) printDBG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") printDBG(fileName) printDBG(tmpFile) printDBG(tmpArchFile) printDBG(tmpDIR) printDBG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") if not self.writeFile(tmpArchFile, data): return None if not self.unpackArchive(tmpArchFile, tmpDIR): rm(tmpArchFile) return None return tmpDIR
def downloadSubtitleFile(self, cItem): printDBG("YoutubeComProvider.downloadSubtitleFile") retData = {} title = cItem['title'] lang = cItem['lang'] subId = cItem.get('ytid', '0') fileName = self._getFileName(title, lang, subId, self.youtubeId) fileName = GetSubtitlesDir(fileName) urlParams = dict(self.defaultParams) urlParams['max_data_size'] = self.getMaxFileSize() sts, data = self.cm.getPage(cItem['url'], urlParams) if not sts: SetIPTVPlayerLastHostError(_('Failed to download subtitle.')) return retData try: with open(fileName, 'w') as f: f.write(data) except Exception: printExc() SetIPTVPlayerLastHostError(_('Failed to write file "%s".') % fileName) rm(fileName) return retData printDBG(">>") printDBG(fileName) printDBG("<<") retData = {'title': title, 'path': fileName, 'lang': lang, 'ytid': self.youtubeId, 'sub_id': subId} return retData
def doDownloadSubtitle(self, item): self.setListMode(False) self["console"].setText(_('Download subtitle "%s"') % item.name) self["console"].show() tmpDir = GetTmpDir() subDir = GetSubtitlesDir() self.subProvider.doDowanloadSubtitle(self.doDownloadSubtitleCallback, item.privateData, tmpDir, subDir)
def downloadSubtitleFile(self, cItem): printDBG("OpenSubOrgProvider.downloadSubtitleFile") retData = {} title = cItem['title'] fileName = cItem['file_name'] url = cItem['url'] lang = cItem['lang'] encoding = cItem['encoding'] imdbid = cItem['imdbid'] urlParams = dict(self.defaultParams) urlParams['max_data_size'] = self.getMaxFileSize() sts, data = self.cm.getPage(url, urlParams) if not sts: SetIPTVPlayerLastHostError(_('Failed to download subtitle.')) return retData try: data = DecodeGzipped(data) except Exception: printExc() SetIPTVPlayerLastHostError(_('Failed to gzip.')) return retData try: data = data.decode(encoding).encode('UTF-8') except Exception: printExc() SetIPTVPlayerLastHostError(_('Failed to decode to UTF-8.')) return retData fileName = GetSubtitlesDir(fileName) printDBG(">>") printDBG(fileName) printDBG("<<") try: with open(fileName, 'w') as f: f.write(data) retData = { 'title': title, 'path': fileName, 'lang': lang, 'imdbid': imdbid } except Exception: SetIPTVPlayerLastHostError( _('Failed to write file "%s".') % fileName) printExc() return retData
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
def _getCacheFileName(self, filePath): tmp = filePath.split('/')[-1] return GetSubtitlesDir(tmp + '.iptv')
def downloadSubtitleFile(self, cItem): printDBG("OpenSubtitlesRest.downloadSubtitleFile") retData = {} title = cItem['title'] fileName = cItem['file_name'] baseUrl = cItem['url'] lang = cItem['lang'] encoding = cItem['encoding'] imdbid = cItem['imdbid'] fps = cItem.get('fps', 0) urlParams = dict(self.defaultParams) urlParams['max_data_size'] = self.getMaxFileSize() login = config.plugins.iptvplayer.opensuborg_login.value password = config.plugins.iptvplayer.opensuborg_password.value loginUrl = 'http://api.opensubtitles.org/xml-rpc' loginData = '''<methodCall> <methodName>LogIn</methodName> <params> <param> <value><string>{0}</string></value> </param> <param> <value><string>{1}</string></value> </param> <param> <value><string>{2}</string></value> </param> <param> <value><string>{3}</string></value> </param> </params> </methodCall>''' if baseUrl.startswith('https://'): baseUrl = 'http://' + baseUrl.split('://', 1)[-1] url = baseUrl attempt = 0 while attempt < 3: attempt += 1 sts, data = self.cm.getPage(url, urlParams) if not sts: params = dict(self.defaultParams) params['raw_post_data'] = True post_data = loginData.format(login, hex_md5(password), 'en', self.USER_AGENT) sts2, data = self.cm.getPage(loginUrl, params, post_data) if sts2: data = self.cm.ph.getDataBeetwenMarkers(data, '<name>token</name>', '</string>', False)[1].rsplit('>', 1)[-1].strip() if data != '': url = baseUrl.replace('/filead/', '/sid-%s/filead/' % data) continue if login != '': login = '' password = '' continue break if not sts: SetIPTVPlayerLastHostError(_('Failed to download subtitle.')) return retData try: buf = StringIO(data) f = gzip.GzipFile(fileobj=buf) data = f.read() except Exception: printExc() SetIPTVPlayerLastHostError(_('Failed to gzip.')) return retData if encoding != '': try: data = data.decode(encoding).encode('UTF-8') except Exception: encoding = '' printExc() filePath = GetSubtitlesDir(fileName) if self.writeFile(filePath, data): if encoding != '': retData = {'title':title, 'path':filePath, 'lang':lang, 'imdbid':imdbid, 'fps':fps} elif self.converFileToUtf8(filePath, filePath, lang): retData = {'title':title, 'path':filePath, 'lang':lang, 'imdbid':imdbid, 'fps':fps} return retData
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
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