예제 #1
0
        private static string ToReadableString(RechatMessage m, bool showBadges)
        {
            string userBadges = $"{(m.UserIsAdmin || m.UserIsStaff ? "*" : "")}{(m.UserIsBroadcaster ? "#" : "")}{(m.UserIsModerator || m.UserIsGlobalModerator ? "@" : "")}{(m.UserIsSubscriber ? "+" : "")}";
            string userName   = m.UserDisplayName.Equals(m.UserName, StringComparison.OrdinalIgnoreCase) ? m.UserDisplayName : $"{m.UserDisplayName} ({m.UserName})";

            return($"[{TimestampToString(m.ContentOffset, true)}] {(showBadges ? userBadges : "")}{userName}{(m.IsAction ? "" : ":")} {m.MessageText}");
        }
예제 #2
0
        public static void DownloadFile(long videoId, string path, bool overwrite = false, DownloadProgressCallback progressCallback = null)
        {
            if (File.Exists(path) && !overwrite)
            {
                throw new Exception("Output file already exists.");
            }
            string  baseUrl          = $"{"https"}://api.twitch.tv/v5/videos/{videoId}/comments";
            string  nextCursor       = null;
            int     segmentCount     = 0;
            JObject firstComment     = null;
            JObject lastComment      = null;
            bool    finishedDownload = false;

            try {
                using var writer = new JsonTextWriter(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));
                    foreach (JObject comment in (JArray)response["comments"])
                    {
                        comment.WriteTo(writer);
                        firstComment ??= comment;
                        lastComment = comment;
                    }
                    nextCursor = (string)response["_next"];
                    segmentCount++;
                    progressCallback?.Invoke(segmentCount, TryGetContentOffset(lastComment));
                }while (nextCursor != null);
                writer.WriteEndArray();
                finishedDownload = true;
            }
            finally {
                if (!finishedDownload)
                {
                    try {
                        File.Delete(path);
                    }
                    catch { }
                }
            }
            if (firstComment != null)
            {
                try {
                    var firstMessage = new RechatMessage(firstComment);
                    var lastMessage  = new RechatMessage(lastComment);
                    File.SetCreationTimeUtc(path, firstMessage.CreatedAt - firstMessage.ContentOffset);
                    File.SetLastWriteTimeUtc(path, lastMessage.CreatedAt);
                }
                catch (Exception ex) {
                    throw new WarningException("Unable to set file created/modified time.", ex);
                }
            }
        }
예제 #3
0
        public void DownloadFile(CancellationToken token, long videoId, string path, TimeSpan?beginTime, TimeSpan?endTime)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            string   baseUrl      = $"{"https"}://api.twitch.tv/v5/videos/{videoId}/comments";
            string   nextCursor   = null;
            int      segmentCount = 0;
            JObject  firstComment = null;
            JObject  lastComment  = null;
            TimeSpan?totalTime    = endTime - beginTime;

            using (var writer = new JsonTextWriter(new StreamWriter(path, false, new UTF8Encoding(true)))) {
                writer.WriteStartArray();
                int progressPerc = 0;
                do
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    TimeSpan?lastCommentTimespan = TryGetContentOffset(lastComment);

                    bool lastCommentWithinTimeFrame = lastCommentTimespan > beginTime && lastCommentTimespan < endTime;

                    if (lastCommentTimespan.HasValue)
                    {
                        if (endTime.HasValue)
                        {
                            progressPerc = Math.Min((int)((lastCommentTimespan.Value.TotalSeconds / endTime.Value.TotalSeconds) * 100), 100);
                            if (lastCommentTimespan != null)
                            {
                                BroadcastNewProgressDownloadingChatLogEvent("Downloading Chat Log..." +
                                                                            "\nCurrent time: " + (lastCommentTimespan) +
                                                                            "\nProgress: " + progressPerc + "%",
                                                                            progressPerc, 0);
                            }
                        }
                        else
                        {
                            progressPerc = ((int)lastCommentTimespan.Value.TotalSeconds % 25) * 4;
                            if (lastCommentTimespan != null)
                            {
                                BroadcastNewProgressDownloadingChatLogEvent("Downloading Chat Log..." +
                                                                            "\nCurrent time: " + (lastCommentTimespan),
                                                                            progressPerc, 0);
                            }
                        }
                    }



                    if (beginTime != null && endTime != null && lastCommentTimespan > endTime)
                    {
                        break;
                    }

                    string url = nextCursor == null ?
                                 $"{baseUrl}?content_offset_seconds=0" :
                                 $"{baseUrl}?cursor={nextCursor}";

                    JObject response = JObject.Parse(DownloadUrlAsString(url, withRequest: AddTwitchApiHeaders));

                    foreach (JObject comment in (JArray)response["comments"])
                    {
                        if ((beginTime == null || endTime == null) || lastCommentWithinTimeFrame)
                        {
                            comment.WriteTo(writer);
                        }
                        firstComment = firstComment ?? comment;
                        lastComment  = comment;
                    }

                    nextCursor = (string)response["_next"];
                    segmentCount++;
                    //progressCallback?.Invoke(segmentCount, lastCommentTimespan);
                }while (nextCursor != null);

                writer.WriteEndArray();
            }
            if (firstComment != null)
            {
                try {
                    var firstMessage = new RechatMessage(firstComment);
                    var lastMessage  = new RechatMessage(lastComment);
                    File.SetCreationTimeUtc(path, firstMessage.CreatedAt - firstMessage.ContentOffset);
                    File.SetLastWriteTimeUtc(path, lastMessage.CreatedAt);
                }
                catch (Exception ex) {
                    throw new WarningException("Unable to set file created/modified time.", ex);
                }
            }
        }