Last active
March 21, 2019 14:48
-
-
Save nuitsjp/d29be1a65ddf9e2281cee7bdf5100b4c to your computer and use it in GitHub Desktop.
Count download of GitHub release module.
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 Newtonsoft.Json.Linq; | |
using System; | |
using System.Net.Http; | |
using System.Linq; | |
using System.Threading.Tasks; | |
public static class GitHubCounter | |
{ | |
public static async Task CountDownload(string owner, string repository) | |
{ | |
using (var httpClient = new HttpClient()) | |
{ | |
httpClient.DefaultRequestHeaders.Add("User-Agent", "C# HttpClient"); | |
var downloadCount = | |
JArray | |
.Parse(await httpClient.GetStringAsync($"https://api.github.com/repos/{owner}/{repository}/releases")) | |
.SelectMany(x => JArray.FromObject(x["assets"])) | |
.Sum(x => int.Parse((string)x["download_count"])); | |
Console.WriteLine(downloadCount); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment