Instapy: Unfollow loop error: Message: unknown error: Cannot read property 'activity_counts' of undefined

Created on 19 Aug 2018  路  23Comments  路  Source: timgrossmann/InstaPy

INFO [2018-08-19 11:27:42] [xxx] --> Ongoing Unfollow From InstaPy 38/400, now unfollowing: b'cwl32'
INFO [2018-08-19 11:27:42] [xxx] Removed '2018-07-18 00:19 ~ x' from followedPool.csv file

ERROR [2018-08-19 11:28:24] [xxx] --> Unfollow error with x', maybe no longer exists...
ERROR [2018-08-19 11:28:24] [xxx] Unfollow loop error: Message: unknown error: Cannot read property 'activity_counts' of undefined
(Session info: headless chrome=68.0.3440.106)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.15.0-32-generic x86_64)

wontfix

Most helpful comment

Alright,
@tompicca
Do this modification to get a total solution,

A: util.py file

1-) Add these 2 import lines to the top of file

from platform   import system
from subprocess import call

2-) Add ping_server definition to the end of file from this gist
3-) Add emergency_exit definition to the end of file from this gist

B: unfollow_util.py file

Replace it with this gist OR do these _changes_ manually,
1-) Import this line at the top of file

from .util import emergency_exit

2-) Add a new _assignment_ in to get_following_status definition

    except:
        logger.error(
            '--> Unfollow error with {},'
            ' maybe no longer exists...'
                .format(person.encode('utf-8')))
        following = None   #ADD THIS LINE

3-) Replace

                    try:
                        following, follow_button = get_following_status(browser, person, logger)
                    except:
                        logger.error(
                            '--> Unfollow error with {},'
                            ' maybe no longer exists...'
                                .format(person.encode('utf-8')))
                        continue

with

                    following, follow_button = get_following_status(browser, person, logger)

                    if following is None:
                        sirens_wailing = emergency_exit(browser, username, logger)
                        if sirens_wailing == True:
                            break

4-) Remove lines

                        # check we are now logged in
                        valid_connection = browser.execute_script(
                            "return window._sharedData.""activity_counts")
                        if not valid_connection:
                            # if no valid connection
                            msg = '--> user:{} have no valid_connection wait 3600'.format(person)
                            logger.warning(msg)
                            break

Now that connection validation is being held at emergency_exit definition.

I have not tested, use with care 馃榾 and report back if something does not work as expected, I will help, hopefully.
_Those are the optimal solutions for now and still needs improvements in methods._


Cheers 馃榿

All 23 comments

@uluQulu do you have any clue?

@fgisslen
It's a typical temporary outage error.
To solve, open up unfollow_util.py file and do this modification,
replace

valid_connection = browser.execute_script(
    "return window._sharedData.""activity_counts")

with

try:
    valid_connection = browser.execute_script(
        "return window._sharedData.activity_counts")

except WebDriverException:
    try:
        browser.execute_script("location.reload()")
        valid_connection = browser.execute_script(
                            "return window._sharedData.activity_counts")

    except WebDriverException:
        valid_connection = False

Cheers 馃榿

You maybe got logged out ? password change is the main reason I added it.
If a costumer changed his password you can mistakenly remove all follow pool since the "follow" button appear as you are not logged in.
another reason is a internet\IG issue.

I seem to hit this when the user no longer exist. So I don't really want to break the unfollow loop. maybe need to differentiate between a "no longer exist" scenario vs a "not logged anymore" scenario? For now, I used continue instead of break to skip to next one in the unfollow loop

Well, @uluQulu solution didn't work out as in my case, user no longer exists and now I'm having

Unfollow error with xxxuser, maybe no longer exists...
user:xxxuser have no valid connection wait 3600

and session ends.

@succulentia
Do you know what I have solved above?

  • It is "_Unfollow loop error: Message: unknown error: Cannot read property 'activity_counts' of undefined_" problem which is the name of this thread and what @fgisslen asked help for.

Shortly, that block I've suggested will solve the program from crashing out of being unable to read activity_counts property and it is not meant to solve,
"_Unfollow error with xxxuser, maybe no longer exists..._" or any other.

If you have a problem ask it, I think somebody would help.


Cheers 馃榿

I think what we are saying is activity_counts undefined is in a lot of cases (including OP) is a symptom of user no longer exists. So catching the exception and breaking the unfollow loop doesn't actually fix the root of the problem.

@hw711
This issue is saying that you are having an exception on that scenario and it does not say,
"_Hey I cannot unfollow anybody (anymore) cos of user no longer exists or sth else_"

I just corrected the missing behavior of that block which is mostly enhanced by @sionking that I'm sure is written efficiently.

