Beispiel #1
0
 public static void RateLimits_ManualAwait()
 {
     TweetinviEvents.QueryBeforeExecute += (sender, args) =>
     {
         var queryRateLimit = RateLimit.GetQueryRateLimit(args.QueryURL);
         RateLimit.AwaitForQueryRateLimit(queryRateLimit);
     };
 }
Beispiel #2
0
        public static void QueryBeforeExecute(object sender, Tweetinvi.Events.QueryBeforeExecuteEventArgs args)
        {
            var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);

            // Some methods are not RateLimited. Invoking such a method will result in the queryRateLimits to be null
            if (queryRateLimits != null)
            {
                if (queryRateLimits.Remaining > 0)
                {
                    // We have enough resource to execute the query
                    return;
                }

                // Strategy #1 : Wait for RateLimits to be available
                Console.WriteLine("Waiting for RateLimits until : {0}", queryRateLimits.ResetDateTime.ToLongTimeString());
                args.Cancel = true;
            }
        }
Beispiel #3
0
        static void Main()
        {
            Auth.SetUserCredentials("XXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
                                    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

            TweetinviEvents.QueryBeforeExecute += (sender, args) =>
            {
                var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);

                if (queryRateLimits == null)
                {
                    return;
                }
                if (queryRateLimits.Remaining > 0)
                {
                    return;
                }
                Thread.Sleep((int)queryRateLimits.ResetDateTimeInMilliseconds);
            };
            const string inputFileName = @"biz_name.txt";
            var          companies     = File.ReadAllLines(inputFileName);

            const string output = @"TwitterOutput.txt";

            foreach (var company in companies)
            {
                var users = Search.SearchUsers(company);
                if (users != null)
                {
                    var usersList = users as IList <IUser> ?? users.ToList();
                    File.AppendAllText(output,
                                       usersList.Any()
                            ? $"{company}\t{usersList[0].ScreenName}\t{usersList[0].StatusesCount}\r\n"
                            : $"{company}\t0\t0\r\n");
                }
                else
                {
                    File.AppendAllText(output, $"{company}\t0\t0\r\n");
                }
            }

            Console.WriteLine(@"END");
            Console.ReadLine();
        }
Beispiel #4
0
        private static void RateLimitCheck(object?sender, QueryBeforeExecuteEventArgs args)
        {
            var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);

            if (queryRateLimits != null)
            {
                if (queryRateLimits.Remaining > 0)
                {
                    return;
                }

                using (var spinner = new Spinner("You have been rate limited by Twitter until " +
                                                 $"{queryRateLimits.ResetDateTime.ToLongTimeString()}. Please wait or Ctrl+C to quit"))
                {
                    Thread.Sleep((int)queryRateLimits.ResetDateTimeInMilliseconds);
                    spinner.Done();
                }
            }
        }
Beispiel #5
0
 // STATUS [ June 21, 2019 ]: Works (I think) but not sure if needed
 /// <summary>
 ///     From tweetinvi:
 ///     "Tweetinvi events are global events that can be used to gain more control and debug over what Tweetinvi does for you."
 /// </summary>
 /// <remarks>
 ///     From tweetinvi:
 ///         * Checks which requests will be performed through Tweetinvi
 ///     More importantly it allows developers to modify or cancel a WebRequest"
 ///     See: https://github.com/linvi/tweetinvi/wiki/Tweetinvi-Events
 /// </remarks>
 public void RunPreCheck()
 {
     TweetinviEvents.QueryBeforeExecute += (sender, args) =>
     {
         IEndpointRateLimit rateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);
         if (rateLimits != null)
         {
             Console.WriteLine($"\nrateLimits: {rateLimits}"); // 2 - Your code to await for rate limits
         }
         const bool shouldChangeCredentials = true;            // 3 - Your strategy to use multiple credentials
         if (shouldChangeCredentials)
         {
             ITwitterQuery queryDetails = args.TwitterQuery;
             Console.WriteLine($"\nqueryDetails: {queryDetails}\n");
         }
         // 4 - Cancel your query
         // args.Cancel = true;
     };
 }
 public TwitterManager()
 {
     _credentials = Auth.SetUserCredentials("", "", "", "");
     TweetinviEvents.QueryBeforeExecute += (sender, args) =>
     {
         var queryRateLimit = RateLimit.GetQueryRateLimit(args.QueryURL);
         if (queryRateLimit != null)
         {
             if (queryRateLimit.Remaining > 0)
             {
                 return;
             }
             else
             {
                 _isRateLimited = true;
             }
         }
     };
 }
