Esempio n. 1
0
        private void button4_Click(object sender, EventArgs e)
        {
            //初始化
            listBox1.Items.Clear();
            dataGridView2.Rows.Clear();
            dataGridView2.ColumnCount = 20;
            dataGridView2.RowCount    = 200;
            var screen_name = textBox1.Text;
            var count       = System.Convert.ToInt32(textBox2.Text);


            //取得資料
            var options = new ListFavoriteTweetsOptions()
            {
                ScreenName = screen_name,
                Count      = count
            };
            var result    = servis.BeginListFavoriteTweets(options);
            var getTweets = servis.EndListFavoriteTweets(result);

            /*var getTweets = servis.ListFavoriteTweets(
             *  new ListFavoriteTweetsOptions()
             *  {
             *      ScreenName = screen_name, Count = count
             *  });*/


            //欄位設定
            int no_num           = 0;
            int url_link_num     = 1;
            int idstr_num        = 2;
            int author_num       = 3;
            int text_num         = 4;
            int entities_url_num = 5;
            int entities_tag_num = 6;

            dataGridView2.Columns[no_num].HeaderText           = "NO.";
            dataGridView2.Columns[url_link_num].HeaderText     = "推文連結";
            dataGridView2.Columns[idstr_num].HeaderText        = "Idstr";
            dataGridView2.Columns[author_num].HeaderText       = "Author";
            dataGridView2.Columns[text_num].HeaderText         = "Text";
            dataGridView2.Columns[entities_url_num].HeaderText = "Entities_Url";
            dataGridView2.Columns[entities_tag_num].HeaderText = "Entities_Tag";


            int row_num   = 0;
            int tweet_num = 0;

            foreach (var tweet in getTweets)
            {
                //No.
                dataGridView2.Rows[row_num].Cells[no_num].Value = tweet_num + 1;

                //推文ID
                var idstr = tweet.IdStr;
                dataGridView2.Rows[row_num].Cells[idstr_num].Value = idstr;

                //推文作者
                var author = tweet.Author.ScreenName;
                dataGridView2.Rows[row_num].Cells[author_num].Value = author;

                //推文文字
                var text = tweet.Text;
                dataGridView2.Rows[row_num].Cells[text_num].Value = text;

                //推文(合併)連結
                var url_link = "http://twitter.com/" + author + "/status/" + idstr;
                dataGridView2.Rows[row_num].Cells[url_link_num].Value = url_link;

                int entitiesEndRow_num   = row_num;
                int entitiesStartRow_num = row_num;
                var entities             = tweet.Entities;

                //推文實體內文連結(外部連結)
                row_num = entitiesStartRow_num;
                var entities_urls = entities.Urls;
                foreach (var url in entities_urls)
                {
                    dataGridView2.Rows[row_num++].Cells[entities_url_num].Value = url.ExpandedValue;
                }
                row_num--;
                entitiesEndRow_num = entitiesEndRow_num > row_num ? entitiesEndRow_num : row_num;

                //推文實體內文標籤
                row_num = entitiesStartRow_num;
                var entities_tags = entities.HashTags;
                foreach (var tag in entities_tags)
                {
                    dataGridView2.Rows[row_num++].Cells[entities_tag_num].Value = tag.Text;
                }
                row_num--;
                entitiesEndRow_num = entitiesEndRow_num > row_num ? entitiesEndRow_num : row_num;



                row_num = entitiesEndRow_num;
                row_num++;
                tweet_num++;

                //擴增列數
                if (row_num + 20 >= dataGridView2.RowCount)
                {
                    dataGridView2.RowCount += 200;
                }
            }
        }