技術をかじる猫

適当に気になった技術や言語、思ったこと考えた事など。

Twitter タイムラインをストリームで引っこ抜く

前回同様にTwitterizer経由で突っ込んだ。
Stream なので、継続的に拾えるようだ。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twitterizer;
using Twitterizer.Streaming;

namespace TwitterTest
{
    public class TweetLine 
    {
        public String User { get; set; }
        public DateTime Time { get; set; }
        public String Message { get; set; }
    }

    public class TwitterEvent : EventArgs
    {
        public TweetLine Value;

        public TwitterEvent(TweetLine arg)
        {
            this.Value = arg;
        }
    }

    public class TwitterAccessor : IDisposable
    {
        const string CONSUMER_KEY = "key";
        const string CONSUMER_SECRET = "secret key";

        OAuthTokens tokens = null;
        TwitterStream stream = null;

        private string userAgent = null;

        public event EventHandler<TwitterEvent> MessageRecieved;

        public TwitterAccessor(string accessToken, string accessTokenRequest, string agentName)
        {
            this.tokens = new OAuthTokens();
            this.tokens.AccessToken = accessToken;
            this.tokens.AccessTokenSecret = accessTokenRequest;
            this.tokens.ConsumerKey = CONSUMER_KEY;
            this.tokens.ConsumerSecret = CONSUMER_SECRET;
            this.userAgent = agentName;
        }

        ~TwitterAccessor()
        {
            Dispose(false);
        }

        public void StartStream()
        {
            if (this.stream == null)
            {
                var created = new StatusCreatedCallback(StatusCallback);
                var opt = new StreamOptions();
                this.stream = new TwitterStream(this.tokens, this.userAgent, opt);
                this.stream.StartUserStream(null, null, created, null, null, null, null);
            }
        }

        public void StopStream()
        {
            if (this.stream != null)
            {
                this.stream.EndStream();
                this.stream.Dispose();
                this.stream = null;
            }
        }

        private bool disposed = false;

        private void StatusCallback(TwitterStatus status)
        {
            if (MessageRecieved != null)
            {
                var line = new TweetLine()
                {
                    User = status.User.Name,
                    Message = status.Text,
                    Time = status.CreatedDate
                };
                var eventArgs = new TwitterEvent(line);
                MessageRecieved.BeginInvoke(this, eventArgs, null, null); 
            }
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed 
                // and unmanaged resources.
                if (disposing)
                {
                    // Dispose managed resources.
                }

                // Dispose unumanagedb resources.
                this.StopStream();
            }
            disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}