extracted some of the more insane iCal handling
This commit is contained in:
parent
c8a641f340
commit
4d5659f69c
46
Program.cs
46
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<Schedulable.Schedulable> workQueue = new ConcurrentQueue<Schedulable.Schedulable>();
|
||||
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<Schedulable.Schedulable>();
|
||||
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)
|
||||
{
|
||||
|
83
iCalHoopJumping.cs
Normal file
83
iCalHoopJumping.cs
Normal file
@ -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<string, Calendar> Calendars = new ConcurrentDictionary<string, Calendar>();
|
||||
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<CalendarOccurrence> getOccurrences(string calLabel)
|
||||
{
|
||||
var usefulList = new List<CalendarOccurrence>();
|
||||
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; }
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user