예제 #1
0
        //algorithm 0
        //for new algorthm need mimic bellow and replace GoogleMapHelper() to ur new function
        public void GoogleMap(int AlgorithmId)
        {
            while (true)
            {
                bool continue1 = startCheck();
                while (continue1)
                {
                    TwittersI ts = new Twitters();
                    ts.allTweets = database.loadBasicTweets(algorithmInfomations[AlgorithmId].lastMaxId + 1, this.TweetbnumberEachCycle);
                    if (ts.allTweets.Count != 0)//if nothing is loaded not run
                    {
                        //for new algorthm need to replace GoogleMapHelper() to ur new function
                        LocationHelper ghp = new GoogleMapHelper();


                        TwitterModel id = ghp.run(ts, AlgorithmId);
                        algorithmInfomations[AlgorithmId].lastMaxId = id.realTwitterId;
                        algorithmInfomations[AlgorithmId].lastDate  = id.createAt;
                        ts.saveLocationInTweets(AlgorithmId);
                        database.updateAlgorithmInfo(algorithmInfomations[AlgorithmId]);
                    }
                    else
                    {
                        continue1 = false;
                    }
                }
                Thread.Sleep(sleepInterval); // wait for sleepInterval, then continue
            }
        }
예제 #2
0
        public bool updateTweetsWithLocation(Twitters ts, int algorithmId)//save location of tweet
        {
            MySqlConnection conn = connect();

            try
            {
                if (ts.allTweets.Count < 1)
                {
                    return(true);
                }
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                int          algorithmNumber = algorithmId;
                string       locationString  = "La" + algorithmNumber + " = @la, Lo" + algorithmNumber + " = @lo, city" + algorithmNumber + " = @city, state" + algorithmNumber + " = @state, country" + algorithmNumber + " = @country";
                string       cmdText         = @"update  tweets set " + locationString + " where realTwitterId = @id";
                MySqlCommand command         = new MySqlCommand(cmdText, conn);


                foreach (TwitterModel tm in ts.allTweets)
                {
                    command.Parameters.Clear();
                    if (tm.location.Count > 0)
                    {
                        command.Parameters.AddWithValue("@la", tm.location[0].la);
                        command.Parameters.AddWithValue("@lo", tm.location[0].lo);
                        command.Parameters.AddWithValue("@city", tm.location[0].city);
                        command.Parameters.AddWithValue("@state", tm.location[0].state);
                        command.Parameters.AddWithValue("@country", tm.location[0].country);
                        command.Parameters.AddWithValue("@id", tm.realTwitterId);

                        if (command.ExecuteNonQuery() <= 0)
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                if (config.debug)
                {
                    throw ex;
                }
                else
                {
                    return(false);
                }
            }
            finally
            {
                conn.Close();
            }
        }
예제 #3
0
        public int  run()
        {
            #region prepare
            System.Diagnostics.Debug.WriteLine("Search " + searchInfo.searchId);
            TimeSpan sleepTime = config.SearchWaitInterval;
            String   para      = "";
            foreach (String s in searchInfo.include)
            {
                para += s;
                para += " ";
            }
            foreach (String s in searchInfo.exclude)
            {
                para += "-";
                para += s;
                para += " ";
            }
            para = para.Substring(0, para.Length - 1);
            var searchParameter = Search.CreateTweetSearchParameter(para);
            searchParameter.TweetSearchType        = TweetSearchType.OriginalTweetsOnly;
            searchParameter.MaximumNumberOfResults = config.searchMaximumNumberOfResults;

            bool finished    = false;
            int  step        = 0;
            long tempMaxId   = -1;
            int  cycelNumber = 0;



            if (searchInfo.status == 1)
            {
                tempMaxId = searchInfo.maxIdOnResume - 1;
            }

            if (searchInfomation.finishedLastTwitterId > 0)
            {
                searchParameter.SinceId = searchInfomation.finishedLastTwitterId + 1;
            }

            #endregion

            while (!finished)
            {
                if (cycelNumber++ >= config.searchCycleNumber)
                {
                    searchInfo.maxIdOnResume = tempMaxId;
                    return(0);
                }
                searchInfo.status = 1;
                dataBase.updateSearchStatus(this);
                if (tempMaxId > 0)
                {
                    searchParameter.MaxId = tempMaxId;
                }
                if (deleteNotify == true)//check if this search is deleted before a long running part of this code
                {
                    return(-1);
                }
                ISearchResult tweets = null;
                try
                {
                    tweets = Search.SearchTweetsWithMetadata(searchParameter);
                }
                catch (System.Net.WebException ex)
                {
                    System.Diagnostics.Debug.WriteLine("Error");
                    throw ex;
                }
                if (!tweets.Tweets.IsEmpty())
                {
                    step = 0;//reset step
                    if (searchInfomation.finishedLastTwitterId == -1)
                    {
                        if (searchInfo.firstTwitterDate == null)
                        {
                            searchInfo.firstTwitterDate = tweets.Tweets.Last().CreatedAt;
                        }
                        if (searchInfo.firstTwitterDate > tweets.Tweets.Last().CreatedAt)
                        {
                            searchInfo.firstTwitterDate = tweets.Tweets.Last().CreatedAt;
                        }
                    }
                    if (tempMaxId < 0)
                    {
                        searchInfo.tempLastTwitterDate     = tweets.Tweets.First().CreatedAt;
                        searchInfomation.tempLastTwitterId = tweets.Tweets.First().Id; // the end of twitter of this search occurs at the first cycle
                        updateOnTemp();
                    }
                    tempMaxId = tweets.Tweets.Last().Id - 1;
                    if (deleteNotify == true)//check if this search is deleted before a long running part of this code
                    {
                        return(-1);
                    }
                    Twitters tws = new Twitters(this, tweets, dataBase);
                    tws.saveBasicTweets();
                    System.Diagnostics.Debug.WriteLine("finished" + cycelNumber + " cycle");
                }
                #region finished
                if (tweets.Tweets.IsEmpty() && tweets.NumberOfQueriesUsedToCompleteTheSearch != 0)
                {
                    searchInfo.status = 2;

                    searchInfo.finishedLastTwitterId   = searchInfo.tempLastTwitterId;
                    searchInfo.finishedLastTwitterDate = searchInfo.tempLastTwitterDate;
                    updateOnFinish(); // will update
                    return(1);        // finished
                }
                #endregion
                if (tweets.NumberOfQueriesUsedToCompleteTheSearch == 0)
                {
                    step++;
                    if (step == 2)
                    {
                        return(-1); //error
                    }
                    System.Diagnostics.Debug.WriteLine("waiting");
                    Thread.Sleep(sleepTime);
                }
                else
                {
                    step = 0;
                }
            }
            return(1);
        }
예제 #4
0
        public bool saveBasicTweets(Twitters s)
        {
            MySqlConnection conn = connect();

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            string cmdText = @"insert into  tweets (APITwitterId ,searchId,userScreenName ,userId,content,createAt,profileLocationContent ,PlaceLa1 ,PlaceLo1 ,PlaceLa2 ,PlaceLo2 ,PlaceLa3 ,PlaceLo3 ,PlaceLa4 ,PlaceLo4 ) values (@APITwitterId ,@searchId,@userScreenName ,@userId,@content,@createAt,@profileLocationContent ,@PlaceLa1 ,@PlaceLo1 ,@PlaceLa2 ,@PlaceLo2 ,@PlaceLa3 ,@PlaceLo3 ,@PlaceLa4 ,@PlaceLo4 )";

            try
            {
                foreach (TwitterModel tm in s.allTweets)
                {
                    MySqlCommand command = new MySqlCommand(cmdText, conn);
                    command.Parameters.AddWithValue("@APITwitterId", tm.APITwitterId);
                    command.Parameters.AddWithValue("@searchId", tm.searchId);
                    command.Parameters.AddWithValue("@userScreenName", tm.userScreenName);
                    command.Parameters.AddWithValue("@userId", tm.userId);
                    command.Parameters.AddWithValue("@content", tm.content);
                    command.Parameters.AddWithValue("@createAt", tm.createAt.ToString("yyyy-MM-dd HH:mm:ss"));
                    command.Parameters.AddWithValue("@profileLocationContent", tm.profileLocationContent);
                    switch (tm.PlaceLa.Count)
                    {
                    case 0:
                        command.Parameters.AddWithValue("@PlaceLa1", null);
                        command.Parameters.AddWithValue("@PlaceLo1", null);
                        command.Parameters.AddWithValue("@PlaceLa2", null);
                        command.Parameters.AddWithValue("@PlaceLo2", null);
                        command.Parameters.AddWithValue("@PlaceLa3", null);
                        command.Parameters.AddWithValue("@PlaceLo3", null);
                        command.Parameters.AddWithValue("@PlaceLa4", null);
                        command.Parameters.AddWithValue("@PlaceLo4", null);
                        break;

                    case 1:
                        command.Parameters.AddWithValue("@PlaceLa1", tm.PlaceLa[0]);
                        command.Parameters.AddWithValue("@PlaceLo1", tm.PlaceLo[0]);
                        command.Parameters.AddWithValue("@PlaceLa2", null);
                        command.Parameters.AddWithValue("@PlaceLo2", null);
                        command.Parameters.AddWithValue("@PlaceLa3", null);
                        command.Parameters.AddWithValue("@PlaceLo3", null);
                        command.Parameters.AddWithValue("@PlaceLa4", null);
                        command.Parameters.AddWithValue("@PlaceLo4", null);
                        break;

                    case 2:
                        command.Parameters.AddWithValue("@PlaceLa1", tm.PlaceLa[0]);
                        command.Parameters.AddWithValue("@PlaceLo1", tm.PlaceLo[0]);
                        command.Parameters.AddWithValue("@PlaceLa2", tm.PlaceLa[1]);
                        command.Parameters.AddWithValue("@PlaceLo2", tm.PlaceLo[1]);
                        command.Parameters.AddWithValue("@PlaceLa3", null);
                        command.Parameters.AddWithValue("@PlaceLo3", null);
                        command.Parameters.AddWithValue("@PlaceLa4", null);
                        command.Parameters.AddWithValue("@PlaceLo4", null);

                        break;

                    case 3:
                        command.Parameters.AddWithValue("@PlaceLa1", tm.PlaceLa[0]);
                        command.Parameters.AddWithValue("@PlaceLo1", tm.PlaceLo[0]);
                        command.Parameters.AddWithValue("@PlaceLa2", tm.PlaceLa[1]);
                        command.Parameters.AddWithValue("@PlaceLo2", tm.PlaceLo[1]);
                        command.Parameters.AddWithValue("@PlaceLa3", tm.PlaceLa[2]);
                        command.Parameters.AddWithValue("@PlaceLo3", tm.PlaceLo[2]);
                        command.Parameters.AddWithValue("@PlaceLa4", null);
                        command.Parameters.AddWithValue("@PlaceLo4", null);

                        break;

                    case 4:
                        command.Parameters.AddWithValue("@PlaceLa1", tm.PlaceLa[0]);
                        command.Parameters.AddWithValue("@PlaceLo1", tm.PlaceLo[0]);
                        command.Parameters.AddWithValue("@PlaceLa2", tm.PlaceLa[1]);
                        command.Parameters.AddWithValue("@PlaceLo2", tm.PlaceLo[1]);
                        command.Parameters.AddWithValue("@PlaceLa3", tm.PlaceLa[2]);
                        command.Parameters.AddWithValue("@PlaceLo3", tm.PlaceLo[2]);
                        command.Parameters.AddWithValue("@PlaceLa4", tm.PlaceLa[3]);
                        command.Parameters.AddWithValue("@PlaceLo4", tm.PlaceLo[3]);
                        break;
                    }

                    command.ExecuteNonQuery();
                }
                return(true);
            }
            catch (Exception e)
            {
                if (config.debug)
                {
                    throw e;
                }
                else
                {
                    return(false);
                }
            }
            finally
            {
                conn.Close();
            }
        }