コード例 #1
0
        private async void button1_Click(object sender, EventArgs e)
        {
            txt_twsearch.ReadOnly = true;
            button1.Enabled       = false;
            var    keyword  = txt_twsearch.Text;
            string battleid = "00000000";
            int    cnt      = 0;

            String[] star = { "★", "|", "☆", "|" };
            flag = true;

            try
            {
                var tokens = CoreTweet.Tokens.Create(appKey, appSKey, Properties.Settings.Default.usertoken, Properties.Settings.Default.usertokenS);
                while (flag)
                {
                    cnt++;
                    var result = new CoreTweet.SearchResult();
                    try
                    {
                        result = await tokens.Search.TweetsAsync(count => 1, q => keyword);
                    }
                    catch (Exception er)
                    {
                        Console.WriteLine(er.Message);
                        continue;
                    }
                    Console.WriteLine(result.Count);
                    foreach (var tweet in result)
                    {
                        string nowbattleid = "";
                        Console.WriteLine("{0}: {1}", tweet.CreatedAt, tweet.Text);
                        System.Text.RegularExpressions.Regex r =
                            new System.Text.RegularExpressions.Regex(
                                @"[a-fA-F0-9]{8,8}",
                                System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                        System.Text.RegularExpressions.Match m = r.Match(tweet.Text);
                        while (m.Success)
                        {
                            nowbattleid = m.Value;
                            // 最初のヒットを利用
                            break;
                        }
                        if (battleid.Equals(nowbattleid))
                        {
                            break;
                        }
                        battleid = nowbattleid;
                        var jst = tweet.CreatedAt.LocalDateTime;

                        Clipboard.SetText(battleid);

                        textBox1.Text = jst + "\r\n" + tweet.User.ScreenName + "\r\n" + tweet.Text.Replace("\n", "\r\n").Replace("\r\r", "\r");

                        notifyIcon1.BalloonTipTitle = battleid;
                        notifyIcon1.BalloonTipText  = tweet.Text;
                        notifyIcon1.ShowBalloonTip(1000);
                    }
                    await Task.Delay(Decimal.ToInt32(numericUpDown1.Value) * 250);

                    label3.Text = star[((cnt * 4 + 0) % 4)] + ":" + cnt;
                    await Task.Delay(Decimal.ToInt32(numericUpDown1.Value) * 250);

                    label3.Text = star[((cnt * 4 + 1) % 4)] + ":" + cnt;
                    await Task.Delay(Decimal.ToInt32(numericUpDown1.Value) * 250);

                    label3.Text = star[((cnt * 4 + 2) % 4)] + ":" + cnt;
                    await Task.Delay(Decimal.ToInt32(numericUpDown1.Value) * 250);

                    label3.Text = star[((cnt * 4 + 3) % 4)] + ":" + cnt;
                }
            }
            catch (CoreTweet.TwitterException er)
            {
                textBox1.Text = er.Message;
            }
        }
コード例 #2
0
        public void Search(string query)
        {
            images.Clear();
            Task.Factory.StartNew(() =>
            {
                // トークンを取得
                if (tokens == null)
                {
                    tokens = CoreTweet.Tokens.Create(
                        SecretSetting.Default.ConsumerKey,
                        SecretSetting.Default.ConsumerSecret,
                        SecretSetting.Default.AccessToken,
                        SecretSetting.Default.AccessTokenSecret
                        );

                    if (tokens == null)
                    {
                        SearchError(this, EventArgs.Empty);
                        return;
                    }
                }

                // Httpエンコード
                var word = HttpUtility.HtmlEncode(query);

                // 検索し、結果を取得
                var dic = new Dictionary <string, object>();
                dic.Add("q", "filter:images " + word);
                dic.Add("include_entities", true);
                dic.Add("count", (int)numericUpDown1.Value);
                if (comboBox1.SelectedIndex > 0)
                {
                    dic.Add("result_type", SearchMethods[comboBox1.SelectedIndex]);
                }
                if (dateTimePicker1.Checked)
                {
                    var date = string.Format("{0:0000}-{1:00}-{2:00}", dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day);
                    dic.Add("until", date);
                }
                if (checkBox1.Checked)
                {
                    dic.Add("since_id", numericUpDown2.Value);
                }
                if (checkBox2.Checked)
                {
                    dic.Add("max_id", numericUpDown3.Value);
                }
                CoreTweet.SearchResult result = null;
                try
                {
                    result = tokens.Search.Tweets(dic);
                }
                catch
                {
                    SearchError(this, EventArgs.Empty);
                    return;
                }

                Parallel.For(0, result.Count, i =>
                {
                    try
                    {
                        var res = result[i];
                        if (res.Entities.Media != null)
                        {
                            foreach (var m in res.Entities.Media)
                            {
                                var wc        = new System.Net.WebClient();
                                Stream stream = wc.OpenRead(m.MediaUrl.AbsoluteUri);

                                var idx = m.MediaUrl.LocalPath.LastIndexOf('/');
                                if (idx < 0)
                                {
                                    idx = 0;
                                }
                                var sub = m.MediaUrl.LocalPath.Substring(idx + 1);

                                var idx1 = sub.LastIndexOf(":large");
                                var idx2 = sub.LastIndexOf("-large");
                                if (idx1 > 0)
                                {
                                    sub = sub.Substring(0, idx1);
                                }
                                else if (idx2 > 0)
                                {
                                    sub = sub.Substring(0, idx2);
                                }
                                var bitmap = new System.Drawing.Bitmap(stream);
                                if (sub.LastIndexOf('.') == -1)
                                {
                                    sub += "." + GetFileFormat(bitmap);
                                }

                                var image = new ImageData()
                                {
                                    Bitmap    = bitmap,
                                    FileName  = sub,
                                    SourceURL = m.ExpandedUrl.AbsoluteUri,
                                };
                                lock (images)
                                {
                                    images.Add(image);
                                    ImageLoaded(this, new ImageLoadedEventArgs()
                                    {
                                        Index = images.Count - 1, ImageData = image
                                    });
                                }
                                stream.Close();
                            }
                        }
                    }
                    catch { }
                });
                SearchFinished(this, EventArgs.Empty);
            });
        }