-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Meyn
committed
Feb 25, 2025
1 parent
0db07c5
commit 4c688c9
Showing
7 changed files
with
348 additions
and
198 deletions.
There are no files selected for viewing
26 changes: 0 additions & 26 deletions
26
Tubifarry/ImportLists/LastFMRecomendation/LastFMRecommend.cs
This file was deleted.
Oops, something went wrong.
169 changes: 0 additions & 169 deletions
169
Tubifarry/ImportLists/LastFMRecomendation/LastFMRecommendParser.cs
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...Lists/LastFMRecomendation/LastFmModels.cs → ...ists/LastFmRecommendation/LastFmModels.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
Tubifarry/ImportLists/LastFmRecommendation/LastFmRecommend.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using FluentValidation.Results; | ||
using NLog; | ||
using NzbDrone.Common.Extensions; | ||
using NzbDrone.Common.Http; | ||
using NzbDrone.Core.Configuration; | ||
using NzbDrone.Core.ImportLists; | ||
using NzbDrone.Core.ImportLists.Exceptions; | ||
using NzbDrone.Core.Indexers.Exceptions; | ||
using NzbDrone.Core.Parser; | ||
using System.Net; | ||
|
||
namespace Tubifarry.ImportLists.LastFmRecommendation | ||
{ | ||
internal class LastFmRecommend : HttpImportListBase<LastFmRecommendSettings> | ||
{ | ||
private readonly IHttpClient _client; | ||
public override string Name => "Last.fm Recommend"; | ||
public override TimeSpan MinRefreshInterval => TimeSpan.FromDays(7); | ||
public override ImportListType ListType => ImportListType.LastFm; | ||
|
||
public override int PageSize => 100; | ||
public override TimeSpan RateLimit => TimeSpan.FromSeconds(5); | ||
|
||
public LastFmRecommend(IHttpClient httpClient, IImportListStatusService importListStatusService, IConfigService configService, IParsingService parsingService, Logger logger) : base(httpClient, importListStatusService, configService, parsingService, logger) => _client = httpClient; | ||
|
||
public override IImportListRequestGenerator GetRequestGenerator() => new LastFmRecomendRequestGenerator(Settings); | ||
|
||
public override IParseImportListResponse GetParser() => new LastFmRecommendParser(Settings, _client); | ||
|
||
protected override void Test(List<ValidationFailure> failures) | ||
{ | ||
failures.AddIfNotNull(TestConnection()); | ||
Check warning on line 32 in Tubifarry/ImportLists/LastFmRecommendation/LastFmRecommend.cs
|
||
} | ||
|
||
protected override ValidationFailure? TestConnection() | ||
{ | ||
try | ||
{ | ||
IImportListRequestGenerator generator = GetRequestGenerator(); | ||
ImportListRequest listItems = generator.GetListItems().GetAllTiers().First().First(); | ||
ImportListResponse response = FetchImportListResponse(listItems); | ||
|
||
// Validate HTTP status first | ||
if (response.HttpResponse.StatusCode != HttpStatusCode.OK) | ||
{ | ||
return new ValidationFailure(string.Empty, "Connection failed: Server returned HTTP " + | ||
$"{(int)response.HttpResponse.StatusCode} ({response.HttpResponse.StatusCode})"); | ||
} | ||
|
||
// Enhanced content type validation | ||
string? contentType = response.HttpResponse.Headers.ContentType; | ||
if (contentType == null || | ||
!IsJsonContentType(contentType)) | ||
{ | ||
string receivedType = contentType ?? "null/no-content-type"; | ||
return new ValidationFailure(string.Empty, $"Unexpected content type: {receivedType}. " + "Server must return JSON (application/json or similar)"); | ||
} | ||
return null; | ||
} | ||
catch (RequestLimitReachedException) | ||
{ | ||
_logger.Warn("API request limit reached"); | ||
return new ValidationFailure(string.Empty, "API rate limit exceeded - try again later"); | ||
} | ||
catch (UnsupportedFeedException ex) | ||
{ | ||
_logger.Warn(ex, "Feed format not supported"); | ||
return new ValidationFailure(string.Empty, $"Unsupported feed format: {ex.Message}"); | ||
} | ||
catch (ImportListException ex) | ||
{ | ||
_logger.Warn(ex, "Connection failed"); | ||
return new ValidationFailure(string.Empty, $"Connection error: {ex.Message}"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.Error(ex, "Critical connection failure"); | ||
return new ValidationFailure(string.Empty, "Configuration error - check logs for details"); | ||
} | ||
} | ||
|
||
private static bool IsJsonContentType(string mediaType) => mediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase) || | ||
mediaType.Equals("text/json", StringComparison.OrdinalIgnoreCase) || | ||
mediaType.EndsWith("+json", StringComparison.OrdinalIgnoreCase); | ||
|
||
} | ||
} |
Oops, something went wrong.