Trying to solf the limit problem

master
Lasse Server 4 years ago
parent 1e8c3b7688
commit b80f39bd90
  1. 106
      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 # Om det inte finns något profilalbum
# Testa ta bort mellanrum och små bokstäver # 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): for a in profile.viewing().find_all("a", href=True):
if "profilepictures" in a.text.lower().replace(" ", ""): if "profilepictures" in a.text.lower().replace(" ", ""):
user.url_album = url_bas + a["href"] 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) # Gå till profilbilden (den första som kommer upp när man går till profilen)
# Om profilen inte har profilalbum # Om profilen inte har profilalbum
if not hasattr(user, "url_album"): if user.url_album == "":
write_error(9, profile, soup=profile.viewing(), user=user) write_error(9, profile, soup=profile.viewing(), user=user)
if user.url_other_pictures != []: if user.url_other_pictures != []:
# Använd eventuella extrabilder och ta bort den från användaren # 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) picture.no_reactions = re.search(r"total_count=(\d+)", url_limit).group(1)
limit = re.search(r"limit=(\d+)", url_limit).group( limit = re.search(r"limit=(\d+)", url_limit).group(
1 1
) # TODO Fortfarande problem med det här )
except UnboundLocalError: # fel9 except UnboundLocalError: # fel9
write_error( write_error(
9, 9,
@ -285,52 +285,75 @@ def check_picture(url_picture, user, profile):
try: try:
if int(picture.no_reactions) > 50: if int(picture.no_reactions) > 50:
no_reactions = 50 no_reactions = 50
elif int(picture.no_reactions) == 0:
no_reactions = 0
else: else:
no_reactions = int(picture.no_reactions) - 1 no_reactions = int(picture.no_reactions) - 1
except TypeError: except TypeError:
#print(picture.no_reactions, type(picture.no_reactions))
no_reactions = picture.no_reactions no_reactions = picture.no_reactions
#print('\nANTAL REAKTIONER TOTALT PÅ BILDEN:', picture.no_reactions)
url_limit = url_bas + url_limit.replace( url_limit = url_bas + url_limit.replace(
"limit=" + str(limit), "limit=" + str(no_reactions) "limit=" + str(limit), "limit=" + str(no_reactions)
) )
list_ids = [] 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
except Exception as e: # Fel2 while True:
write_error( #try:
2, sleep_(4)
profile, #print('\nurl_limit'.upper(), url_limit, '\n')
e=e, profile.open(url_limit)
soup=profile.viewing(), #url_limit = "" # Vad gjorde den här?
user=user,
url=url_limit, update_cookie(profile.browser.session.cookies, profile)
url_name="url_limit",
traceback=traceback.format_exc(), # Hämta länk för "See more" för att se vilka ID:s som visas
) url_see_more = None
pass #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
# Lägg till reaktioner till databasen # Lägg till reaktioner till databasen
db.collection("picture_reactions").insert_many( db.collection("picture_reactions").insert_many(
@ -343,7 +366,8 @@ def check_picture(url_picture, user, profile):
# Uppdatera antalet reaktioner användaren fått # Uppdatera antalet reaktioner användaren fått
user.reactions += len(picture.reactions) 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. """ Gather the reactions on the picture.
Args: 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 # 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"): for li in profile.viewing().find_all("li"):
friend = Friend(user.username) friend = Friend(user.username)
if "seemore" in li.text.lower().replace(' ', '').replace('\n', ''): 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_html = li.find("h3").find("a")
friend.name = friend_html.text friend.name = friend_html.text
friend.url = friend_html["href"] 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 "profile.php" in friend.url:
if "&paipv" in friend.url: if "&paipv" in friend.url:
friend.username = friend.url[ friend.username = friend.url[

Loading…
Cancel
Save