-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
84 lines (73 loc) · 2.45 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
import uuid
import requests
import json
import os
import boto3
from config import Config as config
def _update_job_event(_context, data):
"""
Helper function to serialize JSON
and make sure redis doesnt messup JSON validation
"""
redis_conn = _context['redis_conn']
response_channel = _context['response_channel']
data['data_sequence_no'] = _context['data_sequence_no']
redis_conn.rpush(response_channel, json.dumps(data))
def report_to_crowdai(_context, _payload, submission_id=False,
status=False, message=""):
print(_context, _payload, submission_id, status, message)
if config.DEBUG_MODE:
return str(uuid.uuid4())
headers = {
'Authorization': 'Token token='+config.CROWDAI_TOKEN,
"Content-Type": "application/vnd.api+json"
}
_payload['challenge_client_name'] = config.challenge_id
_payload['api_key'] = _context['api_key']
if len(message) > 0:
_payload['grading_message'] = message
if status:
_payload['grading_status'] = status
if not submission_id:
print "Making POST request...."
r = requests.post(
config.CROWDAI_GRADER_URL,
params=_payload, headers=headers, verify=False)
else:
print "Making PATCH request...."
r = requests.patch(
"{}/{}".format(config.CROWDAI_GRADER_URL, submission_id),
params=_payload, headers=headers, verify=False
)
print("RESPONSE : ", r.text)
if r.status_code == 202:
data = json.loads(r.text)
submission_id = str(data['submission_id'])
return submission_id
else:
_message = """
Unable to register submission on crowdAI.
Please contact the admins."""
response = json.loads(r.text)
_message = response['message']
raise Exception(_message)
def get_boto_client():
return boto3.client(
's3',
aws_access_key_id=config.AWS_ACCESS_KEY_ID,
aws_secret_access_key=config.AWS_SECRET_ACCESS_KEY
)
def download_file(_context, filekey):
# Download MIDI to tempfolder
s3 = get_boto_client()
filepath = "{}/{}".format(
config.TEMP_STORAGE_DIRECTORY_PATH,
str(uuid.uuid4())+".csv"
)
s3.download_file(
config.AWS_S3_BUCKET,
filekey,
filepath
)
return filepath