Beispiel #7
0
        private void CheckRateLimits(object sender, QueryBeforeExecuteEventArgs args)
        {
            var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);

            // Some methods are not RateLimited. Invoking such a method will result in the queryRateLimits to be null
            if (queryRateLimits == null)
            {
                return;
            }

            if (queryRateLimits.Remaining > 0)
            {
                // We have enough resource to execute the query
                return;
            }

            // Wait for RateLimits to be available
            this.logger.LogInformation("Waiting for RateLimits until: {0}", queryRateLimits.ResetDateTime.ToLongTimeString());
            Thread.Sleep((int)queryRateLimits.ResetDateTimeInMilliseconds);
        }
Beispiel #8
0
        public static void RateLimitCheck(object sender, Tweetinvi.Events.QueryBeforeExecuteEventArgs args)
        {
            var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);

            if (queryRateLimits == null)
            {
                // Some methods are not RateLimited. Invoking such a method will result in the queryRateLimits to be null
                return;
            }

            if (queryRateLimits.Remaining == 0)
            {
                Console.WriteLine("Insufficient rate limit for " + args.QueryURL + " cancelling query");
                args.Cancel = true;
                return;
            }

            float remainingPercent = (float)queryRateLimits.Remaining / queryRateLimits.Limit;

            if (remainingPercent < 0.5f)
            {
                Console.WriteLine("Approaching rate limit for " + args.QueryURL + " Remaining: " + queryRateLimits.Remaining);
            }
        }
