-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathParallelForEachAsync.cs
More file actions
101 lines (83 loc) · 2.95 KB
/
ParallelForEachAsync.cs
File metadata and controls
101 lines (83 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Runtime.CompilerServices;
async IAsyncEnumerable<string> GetInfoAsync(string[] users,
[EnumeratorCancellation]CancellationToken cancellationToken = default)
{
if (users is not null)
{
for (int i = 0; i < users.Length; i++)
{
var userHandler = users[i];
if (!cancellationToken.IsCancellationRequested)
yield return await Task.FromResult(userHandler);
else
{
// throw new TaskCancelledException()
await Task.FromCanceled(cancellationToken);
}
}
}
}
using HttpClient httpClient = new()
{
BaseAddress = new Uri("https://api.github.com"),
};
httpClient.DefaultRequestHeaders.
UserAgent.Add(new ProductInfoHeaderValue("Trying_Net6", "GitAPI"));
// GitHub provides OAuth token for 30 days,
// which is capable of recieving and sending 5000 respose/request per minute
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("token",
"Setttings > Developer Settings > Personal Access Tokens ");
ParallelOptions prlOptions = new()
{
MaxDegreeOfParallelism = 5
};
using var cancTokenSource = new CancellationTokenSource(5000);
try
{
await RunTasksParallelAsync(cancTokenSource.Token);
}
catch (TaskCanceledException)
{
Console.WriteLine("Request has been cancelled!");
}
async Task RunTasksParallelAsync(CancellationToken cancellationToken = default)
{
for (int i = 2000; i < 2025; i++)
{
string uri = $"https://api.github.com/users?since={i}&per_page=100";
var all_users =
await httpClient.GetStringAsync(uri,cancellationToken);
var filter_users = all_users.Split(",");
// creation of "users/{username}" string[] list;
var username_list =
filter_users.Where(x =>
{
int index = x.IndexOf("login");
if (index != -1)
return x.Substring(index, 5) == "login";
else
return false;
}).
Select(l => "users/" + l.Substring(l.IndexOf(":") + 1).
Replace("\"", String.Empty)).ToArray();
var userAsync = GetInfoAsync(username_list, cancellationToken);
await Parallel.
ForEachAsync(userAsync, prlOptions, async (uri, token) =>
{
var user =
await httpClient.GetFromJsonAsync<GithubAccount>(uri, token);
Console.WriteLine(
$"Name: {user?.Name}\n" +
$"Bio: {user?.Bio}\n" +
$"Twitter-UserName: {user?.Twitter_UserName}\n" +
$"Location: {user?.Location}\n" +
$"Blog: {user?.Blog}\n" +
$"Company: {user?.Company}\n");
});
}
}
public record GithubAccount(string Name,
string Bio, string Twitter_UserName,
string Location, string Blog,
string Company, string Email);