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

Add badge for NuGet download count, again #945

Merged
merged 3 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
87 changes: 65 additions & 22 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3727,8 +3727,9 @@ function mapNugetFeedv2(pattern, offset, getInfo) {
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)$');
var dtRegex = new RegExp('^\\/' + pattern + '\\/dt\\/(.*)\\.(svg|png|gif|jpg|json)$');

function getNugetVersion(apiUrl, id, includePre, request, done) {
function getNugetData(apiUrl, id, request, done) {
// get service index document
regularUpdate(apiUrl + '/index.json',
// The endpoint changes once per year (ie, a period of n = 1 year).
Expand All @@ -3743,22 +3744,20 @@ function mapNugetFeed(pattern, offset, getInfo) {
function(buffer) {
var data = JSON.parse(buffer);

var autocompleteResources = data.resources.filter(function(resource) {
return resource['@type'] === 'SearchAutocompleteService';
var searchQueryResources = data.resources.filter(function(resource) {
return resource['@type'] === 'SearchQueryService';
});

return autocompleteResources;
return searchQueryResources;
},
function(err, autocompleteResources) {
function(err, searchQueryResources) {
if (err != null) { done(err); return; }

// query autocomplete service
var randomEndpointIdx = Math.floor(Math.random() * autocompleteResources.length);
var reqUrl = autocompleteResources[randomEndpointIdx]['@id']
+ '?id=' + encodeURIComponent(id.toLowerCase()) // NuGet package id (lowercase)
+ '&prerelease=true' // Include prerelease versions?
+ '&skip=0' // Start at first package found
+ '&take=5000'; // Max. number of results
var randomEndpointIdx = Math.floor(Math.random() * searchQueryResources.length);
var reqUrl = searchQueryResources[randomEndpointIdx]['@id']
+ '?q=packageid:' + encodeURIComponent(id.toLowerCase()) // NuGet package id (lowercase)
+ '&prerelease=true'; // Include prerelease versions?

request(reqUrl, function(err, res, buffer) {
if (err != null) {
Expand All @@ -3768,23 +3767,38 @@ function mapNugetFeed(pattern, offset, getInfo) {

try {
var data = JSON.parse(buffer);
var versions = data.data;
if (!includePre) {
// Remove prerelease versions.
var filteredVersions = versions.filter(function(version) {
return !/-/.test(version);
});
if (filteredVersions.length > 0) {
versions = filteredVersions;
}
data = data.data && data.data[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this a more explicit check that it's an array, and with the expected length?

if (data) {
done(null, data);
} else {
done(new Error('Package not found in feed'));
}
var lastVersion = versions[versions.length - 1];
done(null, lastVersion);
} catch (e) { done(e); }
});
});
}

function getNugetVersion(apiUrl, id, includePre, request, done) {
getNugetData(apiUrl, id, request, function(err, data) {
if (err) {
done(err);
return;
}
var versions = data.versions || [];
if (!includePre) {
// Remove prerelease versions.
var filteredVersions = versions.filter(function(version) {
return !/-/.test(version.version);
});
if (filteredVersions.length > 0) {
versions = filteredVersions;
}
}
var lastVersion = versions[versions.length - 1];
done(null, lastVersion.version);
});
}

camp.route(vRegex,
cache(function(data, match, sendBadge, request) {
var info = getInfo(match);
Expand Down Expand Up @@ -3846,6 +3860,35 @@ function mapNugetFeed(pattern, offset, getInfo) {
}
});
}));


camp.route(dtRegex,
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);
getNugetData(apiUrl, repo, request, function(err, nugetData) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
// Official NuGet server uses "totalDownloads" whereas MyGet uses
// "totaldownloads" (lowercase D). Ugh.
var downloads = nugetData.totalDownloads || nugetData.totaldownloads;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are zero downloads? Would 'totalDownloads' in nugetData ? nugetData.totalDownloads : nugetData.totaldownloads be safer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nugetData.totalDownloads || nugetData.totaldownloads || 0 is probably fine.

In theory, everything should be using totalDownloads and totaldownloads should not exist. Certainly there'll never be a feed in which both totalDownloads and totaldownloads exist, as many JSON parsers are not case-sensitive or have an option to disable case-sensitivity. I suspect the lowercase totaldownloads is simply a bug in MyGet.

badgeData.text[1] = metric(downloads);
badgeData.colorscheme = downloadCountColor(downloads);
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));
}

// Chocolatey
Expand Down
12 changes: 12 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,18 @@ <h3 id="downloads"> Downloads </h3>
<td><img src='/chocolatey/dt/scriptcs.svg' alt=''/></td>
<td><code>https://img.shields.io/chocolatey/dt/scriptcs.svg</code></td>
</tr>
<tr><th> NuGet: </th>
<td><img src='/nuget/dt/Microsoft.AspNetCore.Mvc.svg' alt=''/></td>
<td><code>https://img.shields.io/nuget/dt/Microsoft.AspNetCore.Mvc.svg</code></td>
</tr>
<tr><th> MyGet: </th>
<td><img src='/myget/mongodb/dt/MongoDB.Driver.Core.svg' alt=''/></td>
<td><code>https://img.shields.io/myget/mongodb/dt/MongoDB.Driver.Core.svg</code></td>
</tr>
<tr><th> MyGet tenant: </th>
<td><img src='/dotnet.myget/dotnet-coreclr/dt/Microsoft.DotNet.CoreCLR.svg' alt=''/></td>
<td><code>https://img.shields.iodotnet.myget/dotnet-coreclr/dt/Microsoft.DotNet.CoreCLR.svg</code></td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That link is missing a /.

</tr>
<tr><th data-keywords='python'> PyPI: </th>
<td><img src='/pypi/dm/Django.svg' alt=''/></td>
<td><code>https://img.shields.io/pypi/dm/Django.svg</code></td>
Expand Down