def execute(args): """ Executes SubLime with given arguments. """ videos = [] selected_languages = [ babelfish.Language(selected_lang) for selected_lang in args.selected_languages ] # List of filenames directly given by user if args.video_files: videos = [ VideoFactory.make_from_filename(video_filename) for video_filename in args.video_files ] # Or list of filenames by walking through directories elif args.directories: for movie_dir in args.directories: for root, _, files in os.walk(movie_dir): for name in files: video_filename = os.path.join(root, name) video = VideoFactory.make_from_filename(video_filename) if video: videos.append(video) # Informs user that there is already existing subtitles for video in videos: for selected_lang in selected_languages: if video.has_subtitle(selected_lang): video_type = video.__class__.__name__ video_name = os.path.basename(video.filename) if not args.force: LOG.warning( "{} named {} already has a subtitle " "for {} and nothing will happen for it! " "Use option '-f --force' to replace.".format( video_type, video_name, selected_lang.name)) else: LOG.info( 'Replacing {} subtitle for {} named {}.'.format( selected_lang.name, video_type, video_name)) video.languages_to_download.append(selected_lang) else: video.languages_to_download.append(selected_lang) # Search subtitles for videos for sub_server in SubtitleProvider.get_providers(): sub_server.connect() sub_server.download_subtitles( videos, selected_languages, args.rename, args.rename_pattern, args.underscore) sub_server.disconnect()
def _do_search_subtitles(self, videos_hashcode, languages): """ Search list of subtitles. """ subtitles_infos = [] # Search subtitles hashcodes_sizes = [ {'moviehash': hash_code, 'moviebytesize': video.size} for hash_code, video in videos_hashcode.items() ] response = self._proxy.SearchSubtitles( self._session_string, hashcodes_sizes) if self.status_ok(response): if 'data' in response and response['data']: for data_subtitle in response['data']: # Retrieve important info sub_video_hashcode = data_subtitle['MovieHash'] sub_video = videos_hashcode[sub_video_hashcode] sub_lang = Language.fromopensubtitles( data_subtitle['SubLanguageID']) if sub_lang in sub_video.languages_to_download \ and sub_lang in languages: # Subtitle infos sub_id = data_subtitle['IDSubtitleFile'] sub_rating = float(data_subtitle['SubRating']) sub_format = data_subtitle['SubFormat'] # Video infos sub_video_name = data_subtitle['MovieName'] if data_subtitle['MovieKind'] == "movie": sub_video = VideoFactory.make_from_type( sub_video, Movie) elif data_subtitle['MovieKind'] == "episode": sub_video = VideoFactory.make_from_type( sub_video, Episode) videos_hashcode[sub_video_hashcode] = sub_video if isinstance(sub_video, Movie): sub_video.name = sub_video_name elif isinstance(sub_video, Episode): # Retrieves serie name and episode name match_result = re.match( self._series_regexp, sub_video_name) sub_video.name = match_result.group("serie_name") sub_video.episode_name = match_result.group( "episode_name") sub_video.season = int( data_subtitle['SeriesSeason']) sub_video.episode = int( data_subtitle['SeriesEpisode']) subtitle = Subtitle( sub_id, sub_lang, sub_video, sub_rating, sub_format) subtitles_infos.append(subtitle) else: raise SubtitleServerError( self, "There is no result when searching for subtitles.") else: raise SubtitleServerError(self, self.get_status_reason(response)) return subtitles_infos