Repository: InstaPy/InstaPy

Issue:
KeyError: 'like'
Hi, this has started happening today every time I run Instapy and it hits at the first like 

`Traceback (most recent call last):
  File "C:\Users\###\Telegram-InstaPy-Scheduling\scripts.py", line 208, in ###_script
    interact=True)
  File "C:\Users\###\AppData\Local\Programs\Python\Python37-32\lib\site-packages\instapy\instapy.py", line 1811, in like_by_tags
    liked_img)
  File "C:\Users\###\AppData\Local\Programs\Python\Python37-32\lib\site-packages\instapy\like_util.py", line 696, in like_image
    naply = get_action_delay("like")
  File "C:\Users\###\AppData\Local\Programs\Python\Python37-32\lib\site-packages\instapy\util.py", line 1771, in get_action_delay
    custom_delay = config[action]
KeyError: 'like'`

I have 3 setups of the bot on different machines, only one has this problem and I can't really understand why.
Win10, Chrome 73, instapy-chromedriver and every other dependency updated.



Discussion:
im also getting this error

```
Traceback (most recent call last):
  File "insta.py", line 17, in <module>
    bot.like_by_tags(['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'], amount=100)
  File "/usr/local/lib/python3.7/site-packages/instapy/instapy.py", line 1811, in like_by_tags
    liked_img)
  File "/usr/local/lib/python3.7/site-packages/instapy/like_util.py", line 696, in like_image
    naply = get_action_delay("like")
  File "/usr/local/lib/python3.7/site-packages/instapy/util.py", line 1771, in get_action_delay
    custom_delay = config[action]
KeyError: 'like'

```
I have the same Problem. Sadly your fix did not work for me. (or nor for long)

I run it on a raspbnerry pi 3 an everything is up to date (did a new install today)

```
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
INFO [2019-07-03 23:25:28] [sk]  Session started!
oooooooooooooooooooooooooooooooooooooooooooooooooooooo


...................................................................
INFO [2019-07-03 23:26:03] [sk]  Logged in successfully!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
INFO [2019-07-03 23:26:03] [sk]  Saving account progress...
INFO [2019-07-03 23:26:07] [sk]  Tag [1/16]
INFO [2019-07-03 23:26:07] [sk]  --> b'justgoshoot'
INFO [2019-07-03 23:26:37] [sk]  Too few images, skipping this tag
INFO [2019-07-03 23:26:37] [sk]  Tag [2/16]
INFO [2019-07-03 23:28:22] [sk]  Sessional Live Report:

On session start was FOLLOWING 146 users & had 1002 FOLLOWERS
[Session lasted 2.96 minutes]

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
INFO [2019-07-03 23:28:22] [sk]  Session ended!
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooo



Traceback (most recent call last):
  File "quickstart.py", line 41, in <module>
    session.like_by_tags(['justgoshoot', 'djiglobal', 'worldplaces', 'myfeatureshoot', 'blickheimat', 'drohne', 'fotografie', 'artofvisuals', 'mavicpro', 'exploretocreate', 'liveofadventure', 'folkmagazine', 'theglobewanderer', 'peoplescreatives', 'watson_schweiz', 'visualsoflife'], amount=3)   
  File "/home/pi/.local/lib/python3.5/site-packages/instapy/instapy.py", line 1811, in like_by_tags
    liked_img)
  File "/home/pi/.local/lib/python3.5/site-packages/instapy/like_util.py", line 696, in like_image
    naply = get_action_delay("Like")
  File "/home/pi/.local/lib/python3.5/site-packages/instapy/util.py", line 1771, in get_action_delay
    custom_delay = config[action]
KeyError: 'Like'
```

Getting the same error here:

```
On session start was FOLLOWING 438 users & had 3948 FOLLOWERS
[Session lasted 1.36 minutes]
 
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
INFO [2019-07-03 19:18:39] [cc]  Session ended!
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

 
 
Traceback (most recent call last):
  File "quickstart.py", line 90, in <module>
    session.like_by_tags(my_hashtags, amount=60, media=None)
  File "\AppData\Local\Programs\Python\Python37-32\lib\site-packages\instapy\instapy.py", line 1811, in like_by_tags
    liked_img)
  File "\AppData\Local\Programs\Python\Python37-32\lib\site-packages\instapy\like_util.py", line 673, in like_image
    like_xpath = read_xpath(like_image.__name__, "Like")
  File "\AppData\Local\Programs\Python\Python37-32\lib\site-packages\instapy\xpath.py", line 4, in read_xpath
    return xpath[function_name][xpath_name]
KeyError: 'Like'
```

