Last active
March 22, 2019 06:59
-
-
Save kokeiro001/81dca2860a047b083c4db1ece187315f to your computer and use it in GitHub Desktop.
GitHubのコントリビューション数をパースして取得するやつ
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
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; } | |
} |
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> | |
<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