Skip to content

Commit 025116a

Browse files
committed
We're now caching the github feed. Simplified the json parsing
1 parent 5e3407f commit 025116a

File tree

4 files changed

+53
-169
lines changed

4 files changed

+53
-169
lines changed

content/static/feeds/github.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,27 @@
33
//cache the json file and only call again if interval exceeded
44
error_reporting(0);
55

6-
$feed = "https://api.github.com/repos/processing/processing/commits?sha=master";
7-
function getSslPage($url) {
8-
$ch = curl_init();
9-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
10-
curl_setopt($ch, CURLOPT_HEADER, false);
11-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
12-
curl_setopt($ch, CURLOPT_URL, $url);
13-
curl_setopt($ch, CURLOPT_REFERER, $url);
14-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
15-
$result = curl_exec($ch);
16-
curl_close($ch);
17-
return $result;
6+
function get_json($url){
7+
$base = "https://api.github.com/";
8+
$curl = curl_init();
9+
curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
10+
curl_setopt($curl, CURLOPT_URL, $base . $url);
11+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
12+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
13+
14+
$content = curl_exec($curl);
15+
curl_close($curl);
16+
return $content;
1817
}
19-
echo getSslPage($feed);
20-
2118

19+
$feed = "repos/processing/processing/commits";
2220
$cache_file = dirname(__FILE__).'/cache/'.'github-cache';
2321
$modified = filemtime( $cache_file );
2422
$now = time();
2523
$interval = 600; // ten minutes
2624

2725
if ( !$modified || ( ( $now - $modified ) > $interval ) ) {
28-
$json = file_get_contents( $feed );
26+
$json = get_json($feed);
2927

3028
if ( $json ) {
3129
$cache_static = fopen( $cache_file, 'w' );

css/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ font-family: 'theSerifItalic'; }
400400

401401
#Cover .commits,
402402
#Cover .twitter { float: left; margin-top: 14px; }
403-
.github-commits-list li,
403+
.latest-commits li,
404404
.latest-tweets li {
405405
display: block;
406406
float: left;
@@ -412,7 +412,7 @@ font-family: 'theSerifItalic'; }
412412
width: 20px;
413413
height: 20px;
414414
}
415-
.github-commits-list li div {
415+
.latest-commits li div {
416416
width: 370px;
417417
float: left;
418418
margin-left: 10px;

javascript/site.js

Lines changed: 37 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -16,163 +16,33 @@ String.prototype.parseURL = function() {
1616
return url.link(url);
1717
});
1818
};
19+
20+
/* Time Parsing */
1921
function parse_date(date_str) {
20-
return Date.parse(date_str.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));
22+
return Date.parse(date_str.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));
2123
}
22-
2324
function extract_relative_time(date) {
24-
var toInt = function(val) { return parseInt(val, 10); };
25-
var relative_to = new Date();
26-
var delta = toInt((relative_to.getTime() - date) / 1000);
27-
if (delta < 1) delta = 0;
28-
return {
29-
days: toInt(delta / 86400),
30-
hours: toInt(delta / 3600),
31-
minutes: toInt(delta / 60),
32-
seconds: toInt(delta)
33-
};
25+
var toInt = function(val) { return parseInt(val, 10); };
26+
var relative_to = new Date();
27+
var delta = toInt((relative_to.getTime() - date) / 1000);
28+
if (delta < 1) delta = 0;
29+
return {
30+
days: toInt(delta / 86400),
31+
hours: toInt(delta / 3600),
32+
minutes: toInt(delta / 60),
33+
seconds: toInt(delta)
34+
};
3435
}
3536
function format_relative_time(time_ago) {
36-
if ( time_ago.days > 2 ) return ' ' + time_ago.days + ' days ago';
37-
if ( time_ago.hours > 24 ) return ' a day ago';
38-
if ( time_ago.hours > 2 ) return ' ' + time_ago.hours + ' hours ago';
39-
if ( time_ago.minutes > 45 ) return ' an hour ago';
40-
if ( time_ago.minutes > 2 ) return ' ' + time_ago.minutes + ' minutes ago';
41-
if ( time_ago.seconds > 1 ) return ' ' + time_ago.seconds + ' seconds ago';
42-
return 'just now';
37+
if ( time_ago.days > 2 ) return ' ' + time_ago.days + ' days ago';
38+
if ( time_ago.hours > 24 ) return ' a day ago';
39+
if ( time_ago.hours > 2 ) return ' ' + time_ago.hours + ' hours ago';
40+
if ( time_ago.minutes > 45 ) return ' an hour ago';
41+
if ( time_ago.minutes > 2 ) return ' ' + time_ago.minutes + ' minutes ago';
42+
if ( time_ago.seconds > 1 ) return ' ' + time_ago.seconds + ' seconds ago';
43+
return 'just now';
4344
}
4445