Changing "like" to "Like" is doing nothing. Could this be a change of class name on the IG side? Site hashtags went down for a few hours today. Imagine they could have made a change to cause this type of break specifically.
i was able to run my script with release `0.5.3` successfully i guess the commits from there to `0.5.4` need a closer look
I have the same problem, has anyone managed to solve?
@rodrigosimoncalaca Just uninstall instapy==0.5.4 and dependencies and install instapy==0.5.3 as stated by @hijak !!! It will work.

I did this, but the problem continues.
I am using version 3.5 of python, can that be it?
How can I uninstall InstaPy and Reinstall the right version in Raspberry ?
I made it:
sudo pip uninstall instapy
sudo pip install instapy==0.5.3

But problem continues.
same here
i am using it in a pipeline like
```
    - git clone https://github.com/timgrossmann/InstaPy.git /srv/InstaPy
    - cd /srv/InstaPy && git checkout tags/0.5.3
    - pip install /srv/InstaPy
    - python myscript.py
```
Hey guys, can any of you post your script that results in this error ? I did the one-liner change in `get_action_delays` that was merged yesterday. Maybe it broke something else, which I did not test.
For a quick fix

Edit `instapy/util.py` starting at line `1764` change:

```
if (not config or
		config["enabled"] is not True or
		config[action] is None or
		isinstance(config[action], (int, float))) is not True:
	return defaults[action]

else:
	custom_delay = config[action]
```

To:
   
```
if (not config or
		config["enabled"] is not True or
		action not in config or
		isinstance(config[action], (int, float)) is not True):
	return defaults[action]

else:
	custom_delay = config[action]
```


Although @timgrossmann  please stop accepting PR's from unknown sources because InstaPy is becoming bloated and bugged:

- ERROR [2019-07-04 17:18:53] [xxxxxxx]  Unable to go to next profile post
- Unfollow loop issues
- Relationships math is weak
### Problem

from the logs you can see that error occurs for following (`KeyError: 'follow'`) and liking (`KeyError: 'like'`) perfomed by the three following methods:
- `ineract_user_followers()`
- `follow_likers()`
- `like_by_tag()`

### Cause

In `get_action_delay(action)` the `config` variable is not complete. Probably `Settings.action_delays` is not set correctly when InstaPy object is initialized.

```
def get_action_delay(action):
    """ Get the delay time to sleep after doing actions """
    defaults = {"like": 2,
                "comment": 2,
                "follow": 3,
                "unfollow": 10}
    config = Settings.action_delays

    if (not config or
            config["enabled"] is not True or
            config[action] is None or
            isinstance(config[action], (int, float))) is not True:
        return defaults[action]

    else:
        custom_delay = config[action]

...
```

We initialize InstaPy like this: `session = InstaPy(username=insta_username, password=insta_password, headless_browser=False, nogui=True)` 

