Closed ivadenis closed 6 years ago
I recently fall into that error. Looks like the API is not returning the comments
field anymore so the filter is never working. The solution I found was to override the method, and if necessary make a call to getMediaComments
and get all comments.
Still there is another problem, that method just return at most 20 comments, and is not ready to get all (i.e. like getTotalFollowers
does), so again I overrode the method to get all the comments. Find code attached if you needed. Note that I change a bit the method as I found out that a list was returning duplicate elements, so I use a dictionary to avoid duplicates. I also did that on Followers and Friends and now it's a bit more accurate for bigger lists.
def getTotalMediaComments(self, mediaId, amount=None):
"""
Important:
Sometimes the `comment_count` doesn't match with the total comments.
"""
sleep_track = 0
comments = {}
next_max_id = ''
self.getMediaComments(mediaId)
if "comment_count" in self.LastJson:
if amount:
total_comments = min(amount, self.LastJson["comment_count"])
else:
total_comments = self.LastJson["comment_count"]
if total_comments > 20000:
self.logger.warning(
"Consider temporarily saving the result of "
"this big operation. This will take a while.")
else:
return False
with tqdm(
total=total_comments,
desc="Getting comments",
leave=False) as pbar:
while True:
self.getMediaComments(mediaId, next_max_id)
temp = self.LastJson
try:
pbar.update(len(temp["comments"]))
for item in temp["comments"]:
comments.update({item['pk']: item})
sleep_track += 1
if sleep_track >= 2000:
sleep_time = randint(120, 180)
self.logger.warning(
"Waiting %.2f min. due to too many requests." %
float(sleep_time / 60))
time.sleep(sleep_time)
sleep_track = 0
more_comments = temp["has_more_comments"]
limit_comments = len(comments.values()) >= total_comments
if not more_comments or limit_comments:
return list(comments.values())[:total_comments]
except Exception as e:
self.logger.warning('Exception getting comments: %s' % e)
return list(comments.values())[:total_comments]
next_max_id = temp["next_max_id"]
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Please follow the guide below
x
into all the boxes [ ] relevant to your issue (like so [x]).Before submitting an issue, make sure you have:
Purpose of your issue?
_filter_medias_not_commented
is not filtering commented medias. It seems that payload structure has changed.