def before_request_func(): g.request_params = ( request.args if request.method == 'GET' else request.form ) # Skip pre-request actions if verifying session if '/session' in request.path and not valid_user_session(session): return default_config = json.load(open(app.config['DEFAULT_CONFIG'])) \ if os.path.exists(app.config['DEFAULT_CONFIG']) else {} # Generate session values for user if unavailable if (not valid_user_session(session) and 'cookies_disabled' not in request.args): session['config'] = default_config session['uuid'] = str(uuid.uuid4()) session['key'] = generate_user_key() # Skip checking for session on any searches that don't # require a valid session if (not Endpoint.autocomplete.in_path(request.path) and not Endpoint.healthz.in_path(request.path)): return redirect(url_for( 'session_check', session_id=session['uuid'], follow=get_request_url(request.url)), code=307) else: g.user_config = Config(**session['config']) elif 'cookies_disabled' not in request.args: # Set session as permanent session.permanent = True app.permanent_session_lifetime = timedelta(days=365) g.user_config = Config(**session['config']) else: # User has cookies disabled, fall back to immutable default config session.pop('_permanent', None) g.user_config = Config(**default_config) if not g.user_config.url: g.user_config.url = get_request_url(request.url_root) g.user_request = Request( request.headers.get('User-Agent'), get_request_url(request.url_root), config=g.user_config) g.app_location = g.user_config.url
def before_request_func(): g.request_params = request.args if request.method == 'GET' else request.form g.cookies_disabled = False # Generate session values for user if unavailable if not valid_user_session(session): session['config'] = json.load(open(app.config['DEFAULT_CONFIG'])) \ if os.path.exists(app.config['DEFAULT_CONFIG']) else {'url': request.url_root} session['uuid'] = str(uuid.uuid4()) session['fernet_keys'] = generate_user_keys(True) # Flag cookies as possibly disabled in order to prevent against # unnecessary session directory expansion g.cookies_disabled = True if session['uuid'] not in app.user_elements: app.user_elements.update({session['uuid']: 0}) # Always redirect to https if HTTPS_ONLY is set (otherwise default to False) https_only = os.getenv('HTTPS_ONLY', False) if https_only and request.url.startswith('http://'): return redirect(request.url.replace('http://', 'https://', 1), code=308) g.user_config = Config(**session['config']) if not g.user_config.url: g.user_config.url = request.url_root.replace( 'http://', 'https://') if https_only else request.url_root g.user_request = Request(request.headers.get('User-Agent'), language=g.user_config.lang) g.app_location = g.user_config.url
def before_request_func(): json_config = json.load( open(CONFIG_PATH)) if os.path.exists(CONFIG_PATH) else { 'url': request.url_root } g.user_config = Config(**json_config) if not g.user_config.url: g.user_config.url = request.url_root g.user_request = Request(request.headers.get('User-Agent'), language=g.user_config.lang) g.app_location = g.user_config.url
def before_request_func(): # Always redirect to https if HTTPS_ONLY is set (otherwise default to false) https_only = os.getenv('HTTPS_ONLY', False) if https_only and request.url.startswith('http://'): url = request.url.replace('http://', 'https://', 1) code = 308 return redirect(url, code=code) json_config = json.load(open(CONFIG_PATH)) if os.path.exists(CONFIG_PATH) else {'url': request.url_root} g.user_config = Config(**json_config) if not g.user_config.url: g.user_config.url = request.url_root.replace('http://', 'https://') if https_only else request.url_root g.user_request = Request(request.headers.get('User-Agent'), language=g.user_config.lang) g.app_location = g.user_config.url
def get_search_results(data): secret_key = generate_user_key() soup = Filter(user_key=secret_key, config=Config(**demo_config)).clean( BeautifulSoup(data, 'html.parser')) main_divs = soup.find('div', {'id': 'main'}) assert len(main_divs) > 1 result_divs = [] for div in main_divs: # Result divs should only have 1 inner div if (len(list(div.children)) != 1 or not div.findChild() or 'div' not in div.findChild().name): continue result_divs.append(div) return result_divs
def load_config(config_dir: Path = None) -> Config: app_dir: Path = Path(__file__).parent.parent.parent config_dir = config_dir or app_dir / 'config' load_dotenv(str(config_dir / '.env')) log_config = load_log_config(app_dir=app_dir) logging_setup(config_dir, log_config) _bot_token = os.getenv("KARMA_BOT_TOKEN") with (config_dir / "bot-config.yml").open('r', encoding="utf-8") as f: config_file_data = yaml.load(f, Loader=yaml.FullLoader) return Config( auto_restriction=load_karmic_restriction_config(), db=load_db_config(app_dir), webhook=load_webhook_config(), app_dir=app_dir, bot_token=_bot_token, superusers=frozenset(config_file_data['superusers']), log=log_config, dump_chat_id=-1001459777201, # ⚙️Testing Area >>> Python Scripts, tg_client=TgClientConfig(bot_token=_bot_token), )
def _session() -> object: """Return boto3 client object from Config. """ _session = Config(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) return _session.create_client()
def load_config(app_dir: Path) -> Config: load_dotenv(app_dir / ".env") setup_logging(app_dir) return Config(app_dir=app_dir, )
def before_request_func(): global bang_json # Check for latest version if needed now = datetime.now() if now - timedelta(hours=24) > app.config['LAST_UPDATE_CHECK']: app.config['LAST_UPDATE_CHECK'] = now app.config['HAS_UPDATE'] = check_for_update( app.config['RELEASES_URL'], app.config['VERSION_NUMBER']) g.request_params = ( request.args if request.method == 'GET' else request.form ) # Skip pre-request actions if verifying session if '/session' in request.path and not valid_user_session(session): return default_config = json.load(open(app.config['DEFAULT_CONFIG'])) \ if os.path.exists(app.config['DEFAULT_CONFIG']) else {} # Generate session values for user if unavailable if (not valid_user_session(session) and 'cookies_disabled' not in request.args): session['config'] = default_config session['uuid'] = str(uuid.uuid4()) session['key'] = generate_user_key() # Skip checking for session on any searches that don't # require a valid session if (not Endpoint.autocomplete.in_path(request.path) and not Endpoint.healthz.in_path(request.path) and not Endpoint.opensearch.in_path(request.path)): return redirect(url_for( 'session_check', session_id=session['uuid'], follow=get_request_url(request.url)), code=307) else: g.user_config = Config(**session['config']) elif 'cookies_disabled' not in request.args: # Set session as permanent session.permanent = True app.permanent_session_lifetime = timedelta(days=365) g.user_config = Config(**session['config']) else: # User has cookies disabled, fall back to immutable default config session.pop('_permanent', None) g.user_config = Config(**default_config) if not g.user_config.url: g.user_config.url = get_request_url(request.url_root) g.user_request = Request( request.headers.get('User-Agent'), get_request_url(request.url_root), config=g.user_config) g.app_location = g.user_config.url # Attempt to reload bangs json if not generated yet if not bang_json and os.path.getsize(app.config['BANG_FILE']) > 4: try: bang_json = json.load(open(app.config['BANG_FILE'])) except json.decoder.JSONDecodeError: # Ignore decoding error, can occur if file is still # being written pass