Created
November 16, 2021 23:03
-
-
Save stevencohn/a6ed8d51500da3ab6d8bd768c593e9c5 to your computer and use it in GitHub Desktop.
Report GitHub asset downloads, LinqPad snippet
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
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