アットウィキロゴ

twAPI

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TwOAuth;
using System.Xml;
 
namespace twAPI
{
    class TwitterAPI
    {
        public OAuth oauth { get; private set; }
 
        private Dictionary<string, string> parameters;
 
        const string HOME_TIMELINE_URL = "http://twitter.com/statuses/home_timeline";
        const string FRIENDS_TIMELINE_URL = "http://twitter.com/statuses/friends_timeline";
        const string USER_TIMELINE_URL = "http://twitter.com/statuses/user_timeline";
        const string USER_FRIENDS_URL = "http://twitter.com/statuses/friends";
        const string USER_FOLLOWER_URL = "http://twitter.com/statuses/followers";
        const string UPDATE_URL = "http://twitter.com/statuses/update";
 
 
        public TwitterAPI(string consumerKey, string consumerSecret)
        {
            this.parameters = new Dictionary<string, string>();
            this.oauth = new OAuth(consumerKey, consumerSecret);
        }
 
        public TwitterAPI(string consumerKey, string consumerSecret, string accessToken, string accessTokenSecret, string userId, string screenName)
        {
            this.parameters = new Dictionary<string, string>();
            this.oauth = new OAuth( consumerKey, consumerSecret, accessToken, accessTokenSecret, userId, screenName);
        }
 
 
        /// <summary>
        /// 自分と自分のfriendの過去800件分のステータス(retweetを含む)から20件取得します。
        /// </summary>
        /// <returns></returns>
        public List<Status> GetHomeTimeline()
        {
            List<Status> result = new List<Status>();
            this.parameters.Clear();
            String res = this.oauth.Get(HOME_TIMELINE_URL + ".xml", this.parameters);
            //Console.WriteLine(res);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(res);
            XmlNodeList nodes = doc.SelectNodes("//statuses/status");
            foreach (XmlNode node in nodes)
            {
                result.Add(
                    new Status(
                        new Tweet(
                            node.ChildNodes.Item(Tweet.CREATE_AT).InnerText,
                            node.ChildNodes.Item(Tweet.TEXT).InnerText
                        ),
                        //new User(
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.NAME).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.SCREEN_NAME).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.LOCATION).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.DESCRIPTION).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.P_IMAGE_URL).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.FOLLOWERS_COUNT).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.FRIENDS_COUNT).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.STATUSES_COUNT).InnerText
                        //)
                        new User(node.ChildNodes.Item(Tweet.USER).ChildNodes)
                    )
                );
            }
            return (result);
        }
 
        /// <summary>
        /// 自分と自分のfriendの過去800件分のステータス(retweetを含む)からcount件取得します。
        /// (最大200件)
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Status> GetHomeTimeline(int count)
        {
            List<Status> result = new List<Status>();
            this.parameters.Clear();
            this.parameters.Add("count", count.ToString());
            String res = this.oauth.Get(HOME_TIMELINE_URL + ".xml", this.parameters);
            //Console.WriteLine(res);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(res);
            XmlNodeList nodes = doc.SelectNodes("//statuses/status");
            foreach (XmlNode node in nodes)
            {
                result.Add(
                    new Status(
                        new Tweet(
                            node.ChildNodes.Item(Tweet.CREATE_AT).InnerText,
                            node.ChildNodes.Item(Tweet.TEXT).InnerText
                        ),
                        //new User(
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.NAME).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.SCREEN_NAME).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.LOCATION).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.DESCRIPTION).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.P_IMAGE_URL).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.FOLLOWERS_COUNT).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.FRIENDS_COUNT).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.STATUSES_COUNT).InnerText
                        //)
                        new User(node.ChildNodes.Item(Tweet.USER).ChildNodes)
                    )
                );
            }
            return (result);
        }
 
        /// <summary>
        /// screenNameで指定したアカウントのステータスからcount件取得します。
        /// (最大200件)
        /// </summary>
        /// <param name="screenName"></param>
        /// <returns></returns>
        public List<Status> GetUserTimeline(string screenName, int count)
        {
            List<Status> result = new List<Status>();
            this.parameters.Clear();
            this.parameters.Add("count", count.ToString());
            String res = this.oauth.Get(USER_TIMELINE_URL + "/" + screenName + ".xml", this.parameters);
            //Console.WriteLine(res);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(res);
            XmlNodeList nodes = doc.SelectNodes("//statuses/status");
            foreach (XmlNode node in nodes)
            {
                result.Add(
                    new Status(
                        new Tweet(
                            node.ChildNodes.Item(Tweet.CREATE_AT).InnerText,
                            node.ChildNodes.Item(Tweet.TEXT).InnerText
                        ),
                        //new User(
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.NAME).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.SCREEN_NAME).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.LOCATION).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.DESCRIPTION).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.P_IMAGE_URL).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.FOLLOWERS_COUNT).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.FRIENDS_COUNT).InnerText,
                        //    node.ChildNodes.Item(Tweet.USER).ChildNodes.Item(User.STATUSES_COUNT).InnerText
                        //)
                        new User(node.ChildNodes.Item(Tweet.USER).ChildNodes)
                    )
                );
            }
            return (result);
        }
 
        /// <summary>
        /// 自分のfriend(自分がフォローしているアカウント)を100件取得します。
        /// 1ページ100件としてpageを指定します。
        /// </summary>
        /// <returns></returns>
        public List<User> GetFriends(int page)
        {
            List<User> result = new List<User>();
            this.parameters.Clear();
            this.parameters.Add("page", page.ToString());
            String res = this.oauth.Get(USER_FRIENDS_URL + ".xml", this.parameters);
            //Console.WriteLine(res);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(res);
            XmlNodeList nodes = doc.SelectNodes("//users/user");
            foreach (XmlNode node in nodes)
            {
                result.Add(
                    //new User(
                    //    node.ChildNodes.Item(User.NAME              ).InnerText,
                    //    node.ChildNodes.Item(User.SCREEN_NAME       ).InnerText,
                    //    node.ChildNodes.Item(User.LOCATION          ).InnerText,
                    //    node.ChildNodes.Item(User.DESCRIPTION       ).InnerText,
                    //    node.ChildNodes.Item(User.P_IMAGE_URL       ).InnerText,
                    //    node.ChildNodes.Item(User.FOLLOWERS_COUNT   ).InnerText,
                    //    node.ChildNodes.Item(User.FRIENDS_COUNT     ).InnerText,
                    //    node.ChildNodes.Item(User.STATUSES_COUNT    ).InnerText
                    //)
                    new User(node.ChildNodes)
                );
            }
            return (result);
        }
 
        /// <summary>
        /// 自分のfollower(自分のアカウントをフォローしているアカウント)を100件取得します。
        /// 1ページ100件としてpageを指定します。
        /// </summary>
        /// <returns></returns>
        public List<User> GetFollowers(int page)
        {
            List<User> result = new List<User>();
            this.parameters.Clear();
            this.parameters.Add("page", page.ToString());
            String res = this.oauth.Get(USER_FOLLOWER_URL + ".xml", this.parameters);
            //Console.WriteLine(res);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(res);
            XmlNodeList nodes = doc.SelectNodes("//users/user");
            foreach (XmlNode node in nodes)
            {
                result.Add(
                    //new User(
                    //    node.ChildNodes.Item(User.NAME              ).InnerText,
                    //    node.ChildNodes.Item(User.SCREEN_NAME       ).InnerText,
                    //    node.ChildNodes.Item(User.LOCATION          ).InnerText,
                    //    node.ChildNodes.Item(User.DESCRIPTION       ).InnerText,
                    //    node.ChildNodes.Item(User.P_IMAGE_URL       ).InnerText,
                    //    node.ChildNodes.Item(User.FOLLOWERS_COUNT   ).InnerText,
                    //    node.ChildNodes.Item(User.FRIENDS_COUNT     ).InnerText,
                    //    node.ChildNodes.Item(User.STATUSES_COUNT    ).InnerText
                    //)
                    new User(node.ChildNodes)
                );
            }
            return (result);
        }
 
        /// <summary>
        /// status(140字以内)を投稿します。
        /// </summary>
        /// <param name="status"></param>
        public void PostStatus(string status)
        {
            if((status.Length >= 140)&&(string.IsNullOrEmpty(status)))
            {
                return;
            }
            this.parameters.Clear();
            this.parameters.Add("status", this.oauth.UrlEncode(status));
            this.oauth.Post(UPDATE_URL + ".xml", this.parameters);
        }
 
    }
 
    /// <summary>
    /// ツイートを表現します。
    /// </summary>
    class Tweet
    {
        public const int CREATE_AT = 0;
        public const int TEXT = 2;
        public const int USER = 11;
 
        public string createdAt { get; set; }
        public string text { get; set; }
 
        public string tweetString{get{return("[" + this.createdAt + "]" + this.text);}}
 
        public Tweet(string createAt, string text)
        {
            this.createdAt = createAt;
            this.text = text;
        }
 
 
    }
 
    /// <summary>
    /// ユーザの情報を表現します。
    /// </summary>
    class User
    {
        public const int NAME = 1;
        public const int SCREEN_NAME = 2;
        public const int LOCATION = 3;
        public const int DESCRIPTION = 4;
        public const int P_IMAGE_URL = 5;
        public const int FOLLOWERS_COUNT = 8;
        public const int FRIENDS_COUNT = 14;
        public const int FAVOURITES_COUNT = 16;
        public const int STATUSES_COUNT = 26;
        public const int LISTED_COUNT = 30;
 
        public string name { get; set; }
        public string screenName { get; set; }
        public string location { get; set; }
        public string description { get; set; }
        public string profileImageUrl { get; set; }
        public string followersCount { get; set; }
        public string friendsCount { get; set; }
        public string favouritesCount { get; set; }
        public string statusesCount { get; set; }
        public string listedCount { get; set; }
 
        public string userString{ 
            get{ 
                return(this.name + "/" + this.screenName 
                    + "(" + this.friendsCount + ","
                    + this.followersCount + ","
                    + this.profileImageUrl + ","
                    + this.statusesCount+ ")");
            }
        }
 
        public int followersCountInt { 
            get { return (int.Parse(this.followersCount)); } 
            set { this.followersCount = value.ToString(); } 
        }
 
        public int friendsCountInt
        {
            get { return (int.Parse(this.friendsCount)); }
            set { this.friendsCount = value.ToString(); }
        }
 
        public int favouritesCountInt
        {
            get { return (int.Parse(this.favouritesCount)); }
            set { this.favouritesCount = value.ToString(); }
        }
 
        public int statusesCountInt
        {
            get { return (int.Parse(this.statusesCount)); }
            set { this.statusesCount = value.ToString(); }
        }
 
        public int listedCountInt
        {
            get { return (int.Parse(this.listedCount)); }
            set { this.listedCount = value.ToString(); }
        }
 
        public User(string name, string screenName, string location, string description,
            string profileImageUrl, string followersCount, string  friendsCount,
            string favouritesCount, string statusesCount, string listedCount)
        {
            this.name = name;
            this.screenName = screenName;
            this.location = location;
            this.description = description;
            this.profileImageUrl = profileImageUrl;
            this.followersCount = followersCount;
            this.friendsCount = friendsCount;
            this.favouritesCount = favouritesCount;
            this.statusesCount = statusesCount;
            this.listedCount = listedCount;
        }
 
        public User(XmlNodeList node)
        {
            this.name               = node.Item(User.NAME              ).InnerText;
            this.screenName         = node.Item(User.SCREEN_NAME       ).InnerText;
            this.location           = node.Item(User.LOCATION          ).InnerText;
            this.description        = node.Item(User.DESCRIPTION       ).InnerText;
            this.profileImageUrl    = node.Item(User.P_IMAGE_URL       ).InnerText;
            this.followersCount     = node.Item(User.FOLLOWERS_COUNT   ).InnerText;
            this.friendsCount       = node.Item(User.FRIENDS_COUNT     ).InnerText;
            this.favouritesCount    = node.Item(User.FAVOURITES_COUNT  ).InnerText;
            this.statusesCount      = node.Item(User.STATUSES_COUNT    ).InnerText;
            this.listedCount        = node.Item(User.LISTED_COUNT      ).InnerText;
        }
    }
 
    /// <summary>
    /// ステータスを表現します。
    /// </summary>
    class Status
    {
        public Tweet tweet;
        public User user;
 
        public string createdAt { 
            get { return (this.tweet.createdAt); } 
            set { this.tweet.createdAt = value; } 
        }
 
        public string text { 
            get { return (this.tweet.text); } 
            set { this.tweet.text = value; } 
        }
 
 
        public string name { 
            get { return (this.user.name); } 
            set { this.user.name = value; } 
        }
 
        public string screenName { 
            get { return (this.user.screenName); } 
            set { this.user.screenName = value; } 
        }
 
        public string location { 
            get { return (this.user.location); } 
            set { this.user.location = value; } 
        }
 
 
        public string statusString{
            get{return(this.tweet.tweetString + "(" + this.user.userString +")");}
        }
 
        public Status(Tweet status, User user)
        {
            this.tweet = status;
            this.user = user;
        }
    }
}
 

タグ:

+ タグ編集
  • タグ:
最終更新:2011年01月17日 17:44
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。