forked from ANANTAGUNA/cambridgeAnalytica
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplyMagicSauce.py
61 lines (50 loc) · 2.07 KB
/
applyMagicSauce.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
## APPLY MAGIC SAUSE from Cambridge Psychometrics Centre
import json
import requests
def auth(customer_id, api_key):
try:
credentials = {
'customer_id': customer_id,
'api_key': api_key
}
response = requests.post('https://api.applymagicsauce.com/auth', json=credentials)
response.raise_for_status()
return response.json()['token']
except requests.exceptions.HTTPError as e:
print e.response.json()
def predict_from_text(token, text):
try:
response = requests.post(url='https://api.applymagicsauce.com/text',
params={
'source': 'OTHER'
},
data=text,
headers={'X-Auth-Token': token})
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as e:
print e.response.json()
def predict_from_like_ids(token, like_ids):
try:
response = requests.post(url='https://api.applymagicsauce.com/like_ids',
json=like_ids,
headers={'X-Auth-Token': token})
response.raise_for_status()
if response.status_code == 204:
raise ValueError('Not enough predictive like ids provided to make a prediction')
else:
return response.json()
except requests.exceptions.HTTPError as e:
print e.response.json()
except ValueError as e:
print e
# /auth
token = auth(1234, 'key')
# /text
prediction_result = predict_from_text(token, 'Lorem ipsum dolor sit amet')
print json.dumps(prediction_result, indent=4)
# /like ids
prediction_result = predict_from_like_ids(token, ["5845317146", "6460713406", "22404294985", "35312278675",
"105930651606", "171605907303", "199592894970", "274598553922",
"340368556015", "100270610030980"])
print json.dumps(prediction_result, indent=4)