def _get_article_tag_from_url(url: str) -> Optional[ResultSet]: response = session.get(url) if response.status_code // 100 != 2: logger.critical( "Couldn't get response from habr.com - status code: %d", response.status_code, ) return None return _get_article_tag_from_html(response.text)
def test_session_cookie(): # Fake method to verify that we can get the session_id # from the request (cookie should have been set and sent along) # Get the server-side session based on the client-side session_id server_session_data = server_session.get(session.get(settings.SESSION_ID)) # Tokens should match assert server_session_data.get("access_token") == ACCESS_TOKEN assert server_session_data.get("id_token") == ID_TOKEN return jsonify({}), 200
def get_session_data_or_abort(): session_id = session[settings.SESSION_ID] _logger.debug("Found session_id %s in session, getting session data", session_id) session_data = server_session.get(session_id) if not session_data: _logger.warn("Cannot get session data, returning 401") abort(401) _logger.debug("Returning session data %s", session_data) return session_data
def get_session_data_or_none(): """ Return the server-side session data or None, based on the client-side session id. Server-side session is a dict containing an access_token, an id_token, and other information. """ try: session_data = server_session.get(session[settings.SESSION_ID]) return session_data except Exception: return None
def download_article(url: str, storage_path: Path) -> None: logger.debug('Thread sent request to %s', url) response = session.get(url) if response.status_code // 100 != 2: logger.critical( "Couldn't get response from habr.com - status code: %d", response.status_code, ) title, text, img_tags = _parse_article(response.text) logger.debug('Thread saving text and images') store_text_and_images(storage_path, title=title, text=text, img_tags=img_tags)
def store_text_and_images(storage_path: Path, title: str, text: str, img_tags: ResultSet) -> None: new_dir = storage_path / title new_dir.mkdir(exist_ok=True) filepath = new_dir / 'article.txt' with filepath.open('w', encoding='utf-8') as f: f.write(text) for image_num, tag in enumerate(img_tags, 1): logger.debug('Thread acquiring image %s', tag['src']) src = tag['src'] response = session.get(src, stream=True) img_type = src.split('.')[-1] logger.debug('Thread saving that image') img_path = new_dir / f'image-{image_num}.{img_type}' with img_path.open('wb') as img: shutil.copyfileobj(response.raw, img)
def _get_management_token(self): if self.management_session_id: # Get the payload from cache. Might be expired and not exist anymore. payload = server_session.get(self.management_session_id) if payload and "access_token" in payload: return payload["access_token"] payload = self._get_management_token_from_remote() access_token = payload.get("access_token", None) if not access_token: raise Exception("Could not find access_token in response") expires_in = payload.get("expires_in", None) session_uuid = uuid.uuid4().hex server_session.create_session(session_uuid, payload, expire_seconds=expires_in) self.management_session_id = session_uuid return access_token