First of all, I am totally new at python. I am a graphic designer and I need to get group members photos for a group logo. I have found this: https://github.com/lionaneesh/IIITD-Students-Collage and it pretty much should do the thing I need, but apparently I am doing something wrong and it does not work as intended.
When I execute this script:
import json
from urllib2 import urlopen
fp = open("test2.txt")
data = json.loads(fp.read())
fp.close()
user_photos = {} # id -> [User's Name, Photo URL]
for user in data["data"]:
print user
page = urlopen("http://graph.facebook.com/" + user["id"] + "?fields=picture")
page_data = json.loads(page.read())
photo_url = page_data["picture"]["data"]["url"]
user_photos[user["id"]] = [user["name"], photo_url]
fp = open("user_photos.json", "w")
fp.write(json.dumps(user_photos))
I get this error:
Traceback (most recent call last):
File "C:\test.py", line 11, in <module>
for user in data["data"]:
KeyError: 'data'
>>>
Could someone explain to me how to fix it or where to seek for help?
edit: this is how the data in text2.txt looks:
{
"id": "1390694364479028",
"members": {
"data": [
{
"name": "Patryk Wiśniewski",
"administrator": false,
"id": "321297624692717"
},
{
"name": "Backed PL",
"administrator": false,
"id": "1440205746235525"
},
and so on, with other group members infos
Looking at the docs, you should have exactly the same structure as the following in your txt file bar the details.
{
"data": [
{
"name": "Arushi Jain",
"administrator": false,
"id": "100000582289046"
},
{
"name": "Ajay Yadav",
"administrator": false,
"id": "100004213058283"
},
and so on ........
],
"paging": {
"next": "https://graph.facebook.com/114462201948585/members?limit=5000&offset=5000&__after_id=712305377"
}
}
{
{
"data": [ # how yours should look
{
"name": "Patryk Wiśniewski",
"administrator": false,
"id": "321297624692717"
},
{
"name": "Patryk Kurowski",
"administrator": false,
"id": "1429534777317507"
},
{
"name": "Jan Konieczny",
"administrator": false,
"id": "852450774783365"
}
],
"paging": {
"next": "https://graph.facebook.com/114462201948585/members?limit=5000&offset=5000&__after_id=712305377"
}
}
That is the very first thing that is executed in the loop so if it does not match exactly then it will fail as it does in your error.