Fix bing and Itunes image search.

This commit is contained in:
Steven Hildreth 2022-03-16 18:11:30 -05:00
parent 5768316587
commit dd481715a6
2 changed files with 33 additions and 17 deletions

View file

@ -12,15 +12,19 @@ using System.Threading.Tasks;
namespace Roadie.Library.SearchEngines.Imaging namespace Roadie.Library.SearchEngines.Imaging
{ {
/// <summary> /// <summary>
/// https://msdn.microsoft.com/en-us/library/dn760791(v=bsynd.50).aspx /// https://docs.microsoft.com/en-us/bing/search-apis/bing-image-search/how-to/get-images
/// </summary> /// </summary>
public class BingImageSearchEngine : ImageSearchEngineBase, IBingImageSearchEngine public class BingImageSearchEngine : ImageSearchEngineBase, IBingImageSearchEngine
{ {
public override bool IsEnabled => Configuration.Integrations.BingImageSearchEngineEnabled; public override bool IsEnabled => Configuration.Integrations.BingImageSearchEngineEnabled;
public BingImageSearchEngine(IRoadieSettings configuration, ILogger<BingImageSearchEngine> logger, string requestIp = null, string referrer = null) public BingImageSearchEngine(
: base(configuration, logger, "https://api.cognitive.microsoft.com", requestIp, referrer) IRoadieSettings configuration,
ILogger<BingImageSearchEngine> logger,
string requestIp = null,
string referrer = null)
: base(configuration, logger, "https://api.bing.microsoft.com", requestIp, referrer)
{ {
_apiKey = configuration.Integrations.ApiKeys.FirstOrDefault(x => x.ApiName == "BingImageSearch") ?? _apiKey = configuration.Integrations.ApiKeys.FirstOrDefault(x => x.ApiName == "BingImageSearch") ??
new ApiKey(); new ApiKey();
@ -30,7 +34,7 @@ namespace Roadie.Library.SearchEngines.Imaging
{ {
var request = new RestRequest var request = new RestRequest
{ {
Resource = "/bing/v7.0/images/search", Resource = "/v7.0/images/search",
Method = Method.Get, Method = Method.Get,
RequestFormat = DataFormat.Json RequestFormat = DataFormat.Json
}; };
@ -51,17 +55,18 @@ namespace Roadie.Library.SearchEngines.Imaging
var response = await _client.ExecuteAsync<BingImageResult>(request).ConfigureAwait(false); var response = await _client.ExecuteAsync<BingImageResult>(request).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.Unauthorized) if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new AuthenticationException("Api Key is not correct"); throw new AuthenticationException("Api Key is not correct");
}
if (response.ResponseStatus == ResponseStatus.Error) if (response.ResponseStatus == ResponseStatus.Error)
throw new Exception(string.Format("Request Error Message: {0}. Content: {1}.", response.ErrorMessage, {
response.Content)); throw new Exception(string.Format("Request Error Message: {0}. Content: {1}.", response.ErrorMessage, response.Content));
}
if (response.Data == null || response.Data.value == null) if (response.Data == null || response.Data.value == null)
{ {
Logger.LogWarning("Response Is Null on PerformImageSearch [" + response.ErrorMessage + "]"); Logger.LogWarning("Response Is Null on PerformImageSearch [" + response.ErrorMessage + "]");
return null; return null;
} }
return response.Data.value.Select(x => new ImageSearchResult return response.Data.value.Select(x => new ImageSearchResult
{ {
Width = (x.width ?? 0).ToString(), Width = (x.width ?? 0).ToString(),

View file

@ -15,7 +15,12 @@ namespace Roadie.Library.SearchEngines.Imaging
{ {
public class ITunesSearchEngine : ImageSearchEngineBase, IITunesSearchEngine public class ITunesSearchEngine : ImageSearchEngineBase, IITunesSearchEngine
{ {
public ITunesSearchEngine(IRoadieSettings configuration, ICacheManager cacheManager, ILogger<ITunesSearchEngine> logger, string requestIp = null, string referrer = null) public ITunesSearchEngine(
IRoadieSettings configuration,
ICacheManager cacheManager,
ILogger<ITunesSearchEngine> logger,
string requestIp = null,
string referrer = null)
: base(configuration, logger, "http://itunes.apple.com", requestIp, referrer) : base(configuration, logger, "http://itunes.apple.com", requestIp, referrer)
{ {
CacheManager = cacheManager; CacheManager = cacheManager;
@ -36,9 +41,10 @@ namespace Roadie.Library.SearchEngines.Imaging
if (response.ResponseStatus == ResponseStatus.Error) if (response.ResponseStatus == ResponseStatus.Error)
{ {
if (response.StatusCode == HttpStatusCode.Unauthorized) if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new AuthenticationException("Unauthorized"); throw new AuthenticationException("Unauthorized");
throw new Exception(string.Format("Request Error Message: {0}. Content: {1}.", }
response.ErrorMessage, response.Content)); throw new Exception(string.Format("Request Error Message: {0}. Content: {1}.", response.ErrorMessage, response.Content));
} }
var responseData = response?.Data?.results?.FirstOrDefault(); var responseData = response?.Data?.results?.FirstOrDefault();
@ -74,7 +80,7 @@ namespace Roadie.Library.SearchEngines.Imaging
public override RestRequest BuildRequest(string query, int resultsCount) public override RestRequest BuildRequest(string query, int resultsCount)
{ {
return BuildRequest(query, resultsCount, "Release"); return BuildRequest(query, resultsCount, "album");
} }
#pragma warning disable CS1998 #pragma warning disable CS1998
@ -89,12 +95,16 @@ namespace Roadie.Library.SearchEngines.Imaging
if (response.ResponseStatus == ResponseStatus.Error) if (response.ResponseStatus == ResponseStatus.Error)
{ {
if (response.StatusCode == HttpStatusCode.Unauthorized) if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new AuthenticationException("Unauthorized"); throw new AuthenticationException("Unauthorized");
throw new Exception(string.Format("Request Error Message: {0}. Content: {1}.", }
response.ErrorMessage, response.Content)); throw new Exception(string.Format("Request Error Message: {0}. Content: {1}.", response.ErrorMessage, response.Content));
} }
if (response.Data.results == null) return new ImageSearchResult[0]; if (response.Data.results == null)
{
return new ImageSearchResult[0];
}
result = response.Data.results.Select(x => new ImageSearchResult result = response.Data.results.Select(x => new ImageSearchResult
{ {
ArtistId = x.artistId.ToString(), ArtistId = x.artistId.ToString(),
@ -120,9 +130,10 @@ namespace Roadie.Library.SearchEngines.Imaging
if (response.ResponseStatus == ResponseStatus.Error) if (response.ResponseStatus == ResponseStatus.Error)
{ {
if (response.StatusCode == HttpStatusCode.Unauthorized) if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new AuthenticationException("Unauthorized"); throw new AuthenticationException("Unauthorized");
throw new Exception(string.Format("Request Error Message: {0}. Content: {1}.", response.ErrorMessage, }
response.Content)); throw new Exception(string.Format("Request Error Message: {0}. Content: {1}.", response.ErrorMessage, response.Content));
} }
ReleaseSearchResult data = null; ReleaseSearchResult data = null;