From 19cf6a679b1586cbb7f714df5688b0a7a7291bed Mon Sep 17 00:00:00 2001 From: "J.D. Purcell" Date: Sat, 26 Feb 2022 14:41:48 -0500 Subject: [PATCH] Add .NET 6.0 target. --- RechatTool/Rechat.cs | 47 +++++++++++++++++++++++++----------- RechatTool/RechatTool.csproj | 2 +- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/RechatTool/Rechat.cs b/RechatTool/Rechat.cs index f9994cb..e103134 100644 --- a/RechatTool/Rechat.cs +++ b/RechatTool/Rechat.cs @@ -8,11 +8,16 @@ using System.ComponentModel; using System.IO; using System.Linq; -using System.Net; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +#if NETFRAMEWORK +using System.Net; +#else +using System.Net.Http; +#endif + namespace RechatTool { public static class Rechat { public static void DownloadFile(long videoId, string path, bool overwrite = false, DownloadProgressCallback progressCallback = null) { @@ -26,7 +31,7 @@ public static void DownloadFile(long videoId, string path, bool overwrite = fals JObject lastComment = null; bool finishedDownload = false; try { - using var writer = new JsonTextWriter(new StreamWriter(path, false, new UTF8Encoding(true))); + using JsonTextWriter writer = new(new StreamWriter(path, false, new UTF8Encoding(true))); writer.WriteStartArray(); do { string url = nextCursor == null ? @@ -56,8 +61,8 @@ public static void DownloadFile(long videoId, string path, bool overwrite = fals } if (firstComment != null) { try { - var firstMessage = new RechatMessage(firstComment); - var lastMessage = new RechatMessage(lastComment); + RechatMessage firstMessage = new(firstComment); + RechatMessage lastMessage = new(lastComment); File.SetCreationTimeUtc(path, firstMessage.CreatedAt - firstMessage.ContentOffset); File.SetLastWriteTimeUtc(path, lastMessage.CreatedAt); } @@ -67,19 +72,34 @@ public static void DownloadFile(long videoId, string path, bool overwrite = fals } } +#if NETFRAMEWORK private static string DownloadUrlAsString(string url, Action withRequest = null) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); withRequest?.Invoke(request); - using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) - using (StreamReader responseStream = new StreamReader(response.GetResponseStream())) { - return responseStream.ReadToEnd(); - } + using HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + using StreamReader responseStream = new(response.GetResponseStream()); + return responseStream.ReadToEnd(); } private static void AddTwitchApiHeaders(HttpWebRequest request) { request.Accept = "application/vnd.twitchtv.v5+json"; request.Headers.Add("Client-ID", "jzkbprff40iqj646a697cyrvl0zt2m6"); } +#else + private static string DownloadUrlAsString(string url, Action withRequest = null) { + using HttpClient client = new(); + using HttpRequestMessage request = new(HttpMethod.Get, url); + withRequest?.Invoke(request); + using HttpResponseMessage response = client.Send(request); + using StreamReader responseStream = new(response.Content.ReadAsStream()); + return responseStream.ReadToEnd(); + } + + private static void AddTwitchApiHeaders(HttpRequestMessage request) { + request.Headers.Add("Accept", "application/vnd.twitchtv.v5+json"); + request.Headers.Add("Client-ID", "jzkbprff40iqj646a697cyrvl0zt2m6"); + } +#endif private static TimeSpan? TryGetContentOffset(JObject comment) { try { @@ -117,11 +137,10 @@ public static void ProcessFile(string pathIn, string pathOut = null, bool overwr } public static IEnumerable ParseMessages(string path) { - using (var reader = new JsonTextReader(File.OpenText(path))) { - while (reader.Read()) { - if (reader.TokenType != JsonToken.StartObject) continue; - yield return new RechatMessage(JObject.Load(reader)); - } + using JsonTextReader reader = new(File.OpenText(path)); + while (reader.Read()) { + if (reader.TokenType != JsonToken.StartObject) continue; + yield return new RechatMessage(JObject.Load(reader)); } } @@ -149,7 +168,7 @@ public RechatMessage(JObject sourceJson) { public DateTime CreatedAt => Comment.CreatedAt; - public TimeSpan ContentOffset => TimeSpan.FromSeconds(Comment.ContentOffsetSeconds); + public TimeSpan ContentOffset => new TimeSpan((long)Math.Round(Comment.ContentOffsetSeconds * 1000.0) * TimeSpan.TicksPerMillisecond); // User said something with "/me" public bool IsAction => Message.IsAction; diff --git a/RechatTool/RechatTool.csproj b/RechatTool/RechatTool.csproj index eba3c6d..aeedd6b 100644 --- a/RechatTool/RechatTool.csproj +++ b/RechatTool/RechatTool.csproj @@ -1,6 +1,6 @@  - net462 + net462;net6.0 Exe latest false