From 2569dfca7f4bb03ed77ab1673a4bd09a767fb87a Mon Sep 17 00:00:00 2001 From: Lasse Date: Sun, 28 Mar 2021 15:04:35 +0200 Subject: [PATCH] Added _print() --- facebook/helpers.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/facebook/helpers.py b/facebook/helpers.py index 1b6fa0c..5c896b6 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=""): +def write_error(nr, e="", traceback="", soup="", user="", url="", url_name="", profile=""): """Skriver info efter error till arango Args: @@ -45,14 +45,15 @@ def write_error(nr, e="", traceback="", soup="", user="", url="", url_name=""): url = "ingen url" url_name = "ingen url" - print(e) # FELSÖKNING + # BARA VID FELSÖKNING + _print(profile.container, e) + _print(profile.container, traceback) - key = datetime.now().strftime("%Y%m%d_%H:%M:%S") doc = { - "_key": key, + "_key": f"{now()}_{profile.container})", "number": nr, "error": nr, - "user": str(user), + "user": str(user.username), "error": str(e), "url": str(url), "url_name": url_name, @@ -68,5 +69,32 @@ def write_error(nr, e="", traceback="", soup="", user="", url="", url_name=""): silent=True, ) except Exception as e: - print(e) + _print(profile.container, 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): + """ Write a "print" to the database (and prints in in the terminal) + + Args: + container (string): the value of profile.container, 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. + """ + + if silent == False: + print(text, end=end) + if user != '': + user = f"{user} - " + db.insert_document( + "prints", + {'_key': container, 'print':{now(): f"{user}{text.strip()}"}}, + overwrite_mode="update", + silent=True, + merge=True + ) \ No newline at end of file