Skip to content

Commit

Permalink
Reuse HttpClient in .NET 6.0 version to enable keep-alive.
Browse files Browse the repository at this point in the history
Update Twitch client ID.
  • Loading branch information
jdpurcell committed Feb 26, 2022
1 parent 19cf6a6 commit cbc197f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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:
```
Expand Down
2 changes: 1 addition & 1 deletion RechatTool/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
</configuration>
2 changes: 1 addition & 1 deletion RechatTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 2 additions & 36 deletions RechatTool/Rechat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -72,35 +67,6 @@ public static void DownloadFile(long videoId, string path, bool overwrite = fals
}
}

#if NETFRAMEWORK
private static string DownloadUrlAsString(string url, Action<HttpWebRequest> 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<HttpRequestMessage> 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;
Expand Down
17 changes: 17 additions & 0 deletions RechatTool/RechatTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,21 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<PropertyGroup>
<PackageDir>$(TargetDir)Package\</PackageDir>
<PackageZip>$(TargetDir)Package.zip</PackageZip>
</PropertyGroup>
<ItemGroup>
<PackageContents Include="$(TargetDir)*.exe" />
<PackageContents Include="$(TargetDir)*.dll" />
<PackageContents Include="$(TargetDir)*.exe.config" />
<PackageContents Include="$(TargetDir)*.runtimeconfig.json" />
<PackageContents Include="$(SolutionDir)LICENSE.txt" />
</ItemGroup>
<RemoveDir Directories="$(PackageDir)" />
<Copy SourceFiles="@(PackageContents)" DestinationFolder="$(PackageDir)" />
<Delete Files="$(PackageZip)" />
<ZipDirectory SourceDirectory="$(PackageDir)" DestinationFile="$(PackageZip)" />
</Target>
</Project>
40 changes: 40 additions & 0 deletions RechatTool/TwitchApi5Client.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit cbc197f

Please sign in to comment.