Added nowstamp etc

pull/5/head
Lasse Edfast 5 years ago
parent 9c12415d31
commit 5e991c0af3
  1. 55
      facebook/helpers.py

@ -28,7 +28,7 @@ def update_cookie(cookies, profile_name):
pickle.dump(cookies, f) 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 """Skriver info efter error till arango
Args: Args:
@ -36,21 +36,28 @@ def write_error(nr, e="", traceback="", soup="", user="", url="", url_name="", p
e (str, optional): error. Defaults to "". e (str, optional): error. Defaults to "".
traceback (str, optional): The traceback from traceback.format_exc(). Defaults to "". traceback (str, optional): The traceback from traceback.format_exc(). Defaults to "".
soup (str, optional): Soup. 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 "". url (str, optional): Url, if any. Defaults to "".
count (int, optional): Count, if any. Defaults to 0. count (int, optional): Count, if any. Defaults to 0.
url_name (str, optional): The description of the url, if any. Defaults to "". url_name (str, optional): The description of the url, if any. Defaults to "".
profile (user, optional): The profile.
""" """
if url == "": if url == "":
url = "ingen url" url = "ingen url"
url_name = "ingen url" url_name = "ingen url"
try:
# BARA VID FELSÖKNING # BARA VID FELSÖKNING
_print(profile.container, e) print(profile, user, e)
_print(profile.container, traceback) 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 = { doc = {
"_key": f"{now()}_{profile.container})", "_key": f"{now()}_{profile.lookingup})",
"number": nr, "number": nr,
"error": nr, "error": nr,
"user": str(user.username), "user": str(user.username),
@ -58,7 +65,7 @@ def write_error(nr, e="", traceback="", soup="", user="", url="", url_name="", p
"url": str(url), "url": str(url),
"url_name": url_name, "url_name": url_name,
"soup": str(soup), "soup": str(soup),
"traceback": str(traceback), "traceback": str(traceback).split('\n'),
} }
try: try:
@ -69,32 +76,48 @@ def write_error(nr, e="", traceback="", soup="", user="", url="", url_name="", p
silent=True, silent=True,
) )
except Exception as e: except Exception as e:
_print(profile.container, user, e) _print(profile, user, e)
def now(): def now():
""" Returns current date and time as string""" """ Returns current date and time as string"""
return datetime.now().strftime("%Y-%m-%d_%H:%M:%S") return datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
def nowstamp():
""" Returns current date and time as timestamp"""
return int(datetime.now().timestamp())
def _print(container, user, text, end='\n', silent=False): def _print(profile, user, text, end='\n', silent=False, sleeptime=0):
""" Write a "print" to the database (and prints in in the terminal) """ Write a "print" to the database (and prints in in the terminal)
Args: 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. user (class): The user username currelntly beeing looked up.
text (string): What should be written. text (string): What should be written.
end (str, optional): The end value for print. Defaults to '\n'. 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. silent (bool, optional): If a print should be done in the terminal. Defaults to False.
""" """
from classes import User
if silent == False: if silent == False:
print(text, end=end) 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( db.insert_document(
"prints", "prints",
{'_key': container, 'print':{now(): f"{user}{text.strip()}"}}, {'_key': profile.lookingup, 'print':{now(): to_print}},
overwrite_mode="update", overwrite_mode="update",
silent=True, silent=True,
merge=True
) )
sleep(sleeptime)
Loading…
Cancel
Save