Repository navigation
Expand file tree
/
Copy pathScraper.cs
More file actions
62 lines (61 loc) · 3.19 KB
/
Copy pathScraper.cs
File metadata and controls
62 lines (61 loc) · 3.19 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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace WRBot.Scraper {
class WRScraper {
static Regex ContentMatcher = new Regex (@"<pre style=""white-space:pre-wrap; word-wrap:break-word;"">([\w\W]*)<\/pre>", RegexOptions.Multiline | RegexOptions.Compiled);
static Regex TableColumnMatcher = new Regex (@"<tr>([\s\S]*?)<\/tr>", RegexOptions.Multiline | RegexOptions.Compiled);
static Regex WeekMatcher = new Regex (@"location\.href='\/view\/week\/([0-9]*)'", RegexOptions.Multiline | RegexOptions.Compiled);
static Regex BgcolorMatcher = new Regex (@"bgcolor='(.*?)'", RegexOptions.Multiline | RegexOptions.Compiled);
static Regex UserMatcher = new Regex (@"onclick=""location\.href='\/view\/user\/([0-9]*)'"">(.*)<\/td>", RegexOptions.Multiline | RegexOptions.Compiled);
public static Dictionary<string, int> users { private set; get; } = new Dictionary<string, int> ();
public static List<string> submittedUsers { private set; get; } = new List<string> ();
static string indexPage = "", currentWeek = "";
static DateTime lastUpdate = new DateTime ();
static TimeSpan updateInterval = new TimeSpan (0, 1, 0);
static Match GetLastMatch (Match t) {
Match ret = t;
while (t.Success) {
ret = t;
t = t.NextMatch ();
}
return ret;
}
public async static Task updateIndex () {
if (DateTime.Now - lastUpdate < updateInterval)
return;
using (HttpClient cli = new HttpClient ()) {
var res = await cli.GetAsync ("http://wr.xdsec.club/index");
indexPage = await res.Content.ReadAsStringAsync ();
}
Match w = WeekMatcher.Match (indexPage);
currentWeek = GetLastMatch (w).Groups[1].Value; // 6th row
submittedUsers.Clear ();
Match u = TableColumnMatcher.Match (indexPage).NextMatch (); //skips the table headers
while (u.Success) {
Match user = UserMatcher.Match (u.Value);
//assert(user.Success == true)
users[user.Groups[2].Value] = int.Parse (user.Groups[1].Value);
if (GetLastMatch (BgcolorMatcher.Match (u.Value)).Groups[1].Value == "green")
submittedUsers.Add (user.Groups[2].Value);
u = u.NextMatch ();
}
}
public async static Task<string> getWRFor (HttpClient cli, string username) {
await updateIndex ();
if (users.ContainsKey (username) == false) {
throw new System.IndexOutOfRangeException ();
} else if (submittedUsers.Contains (username) == false) {
return "尚未提交";
}
var resp = await cli.GetAsync ($"http://wr.xdsec.club/view/user/{users[username]}/{currentWeek}");
return ContentMatcher.Match (await resp.Content.ReadAsStringAsync ()).Groups[1].Value;
}
public async static Task submitWR(HttpClient cli, string wr){
await updateIndex();
// TODO:
}
}
}