import re import traceback from arangodb import db from classes import Friend, Picture, Reaction from config import * from helpers import sleep_, update_cookie, write_error def profile_picture_reactions(profile, user, all_pictures, first_user=False, mode = 'all'): # Fixa url:er osv if user.username.isnumeric(): user.url = url_bas + "/profile.php?id=" + str(user.username) user.url_photos = user.url + "&v=photos" else: user.username = user.username.replace("/", "") user.url = url_bas + "/" + user.username user.url_photos = user.url + "/photos" # 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("{} blocked\n".format(profile.name).upper()) profile.blocked = True return None elif 'accept all' in profile.viewing().text.lower(): profile.accept_cookies() profile.browser.open(user.url_photos) user.name = user.username # Om inte namnet hittas senare try: for i in profile.viewing().find_all("strong"): if "Notifications" in str(i): continue else: user.name = i.text.strip() except Exception as e: write_error( 6, e=e, traceback=traceback.format_exc(), soup=profile.viewing(), user=user.username, url=user.url_photos, ) if first_user == True: print(profile.viewing().prettify()) exit() print( "Hämtar reaktioner på profilbilder för {name} ({user})".format( name=user.name, user=user.username ) ) # Hitta länk till olika saker hos användarem, inkl facebook-id for a in profile.viewing().find_all("a", href=True): if "Profile pictures" in a.text: user.url_album = url_bas + a["href"] # Länk till album för profilbulder if "profile_id" in a["href"]: l = a["href"] try: user.id = re.search("\d+", l[l.find("id=") + 3 :]).group(0) except: user.id = False if "Likes" in a.text: user.url_likes = url_bas + a["href"] if "About" in a.text: user.url_about = url_bas + a["href"] if "Timeline" in a.text: user.url_timeline = url_bas + a["href"] if "Cover photos" in a.text: user.url_coverphotos = url_bas + a["href"] 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.username) 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 = '' 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('Hittar inget album för profilbilder.') write_error(7, soup=profile.viewing(), user=user.username, url=user.url_album, url_name='user.url_album') return None # ATT GÖRA Här kan andra bilder väljas istället else: # Normalfallet där användaren har profilbildsalbum profile.browser.open(user.url_album) # Samla alla profilbilder i en lista url_pics = [] 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) except: user.profile_pictures = 0 user.checked() user.add_to_db() return # 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(f"Bild {url_pics.index(pic) + 1} av {user.profile_pictures}", end="\r",) picture = Picture(user.username) picture.url = url_bas + pic picture.id = str(picture.url[picture.url.find("fbid=") + 5 :]) try: picture.id = str(re.search('\d+', picture.id).group()) except: pass # if picture.id in all_pictures: # continue sleep_(5) try: profile.browser.open(picture.url) except Exception as e: # Fel3 write_error( 3, e=e, soup=profile.viewing(), user=user.username, url=picture.url, url_name="url_pic", traceback=traceback.format_exc(), ) update_cookie(profile.browser.session.cookies, profile.name) # Hitta info om bilden try: picture.date = profile.viewing().find("abbr").text except Exception as e: # Fel8 write_error(8, e=e, soup=profile.viewing(), url=pic, url_name='picture url', user=user.name, traceback=traceback.format_exc()) # 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 userända sen #print(profile.viewing().prettify()) for a in profile.viewing().find_all("a", href=True): if all( [ "reaction" in a["href"], "reactions" not in a["href"], "=R" not in a["href"], ] ): url_reactions = url_bas + str(a["href"]) # Länk till reaktionerna för bilden elif a.text == "View full size": pic = url_bas + a["href"] picture.url_full = pic[ : pic.find("&") ] # Den fullständiga adressen till bilden, används som _key i pictures if 'url_reactions' not in locals(): for a in profile.viewing().find_all("a", href=True): if '/likes/' in a["href"]: url_reactions = url_bas + str(a["href"]) if 'url_reactions' not in locals(): for div in profile.viewing().find_all("div", href=True): if 'like this' in div.text: url_reactions = url_bas + str(div["href"]) # Hämta reaktioner för bilden sleep_(3) profile.browser.open(url_reactions) update_cookie(profile.browser.session.cookies, profile.name) try: for a in profile.viewing().find_all("a", {"class": "z ba"}, href=True): url_limit = a["href"] picture.no_reactions = re.search(r"total_count=(\d+)", url_limit).group(1) limit = re.search(r"limit=(\d+)", url_limit).group(1) except UnboundLocalError: #fel9 write_error(9, 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? continue # Addera bilden till arrango picture.add_to_db() url_limit = url_bas + url_limit.replace( "limit=" + str(limit), "limit=" + str(picture.no_reactions) ) try: sleep_(4) profile.browser.open(url_limit) url_limit = '' update_cookie(profile.browser.session.cookies, profile.name) # Gå igenom alla som reagerat och för in i arango for li in profile.viewing().find_all("li"): friend = Friend(user.username, mode) if "See more" in li.text: continue try: friend_html = li.find("h3").find("a") friend.name = friend_html.text friend.url = friend_html["href"] if "profile.php" in friend.url: friend.username = friend.url[friend.url.find("id=") + 3 :] else: friend.username = friend.url[friend.url.find("/") + 1 :] reaction = Reaction(user.username, friend.username, picture.id) for type in ["Love", "Wow", "Like", "Care", "Sad", "Angry", "Haha"]: if type in str(li): reaction.type = type picture.reactions.append(reaction.get_dict()) # Lägg till vännens profil till arrango friend.add_to_db() except AttributeError as e: # Fel1 write_error( 1, e=e, soup=profile.viewing(), user=user.username, traceback=traceback.format_exc(), ) pass # Lägg till reaktion till databasen db.collection("picture_reactions").insert_many( picture.reactions, silent=True, overwrite=True ) db.collection("picture_reactions").insert_many(picture.reactions, silent=True, overwrite=True) except Exception as e: # Fel2 write_error( 2, e=e, soup=profile.viewing(), user=user.username, url=url_limit, url_name="url_limit", traceback=traceback.format_exc(), ) pass user.checked()