Not able to extract my friend images from Facebook API using python - python

I have got this response from the Facebook Graph API:
{
"taggable_friends": {
"data": [
{
"name": "Friend1 Name",
"picture": {
"data": {
"url": "https://fb-s-c-a.akamaihd.net/h-ak-fbx/v/t1.0-1/p200x200/completeUrl1"
}
},
"id": "response1d"
},
{
"name": "Friend2 name",
"picture": {
"data": {
"url": "https://fb-s-a-a.akamaihd.net/h-ak-fbx/v/t1.0-1/p200x200/completeURL2"
}
},
"id": "responseid2"
}
],
"paging": {
"cursors": {
"before": "xyz",
"after": "abc"
},
"next": "NextpageURl"
}
},
"id": "xxxxxxxxx"
}
I am willing to extract the URL part of the graph API response with field taggable_friends.
I have tried something like this:
for friends in data_json_liked_pages['taggable_friends']['data']:
friend_url = friends['picture']['data']['url']
print friend_url
I am getting the following error:
Exception Type: TypeError
Exception Value: list indices must be integers, not str
What can I do to improve this?

Related

How to use Here Api to get Max speed limit of road using latitude and longitude in python environment

We are new to Here Api,
Our team is working on one transportation project in which we required to get max speed limit of road using vehicle (latitude ,longitude).
From last few days we are trying to figure out which api should we use in HereApi to achieve what we want.
Regarding this documentation of HERE Route Matching 8: https://developer.here.com/documentation/route-matching/api-reference.html
Send please POST request like:
https://routematching.hereapi.com/v8/match/routelinks?apikey=LXX1Axs75efnlFAlgbPxVekPDR0Hz6rTcRHQMT0EvQs&routeMatch=1&mode=fastest;car;traffic:disabled;&attributes=SPEED_LIMITS_FCn(*),LINK_ATTRIBUTE_FCn(*),TRAFFIC_PATTERN_FCn(*),TRUCK_SPEED_LIMITS_FCn(*),SPEED_LIMITS_VAR_FCn(*),SPEED_LIMITS_COND_FCn(*)
With in body:
LATITUDE,LONGITUDE
37.4201866,15.049515
See please attached the Postman collection for test of HERE Rote Match API v8 https://developer.here.com/documentation/route-matching/dev_guide/index.html :
{
"info": {
"_postman_id": "2563b7cc-2d62-4485-bb64-2f9561318aa2",
"name": "RMEspeed_limit",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "1051680"
},
"item": [
{
"name": "speed_limit",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "LATITUDE,LONGITUDE\r\n37.4201866,15.049515"
},
"url": {
"raw": "https://routematching.hereapi.com/v8/match/routelinks?apikey=LXX1Axs75efnlFAlgbPxVekPDR0Hz6rTcRHQMT0EvQs&routeMatch=1&mode=fastest;car;traffic:disabled;&attributes=SPEED_LIMITS_FCn(*),LINK_ATTRIBUTE_FCn(*),TRAFFIC_PATTERN_FCn(*),TRUCK_SPEED_LIMITS_FCn(*),SPEED_LIMITS_VAR_FCn(*),SPEED_LIMITS_COND_FCn(*) ",
"protocol": "https",
"host": [
"routematching",
"hereapi",
"com"
],
"path": [
"v8",
"match",
"routelinks"
],
"query": [
{
"key": "apikey",
"value": "LXX1Axs75efnlFAlgbPxVekPDR0Hz6rTcRHQMT0EvQs"
},
{
"key": "routeMatch",
"value": "1"
},
{
"key": "mode",
"value": "fastest;car;traffic:disabled;"
},
{
"key": "attributes",
"value": "SPEED_LIMITS_FCn(*),LINK_ATTRIBUTE_FCn(*),TRAFFIC_PATTERN_FCn(*),TRUCK_SPEED_LIMITS_FCn(*),SPEED_LIMITS_VAR_FCn(*),SPEED_LIMITS_COND_FCn(*) "
}
]
}
},
"response": []
}
]
}
An apikey and coordinate you specify your own for sure.
About all layers attributes you can see this example: https://demo.support.here.com/pde/maps?url_root=pde.api.here.com
In Python you should use normal POST request using some http libraries developed for Python:
https://www.geeksforgeeks.org/get-post-requests-using-python/

replace nested document array mongodb with python

i have this document in mongodb
{
"_id": {
"$oid": "62644af0368cb0a46d7c2a95"
},
"insertionData": "23/04/2022 19:50:50",
"ipfsMetadata": {
"Name": "data.json",
"Hash": "Qmb3FWgyJHzJA7WCBX1phgkV93GiEQ9UDWUYffDqUCbe7E",
"Size": "431"
},
"metadata": {
"sessionDate": "20220415 17:42:55",
"dataSender": "user345",
"data": {
"height": "180",
"weight": "80"
},
"addtionalInformation": [
{
"name": "poolsize",
"value": "30m"
},
{
"name": "swimStyle",
"value": "mariposa"
},
{
"name": "modality",
"value": "swim"
},
{
"name": "gender-title",
"value": "schoolA"
}
]
},
"fileId": {
"$numberLong": "4"
}
}
I want to update nested array document, for instance the name with gender-tittle. This have value schoolA and i want to change to adult like the body. I give the parameter number of fileId in the post request and in body i pass this
post request : localhost/sessionUpdate/4
and body:
{
"name": "gender-title",
"value": "adultos"
}
flask
#app.route('/sessionUpdate/<string:a>', methods=['PUT'])
def sessionUpdate(a):
datas=request.json
r=str(datas['name'])
r2=str(datas['value'])
print(r,r2)
r3=collection.update_one({'fileId':a, 'metadata.addtionalInformation':r}, {'$set':{'metadata.addtionalInformation.$.value':r2}})
return str(r3),200
i'm getting the 200 but the document don't update with the new value.
As you are using positional operator $ to work with your array, make sure your select query is targeting array element. You can see in below query that it is targeting metadata.addtionalInformation array with the condition that name: "gender-title"
db.collection.update({
"fileId": 4,
"metadata.addtionalInformation.name": "gender-title"
},
{
"$set": {
"metadata.addtionalInformation.$.value": "junior"
}
})
Here is the Mongo playground for your reference.

API.ai Actions on Google API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: ": Cannot find field."

