Beispiel #1
0
        // Step 2 - Generate User Credentials
        public ITwitterCredentials GetCredentialsFromVerifierCode(string verifierCode, IAuthenticationToken authToken)
        {
            try
            {
                if (verifierCode == null)
                {
                    throw new ArgumentNullException("VerifierCode", "If you've received a verifier code that is null, " +
                                                                    "it means that authentication has failed!");
                }

                var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_verifier", verifierCode, true, true, false);

                var authHandler = new AuthHttpHandler(callbackParameter, authToken);
                var response = _twitterRequestHandler.ExecuteQuery(Resources.OAuthRequestAccessToken, HttpMethod.POST, authHandler, 
                    new TwitterCredentials(authToken.ConsumerCredentials));

                if (response == null)
                {
                    return null;
                }

                var responseInformation = Regex.Match(response, Resources.OAuthTokenAccessRegex);
                if (responseInformation.Groups["oauth_token"] == null || responseInformation.Groups["oauth_token_secret"] == null)
                {
                    return null;
                }

                var credentials = new TwitterCredentials(
                    authToken.ConsumerKey,
                    authToken.ConsumerSecret,
                    responseInformation.Groups["oauth_token"].Value,
                    responseInformation.Groups["oauth_token_secret"].Value);

                return credentials;
            }
            catch (TwitterException ex)
            {
                if (_exceptionHandler.LogExceptions)
                {
                    _exceptionHandler.AddTwitterException(ex);
                }

                if (!_exceptionHandler.SwallowWebExceptions)
                {
                    throw;
                }
            }

            return null;
        }
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            await AuthTokens.KeyRetriever();
            _appCredentials = new TwitterCredentials(AuthTokens.TwitterConsumerKey, AuthTokens.TwitterConsumerSecret);
            TwitterAuthenticatorMethod();

            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
            SystemNavigationManager.GetForCurrentView().BackRequested += (s, ex) =>
            {
                var frame = Window.Current.Content as Frame;
                if (frame != null)
                {

                    frame.Navigate(typeof(MainPage));
                }
            };

        }
Beispiel #3
0
        public bool authenticateUser(string userAccessToken, string userAccessTokenSecret)
        {
            if (this.userCredentials == null)
            {
                var userCredentials = new TwitterCredentials(this.ConsumerKey, this.ConsumerSecret, userAccessToken, userAccessTokenSecret);
                this.User = Tweetinvi.User.GetAuthenticatedUser(userCredentials);

                if(this.user != null)
                {
                    storeUserCredentials(userCredentials.AccessToken, userCredentials.AccessTokenSecret);
                    this.twitterUser = new TwitterUser(this.user);

                    return true;
                }
            }
            else
                throw new Exception("User credensials cannot be null");

            return false;
        }
        public static double ProfileFrequency(string query)
        {
            if (profiledQueries.ContainsKey(query))
                return profiledQueries[query];

            if (!IsNextRequestPossible())
                SleepUntilNextRequest();

            var credentials = new TwitterCredentials(
                ConfigurationManager.AppSettings["ConsumerKey"],
                ConfigurationManager.AppSettings["ConsumerSecret"],
                ConfigurationManager.AppSettings["WorkerToken"],
                ConfigurationManager.AppSettings["WorkerSecret"]
            );

            exceptionError = "";
            List<DateTime> tweetsDateTime = new List<DateTime>();

            Auth.ExecuteOperationWithCredentials(credentials, () =>
            {
                int profilingRetries = 0;
                TweetSearchParameters searchParams = new TweetSearchParameters(query) { SearchType = SearchResultType.Recent };
                
                // retry profiling a few times if there is no response
                while (profilingRetries <= Convert.ToInt32(ConfigurationManager.AppSettings["TwitterProfilerRetries"]))
                {
                    var tweets = Search.SearchTweets(searchParams);

                    if (tweets == null)
                    {
                        var latestException = ExceptionHandler.GetLastException();
                        exceptionError = latestException.TwitterDescription;
                        profilingRetries++;
                    }
                    else
                    {
                        ++numberOfRequestsMade;
                        exceptionError = "";
                        foreach (var tweet in tweets)
                            tweetsDateTime.Add(tweet.CreatedAt);

                        break;
                    }
                }
            });

            lastRequest = DateTime.Now;
            nextPossibleRequest = DateTime.Now.AddMilliseconds(Convert.ToInt32(ConfigurationManager.AppSettings["TwitterProfilerRateLimitMs"]));

            double returnFrequency = double.Epsilon;

            if (tweetsDateTime.Count == 0 || tweetsDateTime.Count == 1)
            {
                if (exceptionError != "") { returnFrequency = (int)InvalidReturns.Exception; }
                profiledQueries.Add(query, returnFrequency);
                Console.WriteLine("Query [{0}] has frequency {1}", query, returnFrequency);
                return returnFrequency;
            }
            
            tweetsDateTime.Sort();
            TimeSpan timeDiffBetweenTweets = tweetsDateTime.Last().Subtract(tweetsDateTime.First());

            returnFrequency = (tweetsDateTime.Count-1)/((Math.Abs(timeDiffBetweenTweets.TotalMilliseconds)+1)/1000.0);
            profiledQueries.Add(query, returnFrequency);

            Console.WriteLine("Query [{0}] has frequency {1}", query, returnFrequency);
            return returnFrequency;
        }
        public ITwitterCredentials GenerateToken(IAuthenticationToken authToken)
        {
            var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_verifier", authToken.VerifierCode, true, true, false);

            try
            {
                var authHandler = new AuthHttpHandler(callbackParameter, authToken);
                var response = _twitterRequestHandler.ExecuteQuery(Resources.OAuthRequestAccessToken, HttpMethod.POST, authHandler, 
                    new TwitterCredentials(authToken.ConsumerCredentials));

                if (response == null)
                {
                    return null;
                }

                Match responseInformation = Regex.Match(response, "oauth_token=(?<oauth_token>(?:\\w|\\-)*)&oauth_token_secret=(?<oauth_token_secret>(?:\\w)*)&user_id=(?<user_id>(?:\\d)*)&screen_name=(?<screen_name>(?:\\w)*)");

                var credentials = new TwitterCredentials();
                credentials.AccessToken = responseInformation.Groups["oauth_token"].Value;
                credentials.AccessTokenSecret = responseInformation.Groups["oauth_token_secret"].Value;
                credentials.ConsumerKey = authToken.ConsumerKey;
                credentials.ConsumerSecret = authToken.ConsumerSecret;

                return credentials;
            }
            catch (TwitterException ex)
            {
                LogExceptionOrThrow(ex);
            }

            return null;
        }
Beispiel #6
0
        public void authenticateApp()
        {
            if(this.userCredentials == null)
            {
                // Create a new set of credentials for the application.
                var appCredentials = new TwitterCredentials(this.ConsumerKey, this.ConsumerSecret);

                // Init the authentication process and store the related `AuthenticationContext`.
                this.authenticationContext = AuthFlow.InitAuthentication(appCredentials);

                // Go to the URL so that Twitter authenticates the user and gives him a PIN code.
                Process.Start(authenticationContext.AuthorizationURL);
            }
        }
        private void StartTask(List<string> streamingQueries, List<string> streamingQueriesIDs, List<ArrayList<ArrayList<QueryToken>>> searchTokens, List<string> searchQueriesIDs, Dictionary<string, string> streamingQueryEntities, List<string> authData)
        {
            if (authData.Count != 4)
            {
                StreamLogger.writeErrorToConsoleAndExit(streamTaskId, String.Format("Twitter authentication data requires exactly 4 values, {0} given", authData.Count),
                    "TwitterStreamingDacq.StreamTask", "StartTask");
            }
            
            if (streamingQueries.Count != streamingQueriesIDs.Count)
            {
                StreamLogger.writeErrorToConsoleAndExit(streamTaskId, String.Format("Length of queries {0} does not mach the length of queries IDs {1}", streamingQueries.Count, streamingQueriesIDs.Count),
                    "TwitterStreamingDacq.StreamTask", "StartTask");
            }

            var credentials = new TwitterCredentials(
                authData[0], // ConsumerKey
                authData[1], // ConsumerSecret
                authData[2], // WorkerToken
                authData[3]  // WorkerSecret
            );

            filteredStream = Stream.CreateFilteredStream(credentials);

            // match queries only on the tweets text
            filteredStream.MatchOn = MatchOn.TweetText;

            for (int i = 0; i < streamingQueries.Count; ++i)
            {
                string track = streamingQueries[i];
                string streamingQueryID = streamingQueriesIDs[i];
                string queryIDbase = StreamConfigParser.ExtractQueryIDBase(streamingQueryID);
                string entity = "";
                if (streamingQueryEntities.ContainsKey(queryIDbase))
                    entity = streamingQueryEntities[queryIDbase];

                filteredStream.AddTrack(track, tweet =>
                {
                    // triggered when this query gets a tweet

                    StreamToSearchAssigner ssa = new StreamToSearchAssigner();
                    List<string> matchingSearchIds = ssa.AssignStreamToSearch(tweet, streamingQueryID, searchTokens, searchQueriesIDs);

                    if (matchingSearchIds.Count > 0)
                    {
                        // check if json tweet matches the retrieved tweet
                        var jsonConvert = TweetinviContainer.Resolve<IJsonObjectConverter>();
                        var tweetDTO = jsonConvert.DeserializeObject<ITweetDTO>(lastTweetJson);
                        var tweetFromJson = Tweet.GenerateTweetFromDTO(tweetDTO);

                        bool matchingId = tweetFromJson.IdStr == tweet.IdStr;

                        if (tweet.IsTweetDestroyed)
                            CreateTweetDeleteNotification(tweet);

                        // if the recieved json of the tweet does not match the tracked tweet skip it and create a warning
                        if (tweetFromJson.IdStr == tweet.IdStr)
                        {
                            // match entity
                            if (String.IsNullOrEmpty(entity))
                            {
                                StreamLogger.writeMsgToConsole(streamTaskId, String.Format("Entity for sreaming query {0} could not be found! Entity set to {1}.", streamingQueryID, queryIDbase), StreamLogger.msgType.Warning,
                                   "TwitterStreamingDacq.StreamTask", "StartTask.AddTrack", true);
                                entity = queryIDbase;
                            }

                            // save to nest db
                            PutStreamedTweetToNest(entity, tweet, lastTweetJson);
                        }
                        else
                        {
                            StreamLogger.writeMsgToConsole(streamTaskId, "Retrived JSON tweet and matched tweet do not match. Skipping this tweet", StreamLogger.msgType.Warning,
                               "TwitterStreamingDacq.StreamTask", "StartTask.AddTrack", true);
                        }
                    }
                });
            }

            filteredStream.JsonObjectReceived += (sender, args) =>
            {
                lastTweetJson = args.Json;
            };

            filteredStream.DisconnectMessageReceived += (senderTwt, argsTwt) =>
            {
                StreamLogger.writeMsgToConsole(streamTaskId, String.Format("DisconnectMessageReceived! {0}", argsTwt.DisconnectMessage), StreamLogger.msgType.Warning,
                   "TwitterStreamingDacq.StreamTask", "StartTask.DisconnectMessageReceived", true);
            };

            // notifies that stream attempts to access more than 1% of the total tweets published
            filteredStream.LimitReached += (sender, args) =>
            {
                StreamLogger.writeMsgToConsole(streamTaskId, String.Format("Streaming limit reached! {0} tweets were not received from the start of streaming.",
                    args.NumberOfTweetsNotReceived), StreamLogger.msgType.Warning,
                   "TwitterStreamingDacq.StreamTask", "StartTask.LimitReached", true);
            };


            //  notifies that a tweet has been deleted
            filteredStream.TweetDeleted += (sender, args) =>
            {
                PutDeletedNotificationToNest(args.TweetDeletedInfo);
            };

            filteredStream.StreamStarted += (sender, args) =>
            {
                StreamLogger.writeMsgToConsole(streamTaskId, "Streaming started", StreamLogger.msgType.Empty, "", "", true);
            };

            // let us manage the exceptions from Tweetinvi
            ExceptionHandler.SwallowWebExceptions = false;

            // streaming has stopped, check for exception, log it, restart streaming
            filteredStream.StreamStopped += (sender, args) =>
            {
                var disconnectMessage = args.DisconnectMessage;
                var exception = args.Exception;

                StreamLogger.writeMsgToConsole(streamTaskId, "Streaming stopped", StreamLogger.msgType.Empty,
                   "", "", true);

                if (disconnectMessage != null)
                    StreamLogger.writeMsgToConsole(streamTaskId, String.Format("Disconnect message: {0}", disconnectMessage), StreamLogger.msgType.Error,
                       "TwitterStreamingDacq.StreamTask", "StartTask.StreamStopped", true);

                if (exception != null)
                    StreamLogger.writeMsgToConsole(streamTaskId, String.Format("Exception message: {0}", exception.Message), StreamLogger.msgType.Error,
                       "TwitterStreamingDacq.StreamTask", "StartTask.StreamStopped", true);
            };

            DateTime streamStartedTime = new DateTime();
            int numOfStreamRestarts = 0;
            int numOfMaxStreamRestarts = Convert.ToInt32(ConfigurationManager.AppSettings["StreamTimeoutRestarts"]);
            if (numOfMaxStreamRestarts < 0)
                numOfMaxStreamRestarts = Int32.MaxValue;

            // start streaming
            while (true)
            {
                streamStartedTime = DateTime.Now;

                try
                {
                    filteredStream.StartStreamMatchingAnyCondition();
                }
                catch (Exception exception)
                {
                    StreamLogger.writeMsgToConsole(streamTaskId, String.Format("Exception message: {0}", exception.Message), StreamLogger.msgType.Error,
                       "TwitterStreamingDacq.StreamTask", "StartTask.StartStreamLoop", true);
                    Console.WriteLine(exception.StackTrace);
                }                
            
                // twitter hates too many streaming restarts in a short period of time, so wait a little bit before restarting
                Thread.Sleep(15000);

                // if there are too many restarts in a short period shutdown this loop altogether
                if (DateTime.Now.Subtract(streamStartedTime).TotalSeconds < 60)
                    numOfStreamRestarts++;
                else
                    numOfStreamRestarts = 0;

                if (numOfStreamRestarts >= numOfMaxStreamRestarts)
                {
                    StreamLogger.writeErrorToConsoleAndExit(streamTaskId, "Stream  stopped completely due to too many restarts",
                       "TwitterStreamingDacq.StreamTask", "StartTask.StartStreamLoop");
                    break;
                }
            }
        }