Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to find out why tweetinvi streaming library is getting stuck when it's getting too much tweets from the followers, that I add with the function AddFollow(). In this function I'm putting 600 followers using a list that I create. Each of this tweets that I receive on my application go directly to a database but in an uncertain time the application just stop bring tweets and it no show any error or exception in the debugging mode. The program it's in c# and I'm using visual studio 2012, here is my code:


C#
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();
        }
    }
Posted
Comments
BillWoodruff 26-Oct-14 21:51pm    
Does it take you any consistent amount of time to get "stuck" ? Try observing memory use in the Task Manager from start to "stuck."
[no name] 26-Oct-14 23:03pm    
To me the recursion is enough to ring alarm bells. Looks a strange design.
Member 9925071 27-Oct-14 12:00pm    
No, I think it's more a traffic problem because it only happened when I'm receiving a lot of tweets. And it doesn't have a consistent amount of time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900