def provider_throttle(name, exception): cls = getattr(exception, "__class__") cls_name = getattr(cls, "__name__") if cls not in VALID_THROTTLE_EXCEPTIONS: for valid_cls in VALID_THROTTLE_EXCEPTIONS: if isinstance(cls, valid_cls): cls = valid_cls throttle_data = PROVIDER_THROTTLE_MAP.get(name, PROVIDER_THROTTLE_MAP["default"]).get(cls, None) or \ PROVIDER_THROTTLE_MAP["default"].get(cls, None) if not throttle_data: return throttle_delta, throttle_description = throttle_data throttle_until = datetime.datetime.now() + throttle_delta if throttled_count(name): tp[name] = (cls_name, throttle_until, throttle_description) settings.general.throtteled_providers = str(tp) with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) logging.info( "Throttling %s for %s, until %s, because of: %s. Exception info: %r", name, throttle_description, throttle_until.strftime("%y/%m/%d %H:%M"), cls_name, exception.message)
def api_post_request(api_url, extra_payload={}, extra_headers={}): token = settings.read('api', 'access_token') if not token: token = get_token() settings.write('api', 'access_token', token) headers = {'Access-Token': token, 'Content-Type': 'application/json'} payload = {} if extra_headers: headers.update(extra_headers) if extra_payload: payload.update(extra_payload) r = requests.post(api_url, data=json.dumps(payload), headers=headers) if r.status_code != 200: log_msg = 'api post error: {0}\n{1}\napi url: {2}\npayload: {3}'.format(r.status_code, r.text, api_url, payload) log(log_msg) raise PbApiException() else: return r.json()
def update_throttled_provider(): changed = False if settings.general.enabled_providers: for provider in list(tp): if provider not in settings.general.enabled_providers: del tp[provider] settings.general.throtteled_providers = str(tp) changed = True reason, until, throttle_desc = tp.get(provider, (None, None, None)) if reason: now = datetime.datetime.now() if now < until: pass else: logging.info( "Using %s again after %s, (disabled because: %s)", provider, throttle_desc, reason) del tp[provider] settings.general.throtteled_providers = str(tp) changed = True if changed: with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle)
def get_providers(): changed = False providers_list = [] if settings.general.enabled_providers: for provider in settings.general.enabled_providers.lower().split(','): reason, until, throttle_desc = tp.get(provider, (None, None, None)) providers_list.append(provider) if reason: now = datetime.datetime.now() if now < until: logging.debug("Not using %s until %s, because of: %s", provider, until.strftime("%y/%m/%d %H:%M"), reason) providers_list.remove(provider) else: logging.info("Using %s again after %s, (disabled because: %s)", provider, throttle_desc, reason) del tp[provider] settings.general.throtteled_providers = str(tp) changed = True if changed: with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) # if forced only is enabled: # fixme: Prepared for forced only implementation to remove providers with don't support forced only subtitles # for provider in providers_list: # if provider in PROVIDERS_FORCED_OFF: # providers_list.remove(provider) if not providers_list: providers_list = None return providers_list
def provider_throttle(name, exception): cls = getattr(exception, "__class__") cls_name = getattr(cls, "__name__") if cls not in VALID_THROTTLE_EXCEPTIONS: for valid_cls in VALID_THROTTLE_EXCEPTIONS: if isinstance(cls, valid_cls): cls = valid_cls throttle_data = PROVIDER_THROTTLE_MAP.get(name, PROVIDER_THROTTLE_MAP["default"]).get(cls, None) or \ PROVIDER_THROTTLE_MAP["default"].get(cls, None) if throttle_data: throttle_delta, throttle_description = throttle_data else: throttle_delta, throttle_description = datetime.timedelta(minutes=10), "10 minutes" throttle_until = datetime.datetime.now() + throttle_delta if cls_name not in VALID_COUNT_EXCEPTIONS or throttled_count(name): if cls_name == 'ValueError' and exception.args[0].startswith('unsupported pickle protocol'): for fn in subliminal_cache_region.backend.all_filenames: try: os.remove(fn) except (IOError, OSError): logging.debug("Couldn't remove cache file: %s", os.path.basename(fn)) else: tp[name] = (cls_name, throttle_until, throttle_description) settings.general.throtteled_providers = str(tp) with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) logging.info("Throttling %s for %s, until %s, because of: %s. Exception info: %r", name, throttle_description, throttle_until.strftime("%y/%m/%d %H:%M"), cls_name, exception.args[0] if exception.args else None)
def reset_throttled_providers(): for provider in list(tp): del tp[provider] settings.general.throtteled_providers = str(tp) with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) update_throttled_provider() logging.info('BAZARR throttled providers have been reset.')
def track_event(category=None, action=None, label=None): if not settings.analytics.getboolean('enabled'): return anonymousConfig = Config() anonymousConfig.anonimize_ip_address = True tracker = Tracker('UA-138214134-3', 'none', conf=anonymousConfig) try: if settings.analytics.visitor: visitor = pickle.loads(codecs.decode(settings.analytics.visitor.encode(), "base64")) if visitor.user_agent is None: visitor.user_agent = os.environ.get("SZ_USER_AGENT") if visitor.unique_id > int(0x7fffffff): visitor.unique_id = random.randint(0, 0x7fffffff) else: visitor = Visitor() visitor.unique_id = random.randint(0, 0x7fffffff) except: visitor = Visitor() visitor.unique_id = random.randint(0, 0x7fffffff) session = Session() event = Event(category=category, action=action, label=label, value=1) tracker.add_custom_variable(CustomVariable(index=1, name='BazarrVersion', value=os.environ["BAZARR_VERSION"], scope=1)) tracker.add_custom_variable(CustomVariable(index=2, name='PythonVersion', value=platform.python_version(), scope=1)) if settings.general.getboolean('use_sonarr'): tracker.add_custom_variable(CustomVariable(index=3, name='SonarrVersion', value=sonarr_version, scope=1)) else: tracker.add_custom_variable(CustomVariable(index=3, name='SonarrVersion', value='unused', scope=1)) if settings.general.getboolean('use_radarr'): tracker.add_custom_variable(CustomVariable(index=4, name='RadarrVersion', value=radarr_version, scope=1)) else: tracker.add_custom_variable(CustomVariable(index=4, name='RadarrVersion', value='unused', scope=1)) tracker.add_custom_variable(CustomVariable(index=5, name='OSVersion', value=platform.platform(), scope=1)) try: tracker.track_event(event, session, visitor) except: logging.debug("BAZARR unable to track event.") pass else: settings.analytics.visitor = codecs.encode(pickle.dumps(visitor), "base64").decode() with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle)
def __init__(self): EventMgr.__init__(self) self.running = True self.token = settings.read('api', 'access_token') self.pushStream = [] self.do_exit = False self.wss = None self.pong = False self.pongTime = time.time() self.maxPongTime = 40 self.connected = False self.disconnectCount = 0 if not self.token: self.token = get_token() settings.write('api', 'access_token', self.token) self.register(Event(self.connect))
def track_event(category=None, action=None, label=None): if not settings.analytics.getboolean('enabled'): return anonymousConfig = Config() anonymousConfig.anonimize_ip_address = True tracker = Tracker('UA-138214134-3', 'none', conf=anonymousConfig) try: visitor = pickle.loads(base64.b64decode(settings.analytics.visitor)) except: visitor = Visitor() unique_id = long(random.getrandbits(32)) visitor.unique_id = unique_id session = Session() event = Event(category=category, action=action, label=label, value=1) path = u"/" + u"/".join([category, action, label]) page = Page(path.lower()) tracker.add_custom_variable(CustomVariable(index=1, name='BazarrVersion', value=os.environ["BAZARR_VERSION"], scope=1)) tracker.add_custom_variable(CustomVariable(index=2, name='PythonVersion', value=platform.python_version(), scope=1)) if settings.general.getboolean('use_sonarr'): tracker.add_custom_variable(CustomVariable(index=3, name='SonarrVersion', value=sonarr_version, scope=1)) if settings.general.getboolean('use_radarr'): tracker.add_custom_variable(CustomVariable(index=4, name='RadarrVersion', value=radarr_version, scope=1)) tracker.add_custom_variable(CustomVariable(index=5, name='OSVersion', value=platform.platform(), scope=1)) try: tracker.track_event(event, session, visitor) tracker.track_pageview(page, session, visitor) except: pass else: settings.analytics.visitor = base64.b64encode(pickle.dumps(visitor)) with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle)
def api_get_request(api_url, extra_payload={}, extra_headers={}): token = settings.read('api', 'access_token') if not token: token = get_token() settings.write('api', 'access_token', token) headers = {'Access-Token': token} payload = {} if extra_headers: headers.update(extra_headers) if extra_payload: payload.update(extra_payload) r = requests.get(api_url, params=extra_payload, headers=headers) if r.status_code != 200: log_msg = 'api get error: {0}\n{1}\napi url: {2}\npayload: {3}'.format(r.status_code, r.text, api_url, payload) log(log_msg) raise PbApiException() else: return r.json()
except Exception as e: logging.error( 'BAZARR Cannot create bazarr.restart file: ' + repr(e)) else: logging.info('Bazarr is being restarted...') restart_file.write(str('')) restart_file.close() os._exit(0) # create random api_key if there's none in config.ini if not settings.auth.apikey or settings.auth.apikey.startswith("b'"): from binascii import hexlify settings.auth.apikey = hexlify(os.urandom(16)).decode() with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) # create random Flask secret_key if there's none in config.ini if not settings.general.flask_secret_key: from binascii import hexlify settings.general.flask_secret_key = hexlify(os.urandom(16)).decode() with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) # change default base_url to '' settings.general.base_url = settings.general.base_url.rstrip('/') with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle)
def get_salt(): salt = settings.read('api', 'user_iden') if not salt: salt = get_me()['iden'] settings.write('api', 'user_iden', salt) return salt