2017-09-24 19:44:25 +00:00
|
|
|
using NLog;
|
2017-05-07 16:58:24 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Music.Events;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Music
|
|
|
|
{
|
|
|
|
public interface IRefreshTrackService
|
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
void RefreshTrackInfo(Album rg);
|
2017-05-07 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class RefreshTrackService : IRefreshTrackService
|
|
|
|
{
|
|
|
|
private readonly ITrackService _trackService;
|
2017-07-03 18:39:06 +00:00
|
|
|
private readonly IAlbumService _albumService;
|
2017-05-07 16:58:24 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2017-07-03 18:39:06 +00:00
|
|
|
public RefreshTrackService(ITrackService trackService, IAlbumService albumService, IEventAggregator eventAggregator, Logger logger)
|
2017-05-07 16:58:24 +00:00
|
|
|
{
|
|
|
|
_trackService = trackService;
|
2017-07-03 18:39:06 +00:00
|
|
|
_albumService = albumService;
|
2017-05-07 16:58:24 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
public void RefreshTrackInfo(Album album)
|
2017-05-07 16:58:24 +00:00
|
|
|
{
|
2017-06-18 02:27:01 +00:00
|
|
|
_logger.Info("Starting track info refresh for: {0}", album);
|
2017-05-07 16:58:24 +00:00
|
|
|
var successCount = 0;
|
|
|
|
var failCount = 0;
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
foreach (var release in album.AlbumReleases.Value)
|
|
|
|
{
|
2017-07-03 18:39:06 +00:00
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
var existingTracks = _trackService.GetTracksByForeignReleaseId(release.ForeignReleaseId);
|
2017-05-07 16:58:24 +00:00
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
var updateList = new List<Track>();
|
|
|
|
var newList = new List<Track>();
|
|
|
|
var dupeFreeRemoteTracks = release.Tracks.Value.DistinctBy(m => new { m.ForeignTrackId, m.TrackNumber }).ToList();
|
2017-05-07 16:58:24 +00:00
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
foreach (var track in OrderTracks(dupeFreeRemoteTracks))
|
2017-05-07 16:58:24 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
try
|
2017-05-07 16:58:24 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
var trackToUpdate = GetTrackToUpdate(track, existingTracks);
|
|
|
|
|
|
|
|
if (trackToUpdate != null)
|
|
|
|
{
|
|
|
|
existingTracks.Remove(trackToUpdate);
|
|
|
|
updateList.Add(trackToUpdate);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trackToUpdate = new Track();
|
|
|
|
trackToUpdate.Id = track.Id;
|
|
|
|
newList.Add(trackToUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use object mapper to automatically handle this
|
|
|
|
trackToUpdate.ForeignTrackId = track.ForeignTrackId;
|
|
|
|
trackToUpdate.ForeignRecordingId = track.ForeignRecordingId;
|
|
|
|
trackToUpdate.AlbumReleaseId = release.Id;
|
|
|
|
trackToUpdate.ArtistMetadataId = track.ArtistMetadata.Value.Id;
|
|
|
|
trackToUpdate.TrackNumber = track.TrackNumber;
|
|
|
|
trackToUpdate.AbsoluteTrackNumber = track.AbsoluteTrackNumber;
|
|
|
|
trackToUpdate.Title = track.Title ?? "Unknown";
|
|
|
|
trackToUpdate.Explicit = track.Explicit;
|
|
|
|
trackToUpdate.Duration = track.Duration;
|
|
|
|
trackToUpdate.MediumNumber = track.MediumNumber;
|
|
|
|
|
|
|
|
successCount++;
|
2017-05-07 16:58:24 +00:00
|
|
|
}
|
2018-12-15 00:02:43 +00:00
|
|
|
catch (Exception e)
|
2017-05-07 16:58:24 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
_logger.Fatal(e, "An error has occurred while updating track info for album {0}. {1}", album, track);
|
|
|
|
failCount++;
|
2017-05-07 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
_trackService.DeleteMany(existingTracks);
|
|
|
|
_trackService.UpdateMany(updateList);
|
|
|
|
_trackService.InsertMany(newList);
|
|
|
|
}
|
2017-05-07 16:58:24 +00:00
|
|
|
|
|
|
|
if (failCount != 0)
|
|
|
|
{
|
2017-06-18 02:27:01 +00:00
|
|
|
_logger.Info("Finished track refresh for album: {0}. Successful: {1} - Failed: {2} ",
|
|
|
|
album.Title, successCount, failCount);
|
2017-05-07 16:58:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-18 02:27:01 +00:00
|
|
|
_logger.Info("Finished track refresh for album: {0}.", album);
|
2017-05-07 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
private Track GetTrackToUpdate(Track track, List<Track> existingTracks)
|
2017-05-07 16:58:24 +00:00
|
|
|
{
|
2017-07-03 18:39:06 +00:00
|
|
|
var result = existingTracks.FirstOrDefault(e => e.ForeignTrackId == track.ForeignTrackId && e.TrackNumber == track.TrackNumber);
|
|
|
|
return result;
|
2017-05-07 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
private IEnumerable<Track> OrderTracks(List<Track> tracks)
|
2017-05-07 16:58:24 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
return tracks.OrderBy(e => e.AlbumReleaseId).ThenBy(e => e.TrackNumber);
|
2017-05-07 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|