InstaPy / InstaPy

📷 Instagram Bot - Tool for automated Instagram interactions
GNU General Public License v3.0
16.8k stars 3.77k forks source link

like_util.py line 618 problem #6470

Open sergenkuzey opened 2 years ago

sergenkuzey commented 2 years ago

I am using instapy-0.6.14

code:

from instapy import InstaPy

import random from instapy import InstaPy from instapy import smart_run

login credentials

insta_username = '' insta_password = ''

login session

session = InstaPy(username=insta_username, password=insta_password) session.login()

session.like_by_tags(["#carz"], amount=5) getting below error

Traceback (most recent call last): File "E:\Study\Python_Automation\Insta_Commentor\quickstart.py", line 56, in session.like_by_tags(my_hashtags, amount=90, media=None) File "C:\Users\sonu3\AppData\Local\Programs\Python\Python310\lib\site-packages\instapy\instapy.py", line 1977, in like_by_tags inappropriate, user_name, is_video, reason, scope = check_link( File "C:\Users\sonu3\AppData\Local\Programs\Python\Python310\lib\site-packages\instapy\like_util.py", line 618, in check_link media = post_page[0]["shortcode_media"] KeyError: 0

I tried everything but I didn't solve this problem.

Chessy11 commented 2 years ago

same

fabianbeta commented 2 years ago

same here

ivan-ngchakming commented 2 years ago

I think this error is caused by a change in from the Instagram web page.

Looks like the data obtained from window.__additionalDataLoaded in the html source (data stored in the post_page variable) is changed.

Some information can be extracted using a slightly different way,

media = post_page["items"][0]
is_video = media["media_type"] == 2  # 1 for photos, 2 for videos
user_name = media["user"]["username"]
image_text = media["caption"]["text"]

but I can't seem to find comments in this data anymore.

fabianbeta commented 2 years ago

I think this error is caused by a change in from the Instagram web page.

Looks like the data obtained from window.__additionalDataLoaded in the html source (data stored in the post_page variable) is changed.

Some information can be extracted using a slightly different way,

media = post_page["items"][0]
is_video = media["media_type"] == 2  # 1 for photos, 2 for videos
user_name = media["user"]["username"]
image_text = media["caption"]["text"]

but I can't seem to find comments in this data anymore.

Thanks Ian. I'll await an update for those with zero programming knowledge :)

ivan-ngchakming commented 2 years ago

Looks like there is already a PR working to fix this.

6467

Schmaggi commented 2 years ago

Same here

hscanlan commented 2 years ago

ditto

pedrobart commented 2 years ago

same here

hscanlan commented 2 years ago

Just a heads up, there is a pull request aval that fixes it. I just updated the 3 files and everything is fine again.

https://github.com/InstaPy/InstaPy/pull/6467

astroramses commented 2 years ago

Just a heads up, there is a pull request aval that fixes it. I just updated the 3 files and everything is fine again.

6467

I did change the 3 files, but still have unknown exception 0 when I try to use the function interact_by_users

fabianbeta commented 2 years ago

Just a heads up, there is a pull request aval that fixes it. I just updated the 3 files and everything is fine again.

6467

I did change the 3 files, but still have unknown exception 0 when I try to use the function interact_by_users

I can confirm this.

X0x1RG9f commented 2 years ago

Same problem while testing... Instagram changed little things on returned data structure.

Problem can be fixed easily for likes, while waiting for a fix by modifying following lines in '/site-packages/instapy/like_utils.py' (lines 619->622) :

media = post_page[0]["shortcode_media"]
is_video = media["is_video"]
user_name = media["owner"]["username"]
image_text = media["caption"]

owner_comments = browser.execute_script(
            """
            latest_comments = window._sharedData.entry_data.PostPage[
            0].media.comments.nodes;
            if (latest_comments === undefined) {
                latest_comments = Array();
                owner_comments = latest_comments
                    .filter(item => item.user.username == arguments[0])
                    .map(item => item.text)
                    .reduce((item, total) => item + '\\n' + total, '');
                return owner_comments;}
            else {
                return null;}
        """,
            user_name,
)

if owner_comments == "":
        owner_comments = None

Replacing with :

media = post_page['items'][0]
is_video = media["is_unified_video"]
user_name = media["user"]["username"]
try : 
    image_text = media["caption"]["text"]
except Exception:
    image_text = ""

owner_comments = None

Note that I just tested it for likes, not comments. No time to debug owner_comment function, so just removed (unchecked then).

lovung commented 2 years ago

@X0x1RG9f I got this error after replacing

  File "/usr/local/lib/python3.9/site-packages/instapy/like_util.py", line 622, in check_link
    image_text = media["caption"]["text"]
TypeError: 'NoneType' object is not subscriptable
X0x1RG9f commented 2 years ago

@lovung Well, seems to means that the IG post has no text. Cannot try it as this is quite random but the modified code should fix it. Again, this is not official fix and just an ugly sticking plaster :) !

impcretor commented 2 years ago

media = post_page['items'][0] is_video = media["is_unified_video"] user_name = media["user"]["username"] try : image_text = media["caption"]["text"] except Exception: image_text = ""

owner_comments = None

It works for me! Thank you

roscoe72 commented 2 years ago

ALL. Just made a small fix that hopefully catches the last issues... Look at this PR for more information. Please close this item if your problem is solved! https://github.com/InstaPy/InstaPy/pull/6467

fabianbeta commented 2 years ago

ALL. Just made a small fix that hopefully catches the last issues... Look at this PR for more information. Please close this item if your problem is solved! #6467

I just did a pip uninstall/install which I found to be easier. Working thank you - just struggling with the usuer_interact function where it's not liking those who I follow via instapy but I susepect that's my lack of experience? I am expecting the below to FOLLOW and then LIKE media of those followed (the "amount=2") defining the likes?) or does interact mean something else?

session.set_user_interact(amount=2, percentage=70, randomize=True, media='Photo')

session.follow_likers(['user1' , 'user2'], photos_grab_amount = 2, follow_likers_per_photo = 3, randomize=True, sleep_delay=600, interact=True)

beeloved commented 2 years ago

ALL. Just made a small fix that hopefully catches the last issues... Look at this PR for more information. Please close this item if your problem is solved! #6467

Works for me.

mrz8097 commented 2 years ago

This problem is due to the changes that Instagram has made in its source code. Go to line "619" from the "like_util.py" file and change that part of the code to the code I gave you below (if you could not find this part of the code, the phrase media = post_page [0] ["shortcode_media "] Search the file" like_util.py ")

     media = post_page ['items'] [0]
     is_video = media ["is_unified_video"]
     user_name = media ["user"] ["username"]
     image_text = media ["caption"] ["text"]
     owner_comments = ""

I am using instapy-0.6.14

code:

from instapy import InstaPy

import random from instapy import InstaPy from instapy import smart_run

login credentials insta_username = '' insta_password = ''

login session session = InstaPy(username=insta_username, password=insta_password) session.login()

session.like_by_tags(["#carz"], amount=5) getting below error

Traceback (most recent call last): File "E:\Study\Python_Automation\Insta_Commentor\quickstart.py", line 56, in session.like_by_tags(my_hashtags, amount=90, media=None) File "C:\Users\sonu3\AppData\Local\Programs\Python\Python310\lib\site-packages\instapy\instapy.py", line 1977, in like_by_tags inappropriate, user_name, is_video, reason, scope = check_link( File "C:\Users\sonu3\AppData\Local\Programs\Python\Python310\lib\site-packages\instapy\like_util.py", line 618, in check_link media = post_page[0]["shortcode_media"] KeyError: 0

I tried everything but I didn't solve this problem.