import pickle import random from datetime import datetime from time import sleep from arangodb import db def sleep_(t): """ Sover en tid nära den angivna (för att inte sökningarna ska bli för lika varandra) """ variation = 4 # Testa olika sovlängder för att inte få användaren blockerad sleep(t * variation * random.randrange(85, 115, 1) / 100) if random.randrange(0, 60, 1) == 1: longsleep = random.randrange(200, 300) print('') for s in range(0, longsleep): print(f"Sover {longsleep - s} sekunder till... ", end="\r") sleep(1) print() sleep(random.randrange(0, 10, 1) / 4) # TODO #1 spara cookies till db def update_cookie(cookies, profile_name): """ Uppdaterar cookie för browser """ with open("data/cookie_{}.pkl".format(profile_name), "wb") as f: pickle.dump(cookies, f) def write_error(nr, e="", traceback="", soup="", user="", url="", url_name="", profile=""): """Skriver info efter error till arango Args: nr ([type]): error number 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 "". 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 "". """ if url == "": url = "ingen url" url_name = "ingen url" # BARA VID FELSÖKNING _print(profile.container, e) _print(profile.container, traceback) doc = { "_key": f"{now()}_{profile.container})", "number": nr, "error": nr, "user": str(user.username), "error": str(e), "url": str(url), "url_name": url_name, "soup": str(soup), "traceback": str(traceback), } try: db.insert_document( "errors", doc, overwrite_mode="update", silent=True, ) except Exception as 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 )