Skip to content

Commit

Permalink
For users with localized display names, show regular username in pare…
Browse files Browse the repository at this point in the history
…ntheses.

Undocumented "-b" option for processing to show user badges.
  • Loading branch information
jdpurcell committed Nov 17, 2017
1 parent 5369f8d commit 1475b1d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
8 changes: 6 additions & 2 deletions RechatTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace RechatTool {
internal class Program {
public const string Version = "1.3.0.0";
public const string Version = "1.4.0.0";

private static int Main(string[] args) {
int iArg = 0;
Expand Down Expand Up @@ -52,17 +52,21 @@ void UpdateProgress(int downloaded) {
paths = Directory.GetFiles(Path.GetDirectoryName(paths[0]), Path.GetFileName(paths[0]));
}
bool overwrite = false;
bool showBadges = false;
while ((arg = GetArg(true)) != null) {
if (arg == "-o") {
overwrite = true;
}
else if (arg == "-b") {
showBadges = true;
}
else {
throw new InvalidArgumentException();
}
}
foreach (string p in paths) {
Console.WriteLine("Processing " + Path.GetFileName(p));
Rechat.ProcessFile(p, overwrite: overwrite);
Rechat.ProcessFile(p, overwrite: overwrite, showBadges: showBadges);
}
Console.WriteLine("Done!");
}
Expand Down
47 changes: 42 additions & 5 deletions RechatTool/Rechat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private static void AddTwitchApiHeaders(HttpWebRequest request) {
request.Headers.Add("Client-ID", "jzkbprff40iqj646a697cyrvl0zt2m6");
}

public static void ProcessFile(string pathIn, string pathOut = null, bool overwrite = false) {
public static void ProcessFile(string pathIn, string pathOut = null, bool overwrite = false, bool showBadges = false) {
if (pathOut == null) {
bool isAlreadyTxt = pathIn.EndsWith(".txt", StringComparison.OrdinalIgnoreCase);
pathOut = Path.Combine(
Expand All @@ -60,7 +60,7 @@ public static void ProcessFile(string pathIn, string pathOut = null, bool overwr
throw new Exception("Output file already exists.");
}
IEnumerable<string> lines = ParseMessages(pathIn)
.Select(ToReadableString)
.Select(n => ToReadableString(n, showBadges))
.Where(n => n != null);
File.WriteAllLines(pathOut, lines, new UTF8Encoding(true));
}
Expand All @@ -72,8 +72,10 @@ public static List<RechatMessage> ParseMessages(string path) {
.ToList();
}

private static string ToReadableString(RechatMessage m) {
return $"[{m.ContentOffset:hh\\:mm\\:ss\\.fff}] {m.UserName}{(m.IsAction ? "" : ":")} {m.MessageText}";
private static string ToReadableString(RechatMessage m, bool showBadges) {
string userBadges = $"{(m.UserIsBroadcaster ? "#" : "")}{(m.UserIsModerator ? "@" : "")}{(m.UserIsSubscriber ? "+" : "")}";
string userName = m.UserDisplayName.Equals(m.UserName, StringComparison.OrdinalIgnoreCase) ? m.UserDisplayName : $"{m.UserDisplayName} ({m.UserName})";
return $"[{m.ContentOffset:hh\\:mm\\:ss\\.fff}] {(showBadges ? userBadges : "")}{userName}{(m.IsAction ? "" : ":")} {m.MessageText}";
}

public class RechatMessage {
Expand All @@ -100,7 +102,19 @@ public RechatMessage(JObject sourceJson) {

public string MessageText => Message.Body;

public string UserName => Commenter.DisplayName.NullIfEmpty() ?? Commenter.Name;
public string UserName => Commenter.Name;

public string UserDisplayName => Commenter.DisplayName.TrimEnd(' ');

public bool UserIsBroadcaster => HasBadge("broadcaster");

public bool UserIsModerator => HasBadge("moderator");

public bool UserIsSubscriber => HasBadge("subscriber");

public IEnumerable<UserBadge> UserBadges => Message.UserBadges?.Select(n => n.ToUserBadge()) ?? Enumerable.Empty<UserBadge>();

private bool HasBadge(string id) => Message.UserBadges?.Any(n => n.Id.Equals(id, StringComparison.OrdinalIgnoreCase)) ?? false;

private class JsonComment {
[JsonProperty("created_at")]
Expand All @@ -127,6 +141,29 @@ private class JsonCommentMessage {
public string Body { get; set; }
[JsonProperty("is_action")]
public bool IsAction { get; set; }
[JsonProperty("user_badges")]
public JsonCommentUserBadge[] UserBadges { get; set; }
}

private class JsonCommentUserBadge {
[JsonProperty("_id")]
public string Id { get; set; }
[JsonProperty("version")]
public int Version { get; set; }

public UserBadge ToUserBadge() {
return new UserBadge {
Id = Id,
Version = Version
};
}
}

public class UserBadge {
internal UserBadge() { }

public string Id { get; internal set; }
public int Version { get; internal set; }
}
}
}
Expand Down

0 comments on commit 1475b1d

Please sign in to comment.