Skip to content

Commit

Permalink
Add .NET 6.0 target.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpurcell committed Feb 26, 2022
1 parent 82798cb commit 19cf6a6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
47 changes: 33 additions & 14 deletions RechatTool/Rechat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 ?
Expand Down Expand Up @@ -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);
}
Expand All @@ -67,19 +72,34 @@ 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 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<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 {
Expand Down Expand Up @@ -117,11 +137,10 @@ public static void ProcessFile(string pathIn, string pathOut = null, bool overwr
}

public static IEnumerable<RechatMessage> 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));
}
}

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion RechatTool/RechatTool.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<LangVersion>latest</LangVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
Expand Down

0 comments on commit 19cf6a6

Please sign in to comment.