2013/05/04

【C#でTwitter】ユーザタイムラインを取得する(API v1.0)

 C#でユーザタイムラインを取得するプログラムです。
OAuth認証の方法は、【C#でTwitter】OAuth認証を行う を参考にしてください。

 今回は認証不要で取得できるユーザータイムラインについてです。
ホームタイムラインは次回以降にまとめようと思います。


※追記:2013/06/15
  API1.1でユーザタイムラインを取得する方法をまとめました。




タイムラインの種類



パブリックタイムライン


   全ユーザの公開しているツイート、リツイートを最新20件を取得します。
    一時期パブリックタイムラインが廃止されるとかされないとか話題になっていたのですが、
   API v1.1では使えるんですかね? 


ユーザータイムライン


   指定したユーザーのツイートを取得します。
   API v1.1からは認証情報を送らないと取得できなくなりました。




ユーザータイムライン(GET statuses/user_timeline)について



URL:http://api.twitter.com/1/statuses/user_timeline/id.format


パラメータ説明
user_id取得したいユーザIDを指定
例:https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=1234
screen_name取得したいスクリーンネーム(アカウント名)を指定 
例:https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=bc_rikko
since_id指定したステータスIDより後の最新ツイートを取得 
例:https://api.twitter.com/1.1/statuses/user_timeline.json?since_id=1234567890
count取得件数を指定(デフォルトは20件) 
例:https://api.twitter.com/1.1/statuses/user_timeline.json?count=100
max_id指定したステータスIDより過去のツイートを取得 
例:https://api.twitter.com/1.1/statuses/user_timeline.json?max_id=1234567890
trim_userユーザ情報の詳細でなくIDのみを取得(true、t、1のいずれか指定) 
例:https://api.twitter.com/1.1/statuses/user_timeline.json?trim_user=1
exclude_repliesリプライを取得しない 
例:https://api.twitter.com/1.1/statuses/user_timeline.json?exclude_replies=true
contributor_details貢献者?のスクリーンネームが表示される 
例:https://api.twitter.com/1.1/statuses/user_timeline.json?contributor_details=true
include_rts リツイートを含めたタイムラインを取得 
例:https://api.twitter.com/1.1/statuses/user_timeline.json?include_rts =true



C#でタイムラインを取得(※API v1.0使用)



 このプログラムを作っているときは、API v1.1で認証が必要になったり、
取得の形式がJSONのみ ということを知りませんでした。

API v1.0を使用し、しかもXML形式で取得しているため、
v1.1では何一つ使うことができませんが、せっかく作ったので実装方法を残します。

public partial class Form1 : Form
{
    // Timeline取得用URL
    static readonly string TIMELINE_URL = "https://api.twitter.com/1/statuses/user_timeline/{0}.xml?count=50"; 

    // OAuth認証時に取得したScreenNameとUserId
    static readonly string SCREEN_NAME = "bc_rikko";
    static readonly string USER_ID = "77967886";

    public Form1()
    {
        InitializeComponent();
    }

    private void btnReload_Click(object sender, EventArgs e)
    {
        try
        {
            int tweetCount = 0;

            // Timeline取得用のURLを生成
            string timelineUrl = string.Format(TIMELINE_URL, SCREEN_NAME);

            // Httpでサーバにアクセスし、インターネットリソースを格納
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(timelineUrl);
            WebResponse response = request.GetResponse();

            // インターネットリソースからデータを読み取る
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
            string result = reader.ReadToEnd();

            // XML形式で読み込む
            XmlDocument xml = new XmlDocument();
            // 空白・改行を保持する
            xml.PreserveWhitespace = true;
            xml.LoadXml(result);

            // xmlの改行コードがCR(\n)のみで、TextBox上で改行されないためCRLF(\r\n)に置換
            textTimelineXML.Text = xml.OuterXml.Replace("\n", "\r\n");

            // XPathで指定した条件に一致するノードを取得
            XmlNodeList nodes = xml.SelectNodes("//statuses/status/text");

            // ノードを読み込み、リストボックスに格納
            foreach (XmlNode node in nodes)
            {
                listTimeline.Items.Add(node.InnerText);
                tweetCount++;
            }

            textMessage.Text = string.Format("{0}件取得しました。", tweetCount);

            stream.Close();
            reader.Close();
            response.Close();
        }
        catch (Exception ex)
        {
            textMessage.Text = ex.Message;
        }
    }
} 

上記のプログラムを実行すると、以下のようにツイートを取得することができます。
左がツイート一覧で、右が取得したXMLデータです。





以上

0 件のコメント :

コメントを投稿