Skip to content

Commit

Permalink
NuGet: use API v3
Browse files Browse the repository at this point in the history
The v2 API returned XML even though we asked for JSON.

MyGet is still not working.

Part of #655.
  • Loading branch information
espadrine committed Mar 14, 2016
1 parent b72e29d commit 4df74ac
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 27 deletions.
12 changes: 2 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,6 @@ <h3 id="downloads"> Downloads </h3>
<td><img src='https://img.shields.io/gem/dt/rails.svg' alt=''/></td>
<td><code>https://img.shields.io/gem/dt/rails.svg</code></td>
</tr>
<tr><th> NuGet: </th>
<td><img src='https://img.shields.io/nuget/dt/Microsoft.AspNet.Mvc.svg' alt=''/></td>
<td><code>https://img.shields.io/nuget/dt/Microsoft.AspNet.Mvc.svg</code></td>

This comment has been minimized.

Copy link
@drewnoakes

drewnoakes Mar 24, 2016

Is there no alternative to get download stats for NuGet packages with this version of the API?

This comment has been minimized.

Copy link
@espadrine

espadrine Apr 16, 2016

Author Member

The full discussion is at #655 (see in particular #655 (comment)). If you can find a solution, it would be well received!

This comment has been minimized.

Copy link
@Daniel15

Daniel15 Apr 16, 2016

Member

Also see #678

This comment has been minimized.

Copy link
@Daniel15

Daniel15 Apr 18, 2017

Member

Just submitted a PR to add download stats again: #945

</tr>
<tr><th> MyGet: </th>
<td><img src='https://img.shields.io/myget/yolodev/dt/FSharpSupport.svg' alt=''/></td>
<td><code>https://img.shields.io/myget/yolodev/dt/FSharpSupport.svg</code></td>
</tr>
<tr><th> Chocolatey: </th>
<td><img src='https://img.shields.io/chocolatey/dt/scriptcs.svg' alt=''/></td>
<td><code>https://img.shields.io/chocolatey/dt/scriptcs.svg</code></td>
Expand Down Expand Up @@ -472,8 +464,8 @@ <h3 id="version"> Version </h3>
<td><code>https://img.shields.io/nuget/v/Nuget.Core.svg</code></td>
</tr>
<tr><th> NuGet Pre Release: </th>
<td><img src='https://img.shields.io/nuget/vpre/Nuget.Core.svg' alt=''/></td>
<td><code>https://img.shields.io/nuget/vpre/Nuget.Core.svg</code></td>
<td><img src='https://img.shields.io/nuget/vpre/Microsoft.AspNet.Mvc.svg' alt=''/></td>
<td><code>https://img.shields.io/nuget/vpre/Microsoft.AspNet.Mvc.svg</code></td>
</tr>
<tr><th> MyGet: </th>
<td><img src='https://img.shields.io/myget/yolodev/v/FSharpSupport.svg' alt=''/></td>
Expand Down
114 changes: 107 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3149,7 +3149,7 @@ cache(function(data, match, sendBadge, request) {
});
}));

function mapNugetFeed(pattern, offset, getInfo) {
function mapNugetFeedv2(pattern, offset, getInfo) {
var vRegex = new RegExp('^\\/' + pattern + '\\/v\\/(.*)\\.(svg|png|gif|jpg|json)$');
var vPreRegex = new RegExp('^\\/' + pattern + '\\/vpre\\/(.*)\\.(svg|png|gif|jpg|json)$');
var dtRegex = new RegExp('^\\/' + pattern + '\\/dt\\/(.*)\\.(svg|png|gif|jpg|json)$');
Expand Down Expand Up @@ -3277,17 +3277,117 @@ function mapNugetFeed(pattern, offset, getInfo) {
}));
}