I am using python to create webhook for Assistat app. I am able to ask user for location permission, but as soon as user gives consent, I receive following error
UnparseableJsonResponse
API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: ": Cannot find field.".
I have checked my webhook server and no request comes to it. This looks like some issue at API.ai side. Below is the Debug response from Actions console when using Python client
{
"assistantToAgentDebug": {
"curlCommand": "curl -v '<URL>'{\"user\":{\"userId\":\"<USED_ID>\",\"locale\":\"en-US\"},\"conversation\":{\"conversationId\":\"1504592665563\",\"type\":\"ACTIVE\",\"conversationToken\":\"[\\\"defaultwelcomeintent-followup\\\"]\"},\"inputs\":[{\"intent\":\"actions.intent.PERMISSION\",\"rawInputs\":[{\"inputType\":\"VOICE\",\"query\":\"yes\"}],\"arguments\":[{\"name\":\"PERMISSION\",\"textValue\":\"true\"}]}],\"surface\":{\"capabilities\":[{\"name\":\"actions.capability.AUDIO_OUTPUT\"},{\"name\":\"actions.capability.SCREEN_OUTPUT\"}]},\"device\":{\"location\":{\"coordinates\":{\"latitude\":37.4219806,\"longitude\":-122.0841979}}},\"isInSandbox\":true}'",
"assistantToAgentJson": {
"user": {
"userId": "<USED_ID>",
"locale": "en-US"
},
"conversation": {
"conversationId": "1504592665563",
"type": "ACTIVE",
"conversationToken": "[\"defaultwelcomeintent-followup\"]"
},
"inputs": [
{
"intent": "actions.intent.PERMISSION",
"rawInputs": [
{
"inputType": "VOICE",
"query": "yes"
}
],
"arguments": [
{
"name": "PERMISSION",
"textValue": "true"
}
]
}
],
"surface": {
"capabilities": [
{
"name": "actions.capability.AUDIO_OUTPUT"
},
{
"name": "actions.capability.SCREEN_OUTPUT"
}
]
},
"device": {
"location": {
"coordinates": {
"latitude": 37.4219806,
"longitude": -122.0841979
}
}
},
"isInSandbox": true
}
},
"agentToAssistantDebug": {
"agentToAssistantJson": {
"message": "Unexpected apiai response format: Empty speech response",
"apiResponse": {
"id": "<ID>",
"timestamp": "2017-09-05T06:24:41.711Z",
"lang": "en",
"result": {},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "1504592665563"
}
}
},
"sharedDebugInfo": [
{
"name": "GOOGLE_SYSTEM_ACTION",
"debugInfo": "Your query was handled by Actions on Google."
},
{
"name": "GOOGLE_SYSTEM_ACTION",
"debugInfo": "Your query was handled by Actions on Google."
},
{
"name": "ResponseValidation",
"subDebugEntry": [
{
"name": "UnparseableJsonResponse",
"debugInfo": "API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: \": Cannot find field.\"."
}
]
}
]
}
Using Python library Flask-Assistant
How can I resolve this issue?
UPDATE
Node JS client works... what is the issue with Python client?
Action Console Debug response
{
"assistantToAgentDebug": {
"curlCommand": "curl -v '<URL>'{\"user\":{\"userId\":\"<USER_ID>\",\"locale\":\"en-US\"},\"conversation\":{\"conversationId\":\"<ID>\",\"type\":\"ACTIVE\",\"conversationToken\":\"[\\\"_actions_on_google_\\\",\\\"defaultwelcomeintent-followup\\\"]\"},\"inputs\":[{\"intent\":\"actions.intent.PERMISSION\",\"rawInputs\":[{\"inputType\":\"VOICE\",\"query\":\"yes\"}],\"arguments\":[{\"name\":\"PERMISSION\",\"textValue\":\"true\"}]}],\"surface\":{\"capabilities\":[{\"name\":\"actions.capability.AUDIO_OUTPUT\"}]},\"device\":{\"location\":{\"coordinates\":{\"latitude\":37.4219806,\"longitude\":-122.0841979},\"formattedAddress\":\"Googleplex, Mountain View, CA 94043, United States\",\"zipCode\":\"94043\",\"city\":\"Mountain View\"}},\"isInSandbox\":true}'",
"assistantToAgentJson": {
"user": {
"userId": "<USER_ID>",
"locale": "en-US"
},
"conversation": {
"conversationId": "<ID>",
"type": "ACTIVE",
"conversationToken": "[\"_actions_on_google_\",\"defaultwelcomeintent-followup\"]"
},
"inputs": [
{
"intent": "actions.intent.PERMISSION",
"rawInputs": [
{
"inputType": "VOICE",
"query": "yes"
}
],
"arguments": [
{
"name": "PERMISSION",
"textValue": "true"
}
]
}
],
"surface": {
"capabilities": [
{
"name": "actions.capability.AUDIO_OUTPUT"
}
]
},
"device": {
"location": {
"coordinates": {
"latitude": 37.4219806,
"longitude": -122.0841979
},
"formattedAddress": "Googleplex, Mountain View, CA 94043, United States",
"zipCode": "94043",
"city": "Mountain View"
}
},
"isInSandbox": true
}
},
"agentToAssistantDebug": {
"agentToAssistantJson": {
"conversationToken": "[\"_actions_on_google_\",\"defaultwelcomeintent-followup\"]",
"expectUserResponse": true,
"expectedInputs": [
{
"inputPrompt": {
"richInitialPrompt": {
"items": [
{
"simpleResponse": {
"textToSpeech": "Given permission"
}
}
]
}
},
"possibleIntents": [
{
"intent": "assistant.intent.action.TEXT"
}
]
}
],
"responseMetadata": {
"status": {
"code": 14
},
"queryMatchInfo": {
"queryMatched": true,
"intent": "Default Welcome Intent - fallback"
}
}
}
}
}
Request from Actions server to my Node JS webhook server
{ originalRequest:
{ source: 'google',
version: '2',
data:
{ isInSandbox: true,
surface: [Object],
inputs: [Array],
user: [Object],
device: [Object],
conversation: [Object] } },
id: '<ID>',
timestamp: '2017-09-06T05:43:21.342Z',
lang: 'en',
result:
{ source: 'agent',
resolvedQuery: 'actions_intent_PERMISSION',
speech: '',
action: 'DefaultWelcomeIntent.DefaultWelcomeIntent-fallback',
actionIncomplete: false,
parameters: {},
contexts: [ [Object], [Object], [Object], [Object], [Object] ],
metadata:
{ intentId: '<ID>',
webhookUsed: 'true',
webhookForSlotFillingUsed: 'false',
nluResponseTime: 2,
intentName: 'Default Welcome Intent - fallback' },
fulfillment: { speech: 'Given permission', messages: [Array] },
score: 1 },
status: { code: 200, errorType: 'success' },
sessionId: '<SID>'
}
API.ai Intent settings
The most likely reason you're not getting any hits on your webhook is that you don't have an intent registered to get the reply.
You can do this by creating an Intent with the Event set to actions_intent_PERMISSION.
See also the following answers on SO:
Unable to accept the permission prompt on Actions on Google
Permission response not handled correctly

