Tweetinvi无法在24/7不停地发送推文而不会卡住

时间:2014-10-27 00:35:57

标签: c# twitter tweetinvi

我试图找出为什么tweetinvi流媒体库因为从关注者那里得到过多推文而陷入困境,我添加了函数AddFollow()。在这个函数中,我使用我创建的列表来放置600个关注者。我在应用程序上收到的这些推文中的每一条都直接发送到数据库,但在不确定的时间,应用程序只是停止发送推文,并且在调试模式下它不会显示任何错误或异常。它在C#中的程序和我使用visual studio 2012

这是我的代码:

public void getTweets()
    {
        string[] sports = new string[] { "NFL" };            
        long[] list1 = critter_connex(sports);
        int cont = 0;
        DateTime pastDate = DateTime.MinValue;
        string tweetType = "";
        int id = 0;

        using (var webClient = new WebClient())
        {
            webClient.Proxy = WebRequest.DefaultWebProxy;
            webClient.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
            webClient.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            webClient.Headers["User-Agent"] = "MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1";

            //using app key NECESITO VER TWEETS3
            var credentials = TwitterCredentials.CreateCredentials("XXXXX", "XXXXX", "XXXXX", "XXXXX");
            try
            {

                TwitterCredentials.ExecuteOperationWithCredentials(credentials, () =>
                {
                    cont++;
                    var filteredStream = Stream.CreateFilteredStream();
                    for (int i = 0; i < list1.Length; i++)
                    {
                        filteredStream.AddFollow(list1[i]);
                    }
                    try
                    {
                        filteredStream.MatchingTweetReceived += (sender, arg) =>
                        {
                            try
                            {

                                if (conn.State.ToString() == "Closed")
                                {
                                    conn.Open();
                                }

                                MySqlCommand command = new MySqlCommand();
                                MySqlDataReader reader;
                                command.Connection = conn;
                                command.CommandText = "SELECT sportName, specTeam FROM twitter_userids where userId = " + arg.Tweet.Creator.Id.ToString() + "";
                                command.Prepare();
                                reader = command.ExecuteReader();
                                try
                                {
                                    if (reader.Read())
                                    {
                                        tweet.setSport(reader.GetString(0));
                                        tweet.setTeam(reader.GetString(1));
                                    }
                                }
                                finally
                                {
                                    reader.Close();
                                    conn.Close();
                                }
                            }
                            catch (MySql.Data.MySqlClient.MySqlException ex)
                            {
                                Console.WriteLine(ex.Message);
                            }

                            try
                            {
                                cont++;
                                if (cont == 10)
                                {
                                    filteredStream.StopStream();
                                    DateTime date = DateTime.Now;
                                    getTweets();
                                }

                                if (tweetType == "Tweet")
                                {
                                    if (pastDate > arg.Tweet.CreatedAt)
                                    {
                                        filteredStream.StopStream();
                                        DateTime date = DateTime.Now;
                                        getTweets();
                                    }
                                }


                                if (arg.Tweet.IsRetweet)
                                {
                                    tweet.setUser_name(arg.Tweet.RetweetedTweet.Creator.ScreenName);
                                    tweet.setText(arg.Tweet.RetweetedTweet.Text);
                                    tweet.setPublish_date(arg.Tweet.RetweetedTweet.CreatedAt);
                                    tweet.setRetweet_date(arg.Tweet.CreatedAt);
                                    tweetType = "Retweet";
                                    tweet.setState("Retweet");
                                    tweet_text = tweet.getText();

                                    Console.WriteLine(tweet.getUser_name());
                                    Console.WriteLine(tweet.getText());
                                    string rt_final_date = tweet.getPublish_date().ToString("yyyy-MM-dd HH:mm:ss");
                                    string rt_date = tweet.getRetweet_date().ToString("yyyy-MM-dd HH:mm:ss");                                        
                                    Console.WriteLine(rt_final_date);
                                    Console.WriteLine(rt_date);
                                    Console.WriteLine(tweet.getSport());
                                    Console.WriteLine(tweet.getTeam());
                                    Console.WriteLine("Retweet");
                                    Console.WriteLine("\n");

                                    if (tweet_text.IndexOf("http://") > -1)
                                    {
                                        int index = tweet_text.IndexOf("http://");
                                        tweet.setLink(tweet_text.Substring(index));
                                        if (tweet.getLink().IndexOf(" ") > -1)
                                        {
                                            int index2 = tweet.getLink().IndexOf(" ");
                                            tweet.setLink(tweet.getLink().Substring(0, index2));
                                        }
                                        tweet.setText(tweet_text.Replace(tweet.getLink(), ""));
                                    }
                                    else
                                        if (tweet_text.IndexOf("https://") > -1)
                                        {
                                            int index = tweet_text.IndexOf("https://");
                                            tweet.setLink(tweet_text.Substring(index));
                                            if (tweet.getLink().IndexOf(" ") > -1)
                                            {
                                                int index2 = tweet.getLink().IndexOf(" ");
                                                tweet.setLink(tweet.getLink().Substring(0, index2));
                                            }
                                            tweet.setText(tweet_text.Replace(tweet.getLink(), ""));
                                        }


                                    MySqlCommand insert_rt = conn.CreateCommand();

                                    if (tweet.getLink() != "")
                                    {
                                        insert_rt.CommandText = "INSERT INTO TABLE (user_name, tweet_text, tweet_link, tweet_date, tweet_sport, tweet_type, tweet_team, isFilter) SELECT * FROM (SELECT '" + tweet.getUser_name() + "', '" +
                                                                tweet.getText().Replace("'", "") + "', '" + tweet.getLink().Replace("'", "") + "', '" + rt_final_date + "', '" + tweet.getSport() + "', '" + tweet.getState() + "', '" + tweet.getTeam() + "', 0 ) AS tmp " +
                                                                "WHERE NOT EXISTS (SELECT tweet_text FROM (select tweet_text from TABLE order by tweet_id desc limit 50 ) as t WHERE t.tweet_text = '" + tweet.getText().Replace("'", "") + "' LIMIT 50) LIMIT 1";
                                    }
                                    else
                                    {
                                        insert_rt.CommandText = "INSERT INTO TABLE (user_name, tweet_text, tweet_date, tweet_sport, tweet_type, tweet_team, isFilter) SELECT * FROM (SELECT '" + tweet.getUser_name() + "', '" +
                                                                tweet.getText().Replace("'", "") + "', '" + rt_final_date + "', '" + tweet.getSport() + "', '" + tweet.getState() + "', '" + tweet.getTeam() + "', 0 ) AS tmp " +
                                                                "WHERE NOT EXISTS (SELECT tweet_text FROM (select tweet_text from TABLE order by tweet_id desc limit 50 ) as t WHERE t.tweet_text = '" + tweet.getText().Replace("'", "") + "' LIMIT 50) LIMIT 1";
                                    }
                                    conn.Open();
                                    id = insert_rt.ExecuteNonQuery();
                                    conn.Close();
                                    tweet.setLink("");                                        

                                }
                                else
                                {
                                    tweet.setUser_name(arg.Tweet.Creator.ScreenName);
                                    tweet.setText(arg.Tweet.Text);
                                    pastDate = arg.Tweet.CreatedAt;
                                    tweet.setPublish_date(arg.Tweet.CreatedAt);
                                    tweetType = "Tweet";
                                    tweet.setState("Tweet");
                                    tweet_text = tweet.getText();

                                    Console.WriteLine(tweet.getUser_name());
                                    Console.WriteLine(tweet.getText());
                                    string final_date = tweet.getPublish_date().ToString("yyyy-MM-dd HH:mm:ss");
                                    Console.WriteLine(final_date);
                                    Console.WriteLine(tweet.getSport());
                                    Console.WriteLine(tweet.getTeam());
                                    Console.WriteLine(tweet.getState());
                                    Console.WriteLine("\n");

                                    if (tweet_text.IndexOf("http://") > -1)
                                    {
                                        int index = tweet_text.IndexOf("http://");
                                        tweet.setLink(tweet_text.Substring(index));
                                        if (tweet.getLink().IndexOf(" ") > -1)
                                        {
                                            int index2 = tweet.getLink().IndexOf(" ");
                                            tweet.setLink(tweet.getLink().Substring(0, index2));
                                        }
                                        tweet.setText(tweet_text.Replace(tweet.getLink(), ""));
                                    }
                                    else
                                        if (tweet_text.IndexOf("https://") > -1)
                                        {
                                            int index = tweet_text.IndexOf("https://");
                                            tweet.setLink(tweet_text.Substring(index));
                                            if (tweet.getLink().IndexOf(" ") > -1)
                                            {
                                                int index2 = tweet.getLink().IndexOf(" ");
                                                tweet.setLink(tweet.getLink().Substring(0, index2));
                                            }
                                            tweet.setText(tweet_text.Replace(tweet.getLink(), ""));
                                        }

                                    MySqlCommand insert_t = conn.CreateCommand();

                                    if (tweet.getLink() != "")
                                    {
                                        insert_t.CommandText = "INSERT INTO TABLE (user_name, tweet_text, tweet_link, tweet_date, tweet_sport, tweet_type, tweet_team, isFilter) SELECT * FROM ( SELECT '" + tweet.getUser_name() + "', '" +
                                                                tweet.getText().Replace("'", "") + "', '" + tweet.getLink().Replace("'", "") + "', '" + final_date + "', '" + tweet.getSport() + "', '" + tweet.getState() + "', '" + tweet.getTeam() + "', 0 ) AS tmp " +
                                                                "WHERE NOT EXISTS (SELECT tweet_text FROM (select tweet_text from TABLE order by tweet_id desc limit 50 ) as t WHERE t.tweet_text = '" + tweet.getText().Replace("'", "") + "') LIMIT 1";
                                    }
                                    else
                                    {
                                        insert_t.CommandText = "INSERT INTO TABLE (user_name, tweet_text, tweet_date, tweet_sport, tweet_type, tweet_team, isFilter) SELECT * FROM ( SELECT '" + tweet.getUser_name() + "', '" +
                                                                tweet.getText().Replace("'", "") + "', '" + final_date + "', '" + tweet.getSport() + "', '" + tweet.getState() + "', '" + tweet.getTeam() + "', 0 ) AS tmp " +
                                                                "WHERE NOT EXISTS (SELECT tweet_text FROM (select tweet_text from TABLE order by tweet_id desc limit 50 ) as t WHERE t.tweet_text = '" + tweet.getText().Replace("'", "") + "') LIMIT 1";
                                    }
                                    conn.Open();
                                    id = insert_t.ExecuteNonQuery();
                                    conn.Close();
                                    tweet.setLink("");


                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex);
                            }

                        };
                        filteredStream.StartStreamMatchingAllConditions();


                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        getTweets();
                    }
                    tweet_text = "";
                    getTweets();
                    filteredStream.StartStreamMatchingAllConditions();
                });
                getTweets();
            }
            catch (Exception ex)
            {
                getTweets();
                Console.WriteLine(ex);
            }
            getTweets();
        }
    }

感谢。

1 个答案:

答案 0 :(得分:4)

您是否尝试过注册StreamStopped事件并检查一切是否正常工作?

filteredStream.StreamStopped += (sender, args) =>
{
    Console.WriteLine(args.DisconnectMessage.Reason);

    if (args.Exception != null)
    {
        Console.WriteLine(args.Exception);
    }
};