In [2]:
import requests
import urllib
import json
import csv
import time

token = (open("../apitoken.txt", "r")).read()

Psudocode

  • Give send_get() a URL with contributors
  • Return JSON
  • Add each contributor from that URL to a list of contributors
  • Check if there is a "Next" url
  • If there is a "Next" url, pass it to send_get()
  • Repeat

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)


Link called by function: https://api.osf.io/v2/nodes/9a5eh/contributors/?
User ID: 3jzys, Permission level: admin
User ID: 7jwh6, Permission level: admin
User ID: 6nywd, Permission level: admin
User ID: utb5k, Permission level: admin
User ID: 2kfwy, Permission level: write
User ID: f77p3, Permission level: write
User ID: hhdup, Permission level: write
User ID: vkthj, Permission level: write
User ID: 7595u, Permission level: admin
User ID: czszc, Permission level: write
~~~~~ THIS IS BREAK 1 ~~~~~
https://api.osf.io/v2/nodes/9a5eh/contributors/?page=2
Link called by function: https://api.osf.io/v2/nodes/9a5eh/contributors/?page=2
User ID: vutud, Permission level: write
User ID: bz3n5, Permission level: write
User ID: h6zc6, Permission level: write
User ID: 9j2ay, Permission level: write
User ID: 5kmjn, Permission level: write
User ID: m6wyb, Permission level: write
User ID: mec2f, Permission level: write
User ID: kfc3y, Permission level: write
User ID: 9t3xc, Permission level: write
User ID: nnjvk, Permission level: write
~~~~~ THIS IS BREAK 1 ~~~~~
https://api.osf.io/v2/nodes/9a5eh/contributors/?page=3
Link called by function: https://api.osf.io/v2/nodes/9a5eh/contributors/?page=3
User ID: tymb5, Permission level: write
User ID: xgqtu, Permission level: write
User ID: nxfj8, Permission level: write
User ID: yj76v, Permission level: write
~~~~~ THIS IS BREAK 1 ~~~~~
no more links
Done

In [14]:
print(my_contributor_list)


[['3jzys', 'admin'], ['7jwh6', 'admin'], ['6nywd', 'admin'], ['utb5k', 'admin'], ['2kfwy', 'write'], ['f77p3', 'write'], ['hhdup', 'write'], ['vkthj', 'write'], ['7595u', 'admin'], ['czszc', 'write'], ['vutud', 'write'], ['bz3n5', 'write'], ['h6zc6', 'write'], ['9j2ay', 'write'], ['5kmjn', 'write'], ['m6wyb', 'write'], ['mec2f', 'write'], ['kfc3y', 'write'], ['9t3xc', 'write'], ['nnjvk', 'write'], ['tymb5', 'write'], ['xgqtu', 'write'], ['nxfj8', 'write'], ['yj76v', 'write']]

In [ ]: