Skip to content

Instantly share code, notes, and snippets.

@kokeiro001
Last active March 22, 2019 06:59
Show Gist options
  • Save kokeiro001/81dca2860a047b083c4db1ece187315f to your computer and use it in GitHub Desktop.
Save kokeiro001/81dca2860a047b083c4db1ece187315f to your computer and use it in GitHub Desktop.
GitHubのコントリビューション数をパースして取得するやつ
public static class GitHubContributionsReader
{
static readonly string HostUrl = @"https://github.com/";
static readonly HttpClient httpClient = new HttpClient();
public static async Task<IEnumerable<ContributionItem>> GetContributionsAsync(string username)
{
var url = HostUrl + username;
var html = await httpClient.GetStringAsync(url);;
return ParseHtml(html);
}
private static IEnumerable<ContributionItem> ParseHtml(string html)
{
var document = new HtmlParser().Parse(html);
var graphCanvas = document
.QuerySelectorAll("div")
.Where(x => x.ClassList.Contains("graph-canvas"))
.First();
var dayNodes = graphCanvas.QuerySelectorAll("rect");
return dayNodes.Select(x => new ContributionItem
{
Date = DateTime.Parse(x.GetAttribute("data-date")),
Contributions = int.Parse(x.GetAttribute("data-count")),
})
.OrderBy(x => x.Date);
}
}
public class ContributionItem
{
public DateTime Date { get; set; }
public int Contributions { get; set; }
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AngleSharp" Version="0.9.9" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment