In [2]:
import requests
import urllib
import json
import csv
import time
token = (open("../apitoken.txt", "r")).read()
In [10]:
def send_get(URL):
#function send get request and return results in json
try:
response = requests.get(
url=URL,
headers={
"Authorization": "Bearer "+token,
"Content-Type": "application/json",
},
allow_redirects=True
)
status = response.status_code
#print("URL: {} , Status: {}".format(url, status))
print("Link called by function: "+URL)
if status < 300:
return response.json()
#res = response.json()
else:
print('Response HTTP Status Code: {status_code}'.format(
status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(
content=response.content))
except requests.exceptions.RequestException:
print("HTTP Request Failed")
In [11]:
def checkForNext(jsonResult):
#takes json from API get call and returns the URL for the next page if there is one
#Assigns x to false in the event that there is no next page
if jsonResult["links"]["next"] is not None:
#print(jsonResult["links"]["next"])
newURL = jsonResult["links"]["next"]
print(newURL)
return newURL
else:
global x
x = False
print("no more links")
In [12]:
def getContributorIdAndPermissions(my_url):
# Takes link to first page of contributors (assigned to my url)
# Goes through the json for each contributor "node" and returns their ID and permissions level
global x
x = True
list_of_contr = []
while x == True:
jsonFromCall = send_get(my_url)
for i in range(0, (len(jsonFromCall["data"]))):
contributor_id = jsonFromCall["data"][i]["embeds"]["users"]["data"]["id"]
permissions = jsonFromCall["data"][i]["attributes"]["permission"]
print("User ID: {}, Permission level: {}".format(contributor_id, permissions))
contributor = [contributor_id, permissions]
list_of_contr.append(contributor)
print("~~~~~ THIS IS BREAK 1 ~~~~~")
my_url = checkForNext(jsonFromCall)
#print(x)
#con_id = jsonFromInitialCall["data"][0]["embeds"]["users"]["data"]["id"] #only the first ID from results
#print(con_id)
#print(json.dumps(con_id))
time.sleep(2)
print("Done")
#print(list_of_contr)
return list_of_contr
# Almost works. Currently not changing to false in the function for some reason
In [13]:
my_url = "https://api.osf.io/v2/nodes/9a5eh/contributors/?"
my_contributor_list = getContributorIdAndPermissions(my_url)
In [14]:
print(my_contributor_list)
In [ ]: