def test_get_compressed_filetype(self): zipfile = os.path.join(UTILS_PATH, 'zipfile') expected_filetype = "zip" filetype = utilities.getCompressedFileType(zipfile) self.assertIsNotNone(filetype, "cannot detect filetype!") self.assertEqual(filetype, expected_filetype, "detected invalid filetype - %s"%filetype) rarfile = os.path.join(UTILS_PATH, 'rarfile') expected_filetype = "rar" filetype = utilities.getCompressedFileType(rarfile) self.assertIsNotNone(filetype, "cannot detect filetype!") self.assertEqual(filetype, expected_filetype, "detected invalid filetype - %s"%filetype)
def test_get_compressed_filetype(self): zipfile = os.path.join(UTILS_PATH, 'zipfile') expected_filetype = "zip" filetype = utilities.getCompressedFileType(zipfile) self.assertIsNotNone(filetype, "cannot detect filetype!") self.assertEqual(filetype, expected_filetype, "detected invalid filetype - %s" % filetype) rarfile = os.path.join(UTILS_PATH, 'rarfile') expected_filetype = "rar" filetype = utilities.getCompressedFileType(rarfile) self.assertIsNotNone(filetype, "cannot detect filetype!") self.assertEqual(filetype, expected_filetype, "detected invalid filetype - %s" % filetype)
def _unpack_subtitles(self, filepath, dest_dir, max_recursion=3): compressed = getCompressedFileType(filepath) if compressed == 'zip': self.log.debug('found "zip" archive, unpacking...') subfiles = self._unpack_zipsub(filepath, dest_dir) elif compressed == 'rar': self.log.debug('found "rar" archive, unpacking...') subfiles = self._unpack_rarsub(filepath, dest_dir) else: self.log.error('unsupported archive - %s', compressed) raise Exception(_("unsupported archive %s", compressed)) for s in subfiles: if os.path.splitext(s)[1] in ('.rar', '.zip') and max_recursion > 0: subfiles.extend(self._unpack_subtitles(s, dest_dir, max_recursion - 1)) subfiles = filter(lambda x:os.path.splitext(x)[1] in self.SUBTILES_EXTENSIONS, subfiles) return subfiles
def downloadSubtitle(self, selected_subtitle, subtitles_dict, choosefile_cb, path=None, fname=None, overwrite_cb=None, settings=None): self.log.info( 'downloading subtitle "%s" with settings "%s"' % (selected_subtitle['filename'], toString(settings) or {})) if settings is None: settings = {} seeker = None for provider_id in subtitles_dict.keys(): if selected_subtitle in subtitles_dict[provider_id]['list']: seeker = self.getProvider(provider_id) break if seeker is None: self.log.error('provider for "%s" subtitle was not found', selected_subtitle['filename']) lang, filepath = seeker.download(subtitles_dict[provider_id], selected_subtitle)[1:3] compressed = getCompressedFileType(filepath) if compressed: subfiles = self._unpack_subtitles(filepath, self.tmp_path) else: subfiles = [filepath] subfiles = [toString(s) for s in subfiles] if len(subfiles) == 0: self.log.error("no subtitles were downloaded!") raise SubtitlesDownloadError( msg="[error] no subtitles were downloaded") elif len(subfiles) == 1: self.log.debug('found one subtitle: "%s"', str(subfiles)) subfile = subfiles[0] else: self.log.debug('found more subtitles: "%s"', str(subfiles)) subfile = choosefile_cb(subfiles) if subfile is None: self.log.debug('no subtitles file choosed!') return self.log.debug('selected subtitle: "%s"', subfile) ext = os.path.splitext(subfile)[1] if ext not in self.SUBTILES_EXTENSIONS: ext = os.path.splitext(toString(selected_subtitle['filename']))[1] if ext not in self.SUBTILES_EXTENSIONS: ext = '.srt' if fname is None: filename = os.path.basename(subfile) save_as = settings.get('save_as', 'default') if save_as == 'version': self.log.debug('filename creating by "version" setting') filename = toString(selected_subtitle['filename']) if os.path.splitext( filename)[1] not in self.SUBTILES_EXTENSIONS: filename = os.path.splitext(filename)[0] + ext elif save_as == 'video': self.log.debug('filename creating by "video" setting') videopath = toString( subtitles_dict[seeker.id]['params'].get('filepath')) filename = os.path.splitext( os.path.basename(videopath))[0] + ext if settings.get('lang_to_filename', False): lang_iso639_1_2 = toString(languageTranslate(lang, 0, 2)) self.log.debug('appending language "%s" to filename', lang_iso639_1_2) filename, ext = os.path.splitext(filename) filename = "%s.%s%s" % (filename, lang_iso639_1_2, ext) else: self.log.debug('using provided filename') filename = toString(fname) + ext self.log.debug('filename: "%s"', filename) download_path = os.path.join(toString(self.download_path), filename) if path is not None: self.log.debug('using custom download path: "%s"', path) download_path = os.path.join(toString(path), filename) self.log.debug('download path: "%s"', download_path) if os.path.isfile(download_path) and overwrite_cb is not None: ret = overwrite_cb(download_path) if ret is None: self.log.debug('overwrite cancelled, returning temp path') return subfile elif not ret: self.log.debug('not overwriting, returning temp path') return subfile elif ret: self.log.debug('overwriting') try: shutil.move(subfile, download_path) return download_path except Exception as e: self.log.error( 'moving "%s" to "%s" - %s' % (os.path.split(subfile)[-2:], os.path.split(download_path)[-2:]), str(e)) return subfile try: shutil.move(subfile, download_path) except Exception as e: self.log.error('moving "%s" to "%s" - %s', (os.path.split(subfile)[-2:], os.path.split(download_path)[-2:]), str(e)) return subfile return download_path
def downloadSubtitle(self, selected_subtitle, subtitles_dict, choosefile_cb, path=None, fname=None, overwrite_cb=None, settings=None): self.log.info('downloading subtitle "%s" with settings "%s"' % (selected_subtitle['filename'], toString(settings) or {})) if settings is None: settings = {} seeker = None for provider_id in subtitles_dict.keys(): if selected_subtitle in subtitles_dict[provider_id]['list']: seeker = self.getProvider(provider_id) break if seeker is None: self.log.error('provider for "%s" subtitle was not found', selected_subtitle['filename']) lang, filepath = seeker.download(subtitles_dict[provider_id], selected_subtitle)[1:3] compressed = getCompressedFileType(filepath) if compressed: subfiles = self._unpack_subtitles(filepath, self.tmp_path) else: subfiles = [filepath] subfiles = [toString(s) for s in subfiles] if len(subfiles) == 0: self.log.error("no subtitles were downloaded!") raise SubtitlesDownloadError(msg="[error] no subtitles were downloaded") elif len(subfiles) == 1: self.log.debug('found one subtitle: "%s"', str(subfiles)) subfile = subfiles[0] else: self.log.debug('found more subtitles: "%s"', str(subfiles)) subfile = choosefile_cb(subfiles) if subfile is None: self.log.debug('no subtitles file choosed!') return self.log.debug('selected subtitle: "%s"', subfile) ext = os.path.splitext(subfile)[1] if ext not in self.SUBTILES_EXTENSIONS: ext = os.path.splitext(toString(selected_subtitle['filename']))[1] if ext not in self.SUBTILES_EXTENSIONS: ext = '.srt' if fname is None: filename = os.path.basename(subfile) save_as = settings.get('save_as', 'default') if save_as == 'version': self.log.debug('filename creating by "version" setting') filename = toString(selected_subtitle['filename']) if os.path.splitext(filename)[1] not in self.SUBTILES_EXTENSIONS: filename = os.path.splitext(filename)[0] + ext elif save_as == 'video': self.log.debug('filename creating by "video" setting') videopath = toString(subtitles_dict[seeker.id]['params'].get('filepath')) filename = os.path.splitext(os.path.basename(videopath))[0] + ext if settings.get('lang_to_filename', False): lang_iso639_1_2 = toString(languageTranslate(lang, 0, 2)) self.log.debug('appending language "%s" to filename', lang_iso639_1_2) filename, ext = os.path.splitext(filename) filename = "%s.%s%s" % (filename, lang_iso639_1_2, ext) else: self.log.debug('using provided filename') filename = toString(fname) + ext self.log.debug('filename: "%s"', filename) download_path = os.path.join(toString(self.download_path), filename) if path is not None: self.log.debug('using custom download path: "%s"', path) download_path = os.path.join(toString(path), filename) self.log.debug('download path: "%s"', download_path) if os.path.isfile(download_path) and overwrite_cb is not None: ret = overwrite_cb(download_path) if ret is None: self.log.debug('overwrite cancelled, returning temp path') return subfile elif not ret: self.log.debug('not overwriting, returning temp path') return subfile elif ret: self.log.debug('overwriting') try: shutil.move(subfile, download_path) return download_path except Exception as e: self.log.error('moving "%s" to "%s" - %s' % ( os.path.split(subfile)[-2:], os.path.split(download_path)[-2:]), str(e)) return subfile try: shutil.move(subfile, download_path) except Exception as e: self.log.error('moving "%s" to "%s" - %s', ( os.path.split(subfile)[-2:], os.path.split(download_path)[-2:]), str(e)) return subfile return download_path