diff --git a/README.md b/README.md index f7cab8e..2e5bb8d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # RechatTool -Command line tool to download the chat log from a Twitch VOD. Saves the full JSON data and optionally processes it to produce a simple text file. Requires .NET Framework 4.6.2+. +Command line tool to download the chat log from a Twitch VOD. Saves the full JSON data and optionally processes it to produce a simple text file. Requires .NET Framework 4.6.2+ (releases labeled Windows), or .NET 6.0 (releases labeled CrossPlatform). Sample usage: ``` diff --git a/RechatTool/App.config b/RechatTool/App.config index deaa9e2..e89424b 100644 --- a/RechatTool/App.config +++ b/RechatTool/App.config @@ -1,6 +1,6 @@ - + diff --git a/RechatTool/Program.cs b/RechatTool/Program.cs index 173da68..535bb91 100644 --- a/RechatTool/Program.cs +++ b/RechatTool/Program.cs @@ -11,7 +11,7 @@ namespace RechatTool { internal class Program { - public const string Version = "1.5.0.7"; + public const string Version = "1.6.0.0"; private static int Main(string[] args) { int iArg = 0; diff --git a/RechatTool/Rechat.cs b/RechatTool/Rechat.cs index e103134..a31556f 100644 --- a/RechatTool/Rechat.cs +++ b/RechatTool/Rechat.cs @@ -12,12 +12,6 @@ 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) { @@ -31,13 +25,14 @@ public static void DownloadFile(long videoId, string path, bool overwrite = fals JObject lastComment = null; bool finishedDownload = false; try { + using TwitchApi5Client apiClient = new(); using JsonTextWriter writer = new(new StreamWriter(path, false, new UTF8Encoding(true))); writer.WriteStartArray(); do { string url = nextCursor == null ? $"{baseUrl}?content_offset_seconds=0" : $"{baseUrl}?cursor={nextCursor}"; - JObject response = JObject.Parse(DownloadUrlAsString(url, withRequest: AddTwitchApiHeaders)); + JObject response = JObject.Parse(apiClient.Request(url)); foreach (JObject comment in (JArray)response["comments"]) { comment.WriteTo(writer); firstComment ??= comment; @@ -72,35 +67,6 @@ 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(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 { return comment == null ? null : new RechatMessage(comment).ContentOffset; diff --git a/RechatTool/RechatTool.csproj b/RechatTool/RechatTool.csproj index aeedd6b..be22f2f 100644 --- a/RechatTool/RechatTool.csproj +++ b/RechatTool/RechatTool.csproj @@ -12,4 +12,21 @@ + + + $(TargetDir)Package\ + $(TargetDir)Package.zip + + + + + + + + + + + + + \ No newline at end of file diff --git a/RechatTool/TwitchApi5Client.cs b/RechatTool/TwitchApi5Client.cs new file mode 100644 index 0000000..78133d3 --- /dev/null +++ b/RechatTool/TwitchApi5Client.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; + +#if NETFRAMEWORK +using System.Net; +#else +using System.Net.Http; +#endif + +namespace RechatTool { + internal class TwitchApi5Client : IDisposable { +#if !NETFRAMEWORK + private readonly HttpClient _httpClient = new(); +#endif + + public void Dispose() { +#if !NETFRAMEWORK + _httpClient.Dispose(); +#endif + } + + public string Request(string url) { +#if NETFRAMEWORK + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Accept = "application/vnd.twitchtv.v5+json"; + request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko"); + using HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + using StreamReader responseStream = new(response.GetResponseStream()); + return responseStream.ReadToEnd(); +#else + using HttpRequestMessage request = new(HttpMethod.Get, url); + request.Headers.Add("Accept", "application/vnd.twitchtv.v5+json"); + request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko"); + using HttpResponseMessage response = _httpClient.Send(request); + using StreamReader responseStream = new(response.Content.ReadAsStream()); + return responseStream.ReadToEnd(); +#endif + } + } +}