-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
82 lines (77 loc) · 2.78 KB
/
app.js
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
var filesystem = require('fs');
var Client = require('node-rest-client').Client;
var client = new Client();
// secrets format: { "customer_id": 1111, "api_key": "string", "token": "" }
var secrets = require('./secrets.json');
var trait = "BIG5,Satisfaction_Life,Intelligence,Age,Female,Gay,Lesbian,Concentration,Politics,Religion,Relationship";
var uid = 1111111111; //e.g. 4 is Mark Zuckerberg's unique Facebook ID
var likes = [
// contributors
"18807449704", // Mashable
"115384328477363", // The Creators Project
"7976226799", // The Daily Show
"10429446003", // The xx
// "5878552155", // Ludovico Einaudi
// "6815841748", // Barack Obama
// "35481394342", // The Godfather
// "9328458887", // adidas Originals
// "12463674069", // Curly Fries
"193081554406", // tagesschau
"215982125159841", // SZ
"38246844868", // Spiegel Online
"110495738982958", // Jan Böhmermann
"127086567317212", // Philipp Poisel
"171844246207985", // TAZ
"59788447049", // Angela Merkel
// not contributors s. result in prediction.json
// "124955570892789", // Bernie Sanders
// "102099916530784", // Humans of New York
];
function getPrediction() {
// https://applymagicsauce.com/documentation_technical.html
var args = {
data: likes,
headers: {
"X-Auth-Token": secrets.token,
"Content-Type": "application/json",
"Accept": "application/json"
}
};
client.post("http://api-v2.applymagicsauce.com/like_ids?interpretations=true&contributors=true&traits=" + trait + "&uid=" + uid,
args,
function(data, response) {
console.log(response.statusCode);
if (response.statusCode == 403) {
console.log("Authtoken expired, get new token!");
getNewToken(getPrediction);
} else if (response.statusCode == 204) {
console.log("No prediction could be made based on like ids provided.");
} else {
console.log(data);
// name of file as parameter
saveData(data, "prediction-dt");
}
});
}
function getNewToken(callback) {
var getTokenArgs = {
data: {
"customer_id": secrets.customer_id,
"api_key": secrets.api_key
},
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
};
client.post("http://api-v2.applymagicsauce.com/auth", getTokenArgs, function(data, response) {
console.log(data);
secrets.token = data.token;
callback();
saveData(secrets, "secrets");
});
}
function saveData(data, name) {
filesystem.writeFile("./" + name + ".json", JSON.stringify(data, null, 2));
}
getPrediction();