Beispiel #1
0
        /// <summary>
        /// Creates new Tweet entity from 'replyText' and
        /// populates RepliesTo property with 'replyTo',
        /// and with its previous replies.
        /// </summary>
        /// <param name="replyText"></param>
        /// <param name="user"></param>
        /// <param name="replyTo"></param>
        /// <returns>Created Tweet entity (reply)</returns>
        public async Task <Tweet> AddReplyAsync(string replyText, TwitterCopyUser user, Tweet replyTo)
        {
            var replyFrom = new Tweet
            {
                Text      = replyText,
                User      = user,
                RepliesTo = new List <TweetToTweet>()
            };

            // Add main replyTo
            replyFrom.RepliesTo.Add(new TweetToTweet
            {
                ReplyFrom = replyFrom,
                ReplyTo   = replyTo
            });
            // Add reply to the RepliesTo property of the replyTo tweet
            // so that you reply not to one tweet but
            // to the whole 'conversation'
            foreach (var tweet in replyTo.RepliesTo)
            {
                replyFrom.RepliesTo.Add(new TweetToTweet
                {
                    ReplyFrom = replyFrom,
                    ReplyTo   = tweet.ReplyTo
                });
            }

            _tweetRepository.Add(replyFrom);
            await _tweetRepository.SaveAsync();

            return(replyFrom);
        }
        public async Task <bool> GetTweets()
        {
            string token = await GetAuthorization();

            string[] tags = _config["Tags"].Split(',');

            #region Remover a base de dados de análise, antes de iniciar nova análise
            _userRepository.RemoveAll();
            _tweetRepository.RemoveAll();
            #endregion

            foreach (var tag in tags)
            {
                using (var httpClient = new HttpClient())
                {
                    var url = string.Format(_config["TwitterService:Tweets"], tag);

                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer AAAAAAAAAAAAAAAAAAAAAF2J%2FAAAAAAArvJ6%2BZG122CmG%2FCZguGfkOAloJE%3DNBk4LAg4JgvUMGpFbSpBjcNxKfLJOT1VmvWfIOHXEvQt1SJQez" /*GetTwitterAuthHeader(url)*/);
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");

                    var result = await httpClient.GetAsync(url);

                    var response = await result.Content.ReadAsStringAsync();

                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        var tweets = JsonConvert.DeserializeObject <Dictionary <string, object> >(response)["statuses"];
                        var objs   = tweets as IEnumerable <dynamic>;
                        foreach (var tweet in objs.Select(t => new { CreatedAt = (string)(t["created_at"].ToString()),
                                                                     IdStr = (string)(t["id_str"].ToString()),
                                                                     Entities = (string)(t["entities"].ToString()),
                                                                     User = (string)(t["user"].ToString()) }))
                        {
                            var tweetDTO = new TweetDTO();
                            tweetDTO.CreatedAt = tweet.CreatedAt;

                            var entities = JsonConvert.DeserializeObject <EntityDTO>(tweet.Entities);

                            tweetDTO.Hashtags = new List <HashtagDTO>();

                            foreach (var hashtag in entities.Hashtags)
                            {
                                tweetDTO.Hashtags.Add(new HashtagDTO()
                                {
                                    Text = hashtag.Text
                                });
                            }

                            var user = JsonConvert.DeserializeObject <UserTweetDTO>(tweet.User);

                            tweetDTO.User = _mapper.Map <UserTweetDTO, UserDTO>(user);

                            var map = _mapper.Map <TweetDTO, Tweet>(tweetDTO);
                            map.SetId(Guid.NewGuid());

                            _tweetRepository.Add(map);

                            var mapUser = _mapper.Map <UserDTO, User>(tweetDTO.User);
                            mapUser.SetId(Guid.NewGuid());

                            if (_userRepository.GetById(mapUser.IdStr) == null)
                            {
                                _userRepository.Add(mapUser);
                            }
                        }
                    }
                }
            }

            return(true);
        }