Maybe, you, @hw711 open the CORRECT thread for your issue and WRITE about it, eh?


Cheers 馃榿

@uluQulu
Thanks for sharing your solution, however it doesn't work for me. Now, after edited unfollow_util.py, I get this error:

INFO [2018-08-23 00:35:55] []  Total 774 users available to unfollow  ~didn't pass `unfollow_after`: 0
ERROR [2018-08-23 00:36:23] []  --> Unfollow error with rahul._.patil__, maybe no longer exists...
WARNING [2018-08-23 00:36:23] []  --> user:rahul._.patil__ have no valid_connection wait 3600
INFO [2018-08-23 00:36:23] []  --> Total people unfollowed : 0

OOOOOOOOOOOOOO
INFO [2018-08-23 00:36:23] []  Session ended - 2018-08-23 00:36:23
oooooooooooooooooo

Welcome @tompicca
Let's solve it!
A few questions to find the problem
1-) Which unfollow method(s) do you use is affected by the problem (_I think in example above it is InstapyFollowed, only it?_)

2-) Were you getting the same error in the title of this thread before applying the changes in my comment?

  • If so, how frequently?
  • Now what changed after applying it?

3-) Do you always get that "_... no longer exists_" error message?

  • If not, how frequently?

4-) What actually you want to achieve?

  • Make program sleep after that time rather than breaking...
  • Solve it in best possible state
  • other?

Also, check manually if rahul._.patil__ (_the username which is said not no exist_) really exists.

UPDATE:

I just checked, the username you have provided in the comment above seems not to be available.
And I got Sorry, this page isn't available. from the webpage while trying to open its profile page.

I think if you get this message on really unavailable pages (_verification of this information must be solid_) then the solution is so easy.

Can you verify on that, @tompicca ?

  • Are you getting that error always on really unavailable profile names like rahul._.patil__?

UPDATE 2:

I have checked, yes, there is a glitch in that mechanism:

  • if the username is not available at the moment it will break the loop immediately regardless of it is a false positive- the user is really unavailable.
    @sionking, you're right about breaking the loop in the cases you have outlined and the verification of that condition must be handled carefully.

Cheers 馃榿

Thanks for you quick reply @uluQulu, these are my answers to your questions:

  • I'm using only InstapyFollowed in this way:
session.unfollow_users(amount=600, InstapyFollowed=(True, "all"), style="FIFO", unfollow_after=1*60*60, sleep_delay=90)
  • Yes the same error of this thread, specifically:
INFO [] []  Total 774 users available to unfollow  ~didn't pass `unfollow_after`: 0
ERROR [] []  --> Unfollow error with rahul._.patil__, maybe no longer exists...
ERROR [] []  Unfollow loop error:  Message: unknown error: Cannot read property 'activity_counts' of undefined
  (Session info: headless chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db),platform=Mac OS X 10.13.6 x86_64)
INFO [] []  --> Total people unfollowed : 0
OOOOOOOOOOOO
INFO [] []  Session ended - 2018-08-23 00:32:07
oooooooooooooooo

it occurred each time I tried to start an unfollow session.
What is changed after applied your patch is the error message as you can see in my previous post.

  • First time I get this error

  • My main aim is to prevent that session stops if a user no longer exist. I noticed that if I manually remove that username from followedPool.csv and restart the session, it works properly but stops again if meets a new user that no-longer-exist. Maybe a solution could be: when a user no longer exist, remove it from followedPool and start to analyze the next user.

Thanks!

Glad to help @tompicca
There are several cases to notice:
a-) the connection between you and servers is not good
b-) you are not logged in
c-) the user has changed username
d-) the user blocked you
e-) etc.

All of those cases return you to the same page:
Sorry, this page is unavailable
And currently it is breaking the loop once encounters it.
Which means it also stops for false positives- c and d for sure

  • c is gonna be solved by the live PR from @CharlesCCC which is looking for the ID of the user even though the username has changed it can find that user and unfollow and pass this problem.

a & b must stop the loop
d is difficult to be verified and can only be evaluated if the c is wrong
even there are possible e_s_...

So @tompicca, as you see it is a bit difficult to solve it as of now.

But let's solve it simply for now.
2 cases:
1-) It must break the loop
2-) It must continue with the next user

So, the thing is checking if the 1 is REALLY true .
I will figure it out and write a few lines for you now.


Cheers 馃榿

Alright,
@tompicca
Do this modification to get a total solution,

A: util.py file

1-) Add these 2 import lines to the top of file

from platform   import system
from subprocess import call

2-) Add ping_server definition to the end of file from this gist
3-) Add emergency_exit definition to the end of file from this gist