// NuGet and Chocolatey
mapNugetFeed('(nuget|chocolatey)', 1, function(match) {
var site = match[1];
function mapNugetFeed(pattern, offset, getInfo) {
var vRegex = new RegExp('^\\/' + pattern + '\\/v\\/(.*)\\.(svg|png|gif|jpg|json)$');
var vPreRegex = new RegExp('^\\/' + pattern + '\\/vpre\\/(.*)\\.(svg|png|gif|jpg|json)$');

function getNugetVersion(apiUrl, id, includePre, request, done) {
var reqUrl = apiUrl + '/flatcontainer/' + id.toLowerCase() + '/index.json';
request(reqUrl, function(err, res, buffer) {
if (err != null) {
done(err);
return;
}

try {
var data = JSON.parse(buffer);
var versions = data.versions;
if (!includePre) {
// Remove prerelease versions.
filteredVersions = versions.filter(function(version) {
return !/-/.test(version);
});
if (filteredVersions.length > 0) {
versions = filteredVersions;
}
}
var lastVersion = versions[versions.length - 1];
done(null, lastVersion);
} catch (e) { done(e); }
});
}

camp.route(vRegex,
cache(function(data, match, sendBadge, request) {
var info = getInfo(match);
var site = info.site; // eg, `Chocolatey`, or `YoloDev`
var repo = match[offset + 1]; // eg, `Nuget.Core`.
var format = match[offset + 2];
var apiUrl = info.feed;
var badgeData = getBadgeData(site, data);
getNugetVersion(apiUrl, repo, false, request, function(err, version) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
badgeData.text[1] = 'v' + version;
if (version.indexOf('-') !== -1) {
badgeData.colorscheme = 'yellow';
} else if (version[0] === '0') {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'blue';
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

camp.route(vPreRegex,
cache(function(data, match, sendBadge, request) {
var info = getInfo(match);
var site = info.site; // eg, `Chocolatey`, or `YoloDev`
var repo = match[offset + 1]; // eg, `Nuget.Core`.
var format = match[offset + 2];
var apiUrl = info.feed;
var badgeData = getBadgeData(site, data);
getNugetVersion(apiUrl, repo, true, request, function(err, version) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
badgeData.text[1] = 'v' + version;
if (version.indexOf('-') !== -1) {
badgeData.colorscheme = 'yellow';
} else if (version[0] === '0') {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'blue';
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));
}

// Chocolatey
mapNugetFeedv2('chocolatey', 0, function(match) {
return {
site: 'chocolatey',
feed: 'https://www.chocolatey.org/api/v2'
};
});

// NuGet
mapNugetFeed('nuget', 0, function(match) {
return {
site: site,
feed: 'https://www.' + site + '.org/api/v2'
site: 'nuget',
feed: 'https://api.nuget.org/v3'
};
});

// MyGet
mapNugetFeed('myget\\/(.*)', 1, function(match) {
mapNugetFeedv2('myget\\/(.*)', 1, function(match) {
var feed = match[1];
return {
site: feed,
Expand Down
12 changes: 2 additions & 10 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,6 @@ <h3 id="downloads"> Downloads </h3>
<td><img src='/gem/dt/rails.svg' alt=''/></td>
<td><code>https://img.shields.io/gem/dt/rails.svg</code></td>
</tr>
<tr><th> NuGet: </th>
<td><img src='/nuget/dt/Microsoft.AspNet.Mvc.svg' alt=''/></td>
<td><code>https://img.shields.io/nuget/dt/Microsoft.AspNet.Mvc.svg</code></td>
</tr>
<tr><th> MyGet: </th>
<td><img src='/myget/yolodev/dt/FSharpSupport.svg' alt=''/></td>
<td><code>https://img.shields.io/myget/yolodev/dt/FSharpSupport.svg</code></td>
</tr>
<tr><th> Chocolatey: </th>
<td><img src='/chocolatey/dt/scriptcs.svg' alt=''/></td>
<td><code>https://img.shields.io/chocolatey/dt/scriptcs.svg</code></td>
Expand Down Expand Up @@ -471,8 +463,8 @@ <h3 id="version"> Version </h3>
<td><code>https://img.shields.io/nuget/v/Nuget.Core.svg</code></td>
</tr>
<tr><th> NuGet Pre Release: </th>
<td><img src='/nuget/vpre/Nuget.Core.svg' alt=''/></td>
<td><code>https://img.shields.io/nuget/vpre/Nuget.Core.svg</code></td>
<td><img src='/nuget/vpre/Microsoft.AspNet.Mvc.svg' alt=''/></td>
<td><code>https://img.shields.io/nuget/vpre/Microsoft.AspNet.Mvc.svg</code></td>
</tr>
<tr><th> MyGet: </th>
<td><img src='/myget/yolodev/v/FSharpSupport.svg' alt=''/></td>
Expand Down

0 comments on commit 4df74ac

Please sign in to comment.