120 lines
4.8 KiB
C#
120 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Threading.Tasks;
|
|
using franz;
|
|
using Ical.Net;
|
|
using Ical.Net.CalendarComponents;
|
|
using Ical.Net.DataTypes;
|
|
using Ical.Net.Serialization;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace director
|
|
{
|
|
class Program
|
|
{
|
|
//private static Telefranz tf;
|
|
public static Config conf;
|
|
private static TimeSpan calendarNaptime = TimeSpan.FromHours(1);
|
|
private 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();
|
|
static void Main(string[] args)
|
|
{
|
|
if (!File.Exists("appsettings.json"))
|
|
{
|
|
Console.Error.WriteLine("appsettings.json was not found!");
|
|
return;
|
|
}
|
|
conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText("appsettings.json"));
|
|
httpClient = new HttpClient();
|
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
|
|
System.Text.Encoding.ASCII.GetBytes($"{conf.webdav_username}:{conf.webdav_password}")));
|
|
//tf = new Telefranz("scheduler", conf.kafka_bootstrap);
|
|
|
|
scratch = Scratch.LoadScratch();
|
|
|
|
while (true)
|
|
{
|
|
Task.WaitAll(
|
|
//await and do next calendar task,
|
|
calendarCheck(),
|
|
Task.Delay(calendarNaptime)
|
|
);
|
|
}
|
|
}
|
|
private static async Task calendarCheck()
|
|
{
|
|
try
|
|
{
|
|
lock (scratch)
|
|
{
|
|
scratch.agenda.Clear();
|
|
}
|
|
Task.WaitAll(
|
|
checkCalendar(conf.calendar_twitch, "youtube", (ref Schedulable.Schedulable n) =>
|
|
{
|
|
n.ScedulableType = Schedulable.ScedulableType.YTRelease;
|
|
}),
|
|
checkCalendar(conf.calendar_twitch, "twitch", (ref Schedulable.Schedulable n) =>
|
|
{
|
|
n.ScedulableType = Schedulable.ScedulableType.TwitchStream;
|
|
})
|
|
);
|
|
lock (scratch)
|
|
{
|
|
scratch.Save();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.Error.WriteLine(e);
|
|
}
|
|
Console.WriteLine($"calendars pulled, scratch has {scratch.agenda.Count} schedulables.");
|
|
|
|
foreach (var s in scratch.agenda)
|
|
{
|
|
if (s.Showtime - DateTime.Now <= calendarNaptime)
|
|
{
|
|
Console.WriteLine("I'm an item on the agenda and showtime is near!");
|
|
Console.WriteLine(s.Event);
|
|
}
|
|
}
|
|
}
|
|
|
|
private delegate void schedulableCreate(ref Schedulable.Schedulable creating);
|
|
private static async Task checkCalendar(string calendarUri, string calLabel, schedulableCreate createSchedulable)
|
|
{
|
|
//?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))
|
|
{
|
|
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,
|
|
};
|
|
createSchedulable(ref newSchedulable);
|
|
scratch.agenda.Add(newSchedulable);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|