diff --git a/Program.cs b/Program.cs index fa72be9..5cf52f1 100644 --- a/Program.cs +++ b/Program.cs @@ -9,26 +9,18 @@ using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; using franz; -using Ical.Net; -using Ical.Net.CalendarComponents; -using Ical.Net.DataTypes; -using Ical.Net.Serialization; using Newtonsoft.Json; using ShowHandlers; namespace director { - class Program + public class Program { //private static Telefranz tf; public static Config conf; private static TimeSpan calendarNaptime = TimeSpan.FromHours(1); - private static Scratch scratch; + public static Scratch scratch; private static HttpClient httpClient; - private static DateTime searchStart = DateTime.Now; //is it slower to just call datetime.now every time? /shrug - private static DateTime searchEnd = DateTime.Now.AddDays(7); - //I don't understand why the entire .net ecosystem insists on ignoring ToString(). Is it really that much fun writing a serializer factory? ...java programmers. - private static EventSerializer ser = new EventSerializer(); private static readonly ConcurrentQueue workQueue = new ConcurrentQueue(); private static readonly AutoResetEvent _signal = new AutoResetEvent(false); private const int concurrentWorkers = 2; @@ -72,7 +64,7 @@ namespace director scratch.agenda.Clear(); } Task.WaitAll( - checkCalendar(conf.calendar_twitch, "youtube", (ref Schedulable.Schedulable n) => + checkCalendar(conf.calendar_youtube, "youtube", (ref Schedulable.Schedulable n) => { n.ScedulableType = Schedulable.ScedulableType.YTRelease; }), @@ -90,7 +82,6 @@ namespace director { Console.Error.WriteLine(e); } - Console.WriteLine($"calendars pulled, scratch has {scratch.agenda.Count} schedulables."); foreach (var s in scratch.agenda) { @@ -101,6 +92,7 @@ namespace director _signal.Set(); } } + Console.WriteLine("calendars checked"); } private delegate void schedulableCreate(ref Schedulable.Schedulable creating); @@ -109,21 +101,17 @@ namespace director //?export is a hack to allow me to access the calendar //it likes to throw an error saying "this is the webDAV interface, use webDAV" at my webDAV client, stopping me from using webDAV. var calString = await httpClient.GetStringAsync(conf.webdav_uri + calendarUri + "?export"); - var calT = Calendar.Load(calString); var knownChecklist = new List(); lock (scratch) { scratch.Calendars[calLabel] = calString; - foreach (var calOccurrence in calT.GetOccurrences(searchStart, searchEnd)) + iCalHoopJumping.LoadCalendar(calLabel, calString); + foreach (var occurrence in iCalHoopJumping.getOccurrences(calLabel)) { - var asEvent = calOccurrence.Source as Ical.Net.CalendarComponents.CalendarEvent; - var newSchedulable = new Schedulable.Schedulable() { - Event = ser.SerializeToString(asEvent), - OccurrenceStart = calOccurrence.Period.StartTime.Date, - OccurrenceEnd = calOccurrence.Period.EndTime.Date, - Showtime = calOccurrence.Period.StartTime.Date, + Occurrence = occurrence, + Showtime = occurrence.OccurrenceStart }; createSchedulable(ref newSchedulable); scratch.agenda.Add(newSchedulable); @@ -139,17 +127,27 @@ namespace director if (!workQueue.TryDequeue(out todo)) { continue; } - Console.WriteLine("threadwork consumes!"); - Task.Delay(todo.Showtime - conf.preshowBufferTime - DateTime.Now); + Console.WriteLine($"threadwork consumes! showtime at {todo.Showtime}; wake me at {todo.Showtime - conf.preshowBufferTime}"); + switch(todo.ScedulableType) + { + case Schedulable.ScedulableType.TwitchStream: + Console.WriteLine("it's a twitch stream"); + break; + case Schedulable.ScedulableType.YTRelease: + Console.WriteLine("it's a yt release"); + break; + } + //Task.WaitAll(Task.Delay((todo.Showtime - conf.preshowBufferTime) - DateTime.Now)); Console.WriteLine("time to prep!"); + switch (todo.ScedulableType) { case Schedulable.ScedulableType.TwitchStream: try { - // var handler = new TwitchStreamHandler(); - // handler.Handle(); + var handler = new TwitchStreamHandler(todo.Occurrence); + handler.Handle(); } catch(Exception e) { diff --git a/iCalHoopJumping.cs b/iCalHoopJumping.cs new file mode 100644 index 0000000..bd78ef3 --- /dev/null +++ b/iCalHoopJumping.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using Ical.Net; +using Ical.Net.CalendarComponents; +using Ical.Net.Serialization; + +namespace director +{ + public class iCalHoopJumping + { + private static DateTime searchStart = DateTime.Now; //is it slower to just call datetime.now every time? /shrug + private static DateTime searchEnd = DateTime.Now.AddDays(7); + public static readonly ConcurrentDictionary Calendars = new ConcurrentDictionary(); + public static void LoadCalendar(string key, string calendarString) + { + Calendars[key] = Calendar.Load(calendarString); + } + + //I don't understand why the entire .net ecosystem insists on ignoring ToString(). Is it really that much fun writing ThingSerializerFactory? ...java programmers. + private static EventSerializer ser = new EventSerializer(); + public static CalendarEvent parseEvent(string calendar, string eventStr) + { + //return new CalendarEvent(eventStr); //lol no + //return CalendarEvent.Parse(eventStr); //lol no + + var cal = Calendar.Load(Program.scratch.Calendars[calendar]); + foreach (var evt in cal.Events) + { + if (ser.SerializeToString(evt) == eventStr) + { + return evt; + } + foreach (var occ in evt.GetOccurrences(DateTime.Now - TimeSpan.FromDays(365), DateTime.Now + TimeSpan.FromDays(365))) + { + var newEvent = occ.Source as Ical.Net.CalendarComponents.CalendarEvent; + + if (ser.SerializeToString(newEvent) == eventStr) + { + return newEvent; + } + } + } + return null; + } + + internal static string Event_ToString(CalendarEvent asEvent) + { + //return asEvent.ToString(); //lol no + return ser.SerializeToString(asEvent); + } + public static IEnumerable getOccurrences(string calLabel) + { + var usefulList = new List(); + foreach(var icalOcc in Calendars[calLabel].GetOccurrences(searchStart, searchEnd)) + { + var asEvent = icalOcc.Source as Ical.Net.CalendarComponents.CalendarEvent; + var newCO = new CalendarOccurrence(); + newCO.CalendarSourceName = calLabel; + newCO._event = asEvent; + newCO.OccurrenceStart = icalOcc.Period.StartTime.AsSystemLocal; + newCO.OccurrenceEnd = icalOcc.Period.EndTime.AsSystemLocal; + usefulList.Add(newCO); + } + return usefulList; + } + public class CalendarOccurrence + { + internal string CalendarSourceName { get; set; } + internal CalendarEvent _event { get; set; } + public string Event + { + get + { + return iCalHoopJumping.Event_ToString(_event); + } + } + public DateTime OccurrenceStart { get; set; } + public DateTime OccurrenceEnd { get; set; } + } + } +} \ No newline at end of file diff --git a/schedulable/Schedulable.cs b/schedulable/Schedulable.cs index 5d3df58..bf87b3a 100644 --- a/schedulable/Schedulable.cs +++ b/schedulable/Schedulable.cs @@ -1,15 +1,11 @@ using System; -using Ical.Net.CalendarComponents; -using Ical.Net.DataTypes; +using director; namespace Schedulable { public class Schedulable { - //no, not calendar event. Circular reference, trips up jsonconvert. - public string Event { get; set; } - public DateTime OccurrenceStart { get; set; } - public DateTime OccurrenceEnd { get; set; } + public iCalHoopJumping.CalendarOccurrence Occurrence { get; set; } public DateTime Showtime { get; set; } public ScedulableType ScedulableType { get; set; } = ScedulableType.Other; } diff --git a/showHandlers/TwitchStreamHandler.cs b/showHandlers/TwitchStreamHandler.cs index 0f1a711..177078b 100644 --- a/showHandlers/TwitchStreamHandler.cs +++ b/showHandlers/TwitchStreamHandler.cs @@ -1,15 +1,16 @@ +using director; using Ical.Net.CalendarComponents; namespace ShowHandlers { public class TwitchStreamHandler { - public TwitchStreamHandler(CalendarEvent evt) + public TwitchStreamHandler(iCalHoopJumping.CalendarOccurrence evt) { Event = evt; } - public CalendarEvent Event { get; } + public iCalHoopJumping.CalendarOccurrence Event { get; } public void Handle() {