From 783b1a65a1b3ca0602ab2e76db39f5c302c88b75 Mon Sep 17 00:00:00 2001 From: "Adam R. Grey" Date: Wed, 18 Aug 2021 23:16:57 -0400 Subject: [PATCH] begin absorbing dave sees events --- .gitignore | 9 +-- Config.cs | 13 ++++ Program.cs | 96 +++++++++++++++++++++++++++++- Scratch.cs | 36 +++++++++++ Show.cs | 6 ++ appsettings.example.json | 9 +++ director.csproj | 7 +++ schedulable/MessageRelay.cs | 10 ++++ schedulable/Schedulable.cs | 8 +++ schedulable/show/Show.cs | 32 ++++++++++ schedulable/show/TwitchStream.cs | 7 +++ schedulable/show/YoutubeRelease.cs | 7 +++ scratch.json | 4 ++ 13 files changed, 239 insertions(+), 5 deletions(-) create mode 100644 Config.cs create mode 100644 Scratch.cs create mode 100644 Show.cs create mode 100644 appsettings.example.json create mode 100644 schedulable/MessageRelay.cs create mode 100644 schedulable/Schedulable.cs create mode 100644 schedulable/show/Show.cs create mode 100644 schedulable/show/TwitchStream.cs create mode 100644 schedulable/show/YoutubeRelease.cs create mode 100644 scratch.json diff --git a/.gitignore b/.gitignore index 8bf1b90..361703c 100644 --- a/.gitignore +++ b/.gitignore @@ -60,8 +60,8 @@ BenchmarkDotNet.Artifacts/ project.lock.json project.fragment.lock.json artifacts/ - -# Tye + +# Tye .tye/ # StyleCop @@ -304,8 +304,8 @@ paket-files/ # FAKE - F# Make .fake/ - -# Ionide - VsCode extension for F# Support + +# Ionide - VsCode extension for F# Support .ionide/ # CodeRush personal settings @@ -446,3 +446,4 @@ $RECYCLE.BIN/ !.vscode/tasks.json !.vscode/launch.json !.vscode/extensions.json +appsettings.json diff --git a/Config.cs b/Config.cs new file mode 100644 index 0000000..f7f4fdf --- /dev/null +++ b/Config.cs @@ -0,0 +1,13 @@ +namespace director +{ + public class Config + { + public string kafka_bootstrap { get; set; } + public string call_for_humans_discord_webhook { get; set; } + public string webdav_uri { get; set; } + public string webdav_username { get; set; } + public string webdav_password { get; set; } + public string calendar_youtube { get; set; } + public string calendar_twitch { get; set; } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 57453a6..9ec2c35 100644 --- a/Program.cs +++ b/Program.cs @@ -1,12 +1,106 @@ using System; +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 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); static void Main(string[] args) { - Console.WriteLine("Hello World!"); + if (!File.Exists("appsettings.json")) + { + Console.Error.WriteLine("appsettings.json was not found!"); + return; + } + conf = JsonConvert.DeserializeObject(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); + //todo: subscribe to event appears message - you don't need to respond to an event disappearing message + + //todo: talk to dave. What's coming up in the next short timeframe? + + scratch = Scratch.LoadScratch(); + + while (true) + { + Task.WaitAll( + //await and do next calendar task, + calendarCheck(), + Task.Delay(calendarNaptime) + ); + } + } + private static async Task calendarCheck() + { + //pull calendar ics's + try + { + Task.WaitAll( + checkYoutube(), + checkTwitch() + ); + } + catch (Exception e) + { + Console.Error.WriteLine("sure would be nice if it threw an error"); + Console.Error.WriteLine(e); + } + Console.WriteLine("k."); + lock (scratch) + { + //verify against existing copy, react to changes + //put in scratch + } + } + private static async Task checkTwitch() + { + var calString = await httpClient.GetStringAsync(conf.webdav_uri + conf.calendar_twitch + "?export"); + lock(scratch) + { + scratch.Calendars["twitch"] = calString; + } + var calT = Calendar.Load(calString); + Console.WriteLine($"calendar name: {calT.Name}"); + foreach(var evt in calT.Events){ + var upcoming = evt.GetOccurrences(searchStart, searchEnd)?.FirstOrDefault(); + if(upcoming != null){ + Console.WriteLine($"[twitch] event {evt.Summary} has an occurence: {upcoming.Period.StartTime}"); + } + + } + } + private static async Task checkYoutube() + { + var calString = await httpClient.GetStringAsync(conf.webdav_uri + conf.calendar_youtube + "?export"); + lock(scratch) + { + scratch.Calendars["youtube"] = calString; + } + var calYT = Calendar.Load(calString); + Console.WriteLine($"calendar name: {calYT.Name}"); + foreach(var evt in calYT.Events){ + var upcoming = evt.GetOccurrences(searchStart, searchEnd)?.FirstOrDefault(); + if(upcoming != null){ + Console.WriteLine($"[youtube] event {evt.Summary} has an occurence: {upcoming.Period.StartTime}"); + } + + } } } } diff --git a/Scratch.cs b/Scratch.cs new file mode 100644 index 0000000..c553c2e --- /dev/null +++ b/Scratch.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Newtonsoft.Json; +using Schedulable; + +namespace director +{ + public class Scratch + { + private const string path = "scratch.json"; + public List agenda { get; set; } = new List(); + public Dictionary Calendars { get; set; } = new Dictionary(); + + //calendar ICSs + + public static Scratch LoadScratch() + { + if (File.Exists(path)) + { + return JsonConvert.DeserializeObject(File.ReadAllText(path)); + } + else + { + var toReturn = new Scratch(); + toReturn.Save(); + return toReturn; + } + } + + public void Save() + { + File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented)); + } + } +} diff --git a/Show.cs b/Show.cs new file mode 100644 index 0000000..b66fbce --- /dev/null +++ b/Show.cs @@ -0,0 +1,6 @@ +namespace director +{ + public class Show + { + } +} \ No newline at end of file diff --git a/appsettings.example.json b/appsettings.example.json new file mode 100644 index 0000000..17c62ef --- /dev/null +++ b/appsettings.example.json @@ -0,0 +1,9 @@ +{ + "kafka_bootstrap": "localhost:9092", + "call_for_humans_discord_webhook": "http://localhost", + "webdav_uri": "http://mywebdav", + "webdav_username": "xX_sephiroth_Xx@aol.com", + "webdav_password": "sw0rdf1sh", + "calendar_youtube": "calendars/1wingedangle/youtube-channel_shared_by_adam", + "calendar_twitch": "calendars/1wingedangle/twitch-channel_shared_by_adam" +} \ No newline at end of file diff --git a/director.csproj b/director.csproj index 2082704..7a9fb58 100644 --- a/director.csproj +++ b/director.csproj @@ -3,6 +3,13 @@ Exe net5.0 + $(RestoreSources);../packages/nuget/;https://api.nuget.org/v3/index.json + + + + + + diff --git a/schedulable/MessageRelay.cs b/schedulable/MessageRelay.cs new file mode 100644 index 0000000..2003a75 --- /dev/null +++ b/schedulable/MessageRelay.cs @@ -0,0 +1,10 @@ +using System; + +namespace Schedulable +{ + //You said to echo back a message + public class MessageRelay : Schedulable + { + public silver_messages.message MessageToRelay { get; set; } + } +} \ No newline at end of file diff --git a/schedulable/Schedulable.cs b/schedulable/Schedulable.cs new file mode 100644 index 0000000..d8e3360 --- /dev/null +++ b/schedulable/Schedulable.cs @@ -0,0 +1,8 @@ +using System; +namespace Schedulable +{ + public abstract class Schedulable + { + public DateTime Time { get; set; } + } +} \ No newline at end of file diff --git a/schedulable/show/Show.cs b/schedulable/show/Show.cs new file mode 100644 index 0000000..2c4b504 --- /dev/null +++ b/schedulable/show/Show.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; + +namespace Schedulable.Show +{ + public class Show + { + public string Name { get; set; } + public Preparation Preperation { get; set; } + public IEnumerable Procedure { get; set; } + public TaskList Postshow { get; set; } + } + public class Phase + { + public string Name { get; set; } + } + public class TaskList + { + public IEnumerable Manual { get; set; } + public IEnumerable Commands { get; set; } + } + public class Preparation : TaskList + { + public IEnumerable AgentsNeeded { get; set; } + public IEnumerable Checks { get; set; } + } + public class Checklistable + { + //for humans + public string Label { get; set; } + public IEnumerable Items { get; set; } //if no items, just throw up a checkbox + } +} \ No newline at end of file diff --git a/schedulable/show/TwitchStream.cs b/schedulable/show/TwitchStream.cs new file mode 100644 index 0000000..053f76f --- /dev/null +++ b/schedulable/show/TwitchStream.cs @@ -0,0 +1,7 @@ +namespace Schedulable.Show +{ + public class TwitchStream : Show + { + public string TwitchCategory { get; set; } + } +} \ No newline at end of file diff --git a/schedulable/show/YoutubeRelease.cs b/schedulable/show/YoutubeRelease.cs new file mode 100644 index 0000000..f74337b --- /dev/null +++ b/schedulable/show/YoutubeRelease.cs @@ -0,0 +1,7 @@ +namespace Schedulable.Show +{ + public class YoutubeRelease : Show + { + + } +} \ No newline at end of file diff --git a/scratch.json b/scratch.json new file mode 100644 index 0000000..df30c15 --- /dev/null +++ b/scratch.json @@ -0,0 +1,4 @@ +{ + "agenda": [], + "Calendars": {} +} \ No newline at end of file