Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Musicbrainz Label Import List #4616

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/NzbDrone.Core/ImportLists/MusicBrainzLabel/MusicBrainzLabel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MetadataSource;
using NzbDrone.Core.Parser;
using NzbDrone.Core.ThingiProvider;

namespace NzbDrone.Core.ImportLists.MusicBrainzLabel
{
public class MusicBrainzLabel : HttpImportListBase<MusicBrainzLabelSettings>
{
public override string Name => "MusicBrainz Label";

public override ProviderMessage Message => new ProviderMessage("MusicBrainz Label only supports release groups within Label, other types of member will not be picked up by Lidarr", ProviderMessageType.Warning);

public override ImportListType ListType => ImportListType.Other;
public override TimeSpan MinRefreshInterval => TimeSpan.FromHours(12);

private readonly IMetadataRequestBuilder _requestBuilder;

public MusicBrainzLabel(IHttpClient httpClient, IImportListStatusService importListStatusService, IConfigService configService, IParsingService parsingService, IMetadataRequestBuilder requestBuilder, Logger logger)
: base(httpClient, importListStatusService, configService, parsingService, logger)
{
_requestBuilder = requestBuilder;
}

public override IImportListRequestGenerator GetRequestGenerator()
{
return new MusicBrainzLabelRequestGenerator(_requestBuilder) { Settings = Settings };
}

public override IParseImportListResponse GetParser()
{
return new MusicBrainzLabelParser(Settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace NzbDrone.Core.ImportLists.MusicBrainzLabel
{
public class MusicBrainzLabelAlbum
{
public string ArtistName { get; set; }
public string AlbumTitle { get; set; }
public string ArtistId { get; set; }
public string AlbumId { get; set; }
public DateTime? ReleaseDate { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.ImportLists.Exceptions;
using NzbDrone.Core.Parser.Model;

namespace NzbDrone.Core.ImportLists.MusicBrainzLabel
{
public class MusicBrainzLabelParser : IParseImportListResponse
{
private readonly MusicBrainzLabelSettings _settings;
private ImportListResponse _importListResponse;

public MusicBrainzLabelParser(MusicBrainzLabelSettings settings)
{
_settings = settings;
}

public IList<ImportListItemInfo> ParseResponse(ImportListResponse importListResponse)
{
_importListResponse = importListResponse;

var items = new List<ImportListItemInfo>();

if (!PreProcess(_importListResponse))
{
return items;
}

var jsonResponse = JsonConvert.DeserializeObject<List<MusicBrainzLabelAlbum>>(_importListResponse.Content);

// no albums were return
if (jsonResponse == null)
{
return items;
}

foreach (var item in jsonResponse)
{
items.AddIfNotNull(new ImportListItemInfo
{
Artist = item.ArtistName,
Album = item.AlbumTitle,
ArtistMusicBrainzId = item.ArtistId,
AlbumMusicBrainzId = item.AlbumId,
ReleaseDate = item.ReleaseDate.GetValueOrDefault()
});
}

return items;
}

protected virtual bool PreProcess(ImportListResponse importListResponse)
{
if (importListResponse.HttpResponse.StatusCode != HttpStatusCode.OK)
{
throw new ImportListException(importListResponse, "Import List API call resulted in an unexpected StatusCode [{0}]", importListResponse.HttpResponse.StatusCode);
}

if (importListResponse.HttpResponse.Headers.ContentType != null && importListResponse.HttpResponse.Headers.ContentType.Contains("text/json") &&
importListResponse.HttpRequest.Headers.Accept != null && !importListResponse.HttpRequest.Headers.Accept.Contains("text/json"))
{
throw new ImportListException(importListResponse, "Import List responded with html content. Site is likely blocked or unavailable.");
}

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using NzbDrone.Core.MetadataSource;

namespace NzbDrone.Core.ImportLists.MusicBrainzLabel
{
public class MusicBrainzLabelRequestGenerator : IImportListRequestGenerator
{
public MusicBrainzLabelSettings Settings { get; set; }

private readonly IMetadataRequestBuilder _requestBulder;

public MusicBrainzLabelRequestGenerator(IMetadataRequestBuilder requestBuilder)
{
_requestBulder = requestBuilder;
}

public virtual ImportListPageableRequestChain GetListItems()
{
var pageableRequests = new ImportListPageableRequestChain();

pageableRequests.Add(GetPagedRequests());

return pageableRequests;
}

private IEnumerable<ImportListRequest> GetPagedRequests()
{
var request = _requestBulder.GetRequestBuilder()
.Create()
.SetSegment("route", "Label/" + Settings.LabelId)
.Build();

yield return new ImportListRequest(request);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Validation;

namespace NzbDrone.Core.ImportLists.MusicBrainzLabel
{
public class MusicBrainzLabelSettingsValidator : AbstractValidator<MusicBrainzLabelSettings>
{
}

public class MusicBrainzLabelSettings : IImportListSettings
{
private static readonly MusicBrainzLabelSettingsValidator Validator = new MusicBrainzLabelSettingsValidator();

public MusicBrainzLabelSettings()
{
BaseUrl = "";
}

public string BaseUrl { get; set; }

[FieldDefinition(0, Label = "Label Id", HelpText = "The GUID at the end of the MusicBrainz URL (e.g. 4b5f2897-9b05-4799-b895-6620e27143e7)")]
public string LabelId { get; set; }

public NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}