B: unfollow_util.py file

Replace it with this gist OR do these _changes_ manually,
1-) Import this line at the top of file

from .util import emergency_exit

2-) Add a new _assignment_ in to get_following_status definition

    except:
        logger.error(
            '--> Unfollow error with {},'
            ' maybe no longer exists...'
                .format(person.encode('utf-8')))
        following = None   #ADD THIS LINE

3-) Replace

                    try:
                        following, follow_button = get_following_status(browser, person, logger)
                    except:
                        logger.error(
                            '--> Unfollow error with {},'
                            ' maybe no longer exists...'
                                .format(person.encode('utf-8')))
                        continue

with

                    following, follow_button = get_following_status(browser, person, logger)

                    if following is None:
                        sirens_wailing = emergency_exit(browser, username, logger)
                        if sirens_wailing == True:
                            break

4-) Remove lines

                        # check we are now logged in
                        valid_connection = browser.execute_script(
                            "return window._sharedData.""activity_counts")
                        if not valid_connection:
                            # if no valid connection
                            msg = '--> user:{} have no valid_connection wait 3600'.format(person)
                            logger.warning(msg)
                            break

Now that connection validation is being held at emergency_exit definition.

I have not tested, use with care 馃榾 and report back if something does not work as expected, I will help, hopefully.
_Those are the optimal solutions for now and still needs improvements in methods._


Cheers 馃榿

@uluQulu Your solution appears to have worked for me. I did have to update the command variable in ping_server.py because the concatenation wasn't working correctly for me.

Thank you for taking the time to provide a solution.

I think a proper state machine for the unfollow method need to be made.
I get all this options:

  1. oops and error occured
  2. page not exist (user blocked you or not exist)
  3. logged out
  4. not following although we think we do
  5. white list
  6. don't include list
  7. confirmation window

and maybe many more.
I do have a my solution I can share but it ugly as it not state machine only a workarounds...

@uluQulu

  1. ping_server not supporting proxy.
  2. the "except:" will never raise since it will be catched in get_following_status function.

Welcome @ximhotep
I will look to update it, thanks for reporting.

Hey @sionking
I hope you have read my comments above cos that's where I ended up a decision.
It is 2 cases:
1-) It must break the loop
2-) It must continue with the next user
So, the thing is checking if the 1 is REALLY true .
To check 1,
I tested it with

  • verify connection between the server by ping requests
  • verify logged in - go to owner's profile page and test activity_counts as you did, if not logged in it returns null - None
  • another conditions may be added to verify in case of the UNFOLLOW LOOP must be stopped

For your notice,
oops and error occurred

  • not a problem, emergency_exit can handle it by verifying if the server is accessible

page not exist (user blocked you or not exist)

  • not a problem, can be verified off checking user_id (_@CharlesCCC's PR_) and if user still unavailable and you have a valid connection then remove user and continue with next unfollow

logged out

  • emergency_exit handles it

not following although we think we do

  • this is not an emergency condition, and must be handled elsewhere

white list

  • this is not an emergency condition, and must be handled elsewhere

don't include list

  • this is not an emergency condition, and must be handled elsewhere

confirmation window

  • this is not an emergency condition, and must be handled elsewhere

@sionking, I will share my big PR soon there I have done one thing which will simplify this stuff a little bit.
Now there is only one unfollow engine - unfollow_user() rather than separate blocks having their custom unfollow steps.

Also I didn't know about proxies cannot ping it, I will think about it and maybe add an alternative for proxies. There are lots of ways to verify if there the server is accessible. But the difficult thing is having hit access denials out of too many server calls- I think it is a rare condition ot be handled xD

"the "except:" will never raise since it will be catched in get_following_status function."

  • First it checks activity_counts in the profile page of the user to be unfollowed and if it is None it starts emergency_exit and pings server and if there is a valid connection then it navigates to the owner's profile page and takes another activity_counts test to see if the user is LOGGED in...

Cheers 馃榿

thanks to all for this big work, is it possible when all the changes works make a new release of InstaPy in order to be sure that we download and restart with the same version of all files?
thanks

Thank you @uluQulu ! It's working great for me now!

@uluQulu Works great, Thank you!

The gist for the unfollow_util.py that was given in the solution is missing on "d" in the "from .util import web_address_navigator"

Hi @thomaslc66
I think this thread is outdated cos InstaPy files already has the capabilities of those lines with my latest commits.
If you still get similar issues, please open a new issue. I would try to help.


Cheers 馃榿

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. > If this problem still occurs, please open a new issue

Was this page helpful?
0 / 5 - 0 ratings