mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
begin work on ScrapeService
This commit is contained in:
parent
e473c18241
commit
7f980b76b4
5 changed files with 70 additions and 2 deletions
|
|
@ -2,7 +2,7 @@
|
|||
"profiles": {
|
||||
"FilterLists.Api": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchBrowser": false,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
|
|
|
|||
22
src/FilterLists.Api/V1/Controllers/ScrapeController.cs
Normal file
22
src/FilterLists.Api/V1/Controllers/ScrapeController.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
45
src/FilterLists.Services/Services/ScrapeService.cs
Normal file
45
src/FilterLists.Services/Services/ScrapeService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue