From 34e57439b5c8f0d4f3ee57aef3e54296c6291bc6 Mon Sep 17 00:00:00 2001 From: Qulu Date: Wed, 15 Aug 2018 18:57:58 +0400 Subject: [PATCH 1/3] Fix: convert exception messages into a string before encoding --- instapy/relationship_tools.py | 28 ++++++++++++++-------------- instapy/unfollow_util.py | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/instapy/relationship_tools.py b/instapy/relationship_tools.py index 0179a469e..d36636309 100644 --- a/instapy/relationship_tools.py +++ b/instapy/relationship_tools.py @@ -103,7 +103,7 @@ def get_followers(browser, sc_rolled_all = graphql_queries[username][query_date]["sc_rolled"] except Exception as exc: - logger.info("Error occured while getting `scroll` data from graphql_queries.json\n{}\n".format(exc.encode("utf-8"))) + logger.info("Error occured while getting `scroll` data from graphql_queries.json\n{}\n".format(str(exc).encode("utf-8"))) local_read_failure = True start_time = time.time() @@ -171,7 +171,7 @@ def get_followers(browser, json.dump(graphql_queries, graphql_queries_file) except Exception as exc: print('\n') - logger.info("Error occured while writing `scroll` data to graphql_queries.json\n{}\n".format(exc.encode("utf-8"))) + logger.info("Error occured while writing `scroll` data to graphql_queries.json\n{}\n".format(str(exc).encode("utf-8"))) #take breaks gradually if sc_rolled > 91: @@ -182,7 +182,7 @@ def get_followers(browser, except BaseException as exc: print('\n') - logger.info("Unable to get `Followers` data:\n\t{}\n".format(exc.encode("utf-8"))) + logger.info("Unable to get `Followers` data:\n\t{}\n".format(str(exc).encode("utf-8"))) #remove possible duplicates all_followers = sorted(set(all_followers), key=lambda x:all_followers.index(x)) @@ -301,7 +301,7 @@ def get_following(browser, graphql_queries[username][query_date] = {"sc_rolled":0} sc_rolled_all = graphql_queries[username][query_date]["sc_rolled"] except Exception as exc: - logger.info("Error occured while getting `scroll` data from graphql_queries.json\n{}\n".format(exc.encode("utf-8"))) + logger.info("Error occured while getting `scroll` data from graphql_queries.json\n{}\n".format(str(exc).encode("utf-8"))) local_read_failure = True start_time = time.time() @@ -369,7 +369,7 @@ def get_following(browser, json.dump(graphql_queries, graphql_queries_file) except Exception as exc: print('\n') - logger.info("Error occured while writing `scroll` data to graphql_queries.json\n{}\n".format(exc.encode("utf-8"))) + logger.info("Error occured while writing `scroll` data to graphql_queries.json\n{}\n".format(str(exc).encode("utf-8"))) #take breaks gradually if sc_rolled > 91: @@ -380,7 +380,7 @@ def get_following(browser, except BaseException as exc: print('\n') - logger.info("Unable to get `Following` data:\n\t{}\n".format(exc.encode("utf-8"))) + logger.info("Unable to get `Following` data:\n\t{}\n".format(str(exc).encode("utf-8"))) #remove possible duplicates all_following = sorted(set(all_following), key=lambda x:all_following.index(x)) @@ -665,7 +665,7 @@ def store_followers_data(username, grab, grabbed_followers, logger, logfolder): logger.info("Stored `Followers` data at {} local file".format(final_file)) except Exception as exc: - logger.info("Failed to store `Followers` data in a local file :Z\n{}".format(exc.encode("utf-8"))) + logger.info("Failed to store `Followers` data in a local file :Z\n{}".format(str(exc).encode("utf-8"))) def store_following_data(username, grab, grabbed_following, logger, logfolder): @@ -691,7 +691,7 @@ def store_following_data(username, grab, grabbed_following, logger, logfolder): logger.info("Stored `Following` data at {} local file".format(final_file)) except Exception as exc: - logger.info("Failed to store `Following` data in a local file :Z\n{}".format(exc.encode("utf-8"))) + logger.info("Failed to store `Following` data in a local file :Z\n{}".format(str(exc).encode("utf-8"))) def store_all_unfollowers(username, all_unfollowers, logger, logfolder): @@ -717,7 +717,7 @@ def store_all_unfollowers(username, all_unfollowers, logger, logfolder): logger.info("Stored all Unfollowers data at {} local file\n".format(final_file)) except Exception as exc: - logger.info("Failed to store all Unfollowers data in a local file :Z\n{}\n".format(exc.encode("utf-8"))) + logger.info("Failed to store all Unfollowers data in a local file :Z\n{}\n".format(str(exc).encode("utf-8"))) def store_active_unfollowers(username, active_unfollowers, logger, logfolder): @@ -743,7 +743,7 @@ def store_active_unfollowers(username, active_unfollowers, logger, logfolder): logger.info("Stored active Unfollowers data at {} local file\n".format(final_file)) except Exception as exc: - logger.info("Failed to store active Unfollowers data in a local file :Z\n{}\n".format(exc.encode("utf-8"))) + logger.info("Failed to store active Unfollowers data in a local file :Z\n{}\n".format(str(exc).encode("utf-8"))) def store_nonfollowers(username, followers_size, following_size, nonfollowers, logger, logfolder): @@ -770,7 +770,7 @@ def store_nonfollowers(username, followers_size, following_size, nonfollowers, l logger.info("Stored Nonfollowers data at {} local file\n".format(final_file)) except Exception as exc: - logger.info("Failed to store Nonfollowers data in a local file :Z\n{}\n".format(exc.encode("utf-8"))) + logger.info("Failed to store Nonfollowers data in a local file :Z\n{}\n".format(str(exc).encode("utf-8"))) def store_fans(username, followers_size, following_size, fans, logger, logfolder): @@ -799,7 +799,7 @@ def store_fans(username, followers_size, following_size, fans, logger, logfolder logger.info("Stored Fans data at {} local file\n".format(final_file)) except Exception as exc: - logger.info("Failed to store Fans data in a local file :Z\n{}\n".format(exc.encode("utf-8"))) + logger.info("Failed to store Fans data in a local file :Z\n{}\n".format(str(exc).encode("utf-8"))) def store_mutual_following(username, followers_size, following_size, mutual_following, logger, logfolder): @@ -828,7 +828,7 @@ def store_mutual_following(username, followers_size, following_size, mutual_foll logger.info("Stored Mutual Following data at {} local file\n".format(final_file)) except Exception as exc: - logger.info("Failed to store Mutual Following data in a local file :Z\n{}\n".format(exc.encode("utf-8"))) + logger.info("Failed to store Mutual Following data in a local file :Z\n{}\n".format(str(exc).encode("utf-8"))) def load_followers_data(username, compare_by, compare_track, logger, logfolder): @@ -992,6 +992,6 @@ def progress_tracker(current_value, highest_value, initial_time, logger): sys.stdout.flush() except Exception as exc: - logger.info("Error occured with Progress Tracker:\n{}".format(exc)) + logger.info("Error occured with Progress Tracker:\n{}".format(str(exc).encode("utf-8"))) diff --git a/instapy/unfollow_util.py b/instapy/unfollow_util.py index 05a2c476a..f30d1dc2b 100644 --- a/instapy/unfollow_util.py +++ b/instapy/unfollow_util.py @@ -466,7 +466,7 @@ def unfollow(browser, continue except Exception as exc: - logger.error("Unfollow loop error:\n\n{}\n\n".format(exc.encode('utf-8'))) + logger.error("Unfollow loop error:\n\n{}\n\n".format(str(exc).encode('utf-8'))) else: logger.info("Please select a proper unfollow method! ~leaving unfollow activity\n") From 9cd18c3a5f9fed6021dba8d2b089d0261b698e27 Mon Sep 17 00:00:00 2001 From: Qulu Date: Thu, 16 Aug 2018 05:57:31 +0400 Subject: [PATCH 2/3] Fix issue with logging in Just a little delay is needed for the DOM elements reside so that `username` box exist before finding it. Otherwise it will throw `StaleElementReferenceException` in good case and will misinform saying 'Wrong login data' in the worst case. --- instapy/login_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instapy/login_util.py b/instapy/login_util.py index dde65af00..2acb138fa 100644 --- a/instapy/login_util.py +++ b/instapy/login_util.py @@ -144,7 +144,7 @@ def login_user(browser, # Enter username and password and logs the user in # Sometimes the element name isn't 'Username' and 'Password' # (valid for placeholder too) - sleep(1) + sleep(2) input_username = browser.find_elements_by_xpath( "//input[@name='username']") From edd91b448e134abf4bc508ca1561593f9c7028d0 Mon Sep 17 00:00:00 2001 From: Qulu Date: Thu, 16 Aug 2018 07:47:55 +0400 Subject: [PATCH 3/3] Fixed error in clicking "Load more comments" or "View all `n` comments" XPATH It directly affected `follow_commenters` feature and any other WHICH uses `extract_post_info` definition. --- instapy/commenters_util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instapy/commenters_util.py b/instapy/commenters_util.py index c77a54a1b..006658a2e 100644 --- a/instapy/commenters_util.py +++ b/instapy/commenters_util.py @@ -40,7 +40,7 @@ def extract_post_info(browser): while (" comments" in comments[1].text): more_comments += 1 print ("loading more comments.") - load_more_comments_element = browser.find_element_by_xpath("//div/ul/li[2]/a") + load_more_comments_element = browser.find_element_by_xpath("//div/ul/li[2]/button") browser.execute_script("arguments[0].click();", load_more_comments_element) sleep(1) #comment_list = post.find_element_by_tag_name('ul') @@ -54,7 +54,7 @@ def extract_post_info(browser): while (" comments" in comments[0].text): more_comments += 1 print ("loading more comments.") - load_more_comments_element = browser.find_element_by_xpath("//div/ul/li[1]/a") + load_more_comments_element = browser.find_element_by_xpath("//div/ul/li[1]/button") browser.execute_script("arguments[0].click();", load_more_comments_element) sleep(1) #comment_list = post.find_element_by_tag_name('ul')