Exemplo n.º 1
0
        public async Task InitTweetViewModel()
        {
            var auth = new ApplicationOnlyAuthorizer()
            {
                CredentialStore = new InMemoryCredentialStore
                {
                    ConsumerKey = "dYNbMI3KFn4zFrHIaAKIWCFYQ",
                    ConsumerSecret = "mQhDSmPov7bbJXb8jPwaVghBbUbgELpQqyHG2QRyf89CsQWKyb",
                },
            };
            await auth.AuthorizeAsync();

            var ctx = new TwitterContext(auth);

            var searchResponse = await
                (from search in ctx.Search
                 where search.Type == SearchType.Search &&
                       search.Query == "\"LINQ to Twitter\""
                 select search)
                .SingleAsync();

            Tweets =
                (from tweet in searchResponse.Statuses
                 select new Tweet
                 {
                     StatusID = tweet.StatusID,
                     ScreenName = tweet.User.ScreenNameResponse,
                     Text = tweet.Text,
                     ImageUrl = tweet.User.ProfileImageUrl
                 })
                .ToList();
        }
    public async Task LoadTweetsCommand(string search)
    {
      if (IsBusy)
        return;

      IsBusy = true;
      try
      {

        Tweets.Clear();
        var auth = new ApplicationOnlyAuthorizer()
        {
          CredentialStore = new InMemoryCredentialStore
          {
            ConsumerKey = "ZTmEODUCChOhLXO4lnUCEbH2I",
            ConsumerSecret = "Y8z2Wouc5ckFb1a0wjUDT9KAI6DUat5tFNdmIkPLl8T4Nyaa2J",
          },
        };
        await auth.AuthorizeAsync();

        var twitterContext = new TwitterContext(auth);
       
        var queryResponse = await
          (from tweet in twitterContext.Search
           where tweet.Type == SearchType.Search &&
             tweet.Query == search &&
             tweet.Count == 100
           select tweet).SingleOrDefaultAsync();

        if (queryResponse == null || queryResponse.Statuses == null)
          return;


        var tweets =
          (from tweet in queryResponse.Statuses
           select new Tweet
           {
             StatusID = tweet.StatusID,
             ScreenName = tweet.User.ScreenNameResponse,
             Text = tweet.Text,
             CurrentUserRetweet = tweet.CurrentUserRetweet,
             CreatedAt = tweet.CreatedAt,
             Url = string.Format("https://m.twitter.com/{0}/status/{1}", tweet.User.ScreenNameResponse, tweet.StatusID),
             Image = (tweet.RetweetedStatus != null && tweet.RetweetedStatus.User != null ?
                            tweet.RetweetedStatus.User.ProfileImageUrl.Replace("http://", "https://") : tweet.User.ProfileImageUrl.Replace("http://", "https://"))
           }).ToList();

        foreach (var tweet in tweets)
        {
          Tweets.Add(tweet);
        }
      }
      catch
      {
      }

      IsBusy = false;
    }
        private async Task InitializeTwitterContextAsync()
        {
            var auth = new ApplicationOnlyAuthorizer
            {
                CredentialStore = new SingleUserInMemoryCredentialStore
                {
                    ConsumerKey = "wamG84e3GpkPjVp08UwsfAZIG",
                    ConsumerSecret = "9lCkCpXJ2FJFINs7329p06alOowqvWNJ2wwFPSTP9MaNu1xGyL"
                }
            };

            await auth.AuthorizeAsync().ConfigureAwait(false);
            Context = new TwitterContext(auth);
        }
Exemplo n.º 4
0
 public static async Task<ApplicationOnlyAuthorizer> Authorize()
 {
     throw new NotImplementedException("Enter API key & secret below");
     var auth = new ApplicationOnlyAuthorizer
     {
         CredentialStore = new InMemoryCredentialStore()
         {
             ConsumerKey = "",
             ConsumerSecret = ""
         }
     };
     await auth.AuthorizeAsync();
     return auth;
 }
		public async static Task<IAuthorizer> GetAuth()
		{
			var auth = new ApplicationOnlyAuthorizer
			{
				CredentialStore = new InMemoryCredentialStore()
				{
					ConsumerKey = Secrets.twitterConsumerKey,
					ConsumerSecret = Secrets.twitterConsumerSecret
				}
			};

			await auth.AuthorizeAsync();
			return auth;
		}
Exemplo n.º 6
0
        async private void button1_Click(object sender, EventArgs e)
        {
            var authorizer = new ApplicationOnlyAuthorizer
            {
                CredentialStore = new InMemoryCredentialStore
                {
                    ConsumerKey = "KQrJnO8Sxc6098dm6OMSbQeoU",
                    ConsumerSecret = "gs7cQhxsMnNGukBnYmCoCPRgIXVTwEPDIjnwOfLUBY9smwsGVv"
                }
            };

            await authorizer.AuthorizeAsync();
            var ctx = new TwitterContext(authorizer);

            string searchString = searchText.Text;

            var searchResponse =
                await
                (from search in ctx.Search
                 where search.Type == SearchType.Search &&
                       search.Query == searchString &&
                       search.Count == 100
                 select search)
                .SingleOrDefaultAsync();

            var tweets =
                (from tweet in searchResponse.Statuses
                 select new TweetViewModel
                 {
                     ImageUrl = tweet.User.ProfileImageUrl,
                     ScreenName = tweet.User.ScreenNameResponse,
                     Text = tweet.Text
                 })
                .ToList();

            string result = "";
            foreach(TweetViewModel tweet in tweets)
            {
                result += tweet.Text + "\n";
            }
            label1.Text = result;

        }
Exemplo n.º 7
0
        async void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            var authorizer = new ApplicationOnlyAuthorizer
            {
                CredentialStore = new InMemoryCredentialStore
                {
                    ConsumerKey = "",
                    ConsumerSecret = ""
                }
            };

            await authorizer.AuthorizeAsync();
            var ctx = new TwitterContext(authorizer);

            string searchString = searchText.Text;

            var searchResponse =
                await
                (from search in ctx.Search
                 where search.Type == SearchType.Search &&
                       search.Query == searchString
                 select search)
                .SingleOrDefaultAsync();

            var tweets =
                (from tweet in searchResponse.Statuses
                 select new TweetViewModel
                 {
                     ImageUrl = tweet.User.ProfileImageUrl,
                     ScreenName = tweet.User.ScreenNameResponse,
                     Text = tweet.Text
                 })
                .ToList();

            tweetListView.ItemsSource = new ObservableCollection<TweetViewModel>(tweets);
        }
Exemplo n.º 8
0
        public IHttpActionResult GetUserInfo(string query, int count = 20, string resultType = "recent", string language = null, string geo = null)
        {
            try
            {
                var auth = new ApplicationOnlyAuthorizer
                {
                    CredentialStore = new SingleUserInMemoryCredentialStore
                    {
                        ConsumerKey = TwitterKeys.consumerKey,
                        ConsumerSecret = TwitterKeys.consumerSecret,
                        AccessToken = TwitterKeys.accessToken,
                        AccessTokenSecret = TwitterKeys.accessTokenSecret
                    }
                };

                var authSync = auth.AuthorizeAsync();
                authSync.Wait();

                var twitterCtx = new TwitterContext(auth);

                var resultadoBusca = (from search in twitterCtx.Search
                                      where search.Type == SearchType.Search &&
                                            search.Query == query &&
                                            search.Count == count &&
                                            search.ResultType == ResultType.Recent &&
                                            search.SearchLanguage == "pt"
                                      select search)
                                     .ToList();

                return Ok(resultadoBusca[0].Statuses);
            }
            catch (Exception ex)
            {
                throw new Exception("Não conseguimos processar a requisição. Por favor verifique a conexão com a internet ou tente mais tarde.");
            }
        }
        private async Task<List<Status>> GetLatestsTwittersWithHashtag(string hashtag, long laterThanThisID = 0)
        {
            try
            {

                hashtag = !hashtag.Contains("#") ? "#" + hashtag : hashtag;

                var authorizer = new ApplicationOnlyAuthorizer
                {
                    CredentialStore = new InMemoryCredentialStore
                    {
                        ConsumerKey = Services.Settings["TwitterConsumerKey"],
                        ConsumerSecret = Services.Settings["TwitterConsumerSecret"],
                    }
                };

                await authorizer.AuthorizeAsync();
                var twitterCtx = new TwitterContext(authorizer);

                var query = twitterCtx.Search.Where(x => x.Type == SearchType.Search && x.Query == hashtag && x.Count == 100);

                if (laterThanThisID > 0)
                {
                    query = query.Where(x => x.SinceID == (ulong)laterThanThisID);
                }

                var searchresponse = query.FirstOrDefault();

                return searchresponse.Statuses;

            }
            catch (Exception ex)
            {
                return new List<Status>();
            }
        }
Exemplo n.º 10
0
		private async void ExecuteLoadTweetsCommand ()
		{
			if (IsBusy)
				return;

			IsBusy = true;
			try {

				Tweets.Clear ();
				var auth = new ApplicationOnlyAuthorizer () {
					CredentialStore = new InMemoryCredentialStore {
						ConsumerKey = "ZTmEODUCChOhLXO4lnUCEbH2I",
						ConsumerSecret = "Y8z2Wouc5ckFb1a0wjUDT9KAI6DUat5tFNdmIkPLl8T4Nyaa2J",
					},
				};
				await auth.AuthorizeAsync ();

				var twitterContext = new TwitterContext (auth);

#if !WINDOWS_PHONE
        IQueryable<LinqToTwitter.Status> queryResponse =
					(from tweet in twitterContext.Status
					where tweet.Type == StatusType.User &&
					  tweet.ScreenName == "shanselman" &&
					  tweet.Count == 100 &&
					  tweet.IncludeRetweets == true &&
					  tweet.ExcludeReplies == true
					select tweet);
					
				var queryTweets = queryResponse.ToList ();
				var tweets =
					(from tweet in queryTweets
					select new Tweet {
						StatusID = tweet.StatusID,
						ScreenName = tweet.User.ScreenNameResponse,
						Text = tweet.Text, 
						CurrentUserRetweet = tweet.CurrentUserRetweet, 
						CreatedAt = tweet.CreatedAt
					}).ToList ();
#else

				var tweets =
					await (from tweet in twitterContext.Status
					where tweet.Type == StatusType.User &&
					  tweet.ScreenName == "shanselman" &&
					  tweet.Count == 100 &&
					  tweet.IncludeRetweets == true &&
					  tweet.ExcludeReplies == true
                 select new Tweet
                 {
                   StatusID = tweet.StatusID,
                   ScreenName = tweet.User.ScreenNameResponse,
                   Text = tweet.Text,
                   CurrentUserRetweet = tweet.CurrentUserRetweet,
                   CreatedAt = tweet.CreatedAt
                 }).ToListAsync();

#endif

        foreach (var tweet in tweets) {
					Tweets.Add (tweet);
				}
			} catch (Exception ex) {
				var page = new ContentPage();
				page.DisplayAlert ("Error", "Unable to load twitter.", "OK", null);
			}

			IsBusy = false;
		}
Exemplo n.º 11
0
        public async Task ExecuteLoadTweetsCommand()
        {
            if (IsBusy)
                return;

            IsBusy = true;
            LoadTweetsCommand.ChangeCanExecute();
            var error = false;
            try
            {

                Tweets.Clear();
                var auth = new ApplicationOnlyAuthorizer()
                {
                    CredentialStore = new InMemoryCredentialStore
                    {
                        ConsumerKey = "ZTmEODUCChOhLXO4lnUCEbH2I",
                        ConsumerSecret = "Y8z2Wouc5ckFb1a0wjUDT9KAI6DUat5tFNdmIkPLl8T4Nyaa2J",
                    },
                };
                await auth.AuthorizeAsync();

                var twitterContext = new TwitterContext(auth);

                var queryResponse = await
                  (from tweet in twitterContext.Status
                   where tweet.Type == StatusType.User &&
                     tweet.ScreenName == "shanselman" &&
                     tweet.Count == 100 &&
                     tweet.IncludeRetweets == true &&
                     tweet.ExcludeReplies == true
                   select tweet).ToListAsync();

                var tweets =
                  (from tweet in queryResponse
                   select new Tweet
                   {
                       StatusID = tweet.StatusID,
                       ScreenName = tweet.User.ScreenNameResponse,
                       Text = tweet.Text,
                       CurrentUserRetweet = tweet.CurrentUserRetweet,
                       CreatedAt = tweet.CreatedAt,
                       Image = tweet.RetweetedStatus != null && tweet.RetweetedStatus.User != null ?
                                      tweet.RetweetedStatus.User.ProfileImageUrl.Replace("http://", "https://") : (tweet.User.ScreenNameResponse == "shanselman" ? "scott159.png" : tweet.User.ProfileImageUrl.Replace("http://", "https://"))
                   }).ToList();
                foreach (var tweet in tweets)
                {
                    Tweets.Add(tweet);
                }

                if (Device.OS == TargetPlatform.iOS)
                {
                    // only does anything on iOS, for the Watch
                    DependencyService.Get<ITweetStore>().Save(tweets);
                }



            }
            catch
            {
                error = true;
            }

            if (error)
            {
                var page = new ContentPage();
                await page.DisplayAlert("Error", "Unable to load tweets.", "OK");
            }

            IsBusy = false;
            LoadTweetsCommand.ChangeCanExecute();
        }
Exemplo n.º 12
0
        public async Task<string> Get()
        {
            
            try
            {
                

                var tweets = new List<Tweet>();
                var tweetsObj = cache.GetValue("tweets");

                if(tweetsObj != null)
                    tweets = tweetsObj as List<Tweet>;

                if (tweets == null)
                    tweets = new List<Tweet>();

                if (tweets.Count > 0)
                    return Newtonsoft.Json.JsonConvert.SerializeObject(tweets);

                

                var auth = new ApplicationOnlyAuthorizer()
                {
                    CredentialStore = new InMemoryCredentialStore
                    {
                        ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"],
                        ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"],
                    },
                };
                await auth.AuthorizeAsync();

                var twitterContext = new TwitterContext(auth);

                var queryResponse = await
                    (from tweet in twitterContext.Search
                     where tweet.Type == SearchType.Search &&
                         (tweet.Query == "#XamarinEvolve") &&
                         tweet.Count == 100
                     select tweet).SingleOrDefaultAsync();

                if (queryResponse == null || queryResponse.Statuses == null)
                    return string.Empty;

                tweets =
                    (from tweet in queryResponse.Statuses
                     where tweet.RetweetedStatus.StatusID == 0 && !tweet.PossiblySensitive && !censor.HasCensoredWord(tweet.Text)
                     select new Tweet
                     {
                         TweetedImage = tweet.Entities?.MediaEntities.Count > 0 ? tweet.Entities?.MediaEntities?[0].MediaUrlHttps ?? string.Empty : string.Empty,
                         ScreenName = tweet.User?.ScreenNameResponse ?? string.Empty,
                         Text = tweet.Text,
                         Name = tweet.User.Name,
                         CreatedDate = tweet.CreatedAt,
                         Url = string.Format("https://twitter.com/{0}/status/{1}", tweet.User.ScreenNameResponse, tweet.StatusID),
                         Image = (tweet.RetweetedStatus != null && tweet.RetweetedStatus.User != null ?
                                 tweet.RetweetedStatus.User.ProfileImageUrl.Replace("http://", "https://") : tweet.User.ProfileImageUrl.Replace("http://", "https://"))
                     }).Take(15).ToList();


                cache.Add("tweets", tweets, DateTimeOffset.UtcNow.AddMinutes(1));
               
                return Newtonsoft.Json.JsonConvert.SerializeObject(tweets);
            }
            catch
            {

            }


            return string.Empty;
        }