11

I'm using this client python-instagram with Python 3.4.3 on MacOS.

Here are my steps:

  • Registered a new client on instagram, received client_id and client_secret
  • Pip install python-instagram
  • Copy sample_app.py to my mac

I followed the instructions on Sample app, I successfully authorized my app via instagram and tried this list of examples, but none of them worked. After my click the <h2> header and counter of API requests changes and I see Remaining API Calls = 486/500.

If I try to get User Recent Media an exception KeyError: 'data' shows in my terminal. If I delete try - except construction, leaving block in try, when I'll see 'Error: 500 Internal Server Error'.

Here is the traceback:

Traceback (most recent call last):
File "/Users/user/.envs/insta/lib/python3.4/site-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/Users/user/.envs/insta/lib/python3.4/site-packages/bottle.py", line 1732, in wrapper
rv = callback(*a, **ka)
File "sample_app.py", line 79, in on_recent
recent_media, next = api.user_recent_media()
File "/Users/user/.envs/insta/lib/python3.4/site-packages/instagram/bind.py", line 197, in _call
return method.execute()
File "/Users/user/.envs/insta/lib/python3.4/site-packages/instagram/bind.py", line 189, in execute
content, next = self._do_api_request(url, method, body, headers)
File "/Users/user/.envs/insta/lib/python3.4/site-packages/instagram/bind.py", line 151, in _do_api_request
obj = self.root_class.object_from_dictionary(entry)
File "/Users/user/.envs/insta/lib/python3.4/site-packages/instagram/models.py", line 99, in object_from_dictionary
for comment in entry['comments']['data']:
KeyError: 'data'

All the code I used is from the sample of the official python API client by Instagram.

Forge
  • 6,538
  • 6
  • 44
  • 64
Andrei Alekseev
  • 367
  • 3
  • 9

4 Answers4

22

There is an open Github issue for this bug, a fix was sent, but it's not merged yet.

Add the one line fix to models.py on your installed package.

Open with sudo:

sudo vi /Library/Python/2.7/site-packages/instagram/models.py  # Use relevant python version 

On line 99, add this:

if "data" in entry["comments"]:

Correct indentation on next two lines:

       for comment in entry['comments']['data']: 
           new_media.comments.append(Comment.object_from_dictionary(comment))
Forge
  • 6,538
  • 6
  • 44
  • 64
  • 1
    That worked for me! Some bug... I tested it on Python 3.4. – First Last Mar 19 '16 at 23:34
  • It took me a while to discover this bug and "hack" it but I can't fix it on our dockers because they get build from the pip sources. I really hope they merge this fix into the main branch. – anber May 04 '16 at 10:31
  • I also have installed python-instagram via pip, but could solve the problem following @Forge instruction. If you type `pip uninstall python-instagram`, the system will show a list of the files to be uninstalled, with respective locations, including models.py. Say "n" to "Proceed (y/n)?", to NOT uninstall python-instagram, and now you know where the file is. – Leandro Guedes Dec 20 '16 at 13:26
  • 1
    What if I want to get comments of the media? With this way, it is possible to get the media info like create time, link, etc. It is only a simple way to pass the error, but still it does not solve the main problem. You cannot get the comments of the specific media. – Mehmet Kagan Kayaalp Feb 08 '17 at 13:26
  • @waterkinq I've been wondering the same thing. Does this mean we can't get comments, or that we'll only get comments when they do exist (avoiding error when they don't)? – Cary Shindell Jul 06 '17 at 13:56
  • @CaryShindell This solved my problem: for comment in entry['comments'].get('data', []): new_media.comments.append(Comment.object_from_dictionary(comment)) – Mehmet Kagan Kayaalp Jul 06 '17 at 14:22
  • Ok, doesn't throw an error, but gives empty lists for comments (for posts where I know there are some) – Cary Shindell Jul 06 '17 at 14:38
5

It appears that there is a bug in models.py. If you comment out line 99 and 100 in that file, the "sample app" will work, or at least appears to work. Obviously, this is not a "real" fix but it does show that it is not a problem with the sample Python program, or Instagram.

    Line 99  #  for comment in entry['comments']['data']:
    Line 100 #      new_media.comments.append(Comment.object_from_dictionary(comment))
1

+1 for answer from @forge

For docker users (as asked in a comment), fork the python-instagram repo, edit, and then pip install via github.

Or just use someone else's fork with the following line in your Dockerfile:

pip install git+https://github.com/zgazak/python-instagram

zgazak
  • 31
  • 3
0

This is not really an answer just a quick workaround based on the answer by @forge when you are working in environments like docker or an environment that doesn't have a readable terminal.

sed -i '99,100 s/^/#/' /usr/local/lib/python3.5/site-packages/instagram/models.py
  • 1
    If it's "not really an answer", then you should leave it as a comment when you have enough reputation to leave comments. – Arya McCarthy May 24 '17 at 04:10