Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# search for titles
titles = self.search_titles(title, season, year)
subtitles = []
# iterate over titles
for title_id, t in titles.items():
# iterate over title's archives
for a in self.get_archives(title_id, language.legendastv):
# clean name of path separators and pack flags
clean_name = a.name.replace('/', '-')
if a.pack and clean_name.startswith('(p)'):
clean_name = clean_name[3:]
# guess from name
guess = guessit(clean_name, {'type': t['type']})
# episode
if season and episode:
# discard mismatches on episode in non-pack archives
if not a.pack and 'episode' in guess and guess['episode'] != episode:
logger.debug('Mismatched episode %s, discarding archive: %s',
guess['episode'], a.name)
continue
# compute an expiration time based on the archive timestamp
expiration_time = (datetime.utcnow().replace(tzinfo=pytz.utc) - a.timestamp).total_seconds()
# attempt to get the releases from the cache
cache_item = releases_key.format(archive_id=a.id, archive_name=a.name)
releases = region.get(cache_item, expiration_time=expiration_time)
if video.episode and self.episode == video.episode:
matches.add('episode')
# guess
matches |= guess_matches(video, guessit(self.version, {'type': 'episode', "single_value": True}))
pass
# movie
elif isinstance(video, Movie):
# title
if video.title and (sanitize(self.title) in (
sanitize(name) for name in [video.title] + video.alternative_titles)):
matches.add('title')
# year
if video.year and self.year == video.year:
matches.add('year')
# guess
matches |= guess_matches(video, guessit(self.version, {'type': 'movie', "single_value": True}))
self.matches = matches
return matches
def get_matches(self, video):
matches = guess_matches(video, guessit(self.release_info.encode("utf-8")))
# episode
if isinstance(video, Episode):
# series
if video.series and sanitize(self.series) == sanitize(video.series):
matches.add('series')
# season
if video.season and self.season == video.season:
matches.add('season')
# episode
if video.episode and self.episode == video.episode:
matches.add('episode')
# imdb_id
if video.series_imdb_id and self.imdb_id and str(self.imdb_id) == str(video.series_imdb_id):
matches.add('series_imdb_id')
matches.add('series')
if not self.matches(guess, expected_title=title, expected_season=season, expected_episode=episode,
expected_year=year, ignore_episode=multiple_episodes):
continue
# Unfortunately, the only possible way to know the release names of a specific candidate is to
# download the compressed file (rar/zip) and list the file names.
handler = LegendasTvArchiveHandler(self)
subtitle_names = handler.get_subtitle_names(subtitle_id, timestamp)
if not subtitle_names:
continue
for name in subtitle_names:
# Filtering out bad candidates (one archive might contain subtitles for the whole season,
# therefore this filtering is necessary)
guess = guessit(os.path.splitext(name)[0], {'type': candidate.get('type')})
if not self.matches(guess, expected_title=title, expected_season=season,
expected_episode=episode, expected_year=year):
continue
subtitle = LegendasTvSubtitle(language, page_link, subtitle_id, name, handler.binary_content,
imdb_id=candidate.get('imdb_id'), type=candidate.get('type'),
season=candidate.get('season'), year=candidate.get('year'),
no_downloads=no_downloads, rating=rating, featured=featured,
multiple_episodes=multiple_episodes, timestamp=timestamp)
logger.debug('Found subtitle %s', subtitle)
subtitles.append(subtitle)
next_page_link = soup.find('a', attrs={'class': 'load_more'}, text='carregar mais')
page_url = self.server_url + next_page_link['href'] if next_page_link else None
# extract subtitle's content
subs_in_archive = []
for name in archive.namelist():
for ext in (".srt", ".sub", ".ssa", ".ass"):
if name.endswith(ext):
subs_in_archive.append(name)
# select the correct subtitle file
matching_sub = None
subs_unsure = []
subs_fallback = []
if len(subs_in_archive) == 1:
matching_sub = subs_in_archive[0]
else:
for sub_name in subs_in_archive:
guess = guessit(sub_name)
sub_name_lower = sub_name.lower()
# consider subtitle valid if:
# - episode and season match
# - format matches (if it was matched before)
# - release group matches (and we asked for one and it was matched, or it was not matched)
# - not asked for forced and "forced" not in filename
is_episode = subtitle.asked_for_episode
if not subtitle.language.forced:
base, ext = os.path.splitext(sub_name_lower)
if base.endswith("forced") or "forced" in guess.get("release_group", ""):
continue
episodes = guess.get("episode")
if is_episode and episodes and not isinstance(episodes, list):
video.year and video.year == self.year):
matches.add('year')
# release_group
if (video.release_group and self.version and
any(r in sanitize_release_group(self.version)
for r in get_equivalent_release_groups(sanitize_release_group(video.release_group)))):
matches.add('release_group')
# resolution
if video.resolution and self.version and video.resolution in self.version.lower():
matches.add('resolution')
# format
if video.format and self.version and video.format.lower() in self.version.lower():
matches.add('format')
# other properties
matches |= guess_matches(video, guessit(self.release_info.encode("utf-8")))
return matches
"""Create a canonical name from a release name or a guessed dictionary.
The return value is always unicode.
:param obj:
:type obj: str or dict
:param fmt:
:type fmt: str or unicode
:param separator:
:type separator: str or unicode
:param ignore_list:
:type ignore_list: set
:return:
:rtype: text_type
"""
guess = obj if isinstance(obj, dict) else guessit.guessit(obj)
return text_type(
text_type(separator).join(
[text_type(fmt).format(key=unicodify(k), value=unicodify(v))
for k, v in guess.items() if k not in ignore_list]))
if video.series and sanitize(self.series_name) == sanitize(video.series):
matches.add('series')
# year
if video.original_series and self.movie_year is None or video.year and video.year == self.movie_year:
matches.add('year')
# season
if video.season and self.series_season == video.season:
matches.add('season')
# episode
if video.episode and self.series_episode == video.episode:
matches.add('episode')
# title
if video.title and sanitize(self.series_title) == sanitize(video.title):
matches.add('title')
# guess
matches |= guess_matches(video, guessit(self.movie_release_name, {'type': 'episode'}))
matches |= guess_matches(video, guessit(self.filename, {'type': 'episode'}))
# hash
if 'opensubtitles' in video.hashes and self.hash == video.hashes['opensubtitles']:
if 'series' in matches and 'season' in matches and 'episode' in matches:
matches.add('hash')
else:
logger.debug('Match on hash discarded')
# movie
elif isinstance(video, Movie) and self.movie_kind == 'movie':
# tag match, assume title and year matches
if self.matched_by == 'tag':
matches |= {'title', 'year'}
# title
if video.title and sanitize(self.movie_name) == sanitize(video.title):
matches.add('title')
# year
if video.series and sanitize(self.series) == sanitize(video.series):
matches.add('series')
# season
if video.season and self.season == video.season:
matches.add('season')
# episode
if video.episode and self.episode == video.episode:
matches.add('episode')
# guess
for release in self.releases:
matches |= guess_matches(video, guessit(release, {'type': 'episode'}))
# movie
elif isinstance(video, Movie):
# guess
for release in self.releases:
matches |= guess_matches(video, guessit(release, {'type': 'movie'}))
# title
if video.title and sanitize(self.title) == sanitize(video.title):
matches.add('title')
return matches
matches.add('series')
# season
if video.season and self.season == video.season:
matches.add('season')
# episode
if video.episode and self.episode == video.episode:
matches.add('episode')
# year
if video.original_series and self.year is None or video.year and video.year == self.year:
matches.add('year')
# release_group
if video.release_group and self.release and video.release_group.lower() in self.release.lower():
matches.add('release_group')
# other properties
if self.release:
matches |= guess_matches(video, guessit(self.release, {'type': 'episode'}), partial=True)
if self.rip:
matches |= guess_matches(video, guessit(self.rip), partial=True)
return matches