Note that there are some explanatory texts on larger screens.

plurals
  1. POoptimize python processing json retrieved from the fb-graph-api
    primarykey
    data
    text
    <p>i'm getting json data from the facebook-graph-api about:</p> <ol> <li>my relationship with my friends</li> <li>my friends relationships with each other. </li> </ol> <p>right now my program looks like this (in python pseudo code, please note some variables have been changed for privacy):</p> <pre><code>import json import requests # protected _accessCode = "someAccessToken" _accessStr = "?access_token=" + _accessCode _myID = "myIDNumber" r = requests.get("https://graph.facebook.com/" + _myID + "/friends/" + _accessStr) raw = json.loads(r.text) terminate = len(raw["data"]) # list used to store the friend/friend relationships a = list() for j in range(0, terminate + 1): # calculate terminating displacement: term_displacement = terminate - (j + 1) print("Currently processing: " + str(j) + " of " + str(terminate)) for dj in range(1, term_displacement + 1): # construct urls based on the raw data: url = "https://graph.facebook.com/" + raw["data"][j]["id"] + "/friends/" + raw["data"][j + dj]["id"] + "/" + _accessStr # visit site *THIS IS THE BOTTLENECK*: reqTemp = requests.get(url) rawTemp = json.loads(reqTemp.text) if len(rawTemp["data"]) != 0: # data dumps to list which dumps to file a.append(str(raw["data"][j]["id"]) + "," + str(rawTemp["data"][0]["id"])) outputFile = "C:/Users/franklin/Documents/gen/friendsRaw.csv" output = open(outputFile, "w") # write all me/friend relationship to file for k in range(0, terminate): output.write(_myID + "," + raw["data"][k]["id"] + "\n") # write all friend/friend relationships to file for i in range(0, len(a)): output.write(a[i]) output.close() </code></pre> <p>So what its doing is: first it calls my page and gets my friend list (this is allowed through the facebook api using an access_token) calling a friend's friend list is NOT allowed but I can work around that by requesting a relationship between a friend on my list and another friend on my list. so in part two (indicated by the double for loops) i'm making another request to see if some friend, a, is also a friend of b, (both of which are on my list); if so there will be a json object of length one with friend a's name.</p> <p>but with about 357 friends there's literally thousands of page requests that need to be made. in other words the program is spending a lot of time just waiting around for the json-requests. </p> <p>my question is then can this be rewritten to be more efficient? currently, due to security restrictions, calling a friend's friend list attribute is disallowed. and it doesn't look like the api will allow this. are there any python tricks that can make this run faster? maybe parallelism? </p> <p><strong>Update</strong> modified code is pasted below in the answers section.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload