Skip to content

Instantly share code, notes, and snippets.

@doccaico
Created June 18, 2026 02:35
Show Gist options
  • Select an option

  • Save doccaico/a104eedcf1957896581b994f6d97ca47 to your computer and use it in GitHub Desktop.

Select an option

Save doccaico/a104eedcf1957896581b994f6d97ca47 to your computer and use it in GitHub Desktop.
Getting Github Repo Star in C#
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
string[,] urlList =
{
{ "nim-lang", "Nim" },
{ "odin-lang", "Odin" },
{ "rust-lang", "rust" },
};
var tasks = new List<Task<(string owner, string repo, int? stars)>>();
int length = urlList.GetLength(0);
for (int i = 0; i < length; i++)
{
string owner = urlList[i, 0];
string repo = urlList[i, 1];
tasks.Add(ProcessRepoAsync(owner, repo));
}
var results = await Task.WhenAll(tasks);
foreach (var result in results)
{
if (result.stars.HasValue)
{
Console.WriteLine($"{result.owner}/{result.repo} - Star Count: {result.stars.Value}");
}
else
{
Console.WriteLine($"{result.owner}/{result.repo} - Failed to retrieve the number of stars");
}
}
static async Task<(string owner, string repo, int? stars)> ProcessRepoAsync(
string owner,
string repo
)
{
int? stars = await GetStar(owner, repo);
return (owner, repo, stars);
}
static async Task<int?> GetStar(string owner, string repo)
{
string url = $"https://github.com/{owner}/{repo}";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
try
{
string html = await client.GetStringAsync(url);
string pattern = @"<span id=""repo-stars-counter-star.+?title=""(.+?)"".+?</span>";
Match match = Regex.Match(html, pattern);
if (!match.Success)
{
return null;
}
var valueString = match.Groups[1].Value.Replace(",", "");
if (int.TryParse(valueString, out int result))
{
return result;
}
return null;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment