Skip to content

Instantly share code, notes, and snippets.

@joelverhagen
Created February 13, 2018 19:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelverhagen/4d32044a8003360e81c68ac82ac318cc to your computer and use it in GitHub Desktop.
Save joelverhagen/4d32044a8003360e81c68ac82ac318cc to your computer and use it in GitHub Desktop.
NuGet API, get download count per version
// All you need is the NuGet.Protocol package from NuGet.org.
using System;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
MainAsync().GetAwaiter().GetResult();
}
static async Task MainAsync()
{
var sourceRepository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
var search = await sourceRepository.GetResourceAsync<RawSearchResourceV3>();
var results = await search.Search(
"packageid:Newtonsoft.Json",
new SearchFilter(includePrerelease: true),
skip: 0,
take: 1,
log: NullLogger.Instance,
cancellationToken: CancellationToken.None);
foreach (var result in results)
{
foreach (var version in result["versions"])
{
Console.WriteLine($"{version["version"]} {version["downloads"]}");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment