diff --git a/facebook/__main__.py b/facebook/__main__.py index 3af1d13..fe0fee7 100644 --- a/facebook/__main__.py +++ b/facebook/__main__.py @@ -7,19 +7,52 @@ from subprocess import check_output from re import split from socket import gethostname -from arangodb import db, write_report, backup, report_blocked, get_profile, remove_profile, checked_members, friends_of_user +from arangodb import * from classes import Profile, User from helpers import sleep_, write_error, _print from scrapers import profile_picture_reactions -def finish(): +def finish(profile, profiles, users): """ Avslutar: skriver rapport och gör profilerna oanvända """ for profile in profiles: profile.unused() - write_report(users, list(all_pictures.difference(all_pictures_start))) + try: + e = traceback.format_exc() + except: + e = 'Unknown traceback.' + _print(profile, None, e.split('\n')) + write_report(users) exit() + +def blocked_profile(profile, profiles): + """ Tar bort profilen från listan med profiles, tar bort ur databasen och returnerar uppdaterad lista. """ + + report_blocked(profile, users) + remove_profile(profile) + # Ta bort från listan på fb-profiler som används + profiles.remove(profile) + # Försök lägga till en ny fb-profil (om det finns en skapad och ledig i databasen) + try: + doc = get_profile() + profiles.append(Profile(doc, lookingup)) + _print(profile, None, f"Laddat ny profil: {profiles[-1].name}") + sleep(3) + except: + _print(profile, None, "Det behövs nya profiler...") + if len(profiles) == 0: + finish(profile, profiles, users) + for s in range(0, int(1600 / len(profiles))): + print(user, f"Sover {600-s} sekunder till... ", end="\r") + _print(profile, None, 'Långsover...') + for s in range (3600, 0, -1): + print(f'Sover {s} sekunder till...', end='\r') + sleep(1) + + return profiles + + if __name__ == "__main__": print() @@ -27,12 +60,12 @@ if __name__ == "__main__": if gethostname() not in ['macbook.local']: # Lägg till för studiodatorn # Hämta namn för containern där skriptet körs try: - containers = check_output(['docker', 'container', 'ls']).decode() - container = split('\W\W+', containers.split('\n')[1])[-1] + lookingups = check_output(['docker', 'lookingup', 'ls']).decode() + lookingup = split('\W\W+', lookingups.split('\n')[1])[-1] except FileNotFoundError: pass else: - container_name = 'macbook' + lookingup_name = 'macbook' # Argument och alternativ argv = argv[1:] @@ -41,7 +74,7 @@ if __name__ == "__main__": for o, a in opts: # mode_nr används för hur ofta profile ska roteras if o in ["-m", "--mode"]: - mode = a + mode = a.strip() if mode == 'single': mode_nr = 1.7 elif mode == 'few': @@ -62,7 +95,7 @@ if __name__ == "__main__": except StopIteration: raise Exception if o in ["-o", "--other"]: - url_other_picture = a + url_other_pictures = a.split(',') if o in ['-b', '--backup']: while True: backup(db) @@ -85,26 +118,29 @@ if __name__ == "__main__": if mode == '': mode = 'all' - if "url_other_picture" in globals(): - users[0].url_other_picture = url_other_picture[url_other_picture.find('facebook.com') + 12:] + if "url_other_pictures" in globals(): + l = [] + for url in url_other_pictures: + l.append(url[url.find('facebook.com') + 12:]) + users[0].url_other_pictures = l print("Kollar profilbilder för:") for user in users: print("-", user.username) print() - if 'container' not in globals(): + if 'lookingup' not in globals(): usernames = [user.username for user in users] if len(usernames) == 1: - container = usernames[0] + lookingup = usernames[0] else: - container = '-'.join(usernames) + lookingup = '-'.join(usernames) # Skapa tre olika profiler att besöka Facebook med profiles = [] for i in range(0, 3): doc = get_profile() - profile = Profile(doc, container) + profile = Profile(doc, lookingup) profile.browser.open("https://api.ipify.org") print(f"Profil {profile.name} använder IP-adress {profile.viewing().text}." ) @@ -116,106 +152,123 @@ if __name__ == "__main__": print() sleep(3) - profile_nr = 1 + profile_nr = 0 profile = profiles[profile_nr] - _print(profile.container, user.username, f"Börjar med profilen {profile.name}") + _print(profile, user, f"Börjar med profilen {profile.name}") # Gå igenom de användare som efterfrågats - try: - while True: - for user in users: - # 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() - profile.container = user.username - - # Hämta reaktioner för den första användaren - if any([user.username not in members_checked, mode == 'force']): - try: - profile_picture_reactions(profile, user, all_pictures, first_user=True, mode=mode) - except: - _print(profile.container, user.username, traceback.format_exc()) - if len(users) == 1: - for profile in profiles: - profile.unused() - friends = friends_of_user(user.username) - friends_unchecked = list(set(friends) - set(members_checked)) - - _print(profile.container, user.username, f"\nKlar med, {user.username}\n") - _print(profile.container, user.username, f"Vänner som reagerat: {len(friends)}") - _print(profile.container, user.username, "\nVänner att kolla:") - - for friend in friends_unchecked: - print(friend) - _print(profile.container, user.username, ', '.join([friend for friend in 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) - sleep_(2) + #try: + while True: + for user in users: + 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() + for p in profiles: + p.lookingup = user.username + # Hämta reaktioner för den första användaren + if any([user.username not in members_checked, mode == 'force']): + try: + t = 0 + while t < 5: + t += 1 + profile = profile_picture_reactions(profile, user, all_pictures, first_user=True, mode=mode) + if profile.blocked == True: + profiles = blocked_profile(profile, profiles) + else: + break + except: + _print(profile, user, traceback.format_exc()) + if len(users) == 1: + for p in profiles: + p.unused() + friends = friends_of_user(user.username) + friends_unchecked = list(set(friends) - set(members_checked)) + _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:") + + for friend in friends_unchecked: + print(friend) + _print(profile, user, [friend for friend in friends_unchecked], silent=True) + print() + + # Hämta reaktioner för users vänner (som reagerat) + count_friends = 0 + for friend in friends_unchecked: + profile.users_checked += 1 + if profile.users_checked > 30: + _print(profile, None, 'Långsover...') + for s in range(2600, 0, -1): + print(f'Sover {s} sekunder till...', end='\r') + sleep(1) + for p in profiles: + _print(p, None, f'Långsovit. {p.name} hade kollat {p.users_checked} användare.') + p.users_checked = 0 + count_friends += 1 + user = User(str(friend), mode, other_pictures=[]) + sleep_(2) + + try: + p = profile_picture_reactions( + profile, user, all_pictures, mode=mode + ) + if isinstance(p, Profile): + profile = p + + except Exception as e: # Fel4 + write_error( + 4, + e=e, + user=user, + profile=profile, + traceback=traceback.format_exc(), + soup=profile.viewing(), + ) + _print(profile, user, f"\nFel: {str(user.username)}\n") + sleep_(15) - try: - profile_picture_reactions( - profile, user, all_pictures, mode=mode - ) - except Exception as e: # Fel4 - write_error( - 4, - e=e, - user=user.username, - profile=profile.container, - traceback=traceback.format_exc(), - soup=profile.viewing(), - ) - _print(profile.container, user.username, f"\nFel: {str(user.username)}\n") - sleep_(15) - - if profile.blocked == False: - _print(profile.container, user.username, f"Klar med {user.username} \n") - - # Rotera fb-profiler - if count_friends > 5 * mode_nr: - if random.randrange(0, 2, 1) == 1: - profile_nr += 1 - count_friends = 0 - _print(profile.container, user.username, f"Växlar till {profiles[profile_nr].name}") - elif count_friends > 9 * mode_nr: + if profile.blocked == False: + _print(profile, user, f"Klar med {user.username} \n") + + # Rotera fb-profiler + if count_friends > 5 * mode_nr: + if random.randrange(0, 2, 1) == 1: profile_nr += 1 + if profile_nr >= len(profiles): + profile_nr = 0 count_friends = 0 - _print(profile.container, user.username, f"Växlar till {profiles[profile_nr].name}") - - if profile_nr > len(profiles) - 1: - profile_nr = 0 - - elif profile.blocked == True: - # Ta bort profilen ur databasen - report_blocked(profile, users) - remove_profile(profile.doc) - # Ta bort från listan på fb-profiler som används - profiles.remove(profile) - # Försök lägga till en ny fb-profil (om det finns en skapad och ledig i databasen) - try: - doc = get_profile() - profiles[profile_nr] = Profile(doc, container) - _print(profile.container, user.username, f"Laddat ny profil: {profiles[profile_nr].name}") - sleep(3) - except e: - _print(profile.container, user.username, "Det behövs nya profiler...") - if len(profiles) == 0: - break - for s in range(0, 1600 / len(profiles)): - print(user, f"Sover {600-s} sekunder till... ", end="\r") + _print(profile, user, f"Växlar till {profiles[profile_nr].name}") + elif count_friends > 9 * mode_nr: profile_nr += 1 - _print(profile.container, user.username, f"Försöker med {profiles[profile_nr].name}.") + if profile_nr >= len(profiles): + profile_nr = 0 + count_friends = 0 + _print(profile, user, f"Växlar till {profiles[profile_nr].name}") + profile = profiles[profile_nr] + elif profile.blocked == True: + _print(profile, user, f"Tar bort {profile.name}\n".upper(), sleeptime=1) + profiles = blocked_profile(profile, profiles) + profile_nr =len(profiles) -1 profile = profiles[profile_nr] + _print(profile, None, [p.name for p in profiles]) + + + # Hämta ny användare från databasen (lookups) när alla är genomgångna + doc_new_user = get_user() + if doc_new_user == None: + _print(profile, user, 'No more to look up.') + finish(profile, profiles, users) + else: + new_user = User(doc_new_user['_key'], mode, doc_new_user['other']) + for p in profiles: + p.lookingup = new_user.username + users.append(new_user) - - except: - finish() + + # except: + # finish(profile, profiles, users) \ No newline at end of file diff --git a/facebook/arangodb.py b/facebook/arangodb.py index 5e24e6d..9a0122b 100644 --- a/facebook/arangodb.py +++ b/facebook/arangodb.py @@ -32,8 +32,7 @@ except FileNotFoundError: db = ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, password=pwd) -from helpers import now, _print - +from helpers import now, _print, nowstamp def checked_members(): cursor = db.aql.execute( @@ -59,34 +58,29 @@ def count_docs(col): return cursor.next() def report_blocked(profile, users): - db.insert_document({ - '_id':f'reports/{now()}', + db.insert_document('reports', { + '_key':now(), 'profile': profile.name, 'users': [user.username for user in users], - }) + }, overwrite=True) -def write_report(users, pictures): - db.insert_document({ - '_id':f'reports/{now()}', - 'users': [user.username for user in users], - 'members': count_docs('members'), - 'total_picture_reactions':count_docs('picture_reactions'), - 'pictures':count_docs('pictures'), - 'new_pictures': pictures - }) +def write_report(users): + db.insert_document('reports', { + '_key':now(), + 'users': [user.username for user in users] + }, overwrite=True) -def get_profile(created=True): +def get_profile(db=db): """ Hämtar profil från profiles """ cursor = db.aql.execute( """ FOR doc IN profiles - FILTER doc.in_use == false - FILTER doc.created == @created + FILTER doc.in_use < @inuse RETURN doc """, - bind_vars={'created': created} + bind_vars={'inuse': nowstamp() - 600} ) return cursor.next() @@ -105,12 +99,35 @@ def friends_of_user(user): def remove_profile(profile): - db.collection("profiles").delete(profile['_key'], silent=True, ignore_missing=True) - _print(profile.container, f'{profile.name} blockerad och borttagen {now()}.' + + db.collection("profiles").delete(profile.doc['_key'], silent=True, ignore_missing=True) + _print(profile, None, f'{profile.name} blockerad och borttagen {now()}.' ) # TODO #2 Bättre funktion för backup av databasen +def arango_connect(pwd): + return ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, password=pwd) + +def get_user(): + """ Hämtar användare att kolla upp från lookups """ + cursor = db.aql.execute( + """ + FOR doc IN lookups + RETURN doc + """ + ) + try: + doc = cursor.next() + if 'other' not in doc: + doc['other'] = [] + db.collection("lookups").delete(doc['_key']) + except StopIteration: + doc = None + + return doc + + def backup(db): """Skapar en json-backup för specificerade collections. @@ -127,5 +144,13 @@ def backup(db): json.dump(d, f) print(f'Senaste backup: {now()}') +def used_servers(): + cursor = db.aql.execute( + """ + FOR doc IN profiles + RETURN doc._key + """ + ) + return [doc for doc in cursor] db = ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, password=pwd) diff --git a/facebook/classes.py b/facebook/classes.py index b69df6d..bc82b0d 100644 --- a/facebook/classes.py +++ b/facebook/classes.py @@ -11,25 +11,30 @@ from robobrowser import RoboBrowser from arangodb import db from config import * -from helpers import sleep_, update_cookie +from helpers import sleep_, update_cookie, write_error, nowstamp class User: - def __init__(self, username, mode): + def __init__(self, username, mode, other_pictures=[]): self.collection = "members" self.username = str(username) self.mode = mode self.fetched = datetime.now().strftime("%Y%m%d") - self.url_coverphotos = '' - self.id = '' - self.url_likes = '' - self.url_about = '' - self.url_timeline = '' - self.profile_pictures = '' - self.url = '' - self.name = '' - self.url_other_picture = '' + self.url_coverphotos = "" + self.id = "" + self.url_likes = "" + self.url_about = "" + self.url_timeline = "" + self.url_profilepictures = "" + self.profile_pictures = 0 + self.pictures = [] + self.url_friends = "" + self.url = "" + self.name = "" + self.url_other_pictures = other_pictures self.reactions = 0 + self.profile_pictures = 0 + self.checked_pictures = [] def add_to_db(self): # Lägg till profilen till arrango @@ -47,33 +52,35 @@ class User: "cover photos": self.url_coverphotos, "fetched": self.fetched, "reactions": self.reactions, - 'mode': self.mode + "mode": self.mode, + "pictures": self.pictures, }, overwrite_mode="update", - silent=True, - keep_none=False + silent=True, + keep_none=False, ) def checked(self): db.update_document( { - "_id": "members/" + str(self.username), - "checked": True, - "pictures_checked": self.profile_pictures, - "reaction": self.reactions - }) - + "_id": "members/" + str(self.username), + "checked": True, + "pictures_checked": len(self.checked_pictures), + "checked_pictures": self.checked_pictures, + "reactions": self.reactions, + } + ) class Picture: def __init__(self, user): self.collection = "pictures" self.user = user - self.id = '' - self.url_full = '' - self.date = '' - self.url = '' - self.no_reactions = '' + self.id = "" + self.url_full = "" + self.date = "" + self.url = "" + self.no_reactions = "" self.reactions = [] def add_to_db(self): @@ -88,22 +95,23 @@ class Picture: "user": self.user, }, overwrite_mode="update", - silent=True, - keep_none=False + silent=True, + keep_none=False, ) + class Profile: - def __init__(self, profile, container): - """ Creates a new profile to do searches with. + def __init__(self, profile, lookingup): + """Creates a new profile to do searches with. Args: profile (dict): Document fetched from database. - container (string): Container name. - """ + lookingup (string): Container name. + """ # Uppdatera dokumentet i arango self.doc = profile - self.doc['in_use'] = True + self.doc["in_use"] = nowstamp() db.update_document(self.doc, check_rev=False) # Användaruppgifter @@ -111,9 +119,12 @@ class Profile: self.email = self.doc["email"] self.pwd = self.doc["pwd"] self.server = self.doc["server"] - + self.cookie = self.doc["cookie"] + self.useragent = self.doc["useragent"] + self.blocked = False - self.container = container + self.lookingup = lookingup + self.users_checked = 0 # Ange proxies session = requests.Session() @@ -123,7 +134,8 @@ class Profile: } # Starta browser - user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1" + # user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1" + user_agent = self.useragent self.browser = RoboBrowser( session=session, user_agent=user_agent, history=False, parser="lxml" ) @@ -133,20 +145,24 @@ class Profile: ) self.logged_in = True except: - self.logged_in = False + try: + self.browser.session.cookies.update(self.cookie) + print(self.browser.session.cookies.values) + self.logged_in = True + except: + self.logged_in = False def viewing(self): """ Returnerar browser i html-format """ return self.browser.parsed - def accept_cookies(self): """ Accepterar cookies """ self.browser.open("https://mbasic.facebook.com") soup = BeautifulSoup(str(self.browser.parsed), "lxml") - if 'accept all' not in soup.text.lower(): + if "accept all" not in soup.text.lower(): sleep_(2) - cookie_accept_url = "https://mbasic.facebook.com/cookie/consent-page" + cookie_accept_url = "https://mbasic.facebook.com/cookie/consent-page" self.browser.open(cookie_accept_url) sleep_(2) try: @@ -155,9 +171,10 @@ class Profile: print(f"Accepterade cookies för {self.name}") sleep_(2) update_cookie(self.browser.session.cookies, self.name) - except Exception as e: + except: + write_error(12, soup=self.browser.parsed, profile=self.name) print(f"Accepterade inte cookies för {self.name}") - + def login(self): """ Loggar in på Facebook. """ @@ -168,43 +185,48 @@ class Profile: # Kolla om browser redan är inloggad soup = BeautifulSoup(str(self.browser.parsed), "lxml") - if 'log out' in soup.text.lower(): + if "log out" in soup.text.lower(): print("Redan inloggad.") - - # Hitta och fyll i formulär - form = self.browser.get_form(id="login_form") - form["email"].value = self.email - form["pass"].value = self.pwd - self.browser.submit_form(form, submit=form["login"]) - # Vänta lite och uppdatera cookie - print("Loggade in.") - sleep_(2) + try: + # Hitta och fyll i formulär + form = self.browser.get_form(id="login_form") + form["email"].value = self.email + form["pass"].value = self.pwd + self.browser.submit_form(form, submit=form["login"]) + # Vänta lite och uppdatera cookie + print("Loggade in.") + sleep_(2) + except TypeError: + write_error(11, soup=soup, profile=self.name) def unused(self): """ Sätter user till False för valda profiler """ self.doc["in_use"] = False - db.update_document(self.doc, silent=True) + db.update_document(self.doc, silent=True, check_rev=False) + class Proxies: def __init__(self): self.proxies = [ - 'gb25-wg.socks5.mullvad.net:1080', - 'gb26-wg.socks5.mullvad.net:1080', - 'gb27-wg.socks5.mullvad.net:1080', - 'gb28-wg.socks5.mullvad.net:1080', - 'gb29-wg.socks5.mullvad.net:1080' - ] + "gb25-wg.socks5.mullvad.net:1080", + "gb26-wg.socks5.mullvad.net:1080", + "gb27-wg.socks5.mullvad.net:1080", + "gb28-wg.socks5.mullvad.net:1080", + "gb29-wg.socks5.mullvad.net:1080", + ] + def get_proxie(self): return self.proxies.pop(random.randrange(0, len(self.proxies), 1)) + class Friend: def __init__(self, user): self.collection = "members" self.user = user # The friends friend - self.username = '' - self.url = '' - self.name = '' - self.single = '' + self.username = "" + self.url = "" + self.name = "" + self.single = "" def add_to_db(self): db.insert_document( @@ -237,3 +259,4 @@ class Reaction: "picture": self.picture_id, "reaction": self.type, } + diff --git a/facebook/scrapers.py b/facebook/scrapers.py index 185d0b2..29fedfc 100644 --- a/facebook/scrapers.py +++ b/facebook/scrapers.py @@ -10,7 +10,7 @@ from helpers import sleep_, update_cookie, write_error, _print def profile_picture_reactions( profile, user, all_pictures, first_user=False, mode="all" ): - + #try: # Fixa url:er osv if user.username.isnumeric(): user.url = url_bas + "/profile.php?id=" + str(user.username) @@ -22,20 +22,21 @@ def profile_picture_reactions( # Gå till sidan för profilbilder profile.browser.open(user.url_photos) - sleep_(4) - if ( - """You can't use Facebook because your account, or activity on it, doesn't follow our Community Standards.""" - in profile.viewing().text - ): - _print(profile.container, user.username, f"{profile.name} blocked\n".upper()) + if """You can't use Facebook because your account, or activity on it, doesn't follow our Community Standards.""" in profile.viewing(): + _print(profile, user, f"{profile.name} blocked\n".upper(), sleeptime=1) profile.blocked = True - return "blocked" - + return profile + elif 'It looks like you were misusing this feature by going too fast.' in profile.viewing().text: + sleep_(600) elif "accept all" in profile.viewing().text.lower(): profile.accept_cookies() profile.browser.open(user.url_photos) + elif profile.viewing().find('title').text.strip() == 'Log in to Facebook | Facebook': + profile.login() + sleep_(5) + profile.browser.open(user.url_photos) user.name = user.username # Om inte namnet hittas senare try: @@ -49,15 +50,15 @@ def profile_picture_reactions( 6, e=e, traceback=traceback.format_exc(), - profile=profile.container, + profile=profile, soup=profile.viewing(), user=user, url=user.url_photos, ) if first_user == True: - _print(profile.container, user.username, profile.viewing().prettify()) + _print(profile, user, profile.viewing().prettify()) exit() - _print(profile.container, user.username, f"Hämtar reaktioner på profilbilder för {user.name} ({user.username})") + _print(profile, user, f"Hämtar reaktioner på profilbilder för {user.name} ({user.username})") # Hitta länk till olika saker hos användarem, inkl facebook-id @@ -79,6 +80,8 @@ def profile_picture_reactions( user.url_timeline = url_bas + a["href"] if "Cover photos" in a.text: user.url_coverphotos = url_bas + a["href"] + if a.text == "Friends": + user.url_friends = url_bas + a["href"] # Om det inte finns något profilalbum # Testa ta bort mellanrum och små bokstäver @@ -90,78 +93,79 @@ def profile_picture_reactions( user.add_to_db() # Gå till profilbilden (den första som kommer upp när man går till profilen) - if not hasattr(user, "url_album"): # Om profilen inte har profilalbum - write_error(9, soup=profile.viewing(), user=user, profile=profile.container) - if user.url_other_picture != "": - # Använd eventuell extrabild och ta bort den från användaren - url_pics = [user.url_other_picture] - user.url_other_picture = "" + # Om profilen inte har profilalbum + if not hasattr(user, "url_album"): + write_error(9, soup=profile.viewing(), user=user, profile=profile) + if user.url_other_pictures != []: + # Använd eventuella extrabilder och ta bort den från användaren + url_pics = user.url_other_pictures + user.url_other_pictures = [] else: # Spara ner profilen till databasen och avsluta sökningen på användaren user.url_album = False if first_user == False: user.checked() user.add_to_db() - _print(profile.container, user.username, "Hittar inget album för profilbilder.") + _print(profile, user, "Hittar inget album för profilbilder.") write_error(#fel7 7, soup=profile.viewing(), - profile=profile.container, + profile=profile, user=user, url=user.url_album, url_name="user.url_album", ) - return None + return profile # ATT GÖRA Här kan andra bilder väljas istället - - else: # Normalfallet där användaren har profilbildsalbum + + # Normalfallet där användaren har profilbildsalbum + else: profile.browser.open(user.url_album) # Samla alla profilbilder i en lista - url_pics = [] + url_pics = user.url_other_pictures pics = profile.viewing().find("div", {"id": "thumbnail_area"}) for i in pics.find_all("a"): a = i["href"] url_pics.append(a[: a.find("&id")]) - if user.url_other_picture != "": - # Lägg till eventuell extrabild och ta bort den från användaren - url_pics.append(user.url_other_picture) - user.url_other_picture = "" try: user.profile_pictures = len(url_pics) + user.pictures = url_pics except: + _print(profile, user, 'Hittade inga profilbilder'.upper()) user.profile_pictures = 0 + user.pictures = url_pics user.checked() user.add_to_db() - return + return profile # Lägg till profilen till arrango. user.add_to_db() - # Välj vilja bilder som ska kollas. - if first_user == False: - if mode == "single": - url_pics = url_pics[:1] - elif mode == "few" and len(url_pics) > 1: - url_pics = url_pics[:1] + url_pics[-1:] - # Gå igenom valda bilder. for pic in url_pics: # Skriv ut vilken bild som behandlas. - _print(profile.container, user.username, + _print(profile, user, f"Bild {url_pics.index(pic) + 1} av {user.profile_pictures}", end="\r", ) check_picture(url_bas + pic, user, profile) + user.checked_pictures.append(url_bas + pic) # Välj vilja bilder som ska kollas. if first_user == False: if mode == "single" and user.reactions > 30: break - elif all([mode == "few", user.reactions > 50, pic != url_pics[-1]]): + elif all([mode == "few", user.reactions > 80, pic != url_pics[-1]]): # Kolla den sista bilder check_picture(url_bas + url_pics[-1], user, profile) + user.checked_pictures.append(url_bas + pic) break user.checked() + return profile + + # except Exception as e: + # _print(profile, None, str(e)) + # return profile def check_picture(url_picture, user, profile): @@ -184,7 +188,7 @@ def check_picture(url_picture, user, profile): write_error( 3, e=e, - profile=profile.container, + profile=profile, soup=profile.viewing(), user=user, url=picture.url, @@ -202,7 +206,7 @@ def check_picture(url_picture, user, profile): 8, e=e, soup=profile.viewing(), - profile=profile.container, + profile=profile, url=picture.url, url_name="picture url", user=user, @@ -211,7 +215,7 @@ def check_picture(url_picture, user, profile): # TODO #3 lägg till fler bilder som kan gås igenom om det är få profilbilder. # Hämta länkar för bilden att anvrända sen - # _print(profile.container, user.username, profile.viewing().prettify()) + # _print(profile, user, profile.viewing().prettify()) for a in profile.viewing().find_all("a", href=True): if all( [ @@ -256,14 +260,14 @@ def check_picture(url_picture, user, profile): write_error( 9, user=user, - profile=profile.container, + profile=profile, soup=profile.viewing(), traceback=traceback.format_exc(), url=url_reactions, url_name="url_reactions", ) # Bilder med väldigt många likes går inte att visa så här? - return None + return profile # Addera bilden till arrango picture.add_to_db() @@ -306,7 +310,7 @@ def check_picture(url_picture, user, profile): e=e, soup=str(li), user=user, - profile=profile.container, + profile=profile, traceback=traceback.format_exc(), ) pass @@ -326,7 +330,7 @@ def check_picture(url_picture, user, profile): 2, e=e, soup=profile.viewing(), - profile=profile.container, + profile=profile, user=user, url=url_limit, url_name="url_limit",