Skip to content

Instantly share code, notes, and snippets.

@vshapenko
Created September 3, 2022 07:01
Show Gist options
  • Save vshapenko/88c820a5cebc1b7fb88a4bb8a3dede0f to your computer and use it in GitHub Desktop.
Save vshapenko/88c820a5cebc1b7fb88a4bb8a3dede0f to your computer and use it in GitHub Desktop.
using System.Collections.Concurrent;
using System.Text.RegularExpressions;
using System.Threading.Channels;
string file = await new HttpClient().GetStringAsync(
"https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/");
MatchCollection pullRequestUrls =
Regex.Matches(file, @"https:\/\/github.com\/[a-zA-Z-]+\/[a-zA-Z-]+\/pull\/[0-9]+");
Dictionary<string,int> authors = new();
var channel = Channel.CreateUnbounded<string>();
TaskCompletionSource res = new TaskCompletionSource();
async Task ReadNickName()
{
await foreach (var name in channel.Reader.ReadAllAsync())
{
if (authors.ContainsKey(name))
{
authors[name]++;
}
else {
authors[name] = 1;
}
Console.Clear();
foreach (var s in authors.OrderByDescending(x => x.Value))
{
Console.WriteLine($"{s.Key} - {s.Value}");
}
}
res.SetResult();
}
ReadNickName();
var counter = 1;
await Parallel.ForEachAsync(pullRequestUrls.Select(m => m.ToString().Trim()).Distinct().AsParallel(),
new ParallelOptions { MaxDegreeOfParallelism = 16 },
async (pullRequestUrl, ct) =>
{
// Thread.Sleep(Random.Shared.Next(100, 300));
TRY_AGAIN:
try
{
string content = await new HttpClient().GetStringAsync(pullRequestUrl);
string title = Regex.Match(content, @"<title>(.*?)<\/title>").Value
.Replace("<title>", "")
.Replace("</title>", "")
.Trim();
if (!title.Contains(" · Pull Request #"))
{
// there are 6 links which are github issues rather than PRs despite /pull/ in the url
return;
}
title = title.Substring(0, title.IndexOf(" · Pull Request #"));
string nick = title.Substring(title.LastIndexOf(" by ") + " by ".Length).Trim();
await channel.Writer.WriteAsync(nick);
}
catch
{
await Task.Delay(Random.Shared.Next(100, 300));
Console.WriteLine("Trying again...");
goto TRY_AGAIN;
}
});
channel.Writer.Complete();
await res.Task;
var authorsToPrint = authors.Select(x=>new {Name = x.Key,Count = x.Value}).OrderByDescending(x=>x.Count);
foreach (var author in authorsToPrint)
{
Console.WriteLine(author.Name.PadRight(
authorsToPrint.Select(a => a.Name.Length).Max() + 1) + " -- " + author.Count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment