2021-08-17 04:21:48 -04:00
using System ;
2021-08-19 08:44:38 -04:00
using System.Collections.Generic ;
2021-08-18 23:16:57 -04:00
using System.IO ;
using System.Linq ;
using System.Net.Http ;
using System.Net.Http.Headers ;
using System.Threading.Tasks ;
using franz ;
using Ical.Net ;
2021-08-19 08:44:38 -04:00
using Ical.Net.CalendarComponents ;
using Ical.Net.DataTypes ;
2021-08-20 00:26:00 -04:00
using Ical.Net.Serialization ;
2021-08-18 23:16:57 -04:00
using Newtonsoft.Json ;
2021-08-17 04:21:48 -04:00
namespace director
{
class Program
{
2021-08-18 23:16:57 -04:00
//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
2021-08-20 00:26:00 -04:00
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 ( ) ;
2021-08-17 04:21:48 -04:00
static void Main ( string [ ] args )
{
2021-08-18 23:16:57 -04:00
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);
2021-08-19 08:44:38 -04:00
2021-08-18 23:16:57 -04:00
scratch = Scratch . LoadScratch ( ) ;
2021-08-19 08:44:38 -04:00
2021-08-18 23:16:57 -04:00
while ( true )
{
Task . WaitAll (
//await and do next calendar task,
calendarCheck ( ) ,
Task . Delay ( calendarNaptime )
) ;
}
}
private static async Task calendarCheck ( )
{
try
{
2021-08-20 00:26:00 -04:00
lock ( scratch )
{
scratch . agenda . Clear ( ) ;
}
2021-08-18 23:16:57 -04:00
Task . WaitAll (
2021-08-20 00:26:00 -04:00
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 ;
} )
2021-08-18 23:16:57 -04:00
) ;
2021-08-20 00:26:00 -04:00
lock ( scratch )
{
scratch . Save ( ) ;
}
2021-08-18 23:16:57 -04:00
}
catch ( Exception e )
{
Console . Error . WriteLine ( e ) ;
}
2021-08-20 00:26:00 -04:00
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 ) ;
}
}
2021-08-18 23:16:57 -04:00
}
2021-08-19 08:44:38 -04:00
2021-08-20 00:26:00 -04:00
private delegate void schedulableCreate ( ref Schedulable . Schedulable creating ) ;
private static async Task checkCalendar ( string calendarUri , string calLabel , schedulableCreate createSchedulable )
2021-08-18 23:16:57 -04:00
{
2021-08-19 08:44:38 -04:00
//?export is a hack to allow me to access the calendar
2021-08-20 00:26:00 -04:00
//it likes to throw an error saying "this is the webDAV interface, use webDAV" at my webDAV client, stopping me from using webDAV.
2021-08-19 08:44:38 -04:00
var calString = await httpClient . GetStringAsync ( conf . webdav_uri + calendarUri + "?export" ) ;
2021-08-18 23:16:57 -04:00
var calT = Calendar . Load ( calString ) ;
2021-08-19 08:44:38 -04:00
var knownChecklist = new List < Schedulable . Schedulable > ( ) ;
lock ( scratch )
{
scratch . Calendars [ calLabel ] = calString ;
2021-08-20 00:26:00 -04:00
foreach ( var calOccurrence in calT . GetOccurrences ( searchStart , searchEnd ) )
2021-08-19 08:44:38 -04:00
{
var asEvent = calOccurrence . Source as Ical . Net . CalendarComponents . CalendarEvent ;
2021-08-20 00:26:00 -04:00
var newSchedulable = new Schedulable . Schedulable ( )
2021-08-19 08:44:38 -04:00
{
2021-08-20 00:26:00 -04:00
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 ) ;
2021-08-19 08:44:38 -04:00
}
2021-08-18 23:16:57 -04:00
}
}
2021-08-17 04:21:48 -04:00
}
}