-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
119 lines (103 loc) · 3.12 KB
/
script.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
$(document).ready(function() {
var token = window.location.hash;
if (!token) {
window.location.replace("./login.html");
}
token = token.replace("#", "?"); // Ready for query param.
getInstaPics(token);
});
function getInstaPics(token) {
var INSTA_API_BASE_URL = "https://api.instagram.com/v1";
var path = "/users/self/media/recent";
var mediaUrl = INSTA_API_BASE_URL + path + token;
$.ajax({
method: "GET",
url: mediaUrl,
dataType: "jsonp",
success: function(response) {
showPictures(response.data);
analyzeSentiments(response.data);
}
});
}
function showPictures(data) {
$("#list").html("");
for (var i=0; i<data.length; i++) {
var picData = data[i];
var postDiv = $("<div></div>");
postDiv.addClass("post");
// Mark this as this with this ID so we can find it for sentiment analysis.
postDiv.attr("id", "post-" + i);
var img = $("<img />")
img.attr("src", picData.images.standard_resolution.url);
var caption = $("<p></p>");
caption.addClass("caption");
caption.html(picData.caption.text);
postDiv.append(img).append(caption);
$("#list").append(postDiv);
}
}
function analyzeSentiments(data) {
$.each(data, function(index, value) {
var phrase = value.caption.text;
var SENTIMENT_API_BASE_URL =
"https://twinword-sentiment-analysis.p.mashape.com/analyze/";
$.ajax({
method: "POST",
url: SENTIMENT_API_BASE_URL,
headers: {
"X-Mashape-Key": "H0IoG8ybyymshvm2IlQmwZk8Vlqlp1KD3F2jsn2RLpghqszsGI"
},
data: {text: phrase},
success: function(response) {
console.log(response);
addSentiment(response.type, response.score, index);
}
});
});
}
function addSentiment(type, score, picNum) {
// Find the post the corresponds to this sentiment
var picDiv = $("#post-" + picNum);
// Create a sentiment div
var sentimentDiv = $("<div></div>");
var sentimentI = $("<i></i>");
sentimentI.addClass("fa");
// Add the appropriate smiley using FontAwesome
var faClass = "fa-meh-o";
if (type === "positive") {
sentimentDiv.addClass("positive");
faClass = "fa-smile-o";
} else if (type === "negative") {
sentimentDiv.addClass("negative");
faClass = "fa-frown-o";
}
sentimentI.addClass(faClass);
sentimentDiv.append("Sentiment: ").append(sentimentI)
.append(" (score: " + score.toFixed(2) + ")");
picDiv.append(sentimentDiv);
updateTotalSentiment(score);
}
var allSentimentScores = []; // Aggregator for all sentiments so far.
function updateTotalSentiment(score) {
allSentimentScores.push(score);
console.log(allSentimentScores, score);
// Calculate the average sentiment.
var sum = 0;
for (var i=0; i<allSentimentScores.length; i++) {
sum += allSentimentScores[i];
}
var avg = sum / allSentimentScores.length;
// Add nice text and colors.
var text = "Neutral"
var textClass = "";
if (avg > 0) {
text = "Positive!";
textClass = "positive";
} else if (avg < 0) {
text = "Negative :(";
textClass = "negative";
}
$("#mood").html(text + " (score: " + avg + ")");
$("#mood").addClass(textClass);
}