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 _logger; private readonly ICrawlScheduler> _scheduler; private readonly ITelegramDistributer _telegram; private readonly HttpClient _httpClient; private static bool _isInit = false; public AdminController(ILogger logger, IHttpClientFactory httpClientFactory, ITelegramDistributer telegram, ICrawlScheduler> scheduler) { _logger = logger; _httpClient = httpClientFactory.CreateClient(); _scheduler = scheduler; _telegram = telegram; } [HttpGet(Name = "Refetch")] public async Task 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 GetStartCrawlerAsync(string pw) { if (pw != "examplepw" || _isInit) return "unauthorized"; _isInit = true; CrawlSessionAccess sessDB = new(new()); List 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"; } } }