-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
77 lines (63 loc) · 2.68 KB
/
Program.cs
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
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using NmuScheduleParser;
namespace NSParserExample
{
internal class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
MainAsync().GetAwaiter().GetResult();
}
static async Task MainAsync()
{
var htmlRaw = await DownloadHtmlSchedule(@"https://nmc.udu.edu.ua", "31ІПЗ");
var schedule = ScheduleParser.GetSchedule(htmlRaw);
/*
Schedule:
List<Day> Days
Day:
string Date
string DayOfWeek
List<Class> Classes
Class:
string StartTime
string EndTime
ClassInfo FirstClass
ClassInfo SecondClass
ClassInfo:
string[] Description
string NameRemote
*/
Console.WriteLine(schedule); // print override ToString()
// Example using a model.
int classCount = 0;
if (schedule?.Days != null)
foreach (var day in schedule.Days)
classCount += day.Classes.Count;
var classAllMinutes = classCount * 80;
Console.WriteLine("\nClass count: {0}, It's {1} hours and {2} minutes", classCount, classAllMinutes / 60, classAllMinutes % 60);
Console.WriteLine("\nPress any key...");
Console.ReadKey();
}
static async Task<string> DownloadHtmlSchedule(string domain, string groupName, string startDate = "", string endDate = "")
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encodingWin1251 = Encoding.GetEncoding("windows-1251");
groupName = HttpUtility.UrlEncode(groupName, encodingWin1251);
var bodyParam = $"group={groupName}&sdate={startDate}&edate={endDate}";
var body = Encoding.UTF8.GetBytes(bodyParam);
var client = new HttpClient { BaseAddress = new Uri(domain) };
//n=700 should be as url parameter, otherwise it doesn't work
var response = await client.PostAsync(@"cgi-bin/timetable.cgi?n=700", new ByteArrayContent(body));
var responseContentBytes = await response.Content.ReadAsByteArrayAsync();
if (!response.IsSuccessStatusCode)
Console.WriteLine($"Status code download: {response.StatusCode}");
return encodingWin1251.GetString(responseContentBytes);
}
}
}