Skip to content

Instantly share code, notes, and snippets.

@stevencohn
Created November 16, 2021 23:03
Show Gist options
  • Save stevencohn/a6ed8d51500da3ab6d8bd768c593e9c5 to your computer and use it in GitHub Desktop.
Save stevencohn/a6ed8d51500da3ab6d8bd768c593e9c5 to your computer and use it in GitHub Desktop.
Report GitHub asset downloads, LinqPad snippet
async Task Main()
{
var releases = new List<Release>();
var page = 1;
while (await GetReleases(releases, page++));
releases.Dump();
}
class Release
{
public string Tag;
public string Name;
public int Downloads64;
public int Downloads86;
}
private async Task<bool> GetReleases(List<Release> releases, int page)
{
($"... fetching page {page}").Dump();
string url = $"https://api.github.com/repos/stevencohn/OneMore/releases?page={page}&per_page=100";
var count = 0;
using var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("OneMore", "1.0"));
using var response = await client.GetAsync(new Uri(url, UriKind.Absolute));
if (response.IsSuccessStatusCode)
{
using var reader = new JsonTextReader(new StreamReader(response.Content.ReadAsStream()));
var dom = await JObject.ReadFromAsync(reader);
foreach (var rel in dom)
{
var release = new Release
{
Tag = (string)rel["tag_name"],
Name = (string)rel["name"]
};
foreach (var asset in rel["assets"])
{
if (((string)asset["name"]).Contains("64"))
{
release.Downloads64 = (int)asset["download_count"];
}
else
{
release.Downloads86 = (int)asset["download_count"];
}
}
releases.Add(release);
count++;
}
}
else
{
$"status code[{response.StatusCode}]".Dump();
response.Dump();
}
return count > 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment