From 5e991c0af39d8bb5f27dcfa87e1db2a56f78e8b8 Mon Sep 17 00:00:00 2001 From: Lasse Date: Mon, 26 Apr 2021 08:33:55 +0200 Subject: [PATCH] Added nowstamp etc --- facebook/helpers.py | 63 +++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/facebook/helpers.py b/facebook/helpers.py index 5c896b6..c27d960 100644 --- a/facebook/helpers.py +++ b/facebook/helpers.py @@ -28,7 +28,7 @@ def update_cookie(cookies, profile_name): pickle.dump(cookies, f) -def write_error(nr, e="", traceback="", soup="", user="", url="", url_name="", profile=""): +def write_error(nr, e=" ", traceback="", soup="", user="", url="", url_name="", profile=""): """Skriver info efter error till arango Args: @@ -36,21 +36,28 @@ def write_error(nr, e="", traceback="", soup="", user="", url="", url_name="", p e (str, optional): error. Defaults to "". traceback (str, optional): The traceback from traceback.format_exc(). Defaults to "". soup (str, optional): Soup. Defaults to "". - user (str, optional): The user. Defaults to "". + user (class, optional): The user. Defaults to "". url (str, optional): Url, if any. Defaults to "". count (int, optional): Count, if any. Defaults to 0. url_name (str, optional): The description of the url, if any. Defaults to "". + profile (user, optional): The profile. """ if url == "": url = "ingen url" url_name = "ingen url" - - # BARA VID FELSÖKNING - _print(profile.container, e) - _print(profile.container, traceback) - + try: + # BARA VID FELSÖKNING + print(profile, user, e) + print(profile, user, traceback.format_exc()) + _print(profile, user, e) + _print(profile, user, traceback.format_exc()) + except Exception as e: + print('Kunde inte skriva error print till databasen.') + print(e) + if "e" not in locals(): + e = 'Unknown error' doc = { - "_key": f"{now()}_{profile.container})", + "_key": f"{now()}_{profile.lookingup})", "number": nr, "error": nr, "user": str(user.username), @@ -58,9 +65,9 @@ def write_error(nr, e="", traceback="", soup="", user="", url="", url_name="", p "url": str(url), "url_name": url_name, "soup": str(soup), - "traceback": str(traceback), + "traceback": str(traceback).split('\n'), } - + try: db.insert_document( "errors", @@ -69,32 +76,48 @@ def write_error(nr, e="", traceback="", soup="", user="", url="", url_name="", p silent=True, ) except Exception as e: - _print(profile.container, user, e) + _print(profile, user, e) def now(): """ Returns current date and time as string""" return datetime.now().strftime("%Y-%m-%d_%H:%M:%S") - -def _print(container, user, text, end='\n', silent=False): +def nowstamp(): + """ Returns current date and time as timestamp""" + return int(datetime.now().timestamp()) + +def _print(profile, user, text, end='\n', silent=False, sleeptime=0): """ Write a "print" to the database (and prints in in the terminal) Args: - container (string): the value of profile.container, meaning the "original" user. + lookingup (string): the value of profile.lookingup, meaning the "original" user. user (class): The user username currelntly beeing looked up. text (string): What should be written. end (str, optional): The end value for print. Defaults to '\n'. silent (bool, optional): If a print should be done in the terminal. Defaults to False. """ - + from classes import User + if silent == False: print(text, end=end) - if user != '': - user = f"{user} - " + + if isinstance(user, User): + if isinstance(text, list): + to_print = {user.username: text} + else: + to_print = f"{user.username} - {text.strip()}" + + else: + if isinstance(text, list): + to_print = {user: text} + else: + to_print = f"{text.strip()}" + + db.insert_document( "prints", - {'_key': container, 'print':{now(): f"{user}{text.strip()}"}}, + {'_key': profile.lookingup, 'print':{now(): to_print}}, overwrite_mode="update", silent=True, - merge=True - ) \ No newline at end of file + ) + sleep(sleeptime) \ No newline at end of file