Skip to content

Instantly share code, notes, and snippets.

@mikekavouras
Created February 15, 2017 17:14
Show Gist options
  • Save mikekavouras/eb07931a80d4c3aa433a7b96e9771e5a to your computer and use it in GitHub Desktop.
Save mikekavouras/eb07931a80d4c3aa433a7b96e9771e5a to your computer and use it in GitHub Desktop.
var Entity = {
render: function(e) {
var t = document.createElement("table");
t.setAttribute("class", "table table-striped"), t.setAttribute("width", "100%"), t.appendChild(this._renderEntityHead(e)), t.appendChild(this._renderEntityBody(e));
var n = document.createElement("div");
return n.setAttribute("class", "entity"), n.appendChild(t), n
},
_renderEntityHead: function() {
return this._renderHeader(["Entity", "Sentiment"])
},
_renderEntityBody: function(e) {
var t = [e.text, e.sentiment_type];
return this._renderBody([t])
},
_renderSentimentHead: function() {
return this._renderHeader(["Emotion", "Score"])
},
_renderSentimentBody: function(e) {
var t = [
["Anger", this._formattedPercentage(e.emotion_anger)],
["Disgust", this._formattedPercentage(e.emotion_disgust)],
["Fear", this._formattedPercentage(e.emotion_fear)],
["Joy", this._formattedPercentage(e.emotion_joy)],
["Sadness", this._formattedPercentage(e.emotion_sadness)]
];
return this._renderBody(t)
},
_renderBody: function(e) {
var t = document.createElement("tbody"),
n = e.map(function(e) {
var t = document.createElement("tr");
return e.forEach(function(e) {
var n = document.createElement("td");
n.innerText = e, t.appendChild(n)
}), t
});
return n.forEach(function(e) {
t.appendChild(e)
}), t
},
_renderHeader: function(e) {
var t = document.createElement("thead"),
n = document.createElement("tr");
return e.forEach(function(e) {
var t = document.createElement("td");
t.innerHTML = "<strong>" + e + "</strong>", n.appendChild(t)
}), t.appendChild(n), t
},
_formattedPercentage: function(e) {
return (100 * e).toFixed(2) + "%"
}
},
Tweet = {
render: function(e) {
var t = document.createElement("blockquote");
t.setAttribute("class", "twitter-tweet"), t.setAttribute("data-lang", "en");
var n = "https://twitter.com/" + e.username + "/status/" + e.tweet_id_str,
i = document.createElement("a");
i.setAttribute("href", n), t.appendChild(i);
var a = document.createElement("div");
return a.setAttribute("class", "tweet col-xs-12 col-sm-6"), a.appendChild(t), a
}
},
Article = {
render: function(e, t) {
var n = document.createElement("p");
return n.setAttribute("class", "headline"), n.innerHTML = '<a target="_blank"href="' + t.url + '">' + t.title + "</a>", n
}
},
ActivityFeed = {
render: function(e, t) {
this.el = t || document.getElementById("activity-feed"), this.clear();
var n = this;
e.forEach(function(e) {
n.renderActivity(e)
})
},
clear: function() {
for (; this.el.hasChildNodes();) this.el.removeChild(this.el.lastChild)
},
renderActivity: function(e) {
var t = this.renderTimestamp(e),
n = this.renderTweets(e),
i = this.renderEntities(e),
a = this.renderArticles(e),
r = document.createElement("div");
r.setAttribute("class", "activity row"), r.appendChild(t), r.appendChild(n), r.appendChild(i), r.appendChild(a), this.el.append(r)
},
renderTweets: function(e) {
return Tweet.render(e)
},
renderEntities: function(e) {
var t = e.sentiment.entities.map(function(e) {
return Entity.render(e)
}),
n = document.createElement("div");
return n.setAttribute("class", "entities col-xs-12 col-sm-6"), t.forEach(function(e) {
n.appendChild(e)
}), n
},
renderArticles: function(e) {
var t = Object.keys(e.articles),
n = [];
t.forEach(function(t) {
var i = e.articles[t].map(function(e) {
return Article.render(t, e)
}),
a = {
"bbc-news": "BBC News",
reuters: "Reuters",
"associated-press": "Associated Press"
}[t];
n.push({
source: a,
articles: i
})
});
var i = document.createElement("h3");
i.setAttribute("class", "articles-header"), i.innerText = "Articles";
var a = document.createElement("div");
return a.setAttribute("class", "articles col-xs-12 col-sm-6 pull-right"), a.appendChild(i), n.forEach(function(e) {
var t = document.createElement("p");
t.setAttribute("class", "article-source"), t.innerHTML = "<strong>" + e.source + "</strong>", a.appendChild(t), e.articles.forEach(function(e) {
a.appendChild(e)
})
}), a
},
renderTimestamp: function(e) {
var t = document.createElement("div");
t.setAttribute("class", "col-xs-12");
var n = document.createElement("div");
n.setAttribute("class", "timestamp-container");
var i = document.createElement("span");
return i.setAttribute("class", "timestamp"), i.innerText = this.formattedTimestamp(e.tweeted_at), t.appendChild(n), n.appendChild(i), t
},
formattedTimestamp: function(e) {
var t = new Date(e),
n = t.getHours() % 12;
n = 0 == n ? 12 : n;
var i = t.getMinutes(),
a = t.getHours() < 12 ? "AM" : "PM";
return this.pad(n) + ":" + this.pad(i) + " " + a
},
pad: function(e) {
return 1 == (e + "").length ? "0" + e : e
}
},
API = {
fetchSummary: function(e, t) {
e = e || new Date;
$.ajax({
url: "/tweets",
type: "GET",
data: {
date: e.toString(),
offset: -e.getTimezoneOffset()
},
success: function(e) {
t(e)
},
error: function(e) {
console.log(e)
}
})
}
};
var Utils = {
reloadTwitterWidgetScript: function() {
var e = document.getElementById("twitter-widget");
e && e.parentNode.removeChild(e);
var t = document.createElement("script");
t.src = "https://platform.twitter.com/widgets.js", t.setAttribute("id", "twitter-widget"), document.body.appendChild(t)
},
initDatePicker: function(e, t) {
new Pikaday({
field: t || document.getElementById("datepicker"),
onSelect: function(t) {
e(t)
}
})
}
};
! function() {
var e = function(e) {
ActivityFeed.render(e), Utils.reloadTwitterWidgetScript()
};
Utils.initDatePicker(function(t) {
API.fetchSummary(t, e)
}), API.fetchSummary(null, e)
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment