Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SourceForge download count integration #303

Merged
merged 2 commits into from
Nov 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,57 @@ cache(function(data, match, sendBadge, request) {
});
}));

// SourceForge integration.
camp.route(/^\/sourceforge\/([^\/]+)\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var info = match[1];
var project = match[2];
var format = match[3];
var apiUrl = 'http://sourceforge.net/projects/' + project + '/files/stats/json';
var badgeData = getBadgeData('sourceforge', data);
var time_period, start_date, end_date;
if (info.charAt(0) === 'd') {
badgeData.text[0] = getLabel('downloads', data);
// get yesterday since today is incomplete
end_date = new Date((new Date()).getTime() - 1000*60*60*24);
switch (info.charAt(1)) {
case 'm':
start_date = new Date(end_date.getTime() - 1000*60*60*24*30);
time_period = '/month';
break;
case 'w':
start_date = new Date(end_date.getTime() - 1000*60*60*24*6); // 6, since date range is inclusive
time_period = '/week';
break;
case 'd':
start_date = end_date;
time_period = '/day';
break;
case 't':
start_date = new Date(99, 0, 1);
time_period = '/total';
break;
}
}
apiUrl += '?start_date=' + start_date.toISOString().slice(0,10) + '&end_date=' + end_date.toISOString().slice(0,10);
request(apiUrl, function(err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
}
try {
var data = JSON.parse(buffer);
var downloads = data.total;
badgeData.text[1] = metric(downloads) + time_period;
badgeData.colorscheme = downloadCountColor(downloads);
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid' + e.toString();
sendBadge(format, badgeData);
}
});
}));

// Any badge.
camp.route(/^\/(:|badge\/)(([^-]|--)+)-(([^-]|--)+)-(([^-]|--)+)\.(svg|png|gif|jpg)$/,
function(data, match, end, ask) {
Expand Down
16 changes: 16 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ <h3> Downloads </h3>
<td><img src='/wordpress/plugin/dt/akismet.svg' alt=''/></td>
<td><code>http://img.shields.io/wordpress/plugin/dt/akismet.svg</code></td>
</tr>
<tr><th> SourceForge: </th>
<td><img src='/sourceforge/dm/sevenzip.svg' alt=''/></td>
<td><code>http://img.shields.io/sourceforge/dm/sevenzip.svg</code></td>
</tr>
<tr><th> SourceForge: </th>
<td><img src='/sourceforge/dw/sevenzip.svg' alt=''/></td>
<td><code>http://img.shields.io/sourceforge/dw/sevenzip.svg</code></td>
</tr>
<tr><th> SourceForge: </th>
<td><img src='/sourceforge/dd/sevenzip.svg' alt=''/></td>
<td><code>http://img.shields.io/sourceforge/dd/sevenzip.svg</code></td>
</tr>
<tr><th> SourceForge: </th>
<td><img src='/sourceforge/dt/sevenzip.svg' alt=''/></td>
<td><code>http://img.shields.io/sourceforge/dt/sevenzip.svg</code></td>
</tr>
</tbody></table>
<h3> Version </h3>
<table><tbody>
Expand Down