Skip to content
Permalink
Browse files

Show proper error message in verify_media_before_comment{} (#2548)

  • Loading branch information
nickgryg committed Feb 15, 2020
1 parent 2c3518c commit 0b3f14613da4cac0e927f8ae7429db82a47b7653
Showing with 16 additions and 27 deletions.
  1. +2 −1 Releases.md
  2. +1 −1 instabot_py/__init__.py
  3. +13 −25 instabot_py/instabot.py
@@ -4,4 +4,5 @@
| 0.5.22 | Stop instabot if we have nonexistent tag in config |
| 0.5.23 | Fixed a bug: `like_followers_per_run` functionality didn't work |
| 0.5.24 | Bot stops working if like action is banned. Bot can be started again with `like_per_run: 0` configuration setting |
| 0.5.25 | Show proper error message if we have issues in getting number of user's followers |
| 0.5.25 | Show proper error message if we have issues in getting number of user's followers |
| 0.5.26 | Show proper error message if we have issues in verifying media before commenting it |
@@ -1,4 +1,4 @@
from .instabot import InstaBot

__all__ = ['InstaBot']
__version__ = '0.5.25'
__version__ = '0.5.26'
@@ -1236,20 +1236,9 @@ def verify_media_before_comment(self, media):
url_check = self.url_media % media_code
try:
resp = self.s.get(url_check)
except Exception as exc:
self.logger.warning(f"Could not comment media {media_code}, url: "
f"{url_check}: status code: {resp.status_code}."
f" Reason: {resp.text}")
self.logger.exception(exc)
return False

if 'dialog-404' in resp.text:
self.logger.warning(f"Tried to comment media {media_code}, url: "
f"{url_check}: it does not exist anymore")
return False

if resp.status_code == 200:
try:
warn = f"Could not comment media {media_code}, url: {url_check}: " \
f"status code: {resp.status_code}. Reason: {resp.text}"
if resp.status_code == 200:
raw_data = re.search(
"window.__additionalDataLoaded\\('/p/\\w*/',(.*?)\\);",
resp.text, re.DOTALL).group(1)
@@ -1258,7 +1247,7 @@ def verify_media_before_comment(self, media):
if all_data['graphql']['shortcode_media']['owner']['id'] == \
self.user_id:
self.logger.debug(f"This media {media_code}, url: "
f"{url_check} is yours")
f"{url_check} is yours. Skipping it")
return False

edges = all_data['graphql']['shortcode_media'].get(
@@ -1268,26 +1257,25 @@ def verify_media_before_comment(self, media):
'edge_media_to_parent_comment', None)

comments = list(edges['edges'])

for comment in comments:
if comment['node']['owner']['id'] == self.user_id:
self.logger.debug(f"This media {media_code}, url: "
f"{url_check} is already commented by"
f" you")
return False
return True

except Exception as exc:
self.logger.critical(f"Could not retrieve comments from media "
f"{media_code}, url: {url_check}. "
f"Response code: {resp.status_code}")
self.logger.exception(exc)
elif resp.status_code == 404:
self.logger.warning(f"This media {media_code}, url: {url_check}"
f"does not exist anymore")
return False

return True
else:
self.logger.warning(warn)
return False

elif resp.status_code == 404:
self.logger.warning(f"This media {media_code}, url: {url_check} "
f"does not exist anymore")
except Exception as exc:
self.logger.warning(warn)
return False

def get_medias_from_recent_feed(self):

0 comments on commit 0b3f146

Please sign in to comment.
You can’t perform that action at this time.