45-
/* Commit Widget */
46-
(function ($) {
47-
function widget(element, options, callback) {
48-
this.element = element;
49-
this.options = options;
50-
this.callback = $.isFunction(callback) ? callback : $.noop;
51-
}
52-
53-
widget.prototype = (function() {
54-
55-
function getCommits(user, repo, branch, callback) {
56-
$.ajax({
57-
url: "https://api.github.com/repos/" + user + "/" + repo + "/commits?sha=" + branch,
58-
//url: "/content/static/feeds/github.php",
59-
dataType: 'jsonp',
60-
success: callback
61-
});
62-
}
63-
64-
function _widgetRun(widget) {
65-
if (!widget.options) {
66-
widget.element.append('<span class="error">Options for widget are not set.</span>');
67-
return;
68-
}
69-
var callback = widget.callback;
70-
var element = widget.element;
71-
var user = widget.options.user;
72-
var repo = widget.options.repo;
73-
var branch = widget.options.branch;
74-
var avatarSize = widget.options.avatarSize || 20;
75-
var last = widget.options.last === undefined ? 0 : widget.options.last;
76-
var limitMessage = widget.options.limitMessageTo === undefined ? 0 : widget.options.limitMessageTo;
77-
78-
element.append('<p>Loading commits...</p>');
79-
getCommits(user, repo, branch, function (data) {
80-
var commits = data.data;
81-
var totalCommits = (last < commits.length ? last : commits.length);
82-
83-
element.empty();
84-
85-
var list = $('<ul class="github-commits-list">').appendTo(element);
86-
for (var c = 0; c < totalCommits; c++) {
87-
var commit = commits[c];
88-
list.append(
89-
'<li ' + itemClass(c, totalCommits) + ' >' +
90-
' ' + ((commit.author !== null) ? avatar(commit.author.gravatar_id, avatarSize) : '') +
91-
' ' + ((commit.author !== null) ? author(commit.author.login) : commit.commit.committer.name) +
92-
' committed ' + message(replaceHtmlTags(commit.commit.message), commit.sha) +
93-
' ' + when(commit.commit.committer.date) +
94-
'</li>');
95-
}
96-
callback(element);
97-
98-
function itemClass(current, totalCommits) {
99-
if (current === 0) {
100-
return 'class="first"';
101-
} else if (current === totalCommits - 1) {
102-
return 'class="last"';
103-
}
104-
return '';
105-
}
106-
107-
function avatar(hash, size) {
108-
return '<img class="github-avatar" src="http://www.gravatar.com/avatar/' + hash + '?s=' + size + '"/>';
109-
}
110-
111-
function author(login) {
112-
return '<div><a class="github-user" href="https://github.com/' + login + '">' + login + '</a>';
113-
}
114-
115-
function message(commitMessage, sha) {
116-
var originalCommitMessage = commitMessage;
117-
if (limitMessage > 0 && commitMessage.length > limitMessage)
118-
{
119-
commitMessage = commitMessage.substr(0, limitMessage) + '...';
120-
}
121-
return '"' + '<a class="github-commit" title="' + originalCommitMessage + '" href="https://github.com/' + user + '/' + repo + '/commit/' + sha + '">' + commitMessage + '</a>"';
122-
}
123-
124-
function replaceHtmlTags(message) {
125-
return message.replace(/&/g, "&amp;")
126-
.replace(/>/g, "&gt;")
127-
.replace(/</g, "&lt;")
128-
.replace(/"/g, "&quot;");
129-
}
130-
131-
function when(commitDate) {
132-
var commitTime = new Date(commitDate).getTime();
133-
var todayTime = new Date().getTime();
134-
135-
var differenceInDays = Math.floor(((todayTime - commitTime)/(24*3600*1000)));
136-
if (differenceInDays === 0) {
137-
var differenceInHours = Math.floor(((todayTime - commitTime)/(3600*1000)));
138-
if (differenceInHours === 0) {
139-
var differenceInMinutes = Math.floor(((todayTime - commitTime)/(600*1000)));
140-
if (differenceInMinutes === 0) {
141-
142-
return 'just now';
143-
}
144-
145-
return 'about ' + differenceInMinutes + ' minutes ago';
146-
}
147-
148-
return 'about ' + differenceInHours + ' hours ago';
149-
} else if (differenceInDays == 1) {
150-
return 'yesterday';
151-
}
152-
return differenceInDays + ' days ago</div>';
153-
}
154-
});
155-
}
156-
157-
return {
158-
run: function () {
159-
_widgetRun(this);
160-
}
161-
};
162-
163-
})();
164-
165-
$.fn.githubInfoWidget = function(options, callback) {
166-
this.each(function () {
167-
new widget($(this), options, callback)
168-
.run();
169-
});
170-
return this;
171-
};
172-
173-
})(jQuery);
174-
175-
17646
$(function(){
17747
$(window).scroll(function(){
17848
if($(this).scrollTop() > 114){
@@ -182,6 +52,7 @@ $(function(){
18252
}
18353
})
18454

55+
// recent tweets
18556
$.getJSON('/content/static/feeds/twitter.php', function(data) {
18657
$.each(data, function(i, tweet) {
18758
var time = parse_date(tweet.created_at);
@@ -196,6 +67,21 @@ $(function(){
19667
})
19768
})
19869

199-
$(".latest-commits").githubInfoWidget({ user: 'processing', repo: 'processing', branch: 'master', last: 4 });
70+
// recent commits
71+
$.getJSON('/content/static/feeds/github.php', function(data) {
72+
$.each(data, function(i, commit) {
73+
if(i<=3){
74+
var time = parse_date(commit.commit.committer.date);
75+
var timeText = format_relative_time(extract_relative_time(time));
76+
77+
var commit_html = '<li><img class="github-avatar" src="http://www.gravatar.com/avatar/' + commit.author.gravatar_id + '?s=20"/>';
78+
commit_html += '<div><a href="' + commit.author.html_url + '">' + commit.author.login + '<\/a> commited';
79+
commit_html += ' <a href="https://github.com/processing/processing/commit/' + commit.sha + '">"' + commit.commit.message + '"<\/a>';
80+
commit_html += ' about ' + timeText + '<\/div>';
81+
82+
$('.latest-commits').append(commit_html)
83+
}
84+
})
85+
})
20086

20187
});

templates/template.cover.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h5>&raquo; <a href="https://twitter.com/processingOrg">Twitter</a></h5>
3737

3838
<div class="commits">
3939
<h5>&raquo; <a href="https://github.com/processing">Github</a></h5>
40-
<div class="latest-commits"></div>
40+
<ul class="latest-commits"></ul>
4141
</div>
4242

4343
</div>

0 commit comments

Comments
 (0)