-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScraper.cs
More file actions
67 lines (58 loc) · 1.97 KB
/
Scraper.cs
File metadata and controls
67 lines (58 loc) · 1.97 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
using System;
using System.IO;
using System.Net.Http;
using HtmlAgilityPack;
namespace CheatEngineForumScraper
{
internal static class Scraper
{
public static Data ScrapeProfiles(int startID, int endID, Data? appendData = null, bool continueScrape = false)
{
const string PROFILE_URL = "https://forum.cheatengine.org/profile.php?mode=viewprofile&u=";
Data data = appendData ?? new();
if (data.Profiles.Count > 0 && continueScrape)
startID = data.Profiles[data.Profiles.Count - 1].Id + 1;
void OnCancelRequested(object? sender, ConsoleCancelEventArgs e)
{
data.Running = false;
e.Cancel = true;
}
Console.CancelKeyPress += OnCancelRequested;
HtmlDocument document = new();
HttpClient client = new();
for (int i = startID; i <= endID; i++)
{
if (!data.Running) break;
Stream? pageStream = null;
try
{
pageStream = client.GetStreamAsync(PROFILE_URL + i.ToString()).Result;
document.Load(pageStream);
}
catch (Exception e)
{
Console.WriteLine(e);
break;
}
finally
{
pageStream?.Dispose();
}
var newProfile = Profile.FromHtml(document.DocumentNode);
if (newProfile is null)
{
data.ErrorIDs.Add(i);
data.LogLastFailure();
}
else
{
newProfile.Id = i;
data.Profiles.Add(newProfile);
data.LogLastSuccess();
}
}
Console.CancelKeyPress -= OnCancelRequested;
return data;
}
}
}