Last active
January 22, 2020 19:27
-
-
Save loic-sharma/761000056cd0c15d8ccc3d7614f0148b to your computer and use it in GitHub Desktop.
Get the download count for all versions of a package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
<LangVersion>8.0</LangVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="NuGet.Protocol" Version="5.5.0-preview.1.6319" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using NuGet.Configuration; | |
using NuGet.Protocol; | |
using NuGet.Protocol.Core.Types; | |
namespace ConsoleApp17 | |
{ | |
public class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
var packageId = "Newtonsoft.Json"; | |
// 1. Find the latest search endpoint using the NuGet Service Index API | |
// See: https://docs.microsoft.com/en-us/nuget/api/service-index | |
var source = new PackageSource("https://api.nuget.org/v3/index.json"); | |
var providers = Repository.Provider.GetCoreV3(); | |
var repository = new SourceRepository(source, providers); | |
var serviceIndex = await repository.GetResourceAsync<ServiceIndexResourceV3>(); | |
var searchEndpoints = serviceIndex.GetServiceEntryUris(ServiceTypes.SearchQueryService); | |
if (!searchEndpoints.Any()) | |
{ | |
Console.WriteLine("Unable to find search endpoints"); | |
return; | |
} | |
// 2. Find your package's latest metadata using the NuGet Search V3 API | |
// See: https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource | |
var query = new Uri(searchEndpoints.First().ToString() + $"?q=packageid:{packageId}&prerelease=true&semVerLevel=2.0.0"); | |
using var httpClient = new HttpClient(); | |
using var response = await httpClient.GetAsync(query); | |
if (response.StatusCode != HttpStatusCode.OK) | |
{ | |
Console.WriteLine($"Unexpected response status code {response.StatusCode}: {response.ReasonPhrase}"); | |
return; | |
} | |
var content = await response.Content.ReadAsStringAsync(); | |
var results = JsonConvert.DeserializeObject<SearchResponse>(content); | |
foreach (var result in results.Data) | |
{ | |
Console.WriteLine($"Package {result.PackageId} has {result.TotalDownloads} total downloads."); | |
foreach (var version in result.Versions) | |
{ | |
Console.WriteLine($"Version {version.Version} has {version.Downloads} downloads"); | |
} | |
} | |
} | |
} | |
/// <summary> | |
/// The response to a search query. | |
/// | |
/// See https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource#response | |
/// </summary> | |
public class SearchResponse | |
{ | |
/// <summary> | |
/// The packages that matched the search query. | |
/// </summary> | |
[JsonProperty("data")] | |
public IReadOnlyList<SearchResult> Data { get; set; } | |
} | |
/// <summary> | |
/// A package that matched a search query. | |
/// | |
/// See https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource#search-result | |
/// </summary> | |
public class SearchResult | |
{ | |
/// <summary> | |
/// The ID of the matched package. | |
/// </summary> | |
[JsonProperty("id")] | |
public string PackageId { get; set; } | |
/// <summary> | |
/// The total downloads for all versions of the matched package. | |
/// </summary> | |
[JsonProperty("totalDownloads")] | |
public long TotalDownloads { get; set; } | |
/// <summary> | |
/// The versions of the matched package. | |
/// </summary> | |
[JsonProperty("versions")] | |
public IReadOnlyList<SearchResultVersion> Versions { get; set; } | |
} | |
/// <summary> | |
/// A single version from a <see cref="SearchResult"/>. | |
/// | |
/// See https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource#search-result | |
/// </summary> | |
public class SearchResultVersion | |
{ | |
/// <summary> | |
/// The package's full NuGet version after normalization, including any SemVer 2.0.0 build metadata. | |
/// </summary> | |
[JsonProperty("version")] | |
public string Version { get; set; } | |
/// <summary> | |
/// The downloads for this single version of the matched package. | |
/// </summary> | |
[JsonProperty("downloads")] | |
public long Downloads { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Package Newtonsoft.Json has 363187770 total downloads. | |
Version 3.5.8 has 371478 downloads | |
Version 4.0.1 has 104026 downloads | |
Version 4.0.2 has 72464 downloads | |
Version 4.0.3 has 56128 downloads | |
Version 4.0.4 has 35853 downloads | |
Version 4.0.5 has 144331 downloads | |
Version 4.0.6 has 19351 downloads | |
Version 4.0.7 has 269272 downloads | |
Version 4.0.8 has 261249 downloads | |
Version 4.5.1 has 338693 downloads | |
Version 4.5.2 has 19936 downloads | |
Version 4.5.3 has 32155 downloads | |
Version 4.5.4 has 72339 downloads | |
Version 4.5.5 has 74870 downloads | |
Version 4.5.6 has 1883480 downloads | |
Version 4.5.7 has 282482 downloads | |
Version 4.5.8 has 250974 downloads | |
Version 4.5.9 has 215814 downloads | |
Version 4.5.10 has 333492 downloads | |
Version 4.5.11 has 7611395 downloads | |
Version 5.0.1 has 1605050 downloads | |
Version 5.0.2 has 270262 downloads | |
Version 5.0.3 has 310856 downloads | |
Version 5.0.4 has 1820025 downloads | |
Version 5.0.5 has 558454 downloads | |
Version 5.0.6 has 2968931 downloads | |
Version 5.0.7 has 471055 downloads | |
Version 5.0.8 has 3091282 downloads | |
Version 6.0.1 has 2495051 downloads | |
Version 6.0.2 has 857469 downloads | |
Version 6.0.3 has 2272229 downloads | |
Version 6.0.4 has 18483146 downloads | |
Version 6.0.5 has 2997978 downloads | |
Version 6.0.6 has 4653944 downloads | |
Version 6.0.7 has 786859 downloads | |
Version 6.0.8 has 15272670 downloads | |
Version 7.0.1 has 12990520 downloads | |
Version 8.0.1 has 1343575 downloads | |
Version 8.0.2 has 4331554 downloads | |
Version 8.0.3 has 9875041 downloads | |
Version 9.0.1 has 37344517 downloads | |
Version 10.0.1 has 29783504 downloads | |
Version 10.0.2 has 28854458 downloads | |
Version 10.0.3 has 58162926 downloads | |
Version 11.0.1 has 18937622 downloads | |
Version 11.0.2 has 30864487 downloads | |
Version 12.0.1 has 27545793 downloads | |
Version 12.0.2 has 25201304 downloads | |
Version 12.0.3 has 3476765 downloads |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment