Пример #1
0
        public IActionResult MoreById(string id)
        {
            bool          invalidId = id == null || id == "";
            SelectedTweet st        = _context.SelectedTweets.FirstOrDefault(x => x.TweetId == id);

            if (invalidId || st == null)
            {
                return(BadRequest());
            }

            return(Ok(new TweetIdAndStateVM[] { new TweetIdAndStateVM(st) }));
        }
Пример #2
0
        public IActionResult Update([FromBody] UpdatedStateBM[] newStates)
        {
            if (newStates == null || newStates.Length == 0)
            {
                return(BadRequest());
            }

            foreach (UpdatedStateBM u in newStates)
            {
                SelectedTweet s = _context.SelectedTweets.FirstOrDefault(x => x.TweetId == u.TweetId);
                if (s != null)
                {
                    s.CurrentState = u.NewState;
                    s.CheckedAt    = DateTime.UtcNow;
                }
                _context.SaveChanges();
            }
            return(Ok());
        }
Пример #3
0
        /// <summary>
        /// Checks the tweets for a given contributor, that is, gets his last 50 tweets,
        /// selects the five best ( those with most likes/retweets) and if they are not in the
        /// database, add them, if they were already added, update them.
        /// </summary>
        /// <param name="contributor">The Contributor whose tweets will be checked.</param>
        public async Task CheckTweets(Contributor contributor)
        {
            const int LAST_N_TWEETS = 50;

            Tweet[] tweets = null;

            // 1) Try and get the last 50 tweets:
            string screen_name = contributor?.TwitterUser?.screen_name;

            if (screen_name == null)
            {
                throw new ArgumentNullException();
            }
            try
            {
                tweets = await _twitterService.GetTweets(screen_name, LAST_N_TWEETS);
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
            }


            using (var serviceScope = _services.CreateScope())
            {
                var         context = serviceScope.ServiceProvider.GetRequiredService <MyDbContext>();
                Contributor c       = context.Contributors.Single(p => p.ContributorId == contributor.ContributorId);

                //2) always update contributors lastChecked date, even if one fails to get tweets:
                c.LastUpdate = DateTime.UtcNow;

                if (tweets != null && tweets.Length > 0)
                {
                    const int HOW_MANY_BEST_TWEETS = 5;

                    //3) if tweets were successfully retrieved, select the five best ones:
                    Tweet[] best = tweets
                                   .OrderByDescending(t => t.favorite_count + t.retweet_count)
                                   .Where(t => t.favorite_count + t.retweet_count > 0)
                                   .Take(HOW_MANY_BEST_TWEETS).ToArray();

                    context.Entry(c).Collection(x => x.SelectedTweets).Load();
                    string[] existingIds = c.SelectedTweets.Select(t => t.TweetId).ToArray();

                    // 4) Add selected tweets to database, as SelectedTweets:
                    foreach (var t in best)
                    {
                        if (existingIds.Contains(t.id_str))
                        {
                            //Update Tweet's selectedAt date, if it already exists and it is still unchecked:
                            SelectedTweet selectedTweetedFromDb = c.SelectedTweets.First(x => x.TweetId == t.id_str);
                            if (selectedTweetedFromDb.CurrentState == PublishingState.Unchecked)
                            {
                                selectedTweetedFromDb.SelectedAt = DateTime.UtcNow;
                                context.Update(t);
                            }
                        }
                        else
                        {
                            //Add if it does not exist yet:
                            SelectedTweet newSelectedTweet = new SelectedTweet();
                            newSelectedTweet.Tweet      = t;
                            newSelectedTweet.SelectedAt = DateTime.UtcNow;
                            c.SelectedTweets.Add(newSelectedTweet);
                        }
                    }
                }

                await context.SaveChangesAsync();
            }
        }