コード例 #1
0
        public Task DownloadLiveStreamChatLogic(string channel, long vodId, CancellationToken cancellationToken)
        {
            using (var irc = new TcpClient("irc.chat.twitch.tv", 6667))
                using (var stream = irc.GetStream())
                    using (var reader = new StreamReader(stream))
                        using (var writer = new StreamWriter(stream)) {
                            writer.AutoFlush = true;
                            string accessToken;

                            using (var context = new MainDataContext()) {
                                accessToken = context.Authentications.First().accessToken;
                                //todo check for working auth on all external calls
                            }
                            var dbUserName = GlobalConfig.GetGlobalConfig("userName");

                            var userName = dbUserName ?? new UserDetails().SaveUserDataToDb();

                            writer.WriteLine($"PASS oauth:{accessToken}");
                            writer.WriteLine($"NICK {userName}");
                            writer.WriteLine($"JOIN #{channel}");
                            writer.WriteLine("CAP REQ :twitch.tv/tags");

                            string      inputLine;
                            int         databaseCounter = 0;
                            List <Chat> chats           = new List <Chat>();
                            while ((inputLine = reader.ReadLine()) != null)
                            {
                                if (cancellationToken.IsCancellationRequested)
                                {
                                    _logger.Warn("IRC shut down initiated, stream must have finished...");
                                    AddLiveStreamChatToDb(chats, vodId);
                                    StreamHelpers.SetChatDownloadToFinished(vodId, true);
                                    _logger.Info("Done!");
                                    break;
                                }

                                if (inputLine == "PING :tmi.twitch.tv")
                                {
                                    writer.WriteLine("PONG :tmi.twitch.tv");
                                }

                                if (inputLine.Contains("PRIVMSG"))
                                {
                                    Chat chat = MessageBuilder(inputLine);
                                    chat.streamId = vodId;
                                    chats.Add(chat);
                                    databaseCounter++;
                                }

                                if (databaseCounter == 50)
                                {
                                    AddLiveStreamChatToDb(chats, vodId);
                                    databaseCounter = 0;
                                    chats.Clear();
                                }
                            }

                            // todo check emote compatibility; does it send offline notification in irc??
                        }

            return(Task.CompletedTask);
        }
コード例 #2
0
        public Task DownloadChat(long streamId)
        {
            TwitchApiHelpers twitchApiHelpers = new TwitchApiHelpers();
            IRestResponse    response;

            try {
                response =
                    twitchApiHelpers.LegacyTwitchRequest($"https://api.twitch.tv/v5/videos/{streamId}/comments",
                                                         Method.GET);
            } catch (NetworkInformationException e) {
                _logger.Error(e);
                _logger.Error("Cleaning database, removing failed chat download from database.");
                RemoveStreamChatFromDb(streamId);
                return(Task.FromException(e));
            }

            ChatMessageJsonClass.ChatMessage deserializeResponse;
            try {
                deserializeResponse = JsonConvert.DeserializeObject <ChatMessageJsonClass.ChatMessage>(response.Content);
            } catch (JsonSerializationException e) {
                _logger.Error(e);
                _logger.Error("Cleaning database, removing failed chat download from database.");
                RemoveStreamChatFromDb(streamId);
                return(Task.FromException(e));
            }

            ChatMessageJsonClass.ChatMessage chatMessage = new ChatMessageJsonClass.ChatMessage();
            chatMessage.comments = new List <ChatMessageJsonClass.Comment>();
            var cursor          = "";
            int databaseCounter = 0;

            // clear out existing vod messages, should only activate when redownloading
            RemoveStreamChatFromDb(streamId);

            foreach (var comment in deserializeResponse.comments)
            {
                if (comment.message.user_badges != null)
                {
                    comment.message.userBadges = ReformatBadges(comment.message.user_badges);
                }

                if (comment.message.emoticons != null)
                {
                    comment.message.formattedEmoticons = ReformatEmoticons(comment.message.emoticons);
                }

                chatMessage.comments.Add(comment);
            }

            if (deserializeResponse._next != null)
            {
                cursor = deserializeResponse._next;
            }
            else
            {
                AddChatMessageToDb(chatMessage.comments, streamId);
            }

            while (cursor != null)
            {
                _logger.Info($"Getting more chat for {streamId}..");
                //if (token.IsCancellationRequested) {
                //await _hubContext.Clients.All.SendAsync("ReceiveMessage", CheckForDownloadingStreams());
                //return; // insta kill
                //}

                IRestResponse paginatedResponse;
                try {
                    paginatedResponse = twitchApiHelpers.LegacyTwitchRequest(
                        $"https://api.twitch.tv/v5/videos/{streamId}/comments" +
                        $"?cursor={deserializeResponse._next}", Method.GET);
                } catch (NetworkInformationException e) {
                    _logger.Error(e);
                    _logger.Error("Cleaning database, removing failed chat download from database.");
                    RemoveStreamChatFromDb(streamId);
                    return(Task.FromException(e));
                }

                try {
                    deserializeResponse = JsonConvert.DeserializeObject <ChatMessageJsonClass.ChatMessage>(paginatedResponse.Content);
                } catch (JsonSerializationException e) {
                    _logger.Error(e);
                    _logger.Error("Cleaning database, removing failed chat download from database.");
                    RemoveStreamChatFromDb(streamId);
                    return(Task.FromException(e));
                }

                foreach (var comment in deserializeResponse.comments)
                {
                    if (comment.message.user_badges != null)
                    {
                        comment.message.userBadges = ReformatBadges(comment.message.user_badges);
                    }

                    if (comment.message.emoticons != null)
                    {
                        comment.message.formattedEmoticons = ReformatEmoticons(comment.message.emoticons);
                    }

                    chatMessage.comments.Add(comment);
                }

                databaseCounter++;
                if (databaseCounter == 50 || deserializeResponse._next == null)
                {
                    // if we have collected 50 comments or there are no more chat messages
                    AddChatMessageToDb(chatMessage.comments, streamId);
                    databaseCounter = 0;
                    chatMessage.comments.Clear();
                }

                cursor = deserializeResponse._next;
            }

            StreamHelpers.SetChatDownloadToFinished(streamId, false);

            //await _hubContext.Clients.All.SendAsync("ReceiveMessage", CheckForDownloadingStreams());
            return(Task.CompletedTask);
        }