from datetime import datetime from typing import Dict, List from requests import Session import html2text import click def check_valid_key(obj, key) -> bool: # print("the keys are", obj.keys()) if key not in obj.keys(): print(f'The key: \"{key}\" is not in the object') return False else: return True def check_response(response) -> bool: invalid_statuscodes = [401, 403, 404] if response.status_code in invalid_statuscodes: print(response.json()['status']) print(response.json()['message']) return False else: return True def check_valid_date(cookies) -> bool: tmp = cookies['BbRouter'] start = int(tmp.find('expires')) + len('expires') + 1 end = int(tmp.find(',')) timestmp = int(tmp[start : end]) print(timestmp) expires = datetime.fromtimestamp(timestmp) now = datetime.now() if expires >= now: return True else: return False def set_cookies(session: Session, cookies: List): for cookie in cookies: session.cookies.set(cookie['name'], cookie['value']) def set_headers(session: Session, headers: List): for header in headers: session.headers.update(header) def html_to_text(html_data: str): to_text = html2text.HTML2Text() return to_text.handle(html_data) def input_body(): MARKER = '# Everything below is ignored. Leave blank if you want empty body.\n' body = click.edit('\n\n' + MARKER) if body is not None: body = body.split(MARKER, 1)[0].rstrip('\n') return body