import pickle import random from datetime import datetime from time import sleep import json from config import url_bas 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, 50, 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): """ Uppdaterar cookie för browser """ # with open("data/cookie_{}.pkl".format(profile.name), "wb") as f: # pickle.dump(cookies, f) # cookies_dict = json.dumps(dict(cookies)) profile.update_cookie(cookies.get_dict()) def write_error(nr, profile, e=" ", traceback="", soup="", user="", url="", url_name=""): """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 (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" # 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})", "number": nr, "error": nr, "user": str(user.username), "error": str(e), "url": str(url), "url_name": url_name, "soup": str(soup), "traceback": str(traceback).split('\n'), } try: db.insert_document( "errors", doc, overwrite_mode="update", silent=True, ) except Exception as 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 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. 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 profile.write == False: return None if profile.container[:4] == 'leak' and len(profile.container) < 7: _key = f'{profile.container}_{now()[2:10]}' elif profile.container[:7] == 'lookups': _key = f'{profile.container}_{now()[2:10]}' else: _key = profile.container try: if isinstance(text, list): to_print = {user.username: text} else: to_print = f"{user.username} - {text.strip()}" except: if isinstance(text, list): to_print = {user: text} else: to_print = f"{text.strip()}" db.insert_document( "prints", {'_key': _key, 'print':{now(): to_print}}, overwrite_mode="update", silent=True, ) sleep(sleeptime) def check_profile_status(profile, user): if profile.browser._cursor == -1: # Om ingen sida har öppnats än. profile.open(url_bas) if any( [ "It looks like you were misusing this feature by going too fast." in profile.viewing().text, "Access Denied" in profile.viewing().text, "Your Account Has Been Disabled" in profile.viewing().text ] ): _print(profile, user, f"{profile.name} blocked\n".upper(), sleeptime=1) _print(profile, user, profile.viewing().text, sleeptime=1) profile.blocked = True # Nu tar jag bort dem, kan göras på annat sätt kanske? elif "accept all" in profile.viewing().text.lower(): profile.accept_cookies() sleep_(3) profile.open(user.url_photos) elif ( profile.viewing().find("title").text.strip() == "Log in to Facebook | Facebook" ): sleep_(5) profile.login() sleep_(5) profile.open(user.url_photos) return profile from arangodb import db