Last active
November 28, 2016 15:58
-
-
Save atelic/8ae0538527dd24b5388320fdf9bee5bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import json | |
import logging | |
import sys | |
import requests | |
logger = logging.getLogger(__name__) | |
TOKEN = 'YOUR_TOKEN' | |
CONTRIBUTORS_URL = 'https://api.osf.io/v2/nodes/{}/contributors/' | |
def get_contribs(url): | |
contributors_to_add = [] | |
resp = requests.get(url).json() | |
contributors = resp['data'] | |
for contrib in contributors: | |
contributors_to_add.append(contrib['embeds']['users']['data']['id']) | |
next_url = resp['links']['next'] | |
if next_url: | |
contributors_to_add += get_contribs(next_url) | |
return contributors_to_add | |
def add_contrib(url, uid, permission='read'): | |
payload = { | |
'data': { | |
'type': 'contributors', | |
'attributes': { | |
'bibliographic': True, | |
'permission': permission | |
}, | |
'relationships': { | |
'user': { | |
'data': { | |
'type': 'users', | |
'id': uid | |
} | |
} | |
} | |
} | |
} | |
return requests.post( | |
url, | |
data=json.dumps(payload), | |
headers={ | |
'Authorization': 'Bearer {}'.format(TOKEN), | |
'Content-Type': 'application/json' | |
}) | |
def main(): | |
try: | |
from_project = sys.argv[1] | |
to_project = sys.argv[2] | |
except IndexError: | |
sys.exit('usage: ./migrate_contribs.py $SOURCE_PROJECT_GUID $TARGET_PROJECT_GUID') | |
url = CONTRIBUTORS_URL.format(from_project) | |
to_add = get_contribs(url) | |
count = 0 | |
url = CONTRIBUTORS_URL.format(to_project) | |
for contrib in to_add: | |
add_contrib(url, contrib) | |
count += 1 | |
logger.log('Done with {} total contributors added'.format(count)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment