Пример #1
0
 def getSubtitlesList(self,
                      subtitles_dict,
                      provider=None,
                      langs=None,
                      synced=False,
                      nonsynced=False):
     subtitles_list = []
     if provider and provider in subtitles_dict:
         subtitles_list = subtitles_dict[provider]['list']
         for sub in subtitles_list:
             if 'provider' not in sub:
                 sub['provider'] = provider
             if 'country' not in sub:
                 sub['country'] = langToCountry(
                     languageTranslate(sub['language_name'], 0, 2))
     else:
         for provider in subtitles_dict:
             provider_list = subtitles_dict[provider]['list']
             subtitles_list += provider_list
             for sub in provider_list:
                 if 'provider' not in sub:
                     sub['provider'] = provider
                 if 'country' not in sub:
                     sub['country'] = langToCountry(
                         languageTranslate(sub['language_name'], 0, 2))
     if synced:
         subtitles_list = filter(lambda x: x['sync'], subtitles_list)
     elif nonsynced:
         subtitles_list = filter(lambda x: not x['sync'], subtitles_list)
     if langs:
         subtitles_list = filter(
             lambda x: x['language_name'] in
             [languageTranslate(lang, 0, 2) for lang in langs])
     return subtitles_list
Пример #2
0
 def getSubtitlesList(self, subtitles_dict, provider=None, langs=None, synced=False, nonsynced=False):
     subtitles_list = []
     if provider and provider in subtitles_dict:
         subtitles_list = subtitles_dict[provider]['list']
         for sub in subtitles_list:
             if 'provider' not in sub:
                 sub['provider'] = provider
             if 'country' not in sub:
                 sub['country'] = langToCountry(languageTranslate(sub['language_name'], 0, 2))
     else:
         for provider in subtitles_dict:
             provider_list = subtitles_dict[provider]['list']
             subtitles_list += provider_list
             for sub in provider_list:
                 if 'provider' not in sub:
                     sub['provider'] = provider
                 if 'country' not in sub:
                     sub['country'] = langToCountry(languageTranslate(sub['language_name'], 0, 2))
     if synced:
         subtitles_list = filter(lambda x:x['sync'], subtitles_list)
     elif nonsynced:
         subtitles_list = filter(lambda x:not x['sync'], subtitles_list)
     if langs:
         subtitles_list = filter(lambda x:x['language_name'] in [languageTranslate(lang, 0, 2) for lang in langs])
     return subtitles_list
Пример #3
0
    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
Пример #4
0
 def sortLangs(x):
     for idx, lang in enumerate(langs):
         if languageTranslate(x['language_name'], 0, 2) == lang:
             return idx
     return len(langs)
Пример #5
0
 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
Пример #6
0
 def sortLangs(x):
     for idx, lang in enumerate(langs):
         if languageTranslate(x['language_name'], 0, 2) == lang:
             return idx
     return len(langs)