Last active
March 22, 2019 01:01
-
-
Save ckcollab/fdea087f19480e6604e3eddbed8e61ed to your computer and use it in GitHub Desktop.
Dataset bulk upload script
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
import requests | |
base_url = "http://competitions" # no trailing slash! | |
username = "admin" | |
password = "admin" | |
file_to_upload = "uploader.py" # uploading this file itself to test things.. | |
file_to_upload_name = "This script, a test!" | |
file_to_upload_type = "None" # see types below for when you use this.. | |
# FYI! All of the types.. | |
dataset_types = ( | |
"Reference Data", | |
"Scoring Program", | |
"Input Data", | |
"Ingestion Program", | |
"Starting Kit", | |
"Public Data", | |
"None", | |
) | |
with requests.Session() as s: | |
# Set CSRF token, will need to do this each time uploading file | |
s.get('{}/accounts/login/'.format(base_url)) | |
csrftoken = s.cookies['csrftoken'] | |
# Login | |
resp = s.post('{}/accounts/login/'.format(base_url), data={ | |
"csrfmiddlewaretoken": csrftoken, | |
"login": username, | |
"password": password, | |
}) | |
if resp.status_code == 200: | |
print("Login succeeded!") | |
else: | |
print("Login failed!") | |
# Upload a file | |
print("Uploading file...") | |
s.get('{}/my/datasets/create'.format(base_url)) | |
csrftoken = s.cookies['csrftoken'] | |
resp = s.post( | |
'{}/my/datasets/create'.format(base_url), | |
data={ | |
"csrfmiddlewaretoken": csrftoken, | |
"name": file_to_upload_name, | |
"type": file_to_upload_type, | |
}, | |
files={ | |
"data_file": open(file_to_upload, "rb"), | |
} | |
) | |
if resp.status_code == 200: | |
print("Upload succeeded!") | |
else: | |
print("Upload failed! Status code = {}".format(resp.status_code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment