Skip to content

Commit

Permalink
feat: add docs, add CIST mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ketronix-dev committed Jan 11, 2025
1 parent ff83a7c commit 868a6b6
Show file tree
Hide file tree
Showing 6 changed files with 587 additions and 217 deletions.
36 changes: 36 additions & 0 deletions Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public static class Cist
throw new Exception("Error while getting auditories", e);
}
}

public static string GetAuditories(bool AsCist)
{
try
{
return NureParser.ParseAuditories(AsCist);
}
catch (Exception e)
{
throw new Exception("Error while getting auditories", e);
}
}

public static List<Group>? GetGroups()
{
Expand All @@ -29,6 +41,18 @@ public static class Cist
throw new Exception("Error while getting groups", e);
}
}

public static string GetGroups(bool AsCist)
{
try
{
return NureParser.ParseGroups(AsCist);
}
catch (Exception e)
{
throw new Exception("Error while getting groups", e);
}
}

public static List<Teacher>? GetTeachers()
{
Expand All @@ -41,6 +65,18 @@ public static class Cist
throw new Exception("Error while getting teachers", e);
}
}

public static string GetTeachers(bool AsCist)
{
try
{
return NureParser.ParseTeachers(AsCist);
}
catch (Exception e)
{
throw new Exception("Error while getting teachers", e);
}
}

public static List<Event>? GetEvents( EventType type, long id, long startTime = 0, long endTime = 0)
{
Expand Down
4 changes: 2 additions & 2 deletions Nure.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<PackageLicenseExpression>GPL-3.0-or-later</PackageLicenseExpression>
<RepositoryUrl>https://github.com/mindenit/Nure.NET</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<ApplicationVersion>1.0.1</ApplicationVersion>
<Version>1.0.1</Version>
<ApplicationVersion>1.0.2</ApplicationVersion>
<Version>1.0.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
32 changes: 32 additions & 0 deletions Parsers/NureParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private static string GetType(int? id)
List<Auditory>? auditories = new List<Auditory>();

var json = Requests.GetAuditoriesJson();

var cistAuditories = JsonSerializer.Deserialize<JsonElement>(json);

if (cistAuditories.TryGetProperty("university", out var university) &&
Expand All @@ -67,6 +68,16 @@ private static string GetType(int? id)

return RemoveDuplicateAuditories(auditories);
}

public static string ParseAuditories(bool asCist)
{
if (asCist)
{
var json = Requests.GetAuditoriesJson();
return json;
}
return "";
}

private static List<Auditory> RemoveDuplicateAuditories(List<Auditory> list)
{
Expand Down Expand Up @@ -136,6 +147,17 @@ private static List<Auditory> RemoveDuplicateAuditories(List<Auditory> list)
return RemoveDuplicateGroups(groups);
}

public static string ParseGroups(bool asCist)
{
if (asCist)
{
var json = Requests.GetGroupsJson();
return json;
}

return "";
}

private static List<Group> RemoveDuplicateGroups(List<Group> deserialize)
{
return deserialize.GroupBy(x => new { x.Id, x.Name })
Expand Down Expand Up @@ -200,6 +222,16 @@ private static List<Group> RemoveDuplicateGroups(List<Group> deserialize)
return RemoveDuplicateTeachers(teachers);
}

public static string ParseTeachers(bool asCist)
{
if (asCist)
{
var json = Requests.GetTeachersJson();
return json;
}
return "";
}

private static List<Teacher> RemoveDuplicateTeachers(List<Teacher>? deserialize)
{
return deserialize.GroupBy(x => new { x.Id })
Expand Down
209 changes: 70 additions & 139 deletions Parsers/Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,130 +6,95 @@ namespace Nure.NET.Parsers;

public class Requests
{
public static string GetAuditoriesJson()
private static readonly string[] Servers = { "cist.nure.ua", "cist2.nure.ua" };

private static string GetAvailableServer()
{
var ping = new System.Net.NetworkInformation.Ping();

var result = ping.Send("cist2.nure.ua", 500);

if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
foreach (var server in Servers)
{
return "";
}
else
{
using (HttpClient httpClient = new HttpClient())
var result = ping.Send(server, 500);
if (result.Status == System.Net.NetworkInformation.IPStatus.Success)
{
var webRequest = WebRequest.Create("https://cist2.nure.ua/ias/app/tt/P_API_AUDITORIES_JSON") as HttpWebRequest;

webRequest.ContentType = "application/json";

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

using (var webResponse = webRequest.GetResponse())
using (var streamReader =
new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1251")))
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8))
// Додаткова перевірка HTTP
try
{
streamWriter.Write(streamReader.ReadToEnd());
streamWriter.Flush();
memoryStream.Position = 0;

var json = Encoding.UTF8.GetString(memoryStream.ToArray());

// Remove BOM
json = json.TrimStart('\uFEFF');

return json;
using (var client = new HttpClient())
{
var response = client.GetAsync($"https://{server}").Result;
if (response.StatusCode == HttpStatusCode.OK)
{
return server;
}
}
}
catch
{
continue;
}
}
}

return string.Empty;
}

public static string GetGroupsJson()
private static string FetchJsonFromUrl(string endpoint)
{
var ping = new System.Net.NetworkInformation.Ping();

var result = ping.Send("cist2.nure.ua", 500);

if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
var server = GetAvailableServer();
if (string.IsNullOrEmpty(server))
{
return "";
}
else
{
using (HttpClient httpClient = new HttpClient())
{
var webRequest =
WebRequest.Create("https://cist2.nure.ua/ias/app/tt/P_API_GROUP_JSON") as HttpWebRequest;

webRequest.ContentType = "application/json";

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var fullUrl = $"https://{server}{endpoint}";

using (var webResponse = webRequest.GetResponse())
using (var streamReader =
new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1251")))
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8))
{
streamWriter.Write(streamReader.ReadToEnd());
streamWriter.Flush();
memoryStream.Position = 0;
try
{
var webRequest = WebRequest.Create(fullUrl) as HttpWebRequest;

Check warning on line 54 in Parsers/Requests.cs

View workflow job for this annotation

GitHub Actions / create_nuget

'WebRequest.Create(string)' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)
webRequest.ContentType = "application/json";

Check warning on line 55 in Parsers/Requests.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Dereference of a possibly null reference.

var json = Encoding.UTF8.GetString(memoryStream.ToArray());
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

// Remove BOM
json = json.TrimStart('\uFEFF');
using (var webResponse = webRequest.GetResponse())
using (var streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1251")))
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8))
{
streamWriter.Write(streamReader.ReadToEnd());
streamWriter.Flush();
memoryStream.Position = 0;

return json;
}
var json = Encoding.UTF8.GetString(memoryStream.ToArray());
json = json.TrimStart('\uFEFF');
return json;
}
}
}

public static string GetTeachersJson()
{
var ping = new System.Net.NetworkInformation.Ping();

var result = ping.Send("cist2.nure.ua", 500);

if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
catch
{
return "";
}
else
{
using (HttpClient httpClient = new HttpClient())
{
var webRequest = WebRequest.Create("https://cist2.nure.ua/ias/app/tt/P_API_PODR_JSON") as HttpWebRequest;

webRequest.ContentType = "application/json";

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

using (var webResponse = webRequest.GetResponse())
using (var streamReader =
new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1251")))
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8))
{
streamWriter.Write(streamReader.ReadToEnd());
streamWriter.Flush();
memoryStream.Position = 0;
}

var json = Encoding.UTF8.GetString(memoryStream.ToArray());
public static string GetAuditoriesJson()
{
return FetchJsonFromUrl("/ias/app/tt/P_API_AUDITORIES_JSON");
}

// Remove BOM
json = json.TrimStart('\uFEFF');
json = json.Remove(json.Length - 2);
json += "]}}";
public static string GetGroupsJson()
{
return FetchJsonFromUrl("/ias/app/tt/P_API_GROUP_JSON");
}

return json;
}
}
public static string GetTeachersJson()
{
var json = FetchJsonFromUrl("/ias/app/tt/P_API_PODR_JSON");
if (!string.IsNullOrEmpty(json))
{
json = json.Remove(json.Length - 2);
json += "]}}";
}
return json;
}

private static long GetUnixTimestamp(DateTime date)
Expand All @@ -152,50 +117,16 @@ private static long EndTime()
var september1 = new DateTime(targetYear, 9, 1);
return GetUnixTimestamp(september1);
}