Beispiel #9
0
        public ConfigWindow()
        {
            InitializeComponent();
            ConfigManager.Start();
            DBHelper.OpenConnection();
            //new DebugTools().Show();
            this.Show();

            RefreshNamesList();
            TweetinviEvents.QueryBeforeExecute += (sender, args) => Debug.WriteLine(RateLimit.GetQueryRateLimit(args.QueryURL));
            OpenUserTimelineStream();

            //ocultar al minimizar, mostrar con doble clic
            System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon()
            {
                Icon    = Properties.Resources.icon,
                Visible = true
            };
            ni.MouseClick +=
                delegate(object sender, System.Windows.Forms.MouseEventArgs args)
            {
                if (args.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    if (this.Visibility == Visibility.Visible)
                    {
                        this.Hide();
                    }
                    else
                    {
                        this.Show();
                        this.WindowState = System.Windows.WindowState.Normal;
                    }
                }
            };
            ni.ContextMenu = new System.Windows.Forms.ContextMenu(CreateTrayIconMenu());
        }
        // Basic Twitter miner, to get tweets that contain a certain keyword. Invoked by a Reminder (MineReminder)
        protected override async Task <bool> mainMineAsync()
        {
            BESearchRequest theSearchRequest;
            TwitterData     theTwitterData;
            bool            miningToOlder;

            string twitterConsumerKey, twitterConsumerSecret, twitterAccessToken, twitterAccessTokenSecret;
            SearchTweetsParameters searchParameters;
            IEnumerable <ITweet>   theTweets;
            ushort windowSize;
            int    tweetsReturned;

            // Get the SearchRequest saved in the Stage Manager
            theSearchRequest = await GetTheSearchRequest();

            theTwitterData = await GetTheMinerData();

            // Disable the exception swallowing to allow exception to be thrown by Tweetinvi
            ExceptionHandler.SwallowWebExceptions = false;

            // Enable RateLimit Tracking
            RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly;

            // Event handler executed before each TwitterAPI call
            TweetinviEvents.QueryBeforeExecute += (sender, args) => {
                var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);

                // Some methods are not RateLimited. Invoking such a method will result in the queryRateLimits to be null
                if (queryRateLimits != null)
                {
                    if (queryRateLimits.Remaining > 0)
                    {
                        // We have enough resource to execute the query
                        return;
                    }
                    // Different policies to handle rate limit expiry

                    // Wait for RateLimits to be available
                    //Task.Delay((int)queryRateLimits.ResetDateTimeInMilliseconds);

                    // Cancel the Query
                    //args.Cancel = true;

                    // Set the value of the time needed by Twitter to reset the RateLimits
                    // so that it sets a Reminder to rerun after this time
                    rateLimitsResetTime = queryRateLimits.ResetDateTimeInSeconds;
                    args.Cancel         = true;
                }
            };

            // Set up your credentials
            var twitterSettings = configSettings.Settings.Sections["TwitterCredentials"];

            twitterConsumerKey       = twitterSettings.Parameters["ConsumerKey"].Value;
            twitterConsumerSecret    = twitterSettings.Parameters["ConsumerSecret"].Value;
            twitterAccessToken       = twitterSettings.Parameters["AccessToken"].Value;
            twitterAccessTokenSecret = twitterSettings.Parameters["AccessTokenSecret"].Value;

            Auth.SetUserCredentials(twitterConsumerKey, twitterConsumerSecret, twitterAccessToken, twitterAccessTokenSecret);

            // Basic Search Parameters
            windowSize       = 100;
            searchParameters = new SearchTweetsParameters(theSearchRequest.TheSearchKeyword)
            {
                Lang                   = LanguageFilter.English,
                SearchType             = SearchResultType.Recent,
                MaximumNumberOfResults = windowSize
            };

            if (theTwitterData.TheIdOldest == -1 && theTwitterData.TheIdNewest == -1)
            {
                miningToOlder = true;
            }
            else if (theTwitterData.TheIdOldest != -1 && theTwitterData.TheIdNewest == -1)
            {
                miningToOlder          = true;
                searchParameters.MaxId = theTwitterData.TheIdOldest - 1;
            }
            else
            {
                miningToOlder            = false;
                searchParameters.SinceId = theTwitterData.TheIdNewest;
            }

            // Find relevant tweets iteratively, in windows of a certain size (windowSize)
            tweetsReturned = windowSize;
            theTweets      = null;
            do
            {
                try {
                    theTweets = Search.SearchTweets(searchParameters);
                    if (theTweets == null)
                    {
                        throw new Exception();
                    }

                    tweetsReturned = theTweets.Count();
                    if (tweetsReturned != 0)
                    {
                        // Store tweets in the database
                        var minedTexts = from t in theTweets
                                         select new BEMinedText()
                        {
                            TheText     = t.FullText,
                            TheSource   = TheSourceID,
                            ExecutionID = theSearchRequest.ActiveExecutionID
                        };
                        await dbHandlerService.StoreMinedTexts(minedTexts);


                        // Update the the TwitterData with the oldest-newest mined tweets to indicate the additional job done
                        if (theTwitterData.IsNew() == true)                    // The first time tweets were mined
                        {
                            theTwitterData.TheIdNewest = theTweets.First().Id; // Save the id of the newest tweet
                        }
                        theTwitterData.TheTextsNum += tweetsReturned;

                        // Determine whether the mining should be done newer to older or inverse
                        if (miningToOlder == true)
                        {
                            searchParameters.MaxId     = theTweets.Last().Id - 1;
                            theTwitterData.TheIdOldest = theTweets.Last().Id;
                            if (tweetsReturned < windowSize)
                            {
                                miningToOlder            = false;
                                searchParameters.MaxId   = -1;
                                searchParameters.SinceId = theTwitterData.TheIdNewest;
                            }
                        }
                        else
                        {
                            searchParameters.SinceId   = theTweets.First().Id;
                            theTwitterData.TheIdNewest = theTweets.First().Id;
                        }

                        await SaveTheSearchRequest(theSearchRequest);
                        await SaveTheMinerData(theTwitterData);
                    }
                } catch {
                    return(false);
                }
            } while(tweetsReturned != 0);

            return(true);
        }
