Skip to content

Commit

Permalink
catch exception for each post separately
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishan Dutta authored and Ishan Dutta committed Apr 12, 2019
1 parent ecba309 commit 4311ac4
Showing 1 changed file with 68 additions and 61 deletions.
129 changes: 68 additions & 61 deletions instapy/instapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5228,6 +5228,7 @@ def join_pods(self, topic='general'):
web_address_navigator(self.browser, user_link)
try:
pod_post_ids = get_recent_posts_from_pods(topic, self.logger)
self.logger.info("Downloaded pod_post_ids : {}".format(pod_post_ids))
sleep(2)
post_link_elems = self.browser.find_elements_by_xpath("//a[contains(@href, '/p/')]")
post_links = []
Expand All @@ -5243,26 +5244,29 @@ def join_pods(self, topic='general'):
post_links = list(set(post_links))
my_recent_post_ids = []
for post_link in post_links:
web_address_navigator(self.browser, post_link)
sleep(2)
time_element = self.browser.find_element_by_xpath("//div/a/time")
post_datetime_str = time_element.get_attribute('datetime')
post_datetime = datetime.strptime(post_datetime_str, "%Y-%m-%dT%H:%M:%S.%fZ")
postid = post_link.split('/')[4]
self.logger.info("Post: {}, Instaposted at: {}".format(postid, post_datetime))
share_restricted = share_with_pods_restriction("read", postid,
self.share_times,
self.logger)
if datetime.now() - post_datetime < timedelta(hours=12, minutes=30) and not share_restricted:
my_recent_post_ids.append(postid)
if share_my_post_with_pods(postid, topic, self.logger):
share_with_pods_restriction("write", postid, None, self.logger)
try:
web_address_navigator(self.browser, post_link)
sleep(2)
time_element = self.browser.find_element_by_xpath("//div/a/time")
post_datetime_str = time_element.get_attribute('datetime')
post_datetime = datetime.strptime(post_datetime_str, "%Y-%m-%dT%H:%M:%S.%fZ")
postid = post_link.split('/')[4]
self.logger.info("Post: {}, Instaposted at: {}".format(postid, post_datetime))
share_restricted = share_with_pods_restriction("read", postid,
self.share_times,
self.logger)
if datetime.now() - post_datetime < timedelta(hours=12, minutes=30) and not share_restricted:
my_recent_post_ids.append(postid)
if share_my_post_with_pods(postid, topic, self.logger):
share_with_pods_restriction("write", postid, None, self.logger)
except Exception as err:
self.logger.error("Failed for {} with Error {}".format(post_link, err))

if len(my_recent_post_ids) > 0:
self.logger.info("I have recent post(s), so I will now help pod members actively.")
self.logger.info("I have recent post(s): {}, so I will now help pod members actively.".format(my_recent_post_ids))
nposts = 200
else:
self.logger.info("I don't have any recent post(s), so I will just help a few pod posts and move on.")
self.logger.info("I don't have any recent post, so I will just help a few pod posts and move on.")
nposts = 40

if len(pod_post_ids) <= nposts:
Expand All @@ -5271,58 +5275,61 @@ def join_pods(self, topic='general'):
pod_post_ids = random.sample(pod_post_ids, nposts)

for pod_post_id in pod_post_ids:
post_link = "https://www.instagram.com/p/{}".format(pod_post_id)
web_address_navigator(self.browser, post_link)

inappropriate, user_name, is_video, reason, scope = (
check_link(self.browser,
post_link,
self.dont_like,
self.mandatory_words,
self.mandatory_language,
self.is_mandatory_character,
self.mandatory_character,
self.check_character_set,
self.ignore_if_contains,
self.logger))

if user_name != self.username:
follow_state, msg = follow_user(self.browser,
"post",
self.username,
user_name,
None,
self.blacklist,
self.logger,
self.logfolder)

self.dont_include.add(user_name)

if not inappropriate and user_name != self.username:
pods_like_percent = max(80, min(100, self.like_percentage))
pods_comment_percentage = max(80, min(100, self.comment_percentage))
liking = (random.randint(0, 100) <= pods_like_percent)
commenting = (random.randint(0, 100) <= pods_comment_percentage)
try:
post_link = "https://www.instagram.com/p/{}".format(pod_post_id)
web_address_navigator(self.browser, post_link)

if liking:
like_state, msg = like_image(self.browser,
inappropriate, user_name, is_video, reason, scope = (
check_link(self.browser,
post_link,
self.dont_like,
self.mandatory_words,
self.mandatory_language,
self.is_mandatory_character,
self.mandatory_character,
self.check_character_set,
self.ignore_if_contains,
self.logger))

if user_name != self.username:
follow_state, msg = follow_user(self.browser,
"post",
self.username,
user_name,
None,
self.blacklist,
self.logger,
self.logfolder)

if commenting:
comments = self.fetch_smart_comments(
is_video,
temp_comments=[])
self.dont_include.add(user_name)

comment_state, msg = comment_image(
self.browser,
user_name,
comments,
self.blacklist,
self.logger,
self.logfolder)
if not inappropriate and user_name != self.username:
pods_like_percent = max(80, min(100, self.like_percentage))
pods_comment_percentage = max(80, min(100, self.comment_percentage))
liking = (random.randint(0, 100) <= pods_like_percent)
commenting = (random.randint(0, 100) <= pods_comment_percentage)

if liking:
like_state, msg = like_image(self.browser,
user_name,
self.blacklist,
self.logger,
self.logfolder)

if commenting:
comments = self.fetch_smart_comments(
is_video,
temp_comments=[])

comment_state, msg = comment_image(
self.browser,
user_name,
comments,
self.blacklist,
self.logger,
self.logfolder)
except Exception as err:
self.logger.error("Failed for {} with Error {}".format(pod_post_id, err))

except Exception as err:
self.logger.error(err)
Expand Down

0 comments on commit 4311ac4

Please sign in to comment.