### Logs
```
INFO     [2019-07-04 15:21:24] [0x2815561e236] [**********] User 'hannahloura' [14/112]
INFO     [2019-07-04 15:21:29] [0x2815561e236] [**********] User: 'hannahloura'  |> followers: 3388  |> following: 906  |> relationship ratio: 3.73
INFO     [2019-07-04 15:21:29] [0x2815561e236] [**********] Interaction [4/112]  |  Total Interaction: 4
INFO     [2019-07-04 15:21:29] [0x2815561e236] [**********] Starting to interact by users..
INFO     [2019-07-04 15:21:29] [0x2815561e236] [**********] Username [1/1]
INFO     [2019-07-04 15:21:29] [0x2815561e236] [**********] --> b'hannahloura'
INFO     [2019-07-04 15:21:29] [0x2815561e236] [**********] username actions: following=False commenting=False liking=True
INFO     [2019-07-04 15:21:29] [0x2815561e236] [**********] Getting hannahloura image list...
INFO     [2019-07-04 15:21:34] [0x2815561e236] [**********] Post [1/3]
INFO     [2019-07-04 15:21:34] [0x2815561e236] [**********] https://www.instagram.com/p/BzC6-qFppD4/
INFO     [2019-07-04 15:21:38] [0x2815561e236] [**********] Image from: b'hannahloura'
INFO     [2019-07-04 15:21:38] [0x2815561e236] [**********] Link: b'https://www.instagram.com/p/BzC6-qFppD4/'
INFO     [2019-07-04 15:21:38] [0x2815561e236] [**********] Description: b'Enjoy it all.\n.\n.\n.\n#photography #blackandwhite #bw #pola #polaroid #pure #natural #smile #happy #longhair #agency #model #newface #modeling #fashion #beauty #commercial #face #sunday'
INFO     [2019-07-04 15:21:41] [0x2815561e236] [**********] --> Image Liked!
ERROR    [2019-07-04 15:21:41] [0x2815561e236] [**********] KeyError error occurred during interact user followers.
Traceback (most recent call last):
  File "/opt/*********-box/main.py", line 584, in bot
    session.interact_user_followers(settings['interactUserFollowers']['users'],**settings['interactUserFollowers']['kwargs'])
  File "/opt/*********-box/libs/InstaPy/instapy/instapy.py", line 2942, in interact_user_followers
    self.user_interact_media)
  File "/opt/*********-box/libs/InstaPy/instapy/instapy.py", line 2350, in interact_by_users
    total_liked_img)
  File "/opt/*********-box/libs/InstaPy/instapy/like_util.py", line 696, in like_image
    naply = get_action_delay("like")
  File "/opt/*********-box/libs/InstaPy/instapy/util.py", line 1771, in get_action_delay
    custom_delay = config[action]
KeyError: 'like'
INFO     [2019-07-04 15:21:41] [0x2815561e236] [**********] Starting to follow likers..
INFO     [2019-07-04 15:22:20] [0x2815561e236] [**********] User: 'marcnideroest'  |> followers: 1549  |> following: 6371  |> relationship ratio: 0.24
INFO     [2019-07-04 15:22:20] [0x2815561e236] [**********] --> Not a valid user: User 'marcnideroest's following count exceeds maximum limit  ~skipping user

INFO     [2019-07-04 15:22:25] [0x2815561e236] [**********] User: 'avidue_official'  |> followers: 298  |> following: 201  |> relationship ratio: 1.48
INFO     [2019-07-04 15:22:30] [0x2815561e236] [**********] Last follow is verified after reloading the page!
INFO     [2019-07-04 15:22:30] [0x2815561e236] [**********] --> Followed 'b'avidue_official''!
ERROR    [2019-07-04 15:22:31] [0x2815561e236] [**********] KeyError error occurred during follow likers.
Traceback (most recent call last):
  File "/opt/*********-box/main.py", line 593, in bot
    session.follow_likers(settings['followLikers']['users'], **settings['followLikers']['kwargs'])
  File "/opt/*********-box/libs/InstaPy/instapy/instapy.py", line 952, in follow_likers
    interact)
  File "/opt/*********-box/libs/InstaPy/instapy/instapy.py", line 1088, in follow_by_list
    self.logfolder)
  File "/opt/*********-box/libs/InstaPy/instapy/unfollow_util.py", line 764, in follow_user
    naply = get_action_delay("follow")
  File "/opt/*********-box/libs/InstaPy/instapy/util.py", line 1771, in get_action_delay
    custom_delay = config[action]
KeyError: 'follow'
INFO     [2019-07-04 15:22:31] [0x2815561e236] [**********] Tag [1/4]
INFO     [2019-07-04 15:22:31] [0x2815561e236] [**********] --> b'z\xc3\xbcrich'
INFO     [2019-07-04 15:22:39] [0x2815561e236] [**********] desired amount: 113  |  top posts [disabled]: 9  |  possible posts: 2350768
INFO     [2019-07-04 15:23:38] [0x2815561e236] [**********] Like# [1/113]
INFO     [2019-07-04 15:23:38] [0x2815561e236] [**********] https://www.instagram.com/p/BzfedOToGL9/
INFO     [2019-07-04 15:23:42] [0x2815561e236] [**********] Image from: b'm_aliaksandra'
INFO     [2019-07-04 15:23:42] [0x2815561e236] [**********] Link: b'https://www.instagram.com/p/BzfedOToGL9/'
INFO     [2019-07-04 15:23:42] [0x2815561e236] [**********] Description: b'\xd0\xbe\xd0\xba\xd1\x83\xd0\xbd\xd1\x83\xd0\xbb\xd0\xb8\xd1\x81\xd1\x8c \xd0\xbd\xd0\xb5 \xd0\xb2 \xd0\xb2\xd0\xbe\xd0\xb4\xd1\x83, \xd0\xbd\xd0\xbe \xd0\xb2 \xd0\xbd\xd0\xb5\xd1\x82\xd1\x80\xd0\xbe\xd0\xbd\xd1\x83\xd1\x82\xd1\x83\xd1\x8e (\xd0\xbd\xd1\x83, \xd0\xbf\xd0\xbe\xd1\x87\xd1\x82\xd0\xb8) \xd0\xbf\xd1\x80\xd0\xb8\xd1\x80\xd0\xbe\xd0\xb4\xd1\x83 \xd0\xb8 \xd0\xb1\xd1\x8b\xd1\x82 \xd0\xb0\xd0\xbb\xd1\x8c\xd0\xbf\xd0\xb8\xd0\xb9\xd1\x81\xd0\xba\xd0\xbe\xd0\xb9 \xd0\xb3\xd0\xbb\xd1\x83\xd0\xb1\xd0\xb8\xd0\xbd\xd0\xba\xd0\xb8 \xd1\x81 \xd0\xb8\xd1\x82\xd0\xb0\xd0\xbb\xd1\x8c\xd1\x8f\xd0\xbd\xd1\x81\xd0\xba\xd0\xb8\xd0\xbc\xd0\xb8 \xd0\xbc\xd0\xbe\xd1\x82\xd0\xb8\xd0\xb2\xd0\xb0\xd0\xbc\xd0\xb8'
INFO     [2019-07-04 15:23:42] [0x2815561e236] [**********] --> Image not liked: b'Mandatory language not fulfilled'
INFO     [2019-07-04 15:23:42] [0x2815561e236] [**********] Like# [2/113]
INFO     [2019-07-04 15:23:42] [0x2815561e236] [**********] https://www.instagram.com/p/BzfaVvYo1pC/
INFO     [2019-07-04 15:23:47] [0x2815561e236] [**********] Image from: b'kenny_bachelorette'
INFO     [2019-07-04 15:23:47] [0x2815561e236] [**********] Link: b'https://www.instagram.com/p/BzfaVvYo1pC/'
INFO     [2019-07-04 15:23:47] [0x2815561e236] [**********] Description: b'Leg\xc3\xa8re\n.\n.\n.\n#menfashion #menwithstyle #menwithstreetstyle #styleblogger #springoutfit #ootd #styleinspiration #fashionblogger #fashionable #themanity #streetstyle #instagood #instafashion #instadaily #photography #lightroom #switzerland #z\xc3\xbcrich #chic #suited'
INFO     [2019-07-04 15:23:47] [0x2815561e236] [**********] Location: b'Cape Town, Western Cape'
INFO     [2019-07-04 15:23:52] [0x2815561e236] [**********] User: 'kenny_bachelorette'  |> followers: 8845  |> following: 175  |> relationship ratio: 50.54
INFO     [2019-07-04 15:24:00] [0x2815561e236] [**********] --> Image Liked!
ERROR    [2019-07-04 15:24:00] [0x2815561e236] [**********] KeyError error occurred during like by tags.
Traceback (most recent call last):
  File "/opt/*********-box/main.py", line 602, in bot
    session.like_by_tags(settings['likeByTags']['tags'], **settings['likeByTags']['kwargs'])
  File "/opt/*********-box/libs/InstaPy/instapy/instapy.py", line 1811, in like_by_tags
    liked_img)
  File "/opt/*********-box/libs/InstaPy/instapy/like_util.py", line 696, in like_image
    naply = get_action_delay("like")
  File "/opt/*********-box/libs/InstaPy/instapy/util.py", line 1771, in get_action_delay
    custom_delay = config[action]
KeyError: 'like'
INFO     [2019-07-04 15:24:00] [0x2815561e236] [**********] Sessional Live Report:
	|> LIKED 0 images  |  ALREADY LIKED: 0
	|> COMMENTED on 0 images
	|> FOLLOWED 0 users  |  ALREADY FOLLOWED: 0
	|> UNFOLLOWED 0 users
	|> LIKED 0 comments
	|> REPLIED to 0 comments
	|> INAPPROPRIATE images: 0
	|> NOT VALID users: 1

On session start was FOLLOWING 1094 users & had 5444 FOLLOWERS
[Session lasted 9.62 minutes]
INFO     [2019-07-04 15:24:00] [0x2815561e236] [**********] Session ended!
```
> Probably `Settings.action_delays` is not set correctly when InstaPy object is initialized.

