def do_search(self, *args, **kwargs): try: return search(*args, **kwargs) except requests.HTTPError: region.delete("subscene_cookies2")
def query(self, video, languages): _searchurl = self.searchurl if isinstance(video, Movie): querytext = video.imdb_id if video.imdb_id else video.title if isinstance(video, Episode): querytext = '"{} S{:02d}E{:02d}"'.format(video.series, video.season, video.episode) querytext = quote(quote(querytext)) # language query filter if isinstance(languages, (tuple, list, set)): language_ids = ','.join(sorted(l.opensubtitles for l in languages)) if 'por' in language_ids: # prioritize portuguese subtitles lang_filter = '&form_cat=28' elif 'pob' in language_ids: lang_filter = '&form_cat=29' else: lang_filter = '' querytext = querytext + lang_filter if lang_filter else querytext try: # sleep for a 1 second before another request sleep(1) self.headers['Referer'] = self.site + '/index.php' self.session.headers.update(self.headers) res = self.session.get(_searchurl.format(query=querytext), allow_redirects=False) res.raise_for_status() if (res.status_code == 200 and "A legenda não foi encontrada" in res.text): logger.warning('Legendasdivx.pt :: query %s return no results!', querytext) # for series, if no results found, try again just with series and season (subtitle packs) if isinstance(video, Episode): logger.debug("Legendasdivx.pt :: trying again with just series and season on query.") querytext = re.sub("(e|E)(\d{2})", "", querytext) res = self.session.get(_searchurl.format(query=querytext), allow_redirects=False) res.raise_for_status() if (res.status_code == 200 and "A legenda não foi encontrada" in res.text): logger.warning('Legendasdivx.pt :: query %s return no results (for series and season only).', querytext) return [] if res.status_code == 302: # got redirected to login page. # seems that our session cookies are no longer valid... clean them from cache region.delete("legendasdivx_cookies2") logger.debug("Legendasdivx.pt :: Logging in again. Cookies have expired!") # login and try again self.login() res = self.session.get(_searchurl.format(query=querytext)) res.raise_for_status() except HTTPError as e: if "bloqueado" in res.text.lower(): logger.error("LegendasDivx.pt :: Your IP is blocked on this server.") raise IPAddressBlocked("LegendasDivx.pt :: Your IP is blocked on this server.") logger.error("Legendasdivx.pt :: HTTP Error %s", e) raise TooManyRequests("Legendasdivx.pt :: HTTP Error %s", e) except Exception as e: logger.error("LegendasDivx.pt :: Uncaught error: %r", e) raise ServiceUnavailable("LegendasDivx.pt :: Uncaught error: %r", e) bsoup = ParserBeautifulSoup(res.content, ['html.parser']) # search for more than 10 results (legendasdivx uses pagination) # don't throttle - maximum results = 6 * 10 MAX_PAGES = 6 # get number of pages bases on results found page_header = bsoup.find("div", {"class": "pager_bar"}) results_found = re.search(r'\((.*?) encontradas\)', page_header.text).group(1) if page_header else 0 logger.debug("Legendasdivx.pt :: Found %s subtitles", str(results_found)) num_pages = (int(results_found) // 10) + 1 num_pages = min(MAX_PAGES, num_pages) # process first page subtitles = self._process_page(video, bsoup) # more pages? if num_pages > 1: for num_page in range(2, num_pages+1): sleep(1) # another 1 sec before requesting... _search_next = self.searchurl.format(query=querytext) + "&page={0}".format(str(num_page)) logger.debug("Legendasdivx.pt :: Moving on to next page: %s", _search_next) res = self.session.get(_search_next) next_page = ParserBeautifulSoup(res.content, ['html.parser']) subs = self._process_page(video, next_page) subtitles.extend(subs) return subtitles
def save_settings(settings_items): configure_debug = False configure_captcha = False update_schedule = False sonarr_changed = False radarr_changed = False update_path_map = False configure_proxy = False exclusion_updated = False sonarr_exclusion_updated = False radarr_exclusion_updated = False # Subzero Mods update_subzero = False subzero_mods = get_array_from(settings.general.subzero_mods) if len(subzero_mods) == 1 and subzero_mods[0] == '': subzero_mods = [] for key, value in settings_items: settings_keys = key.split('-') # Make sure that text based form values aren't pass as list if isinstance(value, list) and len( value) == 1 and settings_keys[-1] not in array_keys: value = value[0] if value in empty_values and value != '': value = None # Make sure empty language list are stored correctly if settings_keys[-1] in array_keys and value[0] in empty_values: value = [] # Handle path mappings settings since they are array in array if settings_keys[-1] in ['path_mappings', 'path_mappings_movie']: value = [v.split(',') for v in value] if value == 'true': value = 'True' elif value == 'false': value = 'False' if key == 'settings-auth-password': if value != settings.auth.password and value is not None: value = hashlib.md5(value.encode('utf-8')).hexdigest() if key == 'settings-general-debug': configure_debug = True if key == 'settings-general-hi_extension': os.environ["SZ_HI_EXTENSION"] = str(value) if key in [ 'settings-general-anti_captcha_provider', 'settings-anticaptcha-anti_captcha_key', 'settings-deathbycaptcha-username', 'settings-deathbycaptcha-password' ]: configure_captcha = True if key in [ 'update_schedule', 'settings-general-use_sonarr', 'settings-general-use_radarr', 'settings-general-auto_update', 'settings-general-upgrade_subs', 'settings-sonarr-series_sync', 'settings-sonarr-episodes_sync', 'settings-radarr-movies_sync', 'settings-sonarr-full_update', 'settings-sonarr-full_update_day', 'settings-sonarr-full_update_hour', 'settings-radarr-full_update', 'settings-radarr-full_update_day', 'settings-radarr-full_update_hour', 'settings-general-wanted_search_frequency', 'settings-general-wanted_search_frequency_movie', 'settings-general-upgrade_frequency', 'settings-backup-frequency', 'settings-backup-day', 'settings-backup-hour' ]: update_schedule = True if key in [ 'settings-general-use_sonarr', 'settings-sonarr-ip', 'settings-sonarr-port', 'settings-sonarr-base_url', 'settings-sonarr-ssl', 'settings-sonarr-apikey' ]: sonarr_changed = True if key in [ 'settings-general-use_radarr', 'settings-radarr-ip', 'settings-radarr-port', 'settings-radarr-base_url', 'settings-radarr-ssl', 'settings-radarr-apikey' ]: radarr_changed = True if key in [ 'settings-general-path_mappings', 'settings-general-path_mappings_movie' ]: update_path_map = True if key in [ 'settings-proxy-type', 'settings-proxy-url', 'settings-proxy-port', 'settings-proxy-username', 'settings-proxy-password' ]: configure_proxy = True if key in [ 'settings-sonarr-excluded_tags', 'settings-sonarr-only_monitored', 'settings-sonarr-excluded_series_types', 'settings-sonarr-exclude_season_zero', 'settings.radarr.excluded_tags', 'settings-radarr-only_monitored' ]: exclusion_updated = True if key in [ 'settings-sonarr-excluded_tags', 'settings-sonarr-only_monitored', 'settings-sonarr-excluded_series_types', 'settings-sonarr-exclude_season_zero' ]: sonarr_exclusion_updated = True if key in [ 'settings.radarr.excluded_tags', 'settings-radarr-only_monitored' ]: radarr_exclusion_updated = True if key == 'settings-addic7ed-username': if key != settings.addic7ed.username: region.delete('addic7ed_data') if key == 'settings-legendasdivx-username': if key != settings.legendasdivx.username: region.delete('legendasdivx_cookies2') if key == 'settings-opensubtitles-username': if key != settings.opensubtitles.username: region.delete('os_token') if key == 'settings-opensubtitlescom-username': if key != settings.opensubtitlescom.username: region.delete('oscom_token') if key == 'settings-subscene-username': if key != settings.subscene.username: region.delete('subscene_cookies2') if key == 'settings-titlovi-username': if key != settings.titlovi.username: region.delete('titlovi_token') if settings_keys[0] == 'settings': settings[settings_keys[1]][settings_keys[2]] = str(value) if settings_keys[0] == 'subzero': mod = settings_keys[1] enabled = value == 'True' if mod in subzero_mods and not enabled: subzero_mods.remove(mod) elif enabled: subzero_mods.append(mod) # Handle color if mod == 'color': previous = None for exist_mod in subzero_mods: if exist_mod.startswith('color'): previous = exist_mod break if previous is not None: subzero_mods.remove(previous) if value not in empty_values: subzero_mods.append(value) update_subzero = True if update_subzero: settings.set('general', 'subzero_mods', ','.join(subzero_mods)) with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) # Reconfigure Bazarr to reflect changes if configure_debug: from logger import configure_logging configure_logging(settings.general.getboolean('debug') or args.debug) if configure_captcha: configure_captcha_func() if update_schedule: from scheduler import scheduler from event_handler import event_stream scheduler.update_configurable_tasks() event_stream(type='task') if sonarr_changed: from signalr_client import sonarr_signalr_client try: sonarr_signalr_client.restart() except Exception: pass if radarr_changed: from signalr_client import radarr_signalr_client try: radarr_signalr_client.restart() except Exception: pass if update_path_map: from helper import path_mappings path_mappings.update() if configure_proxy: configure_proxy_func() if exclusion_updated: from event_handler import event_stream event_stream(type='badges') if sonarr_exclusion_updated: event_stream(type='reset-episode-wanted') if radarr_exclusion_updated: event_stream(type='reset-movie-wanted')
def save_settings(settings_items): from database import database configure_debug = False configure_captcha = False update_schedule = False update_path_map = False configure_proxy = False exclusion_updated = False for key, value in settings_items: # Intercept database stored settings if key == 'enabled_languages': database.execute("UPDATE table_settings_languages SET enabled=0") for item in value: database.execute( "UPDATE table_settings_languages SET enabled=1 WHERE code2=?", (item, )) continue # Make sure that text based form values aren't pass as list unless they are language list if isinstance(value, list) and len(value) == 1 and key not in [ 'settings-general-serie_default_language', 'settings-general-movie_default_language' ]: value = value[0] # Make sure empty language list are stored correctly due to bug in bootstrap-select if key in [ 'settings-general-serie_default_language', 'settings-general-movie_default_language' ] and value == ['null']: value = [] settings_keys = key.split('-') if value == 'true': value = 'True' elif value == 'false': value = 'False' if key == 'settings-auth-password': value = hashlib.md5(value.encode('utf-8')).hexdigest() if key == 'settings-general-debug': configure_debug = True if key in [ 'settings-general-anti_captcha_provider', 'settings-anticaptcha-anti_captcha_key', 'settings-deathbycaptcha-username', 'settings-deathbycaptcha-password' ]: configure_captcha = True if key in [ 'update_schedule', 'settings-general-use_sonarr', 'settings-general-use_radarr', 'settings-general-auto_update', 'settings-general-upgrade_subs' ]: update_schedule = True if key in [ 'settings-general-path_mappings', 'settings-general-path_mappings_movie' ]: update_path_map = True if key in [ 'settings-proxy-type', 'settings-proxy-url', 'settings-proxy-port', 'settings-proxy-username', 'settings-proxy-password' ]: configure_proxy = True if key in [ 'settings-sonarr-excluded_tags', 'settings-sonarr-only_monitored', 'settings-sonarr-excluded_series_types', 'settings.radarr.excluded_tags', 'settings-radarr-only_monitored' ]: exclusion_updated = True if key == 'settings-addic7ed-username': if key != settings.addic7ed.username: region.delete('addic7ed_data') if key == 'settings-legendasdivx-username': if key != settings.legendasdivx.username: region.delete('legendasdivx_cookies2') if key == 'settings-opensubtitles-username': if key != settings.opensubtitles.username: region.delete('os_token') if key == 'settings-subscene-username': if key != settings.subscene.username: region.delete('subscene_cookies2') if key == 'settings-titlovi-username': if key != settings.titlovi.username: region.delete('titlovi_token') if settings_keys[0] == 'settings': settings[settings_keys[1]][settings_keys[2]] = str(value) with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) # Reconfigure Bazarr to reflect changes if configure_debug: from logger import configure_logging configure_logging(settings.general.getboolean('debug') or args.debug) if configure_captcha: configure_captcha_func() if update_schedule: from api import scheduler scheduler.update_configurable_tasks() if update_path_map: from helper import path_mappings path_mappings.update() if configure_proxy: configure_proxy_func() if exclusion_updated: from event_handler import event_stream event_stream(type='badges_series') event_stream(type='badges_movies')