Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to .NET 6.0 #16

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-mgcb": {
"version": "3.8.1.303",
"commands": [
"mgcb"
]
},
"dotnet-mgcb-editor": {
"version": "3.8.1.303",
"commands": [
"mgcb-editor"
]
},
"dotnet-mgcb-editor-linux": {
"version": "3.8.1.303",
"commands": [
"mgcb-editor-linux"
]
},
"dotnet-mgcb-editor-windows": {
"version": "3.8.1.303",
"commands": [
"mgcb-editor-windows"
]
},
"dotnet-mgcb-editor-mac": {
"version": "3.8.1.303",
"commands": [
"mgcb-editor-mac"
]
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,4 @@ bin/Windows/x64/Debug
Content/bin

obj/
bin/
124 changes: 62 additions & 62 deletions Calendar.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
using iCal.PCL.DataModel;
using iCal.PCL.Serialization;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
// using iCal.PCL.DataModel;
// using iCal.PCL.Serialization;
// using RestSharp;
// using System;
// using System.Collections.Generic;
// using System.Linq;

namespace GordonWare {
// namespace GordonWare {

public enum GroupId {
Promotion,
Group1,
Group2,
Group3
}
// public enum GroupId {
// Promotion,
// Group1,
// Group2,
// Group3
// }

public static class Calendar {
// public static class Calendar {

private static bool IsCurrentEvent(DateTime now, iCalVEvent ev) {
return ev.DTStart.Date == now.Date && ev.DTEnd.Date == now.Date &&
ev.DTStart.TimeOfDay <= now.TimeOfDay && ev.DTEnd.TimeOfDay >= now.TimeOfDay;
}
// private static bool IsCurrentEvent(DateTime now, iCalVEvent ev) {
// return ev.DTStart.Date == now.Date && ev.DTEnd.Date == now.Date &&
// ev.DTStart.TimeOfDay <= now.TimeOfDay && ev.DTEnd.TimeOfDay >= now.TimeOfDay;
// }

private static bool ForGroup(iCalVEvent ev, GroupId groupId) {
return ev.Description.Contains($"INFO3 {Groups[(int) groupId]}");
}
// private static bool ForGroup(iCalVEvent ev, GroupId groupId) {
// return ev.Description.Contains($"INFO3 {Groups[(int) groupId]}");
// }

private const string URL =
"http://edt-v2.univ-nantes.fr/calendar/ics?timetables[0]=20739&timetables[1]=20740&timetables[2]=20741&timetables[3]=20742";
// private const string URL =
// "http://edt-v2.univ-nantes.fr/calendar/ics?timetables[0]=20739&timetables[1]=20740&timetables[2]=20741&timetables[3]=20742";

private static readonly string[] Groups = {"PROMOTION", "G1", "G2", "G3"};
// private static readonly string[] Groups = {"PROMOTION", "G1", "G2", "G3"};

/// <summary>
/// Gets current class name of the given <see cref="groupId"/> from the public calendar.
/// </summary>
/// <returns>Returns current class name if there is only one. Otherwise, returns null.</returns>
public static string GetCurrentClass(GroupId groupId) {
//make and execute a HTTP GET request to get the calendar
var client = new RestClient(URL);
var request = new RestRequest(Method.GET);
var response = client.Execute(request);
// /// <summary>
// /// Gets current class name of the given <see cref="groupId"/> from the public calendar.
// /// </summary>
// /// <returns>Returns current class name if there is only one. Otherwise, returns null.</returns>
// public static string GetCurrentClass(GroupId groupId) {
// //make and execute a HTTP GET request to get the calendar
// var client = new RestClient(URL);
// var request = new RestRequest(Method.GET);
// var response = client.Execute(request);

if (!response.IsSuccessful) {
return null;
}
// if (!response.IsSuccessful) {
// return null;
// }

//cut the raw iCal content into lines
var stringSeparators = new[] {"\r\n"};
var lines = response.Content.Split(stringSeparators, StringSplitOptions.None);
// //cut the raw iCal content into lines
// var stringSeparators = new[] {"\r\n"};
// var lines = response.Content.Split(stringSeparators, StringSplitOptions.None);

IEnumerable<iCalVEvent> vEvents;
// IEnumerable<iCalVEvent> vEvents;

try {
//deserialize the iCal file
var iCal = iCalSerializer.Deserialize(lines);
vEvents = iCal.Cast<iCalVEvent>();
} catch (InvalidOperationException e) {
Console.WriteLine(e);
return null;
}
// try {
// //deserialize the iCal file
// var iCal = iCalSerializer.Deserialize(lines);
// vEvents = iCal.Cast<iCalVEvent>();
// } catch (InvalidOperationException e) {
// Console.WriteLine(e);
// return null;
// }

var now = DateTime.Now;
// var now = DateTime.Now;

//add 2 hours to get french time and find current class
var currentClass = vEvents.Select(ev => {
ev.DTStart = ev.DTStart.AddHours(2);
ev.DTEnd = ev.DTEnd.AddHours(2);
return ev;
}).Where(ev =>
IsCurrentEvent(now, ev) && ForGroup(ev, groupId)
).ToList();
// //add 2 hours to get french time and find current class
// var currentClass = vEvents.Select(ev => {
// ev.DTStart = ev.DTStart.AddHours(2);
// ev.DTEnd = ev.DTEnd.AddHours(2);
// return ev;
// }).Where(ev =>
// IsCurrentEvent(now, ev) && ForGroup(ev, groupId)
// ).ToList();

//return class name only if there is exactly one current class
return currentClass.Count == 1 ? currentClass[0].Summary : null;
}
}
}
// //return class name only if there is exactly one current class
// return currentClass.Count == 1 ? currentClass[0].Summary : null;
// }
// }
// }
Loading