How to parse JSON in a Django View

I post some JSON to a view. I want to now parse the data and add it to my database.
I need to get the properties name and theme and iterate over the array pages. My JSON is as follows:
{
"name": "xaAX",
"logo": "",
"theme": "b",
"fullSiteLink": "http://www.hello.com",
"pages": [
{
"id": "1364484811734",
"name": "Page Name",
"type": "basic",
"components": {
"img": "",
"text": ""
}
},
{
"name": "Twitter",
"type": "twitter",
"components": {
"twitter": {
"twitter-username": "zzzz"
}
}
}
]
}
Here is what I have so far:
def smartpage_create_ajax(request):
if request.POST:
# get stuff and loop over each page?
return HttpResponse('done')
python provides json to encode/decode json
import json
json_dict = json.loads(request.POST['your_json_data'])
json_dict['pages']
[
{
"id": "1364484811734",
"name": "Page Name",
"type": "basic",
"components": {
"img": "",
"text": ""
}
},
{
"name": "Twitter",
"type": "twitter",
"components": {
"twitter": {
"twitter-username": "zzzz"
}
}
},
}
]

Check whether the JSON (object property exists) & print it as unicode decoded

I get the following data from the Instagram API, I m trying to get the text property from the caption using the following code:
data = simplejson.load(info) # info is retrieved using the urllib2
for post in data['data']:
if post['caption'] is not "null":
try:
post['caption']['text']
except NameError:
post['caption']['text'] = 0
if post['caption']['text'] is not 0:
print post['caption']['text']
But I keep getting the TypeError: 'NoneType' object has no attribute '__getitem__' error + UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: cha
racter maps to <undefined> error while printing the unicode strings
Here is the JSON data that is retrieved and stored in info
{
"pagination":{
"next_url":"https:\/\/api.instagram.com\/v1\/users\/self\/feed?access_token=184046392.f59def8.c5726b469ad2462f85c7cea5f72083c0&count=3&max_id=247821697921944007_6064449",
"next_max_id":"247821697921944007_6064449"
},
"meta":{
"code":200
},
"data":[
{
"attribution":null,
"tags":[
"usausausa",
"olympics"
],
"type":"image",
"location":{
"latitude":37.785929,
"name":"Aquatech Swim School",
"longitude":-122.278718,
"id":16343815
},
"comments":{
"count":0,
"data":[
]
},
"filter":"Valencia",
"created_time":"1343765260",
"link":"http:\/\/instagr.am\/p\/NwhEktJvEp\/",
"likes":{
"count":0,
"data":[
]
},
"images":{
"low_resolution":{
"url":"http:\/\/distilleryimage1.s3.amazonaws.com\/61d9cbeedb4b11e1b8e822000a1e8b8e_6.jpg",
"width":306,
"height":306
},
"thumbnail":{
"url":"http:\/\/distilleryimage1.s3.amazonaws.com\/61d9cbeedb4b11e1b8e822000a1e8b8e_5.jpg",
"width":150,
"height":150
},
"standard_resolution":{
"url":"http:\/\/distilleryimage1.s3.amazonaws.com\/61d9cbeedb4b11e1b8e822000a1e8b8e_7.jpg",
"width":612,
"height":612
}
},
"caption":{
"created_time":"1343765325",
"text":"Part of my job to watch swimming. #olympics #USAUSAUSA",
"from":{
"username":"kissinkatkelly",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_4672491_75sq_1341713095.jpg",
"id":"4672491",
"full_name":"kissinkatkelly"
},
"id":"247843973390332239"
},
"user_has_liked":false,
"id":"247843429330383145_4672491",
"user":{
"username":"kissinkatkelly",
"website":"",
"bio":"I sing the body electric\r\n\r\nBay Area, CA",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_4672491_75sq_1341713095.jpg",
"full_name":"kissinkatkelly",
"id":"4672491"
}
},
{
"attribution":null,
"tags":[
],
"type":"image",
"location":{
"latitude":36.020832061,
"longitude":-121.548835754
},
"comments":{
"count":4,
"data":[
{
"created_time":"1343763343",
"text":"I wanna cut your mustache off. \ue313\ue313\ue313\ue004",
"from":{
"username":"glorias_noodles",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_24432017_75sq_1343633079.jpg",
"id":"24432017",
"full_name":"\ue340MeGusta Gloria\ue340"
},
"id":"247827343962686703"
},
{
"created_time":"1343763844",
"text":"Ahaha^",
"from":{
"username":"chloe_carter",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_44766575_75sq_1343509145.jpg",
"id":"44766575",
"full_name":"Chloe Carter"
},
"id":"247831551235474746"
},
{
"created_time":"1343763958",
"text":"Amazingg thoo",
"from":{
"username":"saulyp",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_18051263_75sq_1335648741.jpg",
"id":"18051263",
"full_name":"Saul Perez"
},
"id":"247832506790200642"
},
{
"created_time":"1343764298",
"text":"#popesaintvictor where is that? :o",
"from":{
"username":"youknow_jameson",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_194001394_75sq_1343613135.jpg",
"id":"194001394",
"full_name":"Jameson Medina"
},
"id":"247835358103225704"
}
]
},
"filter":"Normal",
"created_time":"1343763202",
"link":"http:\/\/instagr.am\/p\/NwdJRpBkfX\/",
"likes":{
"count":611,
"data":[
{
"username":"jakyvedder",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_18148021_75sq_1336938690.jpg",
"id":"18148021",
"full_name":"Janycken"
},
{
"username":"nadjasinbruker",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_174576513_75sq_1343582260.jpg",
"id":"174576513",
"full_name":"Nadja"
},
{
"username":"vivi11",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_1193390_75sq_1338169730.jpg",
"id":"1193390",
"full_name":"Viviana Rodriguez"
},
{
"username":"me_4_eva",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_181498114_75sq_1343506811.jpg",
"id":"181498114",
"full_name":"Kelly"
},
{
"username":"roxczajkowski",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_9367244_75sq_1343696914.jpg",
"id":"9367244",
"full_name":"Czajkowski \u041a\u043e\u0440\u0448\u0443\u043d\u043e\u0432\u0430"
},
{
"username":"arsi1989",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_201586134_75sq_1343761866.jpg",
"id":"201586134",
"full_name":"Arsalan MemOn"
},
{
"username":"puppyluva",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_201579504_75sq_1343760137.jpg",
"id":"201579504",
"full_name":"puppyluva"
},
{
"username":"paulinamurr",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_49364097_75sq_1343428499.jpg",
"id":"49364097",
"full_name":"Paulina Murray"
},
{
"username":"_mcquadeface_",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_20679753_75sq_1327617901.jpg",
"id":"20679753",
"full_name":"Emily McQuade"
}
]
},
"images":{
"low_resolution":{
"url":"http:\/\/distilleryimage11.s3.amazonaws.com\/96cd5b90db4611e1827612313814176c_6.jpg",
"width":306,
"height":306
},
"thumbnail":{
"url":"http:\/\/distilleryimage11.s3.amazonaws.com\/96cd5b90db4611e1827612313814176c_5.jpg",
"width":150,
"height":150
},
"standard_resolution":{
"url":"http:\/\/distilleryimage11.s3.amazonaws.com\/96cd5b90db4611e1827612313814176c_7.jpg",
"width":612,
"height":612
}
},
"caption":null,
"user_has_liked":false,
"id":"247826160271378391_605400",
"user":{
"username":"popesaintvictor",
"website":"http:\/\/popesaintvictor.com",
"bio":"artist, friend, and brand designer for blood:water mission in nashville, tennessee. \r\n\r\nhusband to #ohsoamy\r\n\r\nbe inspired. be awesome.\r\n",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_605400_75sq_1342893414.jpg",
"full_name":"pope saint victor",
"id":"605400"
}
},
{
"attribution":null,
"tags":[
],
"type":"image",
"location":{
"latitude":40.738834381,
"longitude":-73.994163513
},
"comments":{
"count":6,
"data":[
{
"created_time":"1343762733",
"text":"Nice :)",
"from":{
"username":"belieberpernille99",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_186196238_75sq_1341347304.jpg",
"id":"186196238",
"full_name":"official belieber"
},
"id":"247822232418879860"
},
{
"created_time":"1343762748",
"text":"Those pants \ud83d\ude0d",
"from":{
"username":"morganmarzulli",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_29155556_75sq_1337653621.jpg",
"id":"29155556",
"full_name":"morganmarzulli"
},
"id":"247822351461615990"
},
{
"created_time":"1343762777",
"text":"That outfit is to die for. I love her pants! They're so fun. \ud83d\udc4d",
"from":{
"username":"ninavnegron",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_18421926_75sq_1343356820.jpg",
"id":"18421926",
"full_name":"Nina V"
},
"id":"247822600452278654"
},
{
"created_time":"1343762782",
"text":"YEAH THEIR COOL",
"from":{
"username":"belieberpernille99",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_186196238_75sq_1341347304.jpg",
"id":"186196238",
"full_name":"official belieber"
},
"id":"247822639375419775"
},
{
"created_time":"1343762782",
"text":"Another day another shoot! Look out for me and my chicest staff on #racked!",
"from":{
"username":"rebeccaminkoff",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_6064449_75sq_1332274636.jpg",
"id":"6064449",
"full_name":"Rebecca Minkoff"
},
"id":"247822641497737600"
},
{
"created_time":"1343764430",
"text":"Hot mama! Miss you!",
"from":{
"username":"ashleekoston",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_12925089_75sq_1340081366.jpg",
"id":"12925089",
"full_name":"ashleekoston"
},
"id":"247836463642020446"
}
]
},
"filter":"Walden",
"created_time":"1343762670",
"link":"http:\/\/instagr.am\/p\/NwcIVwRYnH\/",
"likes":{
"count":528,
"data":[
{
"username":"claireyoung48",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_40091175_75sq_1338778945.jpg",
"id":"40091175",
"full_name":"claireyoung48"
},
{
"username":"l_christine_k",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_14871166_75sq_1341962995.jpg",
"id":"14871166",
"full_name":"Lauren Kawano"
},
{
"username":"grcdaly",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_41426567_75sq_1335023058.jpg",
"id":"41426567",
"full_name":"\u24bc\u24c7\u24b6\u24b8\u24ba \u24b9\u24b6\u24c1\u24e8"
},
{
"username":"vanessaalcalaa",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_18115905_75sq_1342828120.jpg",
"id":"18115905",
"full_name":"Vanessa Alcala"
},
{
"username":"makennalenover",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_6464394_75sq_1343268613.jpg",
"id":"6464394",
"full_name":"Makenna Lenover"
},
{
"username":"heyitsmaryanne",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_623979_75sq_1340838647.jpg",
"id":"623979",
"full_name":"Maryanne L"
},
{
"username":"sarabeen",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg",
"id":"6463387",
"full_name":"sarabeen"
},
{
"username":"boldincrimson",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_191122242_75sq_1341889110.jpg",
"id":"191122242",
"full_name":"Pilar Chapa"
},
{
"username":"elizzabethhope",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_6761399_75sq_1342553102.jpg",
"id":"6761399",
"full_name":"Lizz\u270c"
}
]
},
"images":{
"low_resolution":{
"url":"http:\/\/distilleryimage7.s3.amazonaws.com\/59bc708edb4511e1b7ea22000a1cbb16_6.jpg",
"width":306,
"height":306
},
"thumbnail":{
"url":"http:\/\/distilleryimage7.s3.amazonaws.com\/59bc708edb4511e1b7ea22000a1cbb16_5.jpg",
"width":150,
"height":150
},
"standard_resolution":{
"url":"http:\/\/distilleryimage7.s3.amazonaws.com\/59bc708edb4511e1b7ea22000a1cbb16_7.jpg",
"width":612,
"height":612
}
},
"caption":null,
"user_has_liked":false,
"id":"247821697921944007_6064449",
"user":{
"username":"rebeccaminkoff",
"website":"http:\/\/www.rebeccaminkoff.com",
"bio":"The Downtown Romantic. My life, my work, my world.",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_6064449_75sq_1332274636.jpg",
"full_name":"Rebecca Minkoff",
"id":"6064449"
}
}
]
}
You don't need the intricate tests on wether 'text' is present for the post caption.
This code works well with the JSON string you posted:
for post in data['data']:
if post.get('caption'):
print post['caption'].get('text', 0)
Furthermore, you could be more defensive and refer to data.get('data', []) when starting the loop in case Instagram sends you empty JSON.
Basically when json loads and deserializes your object, null in JSON will become None in python.
So your line of:
if post['caption'] is not 'null':
Should become:
if post['caption']:

Categories