import random import traceback from getopt import GetoptError, getopt from sys import argv from time import sleep from subprocess import check_output from re import split from socket import gethostname from arangodb import * from classes import Profile, User from helpers import sleep_, write_error, _print from scrapers import profile_picture_reactions def blocked_profile(profile): """ Tar bort profilen som blivit blockad och returnerar en ny. """ report_blocked(profile) remove_profile(profile) return new_profile() def new_profile(): """ Hämtar en ny profil. """ profile = Profile(get_profile(), container) if profile.logged_in == False: profile.accept_cookies() sleep_(2) profile.login() return profile if __name__ == "__main__": print() # Hämta namn för containern där skriptet körs if gethostname() not in ['macbook.local']: # TODO Lägg till för studiodatorn try: containers = check_output(['docker', 'lookingup', 'ls']).decode() container = split('\W\W+', containers.split('\n')[1])[-1] except FileNotFoundError: pass else: container = 'macbook' # Argument och alternativ argv = argv[1:] try: opts, args = getopt(argv, "bm:u:o:", ['backup=',"mode=", "user=", "other="]) for o, a in opts: # mode_nr används för hur ofta profile ska roteras if o in ["-m", "--mode"]: mode = a.strip() if mode == 'single': mode_nr = 1.7 elif mode == 'few': mode_nr = 1.4 elif mode == 'force': mode_nr = 1 else: mode = 'all' mode_nr = 1 for o, a in opts: if o in ["-u", "--user"]: try: if a.strip()== 'leak': lookups = 'leak_lookups' user = get_user(collection='leak_lookups')['_key'] else: lookups = 'lookups' user = a user = User(str(user).strip(), mode) except StopIteration: raise Exception if o in ["-o", "--other"]: url_other_pictures = a.split(',') if o in ['-b', '--backup']: while True: backup(db) sleep(21600) if "user" not in globals(): lookups = 'lookups' user = User(str(input("Vem/vilka vill du kolla bilder för? ")).strip(), mode) except GetoptError: lookups = 'lookups' user = User(str(input("Vem/vilka vill du kolla bilder för? ")).strip(), mode) mode = input("Söka efter alla, första/sida eller första? (all, few, single)? ").lower().strip() if mode == '': mode = 'all' if "url_other_pictures" in globals(): l = [] for url in url_other_pictures: l.append(url[url.find('facebook.com') + 12:]) user.url_other_pictures = l # Hämta profil profile = new_profile() sleep(3) # Gå igenom de användare som efterfrågats #try: while True: if lookups == 'leak_lookups': profile.browser.open(url_bas + "/" + user.username) url = profile.browser.state.url.strip('/').strip('?_rdr') user = User(str(url[url.rfind('/') + 1:]).strip(), mode) sleep_(4) print(f"Kollar profilbilder för {user.username}") if 'container' not in globals: container = str(user.username) profile.container = container _print(profile, user, f"Börjar med profilen {profile.name}") profile.users_checked += 1 # Set för kollade bilder och kollade medlemmar #all_pictures = set([doc["_key"] for doc in db.collection("pictures").all()]) #all_pictures_start = all_pictures.copy() #members_checked = checked_members() # Hämta reaktioner för den första användaren if any([not check_for_user(user.username), mode == 'force']): try: while True: # Uppdatera in_use profile.update_time() profile = profile_picture_reactions(profile, user, first_user=True, mode=mode) if profile.blocked: profile = blocked_profile(profile) else: break except: _print(profile, user, traceback.format_exc()) friends = friends_of_user(user.username) _print(profile, user, f"\nKlar med, {user.username}\n") _print(profile, user, f"Vänner som reagerat: {len(friends)}") _print(profile, user, "\nVänner att kolla:") friends_unchecked = [] for friend in friends: if not check_for_user(friend): print(friend) friends_unchecked.append(friend) _print(profile, user, [friends_unchecked], silent=True) print() # Hämta reaktioner för users vänner (som reagerat) count_friends = 0 for friend in friends_unchecked: count_friends += 1 user = User(str(friend), mode, other_pictures=[]) sleep_(2) # Uppdatera in_use profile.update_time() try: p = profile_picture_reactions( profile, user, mode=mode ) if isinstance(p, Profile): profile = p except Exception as e: # Fel4 write_error( 4, profile, e=e, user=user, traceback=traceback.format_exc(), soup=profile.viewing(), ) _print(profile, user, f"\nFel: {str(user.username)}\n") sleep_(15) if not profile.blocked: _print(profile, user, f"Klar med {user.username} \n") # Rotera fb-profiler if count_friends > 2 * mode_nr: if random.randrange(0, 2, 1) == 1: profile = new_profile() count_friends = 0 _print(profile, user, f"Växlar till {profile.name}") elif count_friends > 4 * mode_nr: profile = new_profile() count_friends = 0 _print(profile, user, f"Växlar till {profile.name}") elif profile.blocked: _print(profile, user, f"Tar bort {profile.name}\n".upper(), sleeptime=1) profile = blocked_profile(profile) _print(profile, user, f"Växlar till {profile.name}") # Hämta ny användare från databasen när alla är genomgångna while True: user = get_user(collection=lookups) if user == None: sleep(300) else: user = User(str(user['_key']).strip(), mode) break