ohld / igbot

🐙 Free scripts, bots and Python API wrapper. Get free followers with our auto like, auto follow and other scripts!
https://hikerapi.com/p/N2P6iqiM
Apache License 2.0
4.72k stars 1.47k forks source link

filter_medias is not working for checking on commented medias #452

Closed ivadenis closed 6 years ago

ivadenis commented 6 years ago

Please follow the guide below


Before submitting an issue, make sure you have:

Purpose of your issue?


{'taken_at': 1517266040, 'pk': 1703273457976031722, 'id': '1703273457976031722_6236273350', 'device_timestamp': 1517266025477, 'media_type': 1, 'code': 'BejPi-KAznq', 'client_cache_key': 'MTcwMzI3MzQ1Nzk3NjAzMTcyMg==.2', 'filter_type': 0, 'image_versions2': {'candidates': [{'width': 1080, 'height': 1080, 'url': 'https://scontent-sjc3-1.cdninstagram.com/vp/2b7deed585b795683031968a4e97d81f/5B28125D/t51.2885-15/e35/27577235_844145069098602_1528545029189009408_n.jpg?se=7&ig_cache_key=MTcwMzI3MzQ1Nzk3NjAzMTcyMg%3D%3D.2'}, {'width': 240, 'height': 240, 'url': 'https://scontent-sjc3-1.cdninstagram.com/vp/2278b8d7ff72c19f897ec114e164fdd4/5B1F2E07/t51.2885-15/s240x240/e35/27577235_844145069098602_1528545029189009408_n.jpg?ig_cache_key=MTcwMzI3MzQ1Nzk3NjAzMTcyMg%3D%3D.2'}]}, 'original_width': 1080, 'original_height': 1080, 'user': {'pk': 6236273350, 'username': 'skyloftsf', 'full_name': 'SkyLoftSF', 'is_private': False, 'profile_pic_url': 'https://scontent-sjc3-1.cdninstagram.com/vp/a213d7a602fea772fd107edf23b74452/5B083F22/t51.2885-19/s150x150/23735384_792962554161646_6997136186342899712_n.jpg', 'profile_pic_id': '1654052137506615220_6236273350', 'is_verified': False, 'has_anonymous_profile_picture': False, 'is_unpublished': False, 'is_favorite': False}, 'can_viewer_reshare': True, 'caption': {'pk': 17897622304137801, 'user_id': 6236273350, 'text': 'Sunrise bananza! 🍌☀️🌇\n•\n•\n•\n•\n•\n#citylife #cityscape #buildings #skyline #sf #aerial #rooftop #twinpeaks #sanfrancisco #cityview #wildseekers #davidlynch #citylights #paintedladies #bayarea #architecturelovers #skyscraper #alwayssf #landscapes #panorama #mysanfrancisco #sf_insta #sanfran #sfbay #sanfrancitizens #49ers #sfguide', 'type': 1, 'created_at': 1517266041, 'created_at_utc': 1517266041, 'content_type': 'comment', 'status': 'Active', 'bit_flags': 0, 'user': {'pk': 6236273350, 'username': 'skyloftsf', 'full_name': 'SkyLoftSF', 'is_private': False, 'profile_pic_url': 'https://scontent-sjc3-1.cdninstagram.com/vp/a213d7a602fea772fd107edf23b74452/5B083F22/t51.2885-19/s150x150/23735384_792962554161646_6997136186342899712_n.jpg', 'profile_pic_id': '1654052137506615220_6236273350', 'is_verified': False, 'has_anonymous_profile_picture': False, 'is_unpublished': False, 'is_favorite': False}, 'did_report_as_spam': False, 'media_id': 1703273457976031722}, 'caption_is_edited': False, 'like_count': 469, 'has_liked': True, 'top_likers': ['joey_dediashvili'], 'comment_likes_enabled': True, 'comment_threading_enabled': False, 'has_more_comments': True, 'next_max_id': 17923765636006373, 'max_num_visible_preview_comments': 2, 'preview_comments': [{'pk': 17923765636006373, 'user_id': 6236273350, 'text': '@chrisoconnor123 come baaack!', 'type': 2, 'created_at': 1517509232, 'created_at_utc': 1517509232, 'content_type': 'comment', 'status': 'Active', 'bit_flags': 0, 'user': {'pk': 6236273350, 'username': 'skyloftsf', 'full_name': 'SkyLoftSF', 'is_private': False, 'profile_pic_url': 'https://scontent-sjc3-1.cdninstagram.com/vp/a213d7a602fea772fd107edf23b74452/5B083F22/t51.2885-19/s150x150/23735384_792962554161646_6997136186342899712_n.jpg', 'profile_pic_id': '1654052137506615220_6236273350', 'is_verified': False}, 'did_report_as_spam': False, 'media_id': 1703273457976031722}], 'comment_count': 14, 'photo_of_you': False, 'can_viewer_save': True, 'organic_tracking_token': 'eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjp0cnVlLCJ1dWlkIjoiYTI2MzM5MGYzNjhjNGQxZTgzMWExNzU0NmFkMDBkZmIxNzAzMjczNDU3OTc2MDMxNzIyIiwic2VydmVyX3Rva2VuIjoiMTUxOTA2NTYyMjU1M3wxNzAzMjczNDU3OTc2MDMxNzIyfDcwOTQ3NjQ1OTN8ODg2YzY0ODExOWIxNTgxYmI5NTc2NzJhNWYxYjQwYjU1MjM0N2M1NDQ3OWE0ZDFiZGNkYmQ2OWI2NDJiZTE4YyJ9LCJzaWduYXR1cmUiOiIifQ=='}
def _filter_medias_not_commented(self, media_items):
    not_commented_medias = []
    for media in media_items:
       # comments key doesn't seem to be available any more, thus filtering is not working.
        if media.get('comment_count', 0) > 0 and media.get('comments'):
            my_comments = [comment for comment in media['comments']
                           if comment['user_id'] == self.user_id]
            if my_comments:
                continue
        not_commented_medias.append(media)
    return not_commented_medias

_filter_medias_not_commented is not filtering commented medias. It seems that payload structure has changed.

ivadenis commented 6 years ago
screen shot 2018-02-19 at 11 07 11 am
alberdonpi commented 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"]
stale[bot] commented 6 years ago

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.