2023-02-26 09:40:04 -08:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Newsbot.Collector.Database.Repositories;
|
|
|
|
using Newsbot.Collector.Domain.Consts;
|
|
|
|
using Newsbot.Collector.Domain.Dto;
|
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
|
|
using Newsbot.Collector.Domain.Models;
|
2023-03-31 22:49:39 -07:00
|
|
|
using Newsbot.Collector.Services.HtmlParser;
|
2023-02-26 09:40:04 -08:00
|
|
|
|
|
|
|
namespace Newsbot.Collector.Api.Controllers;
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("api/sources")]
|
|
|
|
public class SourcesController : ControllerBase
|
|
|
|
{
|
2023-03-31 22:49:39 -07:00
|
|
|
private readonly IIconsRepository _icons;
|
2023-02-26 09:40:04 -08:00
|
|
|
private readonly ILogger<SourcesController> _logger;
|
2023-03-13 21:57:32 -07:00
|
|
|
|
|
|
|
//private readonly ConnectionStrings _settings;
|
2023-02-26 09:40:04 -08:00
|
|
|
private readonly ISourcesRepository _sources;
|
|
|
|
|
|
|
|
public SourcesController(ILogger<SourcesController> logger, IOptions<ConnectionStrings> settings)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
2023-03-13 21:57:32 -07:00
|
|
|
//_settings = settings.Value;
|
|
|
|
_sources = new SourcesTable(settings.Value.Database);
|
2023-03-31 22:49:39 -07:00
|
|
|
_icons = new IconsTable(settings.Value.Database);
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet(Name = "GetSources")]
|
|
|
|
public IEnumerable<SourceDto> Get(int page)
|
|
|
|
{
|
|
|
|
var res = new List<SourceDto>();
|
|
|
|
var temp = _sources.List(page, 25);
|
2023-03-13 21:57:32 -07:00
|
|
|
foreach (var item in temp) res.Add(SourceDto.Convert(item));
|
2023-02-26 09:40:04 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("by/type")]
|
|
|
|
public IEnumerable<SourceDto> GetByType(string type)
|
|
|
|
{
|
|
|
|
var res = new List<SourceDto>();
|
|
|
|
var temp = _sources.ListByType(type);
|
2023-03-13 21:57:32 -07:00
|
|
|
foreach (var item in temp) res.Add(SourceDto.Convert(item));
|
2023-02-26 09:40:04 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("new/reddit")]
|
2023-03-31 22:49:39 -07:00
|
|
|
public SourceDto NewReddit(string name)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-03-11 10:43:06 -08:00
|
|
|
var res = _sources.GetByNameAndType(name, SourceTypes.Reddit);
|
2023-03-13 21:57:32 -07:00
|
|
|
if (res.ID != Guid.Empty) return SourceDto.Convert(res);
|
2023-03-11 10:43:06 -08:00
|
|
|
|
2023-03-31 22:49:39 -07:00
|
|
|
var uri = new Uri($"https://reddit.com/r/{name}");
|
|
|
|
|
|
|
|
var pageReader = new HtmlPageReader(new HtmlPageReaderOptions
|
|
|
|
{
|
|
|
|
Url = uri.ToString()
|
|
|
|
});
|
|
|
|
pageReader.Parse();
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
var item = _sources.New(new SourceModel
|
|
|
|
{
|
|
|
|
Site = SourceTypes.Reddit,
|
|
|
|
Name = name,
|
|
|
|
Type = SourceTypes.Reddit,
|
|
|
|
Source = "feed",
|
|
|
|
Enabled = true,
|
2023-03-31 22:49:39 -07:00
|
|
|
Url = uri.ToString(),
|
|
|
|
Tags = $"{SourceTypes.Reddit},{name}"
|
2023-02-26 09:40:04 -08:00
|
|
|
});
|
2023-03-31 22:49:39 -07:00
|
|
|
|
|
|
|
// Not all subreddits have an Icon, so we only want to add a record when it has one.
|
|
|
|
if (pageReader.Data.Header.Image != "")
|
|
|
|
_icons.New(new IconModel
|
|
|
|
{
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
FileName = pageReader.Data.Header.Image,
|
|
|
|
SourceId = item.ID
|
|
|
|
});
|
2023-02-26 09:40:04 -08:00
|
|
|
return SourceDto.Convert(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("new/rss")]
|
|
|
|
public SourceDto NewRss(string name, string url)
|
|
|
|
{
|
2023-03-11 10:43:06 -08:00
|
|
|
var res = _sources.GetByNameAndType(name, SourceTypes.Rss);
|
2023-03-13 21:57:32 -07:00
|
|
|
if (res.ID != Guid.Empty) return SourceDto.Convert(res);
|
2023-03-11 10:43:06 -08:00
|
|
|
|
|
|
|
var m = new SourceModel
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
|
|
|
Site = SourceTypes.Rss,
|
|
|
|
Name = name,
|
|
|
|
Type = SourceTypes.Rss,
|
|
|
|
Source = "feed",
|
|
|
|
Enabled = true,
|
|
|
|
Url = url,
|
2023-03-31 22:49:39 -07:00
|
|
|
Tags = $"{SourceTypes.Rss},{name}"
|
2023-03-11 10:43:06 -08:00
|
|
|
};
|
|
|
|
var item = _sources.New(m);
|
2023-02-26 09:40:04 -08:00
|
|
|
return SourceDto.Convert(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("new/youtube")]
|
2023-03-31 22:49:39 -07:00
|
|
|
public SourceDto NewYoutube(string url)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-03-31 22:49:39 -07:00
|
|
|
var res = _sources.GetByUrl(url);
|
2023-03-13 21:57:32 -07:00
|
|
|
if (res.ID != Guid.Empty) return SourceDto.Convert(res);
|
2023-03-11 10:43:06 -08:00
|
|
|
|
2023-03-31 22:49:39 -07:00
|
|
|
var htmlClient = new HtmlPageReader(new HtmlPageReaderOptions
|
|
|
|
{
|
|
|
|
Url = url
|
|
|
|
});
|
|
|
|
htmlClient.Parse();
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
var item = _sources.New(new SourceModel
|
|
|
|
{
|
|
|
|
Site = SourceTypes.YouTube,
|
|
|
|
Type = SourceTypes.YouTube,
|
2023-03-31 22:49:39 -07:00
|
|
|
Name = htmlClient.Data.Header.Title,
|
2023-02-26 09:40:04 -08:00
|
|
|
Source = "feed",
|
2023-03-31 22:49:39 -07:00
|
|
|
Url = "feed",
|
2023-02-26 09:40:04 -08:00
|
|
|
Enabled = true,
|
2023-03-31 22:49:39 -07:00
|
|
|
Tags = $"{SourceTypes.YouTube},{htmlClient.Data.Header.Title}",
|
|
|
|
YoutubeId = htmlClient.Data.Header.YoutubeChannelID ?? ""
|
|
|
|
});
|
|
|
|
|
|
|
|
_icons.New(new IconModel
|
|
|
|
{
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
FileName = htmlClient.Data.Header.Image,
|
|
|
|
SourceId = item.ID
|
2023-02-26 09:40:04 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
return SourceDto.Convert(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("new/twitch")]
|
|
|
|
public SourceDto NewTwitch(string name)
|
|
|
|
{
|
2023-03-11 10:43:06 -08:00
|
|
|
var res = _sources.GetByNameAndType(name, SourceTypes.Twitch);
|
2023-03-13 21:57:32 -07:00
|
|
|
if (res.ID != Guid.Empty) return SourceDto.Convert(res);
|
2023-03-11 10:43:06 -08:00
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
var item = _sources.New(new SourceModel
|
|
|
|
{
|
|
|
|
Site = SourceTypes.Twitch,
|
|
|
|
Type = SourceTypes.Twitch,
|
|
|
|
Name = name,
|
|
|
|
Url = $"https://twitch.tv/{name}",
|
|
|
|
Source = "api",
|
|
|
|
Enabled = true,
|
2023-03-31 22:49:39 -07:00
|
|
|
Tags = $"{SourceTypes.Twitch},{name}"
|
2023-02-26 09:40:04 -08:00
|
|
|
});
|
|
|
|
return SourceDto.Convert(item);
|
|
|
|
}
|
|
|
|
|
2023-03-31 22:49:39 -07:00
|
|
|
[HttpPost("new/github")]
|
|
|
|
public SourceDto NewGithub(string url)
|
|
|
|
{
|
|
|
|
if (!url.Contains("github.com")) return new SourceDto();
|
|
|
|
|
|
|
|
var res = _sources.GetByUrl(url);
|
|
|
|
if (res.ID != Guid.Empty) return SourceDto.Convert(res);
|
|
|
|
|
|
|
|
var slice = url.Split('/');
|
|
|
|
|
|
|
|
var pageReader = new HtmlPageReader(new HtmlPageReaderOptions
|
|
|
|
{
|
|
|
|
Url = url
|
|
|
|
});
|
|
|
|
pageReader.Parse();
|
|
|
|
|
|
|
|
var item = _sources.New(new SourceModel
|
|
|
|
{
|
|
|
|
Site = SourceTypes.GitHub,
|
|
|
|
Type = SourceTypes.GitHub,
|
|
|
|
Name = $"{slice[3]}/{slice[4]}",
|
|
|
|
Url = url,
|
|
|
|
Source = "feed",
|
|
|
|
Enabled = true,
|
|
|
|
Tags = $"{SourceTypes.GitHub}, {slice[3]}, {slice[4]}"
|
|
|
|
});
|
|
|
|
|
|
|
|
_icons.New(new IconModel
|
|
|
|
{
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
FileName = pageReader.Data.Header.Image,
|
|
|
|
SourceId = item.ID
|
|
|
|
});
|
|
|
|
|
|
|
|
return SourceDto.Convert(item);
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
[HttpGet("{id}")]
|
|
|
|
public SourceDto GetById(Guid id)
|
|
|
|
{
|
|
|
|
var item = _sources.GetByID(id);
|
|
|
|
return SourceDto.Convert(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("{id}/disable")]
|
|
|
|
public void Disable(Guid id)
|
|
|
|
{
|
|
|
|
_sources.Disable(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("{id}/enable")]
|
|
|
|
public void Enable(Guid id)
|
|
|
|
{
|
|
|
|
_sources.Enable(id);
|
|
|
|
}
|
|
|
|
}
|