generated from ricardo/MVCLogin
97 lines
3.2 KiB
C#
97 lines
3.2 KiB
C#
using BaseDomain.Results;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Postall.Domain.Dtos;
|
|
using Postall.Domain.Services.Contracts;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Postall.Infra.Services
|
|
{
|
|
public class ChannelYoutubeService: IChannelYoutubeService
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly string _apiKey;
|
|
|
|
public ChannelYoutubeService(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
_apiKey = _configuration["YouTube:ApiKey"];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Busca os detalhes de um canal na API do YouTube
|
|
/// </summary>
|
|
public async Task<Result<ChannelResponse>> GetChannelDetailsAsync(string channelId)
|
|
{
|
|
var youtubeService = new YouTubeBaseServiceWrapper(_apiKey);
|
|
|
|
var channelsRequest = youtubeService.Channels.List("snippet,statistics,contentDetails");
|
|
channelsRequest.Id = channelId;
|
|
|
|
var response = await channelsRequest.ExecuteAsync();
|
|
|
|
if (response.Items.Count == 0)
|
|
throw new Exception($"Canal não encontrado: {channelId}");
|
|
|
|
var channel = response.Items[0];
|
|
|
|
return new ChannelResponse
|
|
{
|
|
Id = channel.Id,
|
|
Title = channel.Snippet.Title,
|
|
Description = channel.Snippet.Description,
|
|
ThumbnailUrl = channel.Snippet.Thumbnails.Default__.Url,
|
|
PublishedAt = channel.Snippet.PublishedAt.Value,
|
|
SubscriberCount = channel.Statistics.SubscriberCount.GetValueOrDefault(),
|
|
VideoCount = channel.Statistics.VideoCount.GetValueOrDefault()
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Busca um canal pelo nome ou ID
|
|
/// </summary>
|
|
public async Task<Result<List<ChannelResponse>>> SearchChannelsAsync(string query, int maxResults = 5)
|
|
{
|
|
if (string.IsNullOrEmpty(query) || query.Length < 3)
|
|
return new List<ChannelResponse>();
|
|
|
|
var youtubeService = new YouTubeBaseServiceWrapper(_apiKey);
|
|
|
|
var searchRequest = youtubeService.Search.List("snippet");
|
|
searchRequest.Q = query;
|
|
searchRequest.Type = "channel";
|
|
searchRequest.MaxResults = maxResults;
|
|
|
|
var searchResponse = await searchRequest.ExecuteAsync();
|
|
|
|
var results = new List<ChannelResponse>();
|
|
|
|
foreach (var item in searchResponse.Items)
|
|
{
|
|
results.Add(new ChannelResponse
|
|
{
|
|
Id = item.Id.ChannelId,
|
|
Title = item.Snippet.Title,
|
|
Description = item.Snippet.Description,
|
|
ThumbnailUrl = item.Snippet.Thumbnails.Default__.Url,
|
|
PublishedAt = item.Snippet.PublishedAt.Value
|
|
});
|
|
}
|
|
|
|
return results;
|
|
}
|
|
}
|
|
|
|
// Classe auxiliar para dados básicos de canal
|
|
public class ChannelData
|
|
{
|
|
public string Id { get; set; }
|
|
public string Title { get; set; }
|
|
}
|
|
}
|