1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB

using Microsoft.AspNetCore.Mvc;
using PhilExampleCrawler.Common.Models;
using PhilExampleCrawler.Core;
using PhilExampleCrawler.DataBase;
using PhilExampleCrawler.DataBase.Models;
using WebAPI.Interfaces;
namespace WebAPI.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
public class AdminController : ControllerBase
{
private readonly ILogger<AdminController> _logger;
private readonly ICrawlScheduler<List<Insertion>> _scheduler;
private readonly ITelegramDistributer _telegram;
private readonly HttpClient _httpClient;
private static bool _isInit = false;
public AdminController(ILogger<AdminController> logger,
IHttpClientFactory httpClientFactory,
ITelegramDistributer telegram,
ICrawlScheduler<List<Insertion>> scheduler)
{
_logger = logger;
_httpClient = httpClientFactory.CreateClient();
_scheduler = scheduler;
_telegram = telegram;
}
[HttpGet(Name = "Refetch")]
public async Task<string> GetAsync(string pw, string? creds)
{
if (pw != "examplepw")
return "unauthorized";
if (string.IsNullOrEmpty(creds))
creds = "+491234567890";
return await _telegram.RefetchCredentialsAsync(creds);
}
[HttpGet(Name = "StartCrawler")]
public async Task<string> GetStartCrawlerAsync(string pw)
{
if (pw != "examplepw" || _isInit)
return "unauthorized";
_isInit = true;
CrawlSessionAccess sessDB = new(new());
List<DB_CrawlSession> allSess = await sessDB.GetAllCrawlSessionsAsync();
foreach(var sess in allSess)
{
var s = sess.ToCommon();
if (s != null)
_scheduler.AddTask(s.ID, new BaseCrawler_Best(s, _httpClient).CrawlAsync);
}
return "crawler started";
}
}
}