It is never set FYI, hence defaults should be used *for now*. See my quick fix. 
The problem was, that without this check https://github.com/timgrossmann/InstaPy/pull/4606/commits/b71563f7381e48c2bd8cccac1a861f720ddbb452 you could not really set any custom action_delays before. Pitty there are no tests, could be caught. I'll make PR with your fix.

Resolving patch:
diff --git a/instapy/__init__.py b/instapy/__init__.py
--- a/instapy/__init__.py
+++ b/instapy/__init__.py
@@ -8,5 +8,5 @@
 
 
 # __variables__ with double-quoted values will be available in setup.py
-__version__ = "0.5.4"
+__version__ = "0.5.5"
 
diff --git a/instapy/util.py b/instapy/util.py
--- a/instapy/util.py
+++ b/instapy/util.py
@@ -1762,9 +1762,10 @@ def get_action_delay(action):
     config = Settings.action_delays
 
     if (not config or
+            action not in config or
             config["enabled"] is not True or
             config[action] is None or
-            isinstance(config[action], (int, float))) is not True:
+            isinstance(config[action], (int, float)) is not True):
         return defaults[action]
 
     else:


Test patch:
diff --git a/tests/test_action_delays.py b/tests/test_action_delays.py
new file mode 100644
--- /dev/null
+++ b/tests/test_action_delays.py
@@ -0,0 +1,8 @@
+from instapy import util
+
+
+def test_default_values_returned():
+    assert util.get_action_delay('like') == 2
+    assert util.get_action_delay('comment') == 2
+    assert util.get_action_delay('follow') == 3
+    assert util.get_action_delay('unfollow') == 10