From b80f39bd907c58fd4f80c446fecb004c2a1bc707 Mon Sep 17 00:00:00 2001 From: Lasse Server Date: Tue, 21 Sep 2021 22:17:58 +0200 Subject: [PATCH] Trying to solf the limit problem --- facebook/scrapers.py | 104 +++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/facebook/scrapers.py b/facebook/scrapers.py index a588b5d..d76e013 100644 --- a/facebook/scrapers.py +++ b/facebook/scrapers.py @@ -82,7 +82,7 @@ def profile_picture_reactions(profile, user, first_user=False, mode="all"): # Om det inte finns något profilalbum # Testa ta bort mellanrum och små bokstäver - if not hasattr(user, "url_album"): + if user.url_album == "": for a in profile.viewing().find_all("a", href=True): if "profilepictures" in a.text.lower().replace(" ", ""): user.url_album = url_bas + a["href"] @@ -91,7 +91,7 @@ def profile_picture_reactions(profile, user, first_user=False, mode="all"): # Gå till profilbilden (den första som kommer upp när man går till profilen) # Om profilen inte har profilalbum - if not hasattr(user, "url_album"): + if user.url_album == "": write_error(9, profile, soup=profile.viewing(), user=user) if user.url_other_pictures != []: # Använd eventuella extrabilder och ta bort den från användaren @@ -264,7 +264,7 @@ def check_picture(url_picture, user, profile): picture.no_reactions = re.search(r"total_count=(\d+)", url_limit).group(1) limit = re.search(r"limit=(\d+)", url_limit).group( 1 - ) # TODO Fortfarande problem med det här + ) except UnboundLocalError: # fel9 write_error( 9, @@ -285,52 +285,75 @@ def check_picture(url_picture, user, profile): try: if int(picture.no_reactions) > 50: no_reactions = 50 + elif int(picture.no_reactions) == 0: + no_reactions = 0 else: no_reactions = int(picture.no_reactions) - 1 except TypeError: + #print(picture.no_reactions, type(picture.no_reactions)) no_reactions = picture.no_reactions + #print('\nANTAL REAKTIONER TOTALT PÅ BILDEN:', picture.no_reactions) + url_limit = url_bas + url_limit.replace( "limit=" + str(limit), "limit=" + str(no_reactions) ) list_ids = [] + while True: - try: - sleep_(4) - profile.open(url_limit) - url_limit = "" - update_cookie(profile.browser.session.cookies, profile) - - # Hämta länk för "See more" för att se vilka ID:s som visas - url_see_more = None - for li in profile.viewing().find_all("li"): - if "seemore" in li.text.lower().replace(' ', '').replace('\n', ''): # Om det finns fler reaktioner att hämta - url_see_more = li.find('a')['href'] - ids_url = url_see_more[url_see_more.find('ids=')+4:url_see_more.find('&total')] - list_ids_picture = ids_url.split('%2C') - list_ids_picture = list_ids_picture[len(list_ids):] # Profilerna på den här sidan - list_ids.extend(list_ids_picture) # Alla profiler hittills - url_limit = url_bas + url_see_more.replace('limit=10', 'limit=50') # Länken till fler profiler - # Gå igenom alla som reagerat och för in i arango - - get_reactions(profile, user, picture, list_ids) - - if url_see_more == None: # När det inte finns fler reaktioner - break + #try: + sleep_(4) + #print('\nurl_limit'.upper(), url_limit, '\n') + profile.open(url_limit) + #url_limit = "" # Vad gjorde den här? + + update_cookie(profile.browser.session.cookies, profile) + + # Hämta länk för "See more" för att se vilka ID:s som visas + url_see_more = None + #print('\nVARJE LÄNK PÅ SIDAN') + for a in profile.viewing().find_all("a"): + #print(a) + if "See More" in a.text: # Om det finns fler reaktioner att hämta + #print('\nHITTADE "SEE MORE"\n') + + url_see_more = a['href'] + ids_url = url_see_more[url_see_more.find('ids=')+4:url_see_more.find('&total')] + list_ids_from_url = ids_url.split('%2C') # Alla IDs hittills + + #print('\nlist_pictures_from_url\n'.upper(), list_ids_from_url) # Lista från länk med profiler kollade hittills(?) + + list_ids_page = list_ids_from_url[len(list_ids):] # Profilerna på den här sidan + #print('\nlist_ids_picture\n'.upper(), list_ids_page) + + list_ids.extend(list_ids_page) #Lägg nästa sidas IDs till listan på alla IDs hittills + + # Sätt rätt limit för nästa sida + limit_next_page = int(picture.no_reactions) - len(list_ids_from_url) + if limit_next_page > 50: + limit_next_page = 50 + url_limit = url_bas + url_see_more.replace('limit=10', f'limit={limit_next_page}') # Länken till fler profiler + #print('\nurl_limit', url_limit, '\n') + + # Gå igenom alla som reagerat och för in i arango + get_reactions(profile, user, picture, list_ids_page) + + if url_see_more == None: # När det inte finns fler reaktioner + break - except Exception as e: # Fel2 - write_error( - 2, - profile, - e=e, - soup=profile.viewing(), - user=user, - url=url_limit, - url_name="url_limit", - traceback=traceback.format_exc(), - ) - pass + # except Exception as e: # Fel2 + # write_error( + # 2, + # profile, + # e=e, + # soup=profile.viewing(), + # user=user, + # url=url_limit, + # url_name="url_limit", + # traceback=traceback.format_exc(), + # ) + # pass # Lägg till reaktioner till databasen db.collection("picture_reactions").insert_many( @@ -343,7 +366,8 @@ def check_picture(url_picture, user, profile): # Uppdatera antalet reaktioner användaren fått user.reactions += len(picture.reactions) -def get_reactions(profile, user, picture, list_ids_picture): + +def get_reactions(profile, user, picture, list_ids_page): """ Gather the reactions on the picture. Args: @@ -354,6 +378,8 @@ def get_reactions(profile, user, picture, list_ids_picture): """ # Gå igenom alla som reagerat och för in i arango + #print('list_ids_picture: ', list_ids_page) + list_ids = list_ids_page.copy() for li in profile.viewing().find_all("li"): friend = Friend(user.username) if "seemore" in li.text.lower().replace(' ', '').replace('\n', ''): @@ -362,7 +388,7 @@ def get_reactions(profile, user, picture, list_ids_picture): friend_html = li.find("h3").find("a") friend.name = friend_html.text friend.url = friend_html["href"] - friend.id = list_ids_picture.pop(0) + friend.id = list_ids.pop(0) if "profile.php" in friend.url: if "&paipv" in friend.url: friend.username = friend.url[