Beispiel #11
0
        public static void Main(string[] cmdlineArgs)
        {
            if (!File.Exists(_configurationFile))
            {
                Console.WriteLine("No configuration found. Can't continue");
                return;
            }

            if (cmdlineArgs.Contains("--dryrun"))
            {
                _isDryRun = true;
            }

            Console.WriteLine("Dry run: " + _isDryRun);

            _configuration = JsonConvert.DeserializeObject <Configuration>(File.ReadAllText(_configurationFile));

            if (_configuration.CacheDir != null && _configuration.CacheDir.Trim().Length > 0)
            {
                _generatedImagesFolder = _configuration.CacheDir;
            }

            if (!Directory.Exists(_generatedImagesFolder))
            {
                Directory.CreateDirectory(_generatedImagesFolder);
            }

            if (File.Exists(_previouslyProcessedTweetsFile))
            {
                _previouslyProcessedTweets = JsonConvert.DeserializeObject <List <long> >(File.ReadAllText(_previouslyProcessedTweetsFile));
            }
            else
            {
                _firstRun = true;
            }

            Auth.SetUserCredentials(
                _configuration.ConsumerKey,
                _configuration.ConsumerSecret,
                _configuration.UserAccessToken,
                _configuration.UserAccessSecret);

            RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly;

            TweetinviEvents.QueryBeforeExecute += (sender, args) =>
            {
                var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);

                if (queryRateLimits != null)
                {
                    if (queryRateLimits.Remaining > 0)
                    {
                        // process
                        return;
                    }

                    args.Cancel = true;
                    System.Console.WriteLine("ERROR: Rate limit hit\nQuery: " + args.QueryURL);
                }
            };

            Console.WriteLine(DateTime.Now);

            var firstPage = true;
            var pageNo    = 1;

            while (true)
            {
                System.Console.WriteLine("Page: " + pageNo++);

                var mentionTLParams = new MentionsTimelineParameters
                {
                    MaximumNumberOfTweetsToRetrieve = _tweetsPerPage
                };

                if (!_firstRun)
                {
                    mentionTLParams.SinceId = _previouslyProcessedTweets.Max();
                }

                if (!firstPage)
                {
                    mentionTLParams.MaxId = _previouslyProcessedTweets.Min() - 1;
                }

                var mentionsTLPage = Timeline.GetMentionsTimeline(mentionTLParams);

                if (mentionsTLPage == null || mentionsTLPage.Count() == 0)
                {
                    System.Console.WriteLine("No mentions to process");
                    break;
                }
                else
                {
                    foreach (var mention in mentionsTLPage)
                    {
                        if (_isDryRun)
                        {
                            ShowMention(mention);
                        }
                        else
                        {
                            ProcessMention(mention);
                        }
                    }
                }

                if (firstPage)
                {
                    firstPage = false;
                }
            }

            File.WriteAllText(_previouslyProcessedTweetsFile, JsonConvert.SerializeObject(_previouslyProcessedTweets));
        }
        static void Main(string[] args)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;

            SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();

            cb.DataSource     = "toptenhashtags-server.database.windows.net";
            cb.UserID         = "AndyTenholder@toptenhashtags-server";
            cb.Password       = "******";
            cb.InitialCatalog = "TestDatabase";
            // Set up your credentials (https://apps.twitter.com)
            // Applies credentials for the current thread.If used for the first time, set up the ApplicationCredentials
            Auth.SetUserCredentials("*************************", "*************************", "*************************-*************************", "*************************");
            var  user = User.GetAuthenticatedUser();
            bool twitterCreds1InUse = true;

            // Enable Automatic RateLimit handling
            RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait;

            var stream = Stream.CreateSampleStream();

            stream.StreamStopped += (sender, arguems) =>
            {
                try
                {
                    var exception = (ITwitterException)arguems.Exception; //<-- here it fails the cast
                }
                catch (Exception ex)
                {
                    var exceptionThatCausedTheStreamToStop = arguems.Exception;
                    Console.WriteLine("------------ Error {0} --------------------", exceptionThatCausedTheStreamToStop);
                    Thread.Sleep(1000);
                    Console.WriteLine("------------ Stream Restarted --------------------");
                    stream.StartStream();
                }
            };

            TweetinviEvents.QueryBeforeExecute += (sender, arguements) =>
            {
                var queryRateLimits = RateLimit.GetQueryRateLimit(arguements.QueryURL);

                // Some methods are not RateLimited. Invoking such a method will result in the queryRateLimits to be null
                if (queryRateLimits != null)
                {
                    if (queryRateLimits.Remaining > 0)
                    {
                        // We have enough resource to execute the query
                        return;
                    }

                    // Strategy #1 : Wait for RateLimits to be available
                    Console.WriteLine("Waiting for RateLimits until : {0}", queryRateLimits.ResetDateTime.ToLongTimeString());
                    Thread.Sleep((int)queryRateLimits.ResetDateTimeInMilliseconds);
                }
            };

            int tweetcount = 0;

            stream.TweetReceived += (sender, recievedTweet) =>
            {
                tweetcount += 1;
                Console.WriteLine(tweetcount);
                if (recievedTweet.Tweet.Hashtags.Count() > 0)
                {
                    Int32  unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                    string tweetLanguage = recievedTweet.Tweet.Language.ToString();

                    // if language is not in DB add it
                    if (GetLanguage(recievedTweet.Tweet.Language.ToString()) == "")
                    {
                        using (var connection = new SqlConnection(cb.ConnectionString))
                        {
                            connection.Open();
                            StringBuilder sb = new StringBuilder();
                            sb.Append("INSERT INTO Languages (Name) ");
                            sb.Append(String.Format("VALUES ('{0}');", tweetLanguage));
                            String sql = sb.ToString();

                            using (var command = new SqlCommand(sql, connection))
                            {
                                Console.WriteLine("Added Language {0} to database.", tweetLanguage);
                                command.ExecuteNonQuery();
                            }
                            connection.Close();
                        }
                    }

                    StringBuilder stringBuilder = new StringBuilder();
                    for (int x = 0; x < recievedTweet.Tweet.Hashtags.Count; x++)
                    {
                        if (x > 0)
                        {
                            stringBuilder.Append("/");
                        }
                        stringBuilder.Append(recievedTweet.Tweet.Hashtags[x].ToString().ToUpper());
                    }
                    string hashtags = stringBuilder.ToString();

                    // Create new tweet object and add to db
                    // TODO Retrieve Hashtag Strings

                    using (var connection = new SqlConnection(cb.ConnectionString))
                    {
                        connection.Open();
                        StringBuilder sb = new StringBuilder();
                        sb.Append("INSERT INTO Tweets (UnixTimeStamp, LanguageID, TweetIdString, Hashtags) ");
                        sb.Append(String.Format("VALUES ({0} , {1} , '{2}', N'{3}');", unixTimestamp, GetLanguageID(tweetLanguage), recievedTweet.Tweet.IdStr, hashtags));
                        String sql = sb.ToString();

                        using (var command = new SqlCommand(sql, connection))
                        {
                            int rowsAffected = command.ExecuteNonQuery();
                        }
                        connection.Close();
                    }

                    List <string> hashtagList = new List <string>();

                    // if hashtag is not in DB add it
                    foreach (var hashtag in recievedTweet.Tweet.Hashtags)
                    {
                        // Convert hashtag to uppercase string
                        var upperHashtag = hashtag.ToString().ToUpper();

                        if (GetHashtag(upperHashtag) != "")
                        {
                            if (!hashtagList.Contains(upperHashtag))
                            {
                                hashtagList.Add(upperHashtag);
                            }
                        }
                        else
                        {
                            using (var connection = new SqlConnection(cb.ConnectionString))
                            {
                                connection.Open();
                                StringBuilder sb = new StringBuilder();
                                sb.Append("INSERT INTO Hashtags (Name) ");
                                sb.Append(String.Format("VALUES (N'{0}');", upperHashtag));
                                String sql = sb.ToString();

                                using (var command = new SqlCommand(sql, connection))
                                {
                                    command.ExecuteNonQuery();
                                    Console.WriteLine("Added Hashtag {0} to database.", upperHashtag);
                                }
                                connection.Close();
                            }
                            if (!hashtagList.Contains(upperHashtag))
                            {
                                hashtagList.Add(upperHashtag);
                            }
                        }
                    }

                    // Create TweetHashtag object for each hashtag
                    foreach (var hashtag in hashtagList)
                    {
                        if (GetTweetID(recievedTweet.Tweet.IdStr).Length > 7)
                        {
                            break;
                        }
                        using (var connection = new SqlConnection(cb.ConnectionString))
                        {
                            connection.Open();
                            StringBuilder sb = new StringBuilder();
                            sb.Append("INSERT INTO TweetHashtags (HashtagID, TweetID, UnixTimeStamp) ");
                            sb.Append(String.Format("VALUES ({0} , '{1}', {2});", GetHashtagID(hashtag.ToString().ToUpper()), GetTweetID(recievedTweet.Tweet.IdStr), unixTimestamp));
                            String sql = sb.ToString();

                            using (var command = new SqlCommand(sql, connection))
                            {
                                command.ExecuteNonQuery();
                            }
                            connection.Close();
                        }
                    }
                }
            };

            stream.StartStream();

            Console.ReadKey();

            // Gets ID of Tweet
            string GetTweetID(string tweetIdString)
            {
                StringBuilder result = new StringBuilder();

                using (SqlConnection connection = new SqlConnection(cb.ConnectionString))
                {
                    connection.Open();

                    StringBuilder sb = new StringBuilder();
                    sb.Append("SELECT ID ");
                    sb.Append("FROM Tweets ");
                    sb.Append(String.Format("WHERE TweetIdString={0};", tweetIdString));
                    String sql = sb.ToString();

                    SqlCommand    command = new SqlCommand(sql, connection);
                    SqlDataReader reader  = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            result.Append(reader.GetInt32(0));
                        }
                        reader.Close();
                    }
                    connection.Close();
                }
                return(result.ToString());
            }

            // Gets ID of Hashtag
            int GetHashtagID(string hashtag)
            {
                StringBuilder result = new StringBuilder();

                using (SqlConnection connection = new SqlConnection(cb.ConnectionString))
                {
                    connection.Open();

                    StringBuilder sb = new StringBuilder();
                    sb.Append("SELECT ID ");
                    sb.Append("FROM Hashtags ");
                    sb.Append(String.Format("WHERE Name=N'{0}';", hashtag));
                    String sql = sb.ToString();

                    SqlCommand    command = new SqlCommand(sql, connection);
                    SqlDataReader reader  = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            result.Append(reader.GetInt32(0));
                        }
                        reader.Close();
                    }
                    connection.Close();
                }
                return(Int32.Parse(result.ToString()));
            }

            // Gets ID of language
            int GetLanguageID(string language)
            {
                StringBuilder result = new StringBuilder();

                using (SqlConnection connection = new SqlConnection(cb.ConnectionString))
                {
                    connection.Open();

                    StringBuilder sb = new StringBuilder();
                    sb.Append("SELECT ID ");
                    sb.Append("FROM Languages ");
                    sb.Append(String.Format("WHERE Name='{0}';", language));
                    String sql = sb.ToString();

                    SqlCommand    command = new SqlCommand(sql, connection);
                    SqlDataReader reader  = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            result.Append(reader.GetInt32(0));
                        }
                        reader.Close();
                    }
                    connection.Close();
                }
                return(Int32.Parse(result.ToString()));
            }

            // Checks DB for existing Hastag with same name
            string GetHashtag(string hashtag)
            {
                StringBuilder result = new StringBuilder();

                using (SqlConnection connection = new SqlConnection(cb.ConnectionString))
                {
                    connection.Open();

                    StringBuilder sb = new StringBuilder();
                    sb.Append("SELECT 1 ");
                    sb.Append("FROM Hashtags ");
                    sb.Append(String.Format("WHERE Name=N'{0}';", hashtag));
                    String sql = sb.ToString();

                    SqlCommand    command = new SqlCommand(sql, connection);
                    SqlDataReader reader  = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            result.Append(reader.GetInt32(0));
                        }
                        reader.Close();
                    }
                    connection.Close();
                }
                return(result.ToString());
            }

            // Checks DB for existing language with same name
            string GetLanguage(string language)
            {
                StringBuilder result = new StringBuilder();

                using (SqlConnection connection = new SqlConnection(cb.ConnectionString))
                {
                    connection.Open();

                    StringBuilder sb = new StringBuilder();
                    sb.Append("SELECT 1 ");
                    sb.Append("FROM Languages ");
                    sb.Append(String.Format("WHERE Name='{0}';", language));
                    String sql = sb.ToString();

                    SqlCommand    command = new SqlCommand(sql, connection);
                    SqlDataReader reader  = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            result.Append(reader.GetInt32(0));
                        }
                        reader.Close();
                    }
                    connection.Close();
                }
                return(result.ToString());
            }
        }
Beispiel #13
0
        public IHttpActionResult TwitterNetwork([FromBody] FormTwitterNetwork form)
        {
            //data utworzenia krawedzi
            var data = DateTime.Now;
            //ilosc zapytań
            int queries = 0;
            //akredytacja
            var credentials = network.Credentials.Where <Credentials>(x => x.ServiceDb.name == "Twitter").ToList();

            Auth.SetApplicationOnlyCredentials(credentials[0].key, credentials[0].secret, true);
            //okiełznanie limitów twitter
            RateLimit.RateLimitTrackerMode      = RateLimitTrackerMode.TrackOnly;
            TweetinviEvents.QueryBeforeExecute += (sender, args2) =>
            {
                var queryRateLimits = RateLimit.GetQueryRateLimit(args2.QueryURL);

                // Some methods are not RateLimited. Invoking such a method will result in the queryRateLimits to be null
                if (queryRateLimits != null)
                {
                    if (queryRateLimits.Remaining > 0)
                    {
                        // We have enough resource to execute the query
                        return;
                    }
                    else
                    {
                        while (queryRateLimits.Remaining < 1)
                        {
                            foreach (var cred in network.Credentials.Where <Credentials>(x => x.ServiceDb.name == "Twitter").ToList())
                            {
                                var credential = Auth.SetApplicationOnlyCredentials(cred.key, cred.secret, true);
                                queryRateLimits = RateLimit.GetQueryRateLimit(args2.QueryURL, credential);
                                if (queryRateLimits.Remaining > 0)
                                {
                                    args2.TwitterQuery.TwitterCredentials = Auth.SetApplicationOnlyCredentials(cred.key, cred.secret, true);
                                    break;
                                }
                            }
                        }
                    }
                }
            };
            //wybor id serwisu
            var serviceObj = network.ServiceDb.Where <ServiceDb>(x => x.name == "Twitter").ToList();
            var serviceId  = serviceObj[0].id;
            //utworzenie uzytkownika poczatkowego twittera
            var userName = Tweetinvi.User.GetUserFromId(form.InitialVertex);

            if (userName != null)
            {
                IsUserIdInVertex(form.InitialVertex, serviceObj[0].id, userName.ScreenName);
            }
            else
            {
                IsUserIdInVertex(form.InitialVertex, serviceObj[0].id);
            }
            var userId = network.VertexDb.Where <VertexDb>(x => x.identifier == form.InitialVertex.ToString() && x.service_id == serviceId).ToList();
            //tworzenie listy followersow i friendsow wierzcholka poczatkowego
            var twitterFollowers = Tweetinvi.User.GetFollowerIds(form.InitialVertex, form.QueryLimit);
            var twitterFriends   = Tweetinvi.User.GetFriendIds(form.InitialVertex, form.QueryLimit);

            queries = queries + 2;
            //zapis followersow dobazy danych
            foreach (var follower in twitterFollowers)
            {
                //dodawnie nowego uzytkownika do wierzcholkow
                var followerName = Tweetinvi.User.GetUserFromId(follower);
                if (followerName != null)
                {
                    IsUserIdInVertex(follower, serviceObj[0].id, followerName.ScreenName);
                }
                else
                {
                    IsUserIdInVertex(follower, serviceObj[0].id);
                }
                //pobranie id wiersza bazy z wierzcholkami
                var followerId = network.VertexDb.Where <VertexDb>(x => x.identifier == follower.ToString() && x.service_id == serviceId).ToList();
                var link       = new LinkDb();
                link.date_modified = data;
                link.network_id    = form.NetworkID;
                link.source_id     = followerId[0].id;
                link.target_id     = userId[0].id;
                network.LinkDb.Add(link);
            }
            network.SaveChanges();
            //zapis friendsow do bazy danych
            foreach (var friend in twitterFriends)
            {
                //dodawnie nowego uzytkownika do wierzcholkow
                var followerName = Tweetinvi.User.GetUserFromId(friend);
                if (followerName != null)
                {
                    IsUserIdInVertex(friend, serviceObj[0].id, followerName.ScreenName);
                }
                else
                {
                    IsUserIdInVertex(friend, serviceObj[0].id);
                }
                //pobranie id wiersza b azy z wierzcholkami
                var friendId = network.VertexDb.Where <VertexDb>(x => x.identifier == friend.ToString() && x.service_id == serviceId).ToList();
                var link     = new LinkDb();
                link.date_modified = data;
                link.network_id    = form.NetworkID;
                link.source_id     = userId[0].id;
                link.target_id     = friendId[0].id;
                network.LinkDb.Add(link);
            }
            network.SaveChanges();
            //utworzenie list uzytkownikow
            //List<long> usersTwitter = twitterFollowers.Union(twitterFriends).ToList();
            List <long> twitterFollowersList = twitterFollowers.ToList();
            List <long> twitterFriendsList   = twitterFriends.ToList();
            List <long> usedUsers            = new List <long>();

            usedUsers.Add(form.InitialVertex);
            //rozpoczynamy pobieranie dla kolejnych wierzcholkow
            while (queries < form.NumberQueries && (twitterFriendsList.Any() || twitterFollowersList.Any()))
            {
                var  rand  = new Random();
                bool bool1 = (twitterFriendsList.Any()) ? true : false;
                bool bool2 = (twitterFollowersList.Any()) ? true : false;
                if (rand.Next(100) % 2 == 1 && bool1 == true)
                {
                    queries = queries + 2;
                    var newInitialVertexId = twitterFriendsList[0];
                    userId = network.VertexDb.Where <VertexDb>(x => x.identifier == newInitialVertexId.ToString() && x.service_id == serviceId).ToList();
                    usedUsers.Add(newInitialVertexId);
                    var newUsersFriends   = Tweetinvi.User.GetFriendIds(newInitialVertexId, form.QueryLimit);
                    var newUsersFollowers = Tweetinvi.User.GetFollowerIds(newInitialVertexId, form.QueryLimit);
                    twitterFriendsList   = twitterFriendsList.Union(newUsersFriends).Except(usedUsers).ToList();
                    twitterFollowersList = twitterFollowersList.Union(newUsersFollowers).Except(usedUsers).ToList();
                    //zapis followersow dobazy danych
                    foreach (var follower in newUsersFollowers)
                    {
                        //dodawnie nowego uzytkownika do wierzcholkow
                        var followerName = Tweetinvi.User.GetUserFromId(follower);
                        if (followerName != null)
                        {
                            IsUserIdInVertex(follower, serviceObj[0].id, followerName.ScreenName);
                        }
                        else
                        {
                            IsUserIdInVertex(follower, serviceObj[0].id);
                        }
                        //pobranie id wiersza b azy z wierzcholkami
                        var followerId = network.VertexDb.Where <VertexDb>(x => x.identifier == follower.ToString() && x.service_id == serviceId).ToList();
                        var link       = new LinkDb();
                        link.date_modified = data;
                        link.network_id    = form.NetworkID;
                        link.source_id     = followerId[0].id;
                        link.target_id     = userId[0].id;
                        network.LinkDb.Add(link);
                    }
                    network.SaveChanges();
                    //zapis friendsow do bazy danych
                    foreach (var friend in newUsersFriends)
                    {
                        //dodawnie nowego uzytkownika do wierzcholkow
                        var followerName = Tweetinvi.User.GetUserFromId(friend);
                        if (followerName != null)
                        {
                            IsUserIdInVertex(friend, serviceObj[0].id, followerName.ScreenName);
                        }
                        else
                        {
                            IsUserIdInVertex(friend, serviceObj[0].id);
                        }
                        //pobranie id wiersza b azy z wierzcholkami
                        var friendId = network.VertexDb.Where <VertexDb>(x => x.identifier == friend.ToString() && x.service_id == serviceId).ToList();
                        var link     = new LinkDb();
                        link.date_modified = data;
                        link.network_id    = form.NetworkID;
                        link.source_id     = userId[0].id;
                        link.target_id     = friendId[0].id;
                        network.LinkDb.Add(link);
                    }
                    network.SaveChanges();
                }
                else if (rand.Next(100) % 2 == 0 && bool2 == true)
                {
                    queries = queries + 2;;
                    var newInitialVertexId = twitterFollowersList[0];
                    userId = network.VertexDb.Where <VertexDb>(x => x.identifier == newInitialVertexId.ToString() && x.service_id == serviceId).ToList();
                    usedUsers.Add(newInitialVertexId);
                    var newUsersFriends   = Tweetinvi.User.GetFriendIds(newInitialVertexId, form.QueryLimit);
                    var newUsersFollowers = Tweetinvi.User.GetFollowerIds(newInitialVertexId, form.QueryLimit);
                    twitterFriendsList   = twitterFriendsList.Union(newUsersFriends).Except(usedUsers).ToList();
                    twitterFollowersList = twitterFollowersList.Union(newUsersFollowers).Except(usedUsers).ToList();
                    //zapis followersow dobazy danych
                    foreach (var follower in newUsersFollowers)
                    {
                        //dodawnie nowego uzytkownika do wierzcholkow
                        var followerName = Tweetinvi.User.GetUserFromId(follower);
                        if (followerName != null)
                        {
                            IsUserIdInVertex(follower, serviceObj[0].id, followerName.ScreenName);
                        }
                        else
                        {
                            IsUserIdInVertex(follower, serviceObj[0].id);
                        }
                        //pobranie id wiersza b azy z wierzcholkami
                        var followerId = network.VertexDb.Where <VertexDb>(x => x.identifier == follower.ToString() && x.service_id == serviceId).ToList();
                        var link       = new LinkDb();
                        link.date_modified = data;
                        link.network_id    = form.NetworkID;
                        link.source_id     = followerId[0].id;
                        link.target_id     = userId[0].id;
                        network.LinkDb.Add(link);
                    }
                    network.SaveChanges();
                    //zapis friendsow do bazy danych
                    foreach (var friend in newUsersFriends)
                    {
                        //dodawnie nowego uzytkownika do wierzcholkow
                        var followerName = Tweetinvi.User.GetUserFromId(friend);
                        if (followerName != null)
                        {
                            IsUserIdInVertex(friend, serviceObj[0].id, followerName.ScreenName);
                        }
                        else
                        {
                            IsUserIdInVertex(friend, serviceObj[0].id);
                        }
                        //pobranie id wiersza b azy z wierzcholkami
                        var friendId = network.VertexDb.Where <VertexDb>(x => x.identifier == friend.ToString() && x.service_id == serviceId).ToList();
                        var link     = new LinkDb();
                        link.date_modified = data;
                        link.network_id    = form.NetworkID;
                        link.source_id     = userId[0].id;
                        link.target_id     = friendId[0].id;
                        network.LinkDb.Add(link);
                    }
                    network.SaveChanges();
                }
            }
            return(Ok());
        }