begin work on ScrapeService

This commit is contained in:
Collin M. Barrett 2018-01-26 12:59:31 -06:00
parent e473c18241
commit 7f980b76b4
5 changed files with 70 additions and 2 deletions

View file

@ -2,7 +2,7 @@
"profiles": {
"FilterLists.Api": {
"commandName": "Project",
"launchBrowser": true,
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},

View file

@ -0,0 +1,22 @@
using FilterLists.Services.Services;
using Microsoft.AspNetCore.Mvc;
namespace FilterLists.Api.V1.Controllers
{
public class ScrapeController : BaseController
{
private readonly ScrapeService scrapeService;
public ScrapeController(ScrapeService scrapeService)
{
this.scrapeService = scrapeService;
}
[HttpPost]
public void Index()
{
//TODO: Convert endpoint into scheduled job, don't expose on prod
//scrapeService.Scrape();
}
}
}

View file

@ -10,7 +10,7 @@ public override void Configure(EntityTypeBuilder<Snapshot> entityTypeBuilder)
{
entityTypeBuilder.ToTable("snapshots");
entityTypeBuilder.Property(x => x.Content)
.HasColumnType("TEXT")
.HasColumnType("MEDIUMTEXT")
.IsRequired();
entityTypeBuilder.Ignore(x => x.ModifiedDateUtc);
base.Configure(entityTypeBuilder);

View file

@ -18,6 +18,7 @@ public static void AddFilterListsServices(this IServiceCollection services, ICon
b => b.MigrationsAssembly("FilterLists.Api")));
services.TryAddScoped<SeedService>();
services.TryAddScoped<FilterListService>();
services.TryAddScoped<ScrapeService>();
services.AddAutoMapper();
}
}

View file

@ -0,0 +1,45 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using FilterLists.Data;
using FilterLists.Data.Entities;
namespace FilterLists.Services.Services
{
public class ScrapeService
{
private readonly FilterListsDbContext filterListsDbContext;
public ScrapeService(FilterListsDbContext filterListsDbContext)
{
this.filterListsDbContext = filterListsDbContext;
}
public void Scrape()
{
var list = filterListsDbContext.FilterLists.First();
var snapshot = GetContent(list.ViewUrl);
filterListsDbContext.Snapshots.Add(new Snapshot
{
Content = snapshot.Result,
FilterListId = list.Id
});
filterListsDbContext.SaveChangesAsync();
}
private static async Task<string> GetContent(string url)
{
using (var httpClient = new HttpClient())
{
using (var httpResponseMessage = await httpClient.GetAsync(url))
{
if (httpResponseMessage.IsSuccessStatusCode)
return await httpResponseMessage.Content.ReadAsStringAsync();
}
}
return null;
}
}
}