-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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). | ||
|
@@ -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) { | ||
|
@@ -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]; | ||
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); | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if there are zero downloads? Would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In theory, everything should be using |
||
badgeData.text[1] = metric(downloads); | ||
badgeData.colorscheme = downloadCountColor(downloads); | ||
sendBadge(format, badgeData); | ||
} catch(e) { | ||
badgeData.text[1] = 'invalid'; | ||
sendBadge(format, badgeData); | ||
} | ||
}); | ||
})); | ||
} | ||
|
||
// Chocolatey | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
There was a problem hiding this comment.
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?