Exemplo n.º 1
0
        public bool updateStreamList( )
        {
            lock (streamListLock)
            {
                using (CookieAwareWebClient cwc = new CookieAwareWebClient())
                {
                    System.IO.Stream stream = cwc.downloadURL(String.Format(channelsUrl, (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds));
                    if (stream == null)
                    {
                        return(false);
                    }

                    channelList = ParseJson <Channels> .ReadObject(stream);

                    if (channelList == null)
                    {
                        return(false);
                    }

                    OnChannelList(new Sc2Event());
                }

                return(true);
            }
        }
Exemplo n.º 2
0
        public bool DownloadChat(bool reload)
        {
            lock ( chatLock )
            {
                chatWC.Cookies = loginWC.Cookies;

                if (reload)
                {
                    _lastStatus   = null;
                    chat.messages = null;
                }

                var url = String.Format(messagesUrl, ChannelId, TimeUtils.UnixTimestamp());
                System.IO.Stream stream = chatWC.downloadURL(url);

                _lastStatus = chatWC.LastWebError;
                if (_lastStatus == "ProtocolError")
                {
                    //Chat json isn't available at the moment
                    return(false);
                }

                if (stream == null)
                {
                    Debug.Print(String.Format("Sc2tv: stream is null"));
                    return(false);
                }

                var newchat = ParseJson <ChatMessages> .ReadObject(stream);

                if (newchat == null)
                {
                    return(false);
                }
                else if (newchat.messages.Count <= 0)
                {
                    return(false);
                }


                if (chat.messages == null)
                {
                    chat.messages = new List <ChatMessage>();
                    chat.messages = newchat.messages.Where(msg => msg.id < LastMessageId).ToList();
                }

                // Find new messages
                var newmessages = newchat.messages.Except(
                    chat.messages, new LambdaComparer <ChatMessage>((x, y) => x.id == y.id));

                chat = newchat;

                if (newmessages.Count() > 0)
                {
                    LastMessageId = newchat.MaxID();
                }

                // Put "to" nickname into separate property
                foreach (var m in newmessages)
                {
                    var re            = @"\[b\](.*)?\[/b\],";
                    var matchesToUser = Regex.Matches(m.message, re, RegexOptions.IgnoreCase | RegexOptions.Multiline);

                    if (matchesToUser.Count > 0)
                    {
                        if (matchesToUser[0].Groups.Count > 0)
                        {
                            m.to      = matchesToUser[0].Groups[1].Value;
                            m.message = Regex.Replace(m.message, re, "", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                        }
                    }
                    OnMessageReceived(new Sc2MessageEvent(m));
                }
                return(true);
            }
        }