Skip to content

Instantly share code, notes, and snippets.

@scoopermccann
Last active September 23, 2016 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save scoopermccann/891b5279cc2cc105f4dd to your computer and use it in GitHub Desktop.
Save scoopermccann/891b5279cc2cc105f4dd to your computer and use it in GitHub Desktop.
#####import all of our friends#####
import time
import jwt
import urllib2
import json
import MySQLdb
#####Step 0: define things#####
db = MySQLdb.connect(host="127.0.0.1", # your host, usually localhost
user="user", # your username
passwd="pass", # your password
db="activity_DB") # name of the database
cur = db.cursor()
#'since' holds the most recent timestamp for data retrieval
cur.execute("SELECT since FROM stamp_storage")
db.commit()
row = cur.fetchone()
since = row[0]
# network data
network = "" #your network
network_secret = "" #your network key
# URNs
api_urn = "urn:livefyre:api:core=GetActivityStream"
network_urn = "urn:livefyre:" + network
# site ID
site = "" #your site ID
#####Step 1: create a fresh token#####
def make_token():
expiration = int(time.time() + 10)
data = dict(iss=network_urn, aud=network_urn,
sub=network_urn, scope=api_urn, exp=expiration)
global token
token = jwt.encode(data, key=network_secret)
#####Step 2: make request for data#####
def grab_data(since):
# generate a token
make_token()
# define request
req = urllib2.Request("https://bootstrap.livefyre.com/api/v3.1/activity/?resource=" + network_urn + ":site=" + site + "&since=" + since,
headers={
"Authorization": "Bearer " + token
})
# make the request
f = urllib2.urlopen(req)
# get json formatted data
data = json.load(f)
# check 'next' for a new timestamp
if (data["meta"]["cursor"]["next"] != None):
# update 'since' and get more data
since = str(data["meta"]["cursor"]["next"])
cur.execute("UPDATE stamp_storage SET since='" + since + "' LIMIT 1")
# store data
for entry in data["data"]["states"]:
cur.execute("INSERT INTO content_storage VALUES(NULL,'" + str(entry) + "', '" + data["data"]["states"][entry]["content"]["bodyHtml"] + "', '" + data["data"]["states"][entry][
"content"]["authorId"] + "', '" + str(data["data"]["states"][entry]["content"]["createdAt"]) + "', '" + str(data["data"]["states"][entry]["content"]["updatedAt"]) + "')")
db.commit()
# get more data
grab_data(since)
#####start the cycle#####
grab_data(since)
<?php
//create connection
$con=mysqli_connect("127.0.0.1","user","pass","activity_DB");
//check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
//get count of posts per minute
$result = mysqli_query($con,"SELECT FROM_UNIXTIME(createdAt) as create_date, COUNT(*) as cont_count FROM content_storage GROUP BY MINUTE(create_date) ORDER BY create_date ASC;");
//construct string to pass to javascript
$count_data = "";
//iterate through results and append minutes and count totals
while($row = mysqli_fetch_array($result)) {
$count_data .= "'".$row['create_date']."':{'count':".$row['cont_count']."},";
}
//strip off final ','
$count_data = rtrim($count_data, ",");
//close connection
mysqli_close($con);
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/Chart.js/0.2.0/Chart.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
//define option settings for the chart
options = {
//Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
scaleBeginAtZero : true,
//Boolean - Whether grid lines are shown across the chart
scaleShowGridLines : true,
//String - Colour of the grid lines
scaleGridLineColor : "rgba(0,0,0,.05)",
//Number - Width of the grid lines
scaleGridLineWidth : 1,
//Boolean - If there is a stroke on each bar
barShowStroke : true,
//Number - Pixel width of the bar stroke
barStrokeWidth : 2,
//Number - Spacing between each of the X value sets
barValueSpacing : 5,
//Number - Spacing between data sets within X values
barDatasetSpacing : 1,
//String - A legend template
legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].lineColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
}
//function to grab the document and process the data
$(document).ready(function() {
var labels = []; //an array for the labels
var counts = []; //an array for the counts
//set and cycle through data
datax = {<?php echo $count_data;?>} //json object with data
for (i in datax) {
labels.push(i); //store the label. just numerical in this example
counts.push(datax[i].count); //store the word count
}
//create the data object for chart.js
var data = {
labels: labels,
datasets: [
{
label: "labels",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: counts
}
]
};
var ctx = document.getElementById("bar_chart").getContext("2d");
var bar_chart = new Chart(ctx).Bar(data, options);
});
</script>
</head>
<body>
<br />
<center>
<span style="color: orange; font-size: 28px;"><b>Activity Per Minute</b></span>
<br />
<canvas id="bar_chart" width="1000" height="400"></canvas>
</center>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment