Esempio n. 1
0
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            TwitterService service = new TwitterService(consumerKeyTextBox.Text, consumerSecretTextBox.Text, accessTokenTextBox.Text, accessTokenSecretTextBox.Text);

            TwitterUser user = service.VerifyCredentials();

            if (service.Response.InnerException != null)
            {
                TwitterError error = service.Deserialize<TwitterError>(service.Response.Response);
                if (!string.IsNullOrEmpty(error.ErrorMessage))
                {
                    e.Result = error; // return the error object on failure
                }
                else
                {
                    e.Result = null; // err, dunno. return null
                }
            }
            else if (user != null)
            {
                e.Result = user; // return user object on success
            }
            else
            {
                e.Result = null; // unknown error
            }
        }
Esempio n. 2
0
        private static Action<TwitterStreamArtifact, TwitterResponse> ProcessTweets(TwitterService service)
        {
            return (tweets, response) =>
                       {
                           if (tweets == null)
                           {
                           }
                           else
                           {
                               try
                               {
                                   var status = service.Deserialize<TwitterStatus>(tweets);
                                   if (status.User != null)
                                   {
                                       Console.WriteLine(string.Format("{0}:{1}\n{2}", status.User.ScreenName, status.Text,status.Id));
                                       if(!CurrentGames.ContainsKey(status.User.Id))
                                       {
                                           
                                           var twitterThread = new TwitterThread(status.Id, service);
                                           twitterThread.OnStateChange += args => tweetsToSend.Enqueue(args);
                                          
                                           CurrentGames.Add(status.User.Id, twitterThread);
                                           
                                       }
                                        var thisgame = CurrentGames[status.User.Id];
                                       
                                       
                                        if(thisgame.IsStarted)
                                        {
                                            var cluelastpattern = @"(\S*$)";
                                            var match = Regex.Match(status.Text, cluelastpattern);

                                            var clue = match.Groups[0].Value;

                                            thisgame.Update(clue,status.Id);
                                            
                                        }else
                                        {
                                            var clue = ParseClueFromTweet(status);
                                            thisgame.Start(clue);
                                        }
                                        
                                   }
                               }
                               catch (Exception ex)
                               {
                                   Console.ForegroundColor = ConsoleColor.DarkBlue;
                                   Console.WriteLine(ex.Message);
                                   Console.ResetColor();
                               }
                               
                           }
                       };
        }
        bool IsFail(IEnumerable<TwitterStatus> statuses, string sendto, string feedname = "none")
        {
            var service = new TwitterService();
            bool result = false;
            string errorMessage = "no error";

            if (TwitterWrapper.LastResponse != null && TwitterWrapper.LastResponse.StatusCode == HttpStatusCode.Unauthorized)
            {
                errorMessage = "401 Unauthorized";
                result = true;
            }
            else
            {
                if (statuses.Count() == 1)
                {
                    TwitterStatus mention = statuses.First();
                    if (mention.Id == 0)
                    {
                        errorMessage = "Unsuccessfull deserialization";
                        result = true;
                    }

                    var error = service.Deserialize<TwitterError>(statuses.First());
                    if (!string.IsNullOrEmpty(error.ErrorMessage))
                    {
                        errorMessage = error.ErrorMessage;
                        result = true;
                    }
                }
            }

            if (result)
            {
                BotMethods.SendMessage(SendType.Message, sendto, "Error on feed '{0}': {1}".Fill(feedname, errorMessage));
            }

            return result;
        }