-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.cs
More file actions
129 lines (112 loc) · 4.43 KB
/
Copy pathparser.cs
File metadata and controls
129 lines (112 loc) · 4.43 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using HtmlAgilityPack;
using System.Net;
using System;
namespace Parser
{
public struct Lession {
public string time;
public string place;
public string type;
public string name;
public bool isOnUpWeek;
public bool isOnDownWeek;
//public List<string> preps;
//public List<string> groups;
public void Print() {
Console.WriteLine($"{time} - {name}");
}
}
public struct DaySchedule {
public string weekday = "";
public List<Lession> lessions = new List<Lession>();
public void Print() {
Console.WriteLine(weekday);
foreach (Lession i in lessions) {
i.Print();
};
}
}
public struct Schedule
{
public List<DaySchedule> days = new List<DaySchedule>();
public DaySchedule out_grid = new DaySchedule();
public void Print() {
out_grid.Print();
foreach (DaySchedule i in days) {
i.Print();
};
}
}
public static class Parser
{
public const string DEFAULT_URL = "https://guap.ru/rasp/?g=303";
public static async Task<string> getHTML(string url) {
HttpClient client = new HttpClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;
client.DefaultRequestHeaders.Accept.Clear();
Task<string> response = client.GetStringAsync(url);
return await response;
}
public static Schedule ParseHtml(string html)
{
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
HtmlAgilityPack.HtmlNodeCollection elements = htmlDoc.DocumentNode.SelectNodes("//div[@class='result']")[0].ChildNodes;
List<DaySchedule> list = new List<DaySchedule>();
// first element is legend (not need)
// second element is name of group (not need)
string time = "";
DaySchedule day_schedule = new DaySchedule();
for (int i = 2; i < elements.Count; i++) {
if (elements[i].Name == "h3") { // new day
list.Add(day_schedule);
day_schedule = new DaySchedule();
day_schedule.weekday = elements[i].InnerText;
if (day_schedule.weekday.Trim() == "Вне сетки расписания") {
day_schedule.weekday = "None";
};
} else if (elements[i].Name == "h4") { // new couple
time = elements[i].InnerText;
if (time.Trim() == "вне сетки расписания (—)") {
time = "None";
};
} else if (elements[i].Name == "div") { // new description
Lession lession = new Lession();
lession.time = time;
HtmlAgilityPack.HtmlNode temp0 = elements[i].ChildNodes[0];
string[] temp1 = temp0.InnerText.Replace("▼", "▲").Split("▲");
List<string> temp2;
if (elements[i].ChildNodes[0].ChildNodes.Count == 3) {
lession.isOnUpWeek = true;
lession.isOnDownWeek = true;
temp2 = temp1[0].Split("–").ToList();
} else {
lession.isOnUpWeek = temp0.ChildNodes[0].Attributes["class"].Value == "up";
lession.isOnDownWeek = !lession.isOnUpWeek;
temp2 = temp1[1].Split("–").ToList();
};
lession.type = temp2[0].Trim();
lession.name = temp2[1].Trim();
lession.place = temp2[2].Trim();
day_schedule.lessions.Add(lession);
};
}
list.Add(day_schedule);
Schedule schedule = new Schedule();
schedule.out_grid = list[2];
list.RemoveRange(0, 3);
schedule.days = list;
return schedule;
}
public static Schedule GetSchedule(string url = DEFAULT_URL)
{
return ParseHtml(getHTML(url).Result);
}
}
public class Program {
static void Main() {
Schedule answer = Parser.GetSchedule();
answer.Print();
}
}
}