public async Task <ActionResult <IEnumerable <SearchEntry> > > GetRankCheckRequestSearchEntries(int rankCheckRequestId)
        {
            RankCheckRequest rankCheckRequest = await _rankCheckService.GetRankCheckRequestAsync(rankCheckRequestId);

            if (rankCheckRequest == null)
            {
                return(NotFound());
            }

            return((await _rankCheckService.GetSearchEntriesAsync(rankCheckRequestId)).ToList());
        }
        /// <summary>
        /// Creates and queues a rank check request.
        /// </summary>
        /// <param name="rankCheckRequest">The parameters of the rank check request to create with.</param>
        /// <returns>The persisted rank check request.</returns>
        public async Task <RankCheckRequest> QueueRankCheckRequestAsync(RankCheckRequest rankCheckRequest)
        {
            rankCheckRequest.Status = RankCheckRequestStatus.InQueue;
            _context.RankCheckRequests.Add(rankCheckRequest);
            await _context.SaveChangesAsync();

            _queue.QueueBackgroundWorkItem(async(token, provider) =>
            {
                RankCheckRequest updatedRankCheckRequest;
                var context = provider.GetService <RankCheckerDbContext>();

                updatedRankCheckRequest        = await context.RankCheckRequests.Where(x => x.Id == rankCheckRequest.Id).FirstAsync();
                updatedRankCheckRequest.Status = RankCheckRequestStatus.InProgress;
                context.Attach(updatedRankCheckRequest);
                context.Entry(updatedRankCheckRequest).Property(x => x.StatusId).IsModified = true; // Mark the specific field as modified to only update that field
                await context.SaveChangesAsync();
                try
                {
                    var scrapingRankCheckerFactory           = provider.GetService <IRankCheckerFactory>();
                    IRankChecker rankChecker                 = scrapingRankCheckerFactory.CreateRankChecker(updatedRankCheckRequest);
                    List <SearchEntry> relevantSearchEntries = await rankChecker.ExtractRankEntriesAsync();
                    if (relevantSearchEntries != null && relevantSearchEntries.Count > 0)
                    {
                        await context.SearchEntries.AddRangeAsync(relevantSearchEntries);
                    }

                    updatedRankCheckRequest.Status = RankCheckRequestStatus.Completed;
                    context.Attach(updatedRankCheckRequest);
                    context.Entry(updatedRankCheckRequest).Property(x => x.StatusId).IsModified = true;
                    await context.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    updatedRankCheckRequest.Status       = RankCheckRequestStatus.Error;
                    updatedRankCheckRequest.ErrorMessage = ex.Message;
                    context.Attach(updatedRankCheckRequest);
                    context.Entry(updatedRankCheckRequest).Property(x => x.StatusId).IsModified     = true;
                    context.Entry(updatedRankCheckRequest).Property(x => x.ErrorMessage).IsModified = true;
                    await context.SaveChangesAsync();
                }
            });

            return(rankCheckRequest);
        }
 /// <summary>
 /// Creates a scraping rank checker
 /// based on a set of parameters.
 /// </summary>
 /// <param name="rankCheckRequest">The model detailing on the rank check information and parameters to configure the rank check.</param>
 /// <returns></returns>
 public IRankChecker CreateRankChecker(RankCheckRequest rankCheckRequest)
 {
     if (string.IsNullOrWhiteSpace(rankCheckRequest.SearchUrl))
     {
         throw new InvalidOperationException($"To create a rank checker, the SearchUrl must be provided with the rank check request.");
     }
     if (rankCheckRequest.SearchUrl.Contains("www.google.com/search"))
     {
         return(new GoogleScrapingRankChecker(rankCheckRequest, Client));
     }
     else if (rankCheckRequest.SearchUrl.Contains("www.bing.com/search"))
     {
         return(new BingScrapingRankChecker(rankCheckRequest, Client));
     }
     else if (rankCheckRequest.SearchUrl.Contains("au.search.yahoo.com/search"))
     {
         return(new YahooScrapingRankChecker(rankCheckRequest, Client));
     }
     throw new NotSupportedException($"No rank checker could be created from the given URL of '{rankCheckRequest.SearchUrl}'.");
 }
        public void TestYahooRankChecker()
        {
            var rankCheckRequest = new RankCheckRequest()
            {
                SearchUrl      = "au.search.yahoo.com/search",
                MaximumRecords = 100,
                StatusId       = RankCheckRequestStatus.InQueue.Id,
                TermToSearch   = "Online Title Search",
                WebsiteUrl     = "www.infotrack.com.au",
                CreatedAt      = DateTime.Now,
                LastUpdatedAt  = DateTime.Now
            };

            IRankCheckerFactory factory     = new ScrapingRankCheckerFactory(new HttpClient());
            IRankChecker        rankChecker = factory.CreateRankChecker(rankCheckRequest);

            Assert.IsAssignableFrom <YahooScrapingRankChecker>(rankChecker);

            List <SearchEntry> entries = rankChecker.ExtractRankEntries();

            Assert.True(entries.Count > 0);
        }
 /// <summary>
 /// Instantiates a new rank checker.
 /// </summary>
 /// <param name="rankCheckRequest">The model detailing on the rank check information and parameters to configure the rank check.</param>
 /// <param name="client">The HTTP client, used to scrape the data from search engines with.</param>
 public ScrapingRankChecker(RankCheckRequest rankCheckRequest, HttpClient client)
 {
     RankCheckRequest = rankCheckRequest;
     _client          = client;
 }
Exemple #6
0
        public async Task <ActionResult <RankCheckRequest> > PostRankCheckRequest(RankCheckRequest rankCheckRequest)
        {
            RankCheckRequest persistedRankCheckRequest = await _rankCheckService.QueueRankCheckRequestAsync(rankCheckRequest);

            return(CreatedAtAction("GetRankCheckRequest", new { id = rankCheckRequest.Id }, rankCheckRequest));
        }
 /// <summary>
 /// Instantiates a new rank checker.
 /// </summary>
 /// <param name="rankCheckRequest">The model detailing on the rank check information and parameters to configure the rank check.</param>
 /// <param name="client">The HTTP client, used to scrape the data from search engines with.</param>
 public BingScrapingRankChecker(RankCheckRequest rankCheckRequest, HttpClient client) : base(rankCheckRequest, client)
 {
 }