public static string GetEventsJson(EventType type, long id)
{
var ping = new System.Net.NetworkInformation.Ping();

var result = ping.Send("cist2.nure.ua", 500);

if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
{
return "";
}
else
{
using (HttpClient httpClient = new HttpClient())
{
var webRequest = WebRequest.Create($"https://cist2.nure.ua/ias/app/tt/P_API_EVEN_JSON?" +
$"type_id={(int)type}" +
$"&timetable_id={id}" +
$"&time_from={StartTime()}" +
$"&time_to={EndTime()}" +
"&idClient=KNURESked") as HttpWebRequest;

webRequest.ContentType = "application/json";

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

using (var webResponse = webRequest.GetResponse())
using (var streamReader =
new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1251")))
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8))
{
streamWriter.Write(streamReader.ReadToEnd());
streamWriter.Flush();
memoryStream.Position = 0;

var json = Encoding.UTF8.GetString(memoryStream.ToArray());

// Remove BOM
json = json.TrimStart('\uFEFF');

return json;
}
}
}
var url = $"/ias/app/tt/P_API_EVEN_JSON?" +
$"type_id={(int)type}" +
$"&timetable_id={id}" +
$"&time_from={StartTime()}" +
$"&time_to={EndTime()}" +
"&idClient=KNURESked";

return FetchJsonFromUrl(url);
}
}
}
Loading

0 comments on commit 868a6b6

Please sign in to comment.