add youtube api

This commit is contained in:
Jicheng Lu 2024-03-20 17:59:38 -05:00
parent 94d2b6aaa6
commit 7633d881b0
5 changed files with 129 additions and 53 deletions

View file

@ -0,0 +1,21 @@
namespace BotSharp.Abstraction.Google.Models;
public class GoogleVideoResult
{
public string Kind { get; set; }
public IList<VideoItem> Items { get; set; } = new List<VideoItem>();
}
public class VideoItem
{
public string Kind { get; set; }
[JsonPropertyName("etag")]
public string Etag { get; set; }
public VideoItemId Id { get; set; }
}
public class VideoItemId
{
public string Kind { get; set; }
public string VideoId { get; set; }
}

View file

@ -3,7 +3,22 @@ namespace BotSharp.Abstraction.Google.Settings;
public class GoogleApiSettings
{
public string ApiKey { get; set; }
public MapSettings Map { get; set; }
public YoutubeSettings Youtube { get; set; }
}
public class MapSettings
{
public string Endpoint { get; set; }
public string Language { get; set; }
public string Components { get; set; }
}
public class YoutubeSettings
{
public string Endpoint { get; set; }
public string Part { get; set; }
public string RegionCode { get; set; }
public string Language { get; set; }
}

View file

@ -1,50 +0,0 @@
using BotSharp.Abstraction.Google.Models;
using BotSharp.Abstraction.Google.Settings;
using BotSharp.Abstraction.Options;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
public class AddressController : ControllerBase
{
private readonly IServiceProvider _services;
private readonly BotSharpOptions _options;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger _logger;
public AddressController(IServiceProvider services,
IHttpClientFactory httpClientFactory,
BotSharpOptions options)
{
_services = services;
_options = options;
_httpClientFactory = httpClientFactory;
}
[HttpGet("/address/options")]
public async Task<GoogleAddressResult> GetAddressOptions([FromQuery] string address)
{
var result = new GoogleAddressResult();
try
{
var settings = _services.GetRequiredService<GoogleApiSettings>();
using var client = _httpClientFactory.CreateClient();
var url = $"{settings.Endpoint}?key={settings.ApiKey}&" +
$"components={settings.Components}&" +
$"language={settings.Language}&" +
$"address={address}";
var response = await client.GetAsync(url);
var responseStr = await response.Content.ReadAsStringAsync();
result = JsonSerializer.Deserialize<GoogleAddressResult>(responseStr, _options.JsonSerializerOptions);
}
catch (Exception ex)
{
_logger.LogError($"Error when calling google geocoding api... ${ex.Message}");
}
return result;
}
}

View file

@ -0,0 +1,82 @@
using BotSharp.Abstraction.Google.Models;
using BotSharp.Abstraction.Google.Settings;
using BotSharp.Abstraction.Options;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
public class GoogleController : ControllerBase
{
private readonly IServiceProvider _services;
private readonly BotSharpOptions _options;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger _logger;
public GoogleController(IServiceProvider services,
IHttpClientFactory httpClientFactory,
BotSharpOptions options)
{
_services = services;
_options = options;
_httpClientFactory = httpClientFactory;
}
[HttpGet("/address/options")]
public async Task<GoogleAddressResult> GetAddressOptions([FromQuery] string address)
{
var result = new GoogleAddressResult();
try
{
var settings = _services.GetRequiredService<GoogleApiSettings>();
using var client = _httpClientFactory.CreateClient();
var url = $"{settings.Map.Endpoint}?key={settings.ApiKey}&" +
$"components={settings.Map.Components}&" +
$"language={settings.Map.Language}&" +
$"address={address}";
var response = await client.GetAsync(url);
var responseStr = await response.Content.ReadAsStringAsync();
result = JsonSerializer.Deserialize<GoogleAddressResult>(responseStr, _options.JsonSerializerOptions);
}
catch (Exception ex)
{
_logger.LogError($"Error when calling google geocoding api... ${ex.Message}");
}
return result;
}
[HttpGet("/video/options")]
public async Task<GoogleVideoResult> GetVideoOptions([FromQuery] string query, [FromQuery] int? maxNumOfResults)
{
var result = new GoogleVideoResult();
try
{
var settings = _services.GetRequiredService<GoogleApiSettings>();
using var client = _httpClientFactory.CreateClient();
var url = $"{settings.Youtube.Endpoint}?key={settings.ApiKey}&" +
$"part={settings.Youtube.Part}&" +
$"relevanceLanguage={settings.Youtube.Language}&" +
$"regionCode={settings.Youtube.RegionCode}&" +
$"q={query}";
if (maxNumOfResults.HasValue && maxNumOfResults > 0)
{
url += $"&maxResults={maxNumOfResults}";
}
var response = await client.GetAsync(url);
var responseStr = await response.Content.ReadAsStringAsync();
result = JsonSerializer.Deserialize<GoogleVideoResult>(responseStr, _options.JsonSerializerOptions);
}
catch (Exception ex)
{
_logger.LogError($"Error when calling google youtube search api... ${ex.Message}");
}
return result;
}
}

View file

@ -216,10 +216,18 @@
},
"GoogleApi": {
"Endpoint": "https://maps.googleapis.com/maps/api/geocode/json",
"ApiKey": "",
"Components": "country=US|country=CA",
"Language": "en"
"Map": {
"Endpoint": "https://maps.googleapis.com/maps/api/geocode/json",
"Components": "country=US|country=CA",
"Language": "en"
},
"Youtube": {
"Endpoint": "https://www.googleapis.com/youtube/v3/search",
"RegionCode": "US",
"Language": "en",
"Part": "id"
}
},
"PluginLoader": {