mirror of https://github.com/lidarr/Lidarr
cleaned up refresh series a bit.
This commit is contained in:
parent
cf6512bc57
commit
4e7468e6fe
|
@ -49,6 +49,7 @@ namespace NzbDrone.Core.Test.MetadataSourceTests
|
|||
{
|
||||
series.Should().NotBeNull();
|
||||
series.Title.Should().NotBeBlank();
|
||||
series.CleanTitle.Should().Be(Parser.Parser.CleanSeriesTitle(series.Title));
|
||||
series.Overview.Should().NotBeBlank();
|
||||
series.AirTime.Should().NotBeBlank();
|
||||
series.FirstAired.Should().HaveValue();
|
||||
|
|
|
@ -164,6 +164,7 @@
|
|||
<Compile Include="ProviderTests\RecycleBinProviderTests\DeleteFileFixture.cs" />
|
||||
<Compile Include="ProviderTests\RecycleBinProviderTests\DeleteDirectoryFixture.cs" />
|
||||
<Compile Include="NotificationTests\PlexProviderTest.cs" />
|
||||
<Compile Include="TvTests\RefreshEpisodeServiceFixture.cs" />
|
||||
<Compile Include="TvTests\EpisodeProviderTests\HandleEpisodeFileDeletedFixture.cs" />
|
||||
<Compile Include="TvTests\EpisodeRepositoryTests\FindEpisodeFixture.cs" />
|
||||
<Compile Include="TvTests\EpisodeRepositoryTests\EpisodesRepositoryReadFixture.cs" />
|
||||
|
|
Binary file not shown.
|
@ -47,6 +47,7 @@ namespace NzbDrone.Core.MetadataSource
|
|||
series.TvRageId = show.tvrage_id;
|
||||
series.ImdbId = show.imdb_id;
|
||||
series.Title = show.title;
|
||||
series.CleanTitle = Parser.Parser.CleanSeriesTitle(show.title);
|
||||
series.FirstAired = FromIso(show.first_aired_iso);
|
||||
series.Overview = show.overview;
|
||||
series.Runtime = show.runtime;
|
||||
|
|
|
@ -343,6 +343,7 @@
|
|||
<Compile Include="Tv\Events\SeriesUpdatedEvent.cs" />
|
||||
<Compile Include="Datastore\ResultSet.cs" />
|
||||
<Compile Include="Tv\Commands\RefreshSeriesCommand.cs" />
|
||||
<Compile Include="Tv\RefreshEpisodeService.cs" />
|
||||
<Compile Include="Tv\SeasonRepository.cs" />
|
||||
<Compile Include="Tv\SeriesRepository.cs" />
|
||||
<Compile Include="Tv\QualityModel.cs" />
|
||||
|
|
|
@ -30,6 +30,7 @@ namespace NzbDrone.Core.Tv
|
|||
List<Episode> EpisodesBetweenDates(DateTime start, DateTime end);
|
||||
void InsertMany(List<Episode> episodes);
|
||||
void UpdateMany(List<Episode> episodes);
|
||||
void DeleteMany(List<Episode> episodes);
|
||||
}
|
||||
|
||||
public class EpisodeService : IEpisodeService,
|
||||
|
@ -170,6 +171,11 @@ namespace NzbDrone.Core.Tv
|
|||
_episodeRepository.UpdateMany(episodes);
|
||||
}
|
||||
|
||||
public void DeleteMany(List<Episode> episodes)
|
||||
{
|
||||
_episodeRepository.DeleteMany(episodes);
|
||||
}
|
||||
|
||||
public void HandleAsync(SeriesDeletedEvent message)
|
||||
{
|
||||
var episodes = GetEpisodeBySeries(message.Series.Id);
|
||||
|
|
|
@ -0,0 +1,154 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Core.Tv.Events;
|
||||
|
||||
namespace NzbDrone.Core.Tv
|
||||
{
|
||||
public interface IRefreshEpisodeService
|
||||
{
|
||||
void RefreshEpisodeInfo(Series series, IEnumerable<Episode> remoteEpisodes);
|
||||
}
|
||||
|
||||
public class RefreshEpisodeService : IRefreshEpisodeService
|
||||
{
|
||||
private readonly IEpisodeService _episodeService;
|
||||
private readonly ISeasonService _seasonService;
|
||||
private readonly IMessageAggregator _messageAggregator;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public RefreshEpisodeService(IEpisodeService episodeService,
|
||||
ISeasonService seasonService, IMessageAggregator messageAggregator, Logger logger)
|
||||
{
|
||||
_episodeService = episodeService;
|
||||
_seasonService = seasonService;
|
||||
_messageAggregator = messageAggregator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public void RefreshEpisodeInfo(Series series, IEnumerable<Episode> remoteEpisodes)
|
||||
{
|
||||
_logger.Info("Starting series info refresh for: {0}", series);
|
||||
var successCount = 0;
|
||||
var failCount = 0;
|
||||
|
||||
var existinEpisodes = _episodeService.GetEpisodeBySeries(series.Id);
|
||||
var seasons = _seasonService.GetSeasonsBySeries(series.Id);
|
||||
|
||||
var updateList = new List<Episode>();
|
||||
var newList = new List<Episode>();
|
||||
|
||||
foreach (var episode in remoteEpisodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
||||
{
|
||||
try
|
||||
{
|
||||
var episodeToUpdate = existinEpisodes.SingleOrDefault(e => e.TvDbEpisodeId == episode.TvDbEpisodeId) ??
|
||||
existinEpisodes.SingleOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
|
||||
|
||||
if (episodeToUpdate != null)
|
||||
{
|
||||
existinEpisodes.Remove(episodeToUpdate);
|
||||
updateList.Add(episodeToUpdate);
|
||||
|
||||
if ((episodeToUpdate.EpisodeNumber != episode.EpisodeNumber || episodeToUpdate.SeasonNumber != episode.SeasonNumber) && episodeToUpdate.EpisodeFileId != 0)
|
||||
{
|
||||
_logger.Debug("Un-linking episode file because the episode number has changed");
|
||||
episodeToUpdate.EpisodeFileId = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
episodeToUpdate = new Episode();
|
||||
episodeToUpdate.Monitored = GetMonitoredStatus(episode, seasons);
|
||||
newList.Add(episodeToUpdate);
|
||||
}
|
||||
|
||||
episodeToUpdate.SeriesId = series.Id;
|
||||
episodeToUpdate.TvDbEpisodeId = episode.TvDbEpisodeId;
|
||||
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
||||
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
||||
episodeToUpdate.Title = episode.Title;
|
||||
episodeToUpdate.Overview = episode.Overview;
|
||||
episodeToUpdate.AirDate = episode.AirDate;
|
||||
episodeToUpdate.AirDateUtc = episode.AirDateUtc;
|
||||
|
||||
successCount++;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.FatalException(String.Format("An error has occurred while updating episode info for series {0}. {1}", series, episode), e);
|
||||
failCount++;
|
||||
}
|
||||
}
|
||||
|
||||
var allEpisodes = new List<Episode>();
|
||||
allEpisodes.AddRange(newList);
|
||||
allEpisodes.AddRange(updateList);
|
||||
|
||||
AdjustMultiEpisodeAirTime(series, allEpisodes);
|
||||
|
||||
_episodeService.DeleteMany(existinEpisodes);
|
||||
_episodeService.UpdateMany(updateList);
|
||||
_episodeService.InsertMany(newList);
|
||||
|
||||
|
||||
if (newList.Any())
|
||||
{
|
||||
_messageAggregator.PublishEvent(new EpisodeInfoAddedEvent(newList, series));
|
||||
}
|
||||
|
||||
if (updateList.Any())
|
||||
{
|
||||
_messageAggregator.PublishEvent(new EpisodeInfoUpdatedEvent(updateList));
|
||||
}
|
||||
|
||||
if (failCount != 0)
|
||||
{
|
||||
_logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ",
|
||||
series.Title, successCount, failCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Info("Finished episode refresh for series: {0}.", series);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool GetMonitoredStatus(Episode episode, IEnumerable<Season> seasons)
|
||||
{
|
||||
if (episode.SeasonNumber == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (episode.EpisodeNumber == 0 && episode.SeasonNumber != 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var season = seasons.SingleOrDefault(c => c.SeasonNumber == episode.SeasonNumber);
|
||||
return season == null || season.Monitored;
|
||||
}
|
||||
|
||||
private static void AdjustMultiEpisodeAirTime(Series series, IEnumerable<Episode> allEpisodes)
|
||||
{
|
||||
var groups =
|
||||
allEpisodes.Where(c => c.AirDateUtc.HasValue)
|
||||
.GroupBy(e => new { e.SeriesId, e.AirDate })
|
||||
.Where(g => g.Count() > 1)
|
||||
.ToList();
|
||||
|
||||
foreach (var group in groups)
|
||||
{
|
||||
var episodeCount = 0;
|
||||
foreach (var episode in @group.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
||||
{
|
||||
episode.AirDateUtc = episode.AirDateUtc.Value.AddMinutes(series.Runtime * episodeCount);
|
||||
episodeCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Messaging;
|
||||
|
@ -15,19 +14,16 @@ namespace NzbDrone.Core.Tv
|
|||
{
|
||||
private readonly IProvideSeriesInfo _seriesInfo;
|
||||
private readonly ISeriesService _seriesService;
|
||||
private readonly IEpisodeService _episodeService;
|
||||
private readonly ISeasonRepository _seasonRepository;
|
||||
private readonly IRefreshEpisodeService _refreshEpisodeService;
|
||||
private readonly IMessageAggregator _messageAggregator;
|
||||
private readonly IDailySeriesService _dailySeriesService;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public RefreshSeriesService(IProvideSeriesInfo seriesInfo, ISeriesService seriesService, IEpisodeService episodeService,
|
||||
ISeasonRepository seasonRepository, IMessageAggregator messageAggregator, IDailySeriesService dailySeriesService, Logger logger)
|
||||
public RefreshSeriesService(IProvideSeriesInfo seriesInfo, ISeriesService seriesService, IRefreshEpisodeService refreshEpisodeService, IMessageAggregator messageAggregator, IDailySeriesService dailySeriesService, Logger logger)
|
||||
{
|
||||
_seriesInfo = seriesInfo;
|
||||
_seriesService = seriesService;
|
||||
_episodeService = episodeService;
|
||||
_seasonRepository = seasonRepository;
|
||||
_refreshEpisodeService = refreshEpisodeService;
|
||||
_messageAggregator = messageAggregator;
|
||||
_dailySeriesService = dailySeriesService;
|
||||
_logger = logger;
|
||||
|
@ -74,7 +70,7 @@ namespace NzbDrone.Core.Tv
|
|||
series.AirTime = seriesInfo.AirTime;
|
||||
series.Overview = seriesInfo.Overview;
|
||||
series.Status = seriesInfo.Status;
|
||||
series.CleanTitle = Parser.Parser.CleanSeriesTitle(seriesInfo.Title);
|
||||
series.CleanTitle = seriesInfo.CleanTitle;
|
||||
series.LastInfoSync = DateTime.UtcNow;
|
||||
series.Runtime = seriesInfo.Runtime;
|
||||
series.Images = seriesInfo.Images;
|
||||
|
@ -88,132 +84,11 @@ namespace NzbDrone.Core.Tv
|
|||
|
||||
_seriesService.UpdateSeries(series);
|
||||
|
||||
RefreshEpisodeInfo(series, tuple.Item2);
|
||||
_refreshEpisodeService.RefreshEpisodeInfo(series, tuple.Item2);
|
||||
|
||||
_messageAggregator.PublishEvent(new SeriesUpdatedEvent(series));
|
||||
}
|
||||
|
||||
private void RefreshEpisodeInfo(Series series, IEnumerable<Episode> remoteEpisodes)
|
||||
{
|
||||
_logger.Info("Starting series info refresh for: {0}", series);
|
||||
var successCount = 0;
|
||||
var failCount = 0;
|
||||
|
||||
var seriesEpisodes = _episodeService.GetEpisodeBySeries(series.Id);
|
||||
var seasons = _seasonRepository.GetSeasonBySeries(series.Id);
|
||||
|
||||
var updateList = new List<Episode>();
|
||||
var newList = new List<Episode>();
|
||||
|
||||
foreach (var episode in remoteEpisodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
||||
{
|
||||
try
|
||||
{
|
||||
var episodeToUpdate = seriesEpisodes.SingleOrDefault(e => e.TvDbEpisodeId == episode.TvDbEpisodeId) ??
|
||||
seriesEpisodes.SingleOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
|
||||
|
||||
if (episodeToUpdate == null)
|
||||
{
|
||||
episodeToUpdate = new Episode();
|
||||
newList.Add(episodeToUpdate);
|
||||
|
||||
//If it is Episode Zero or Season zero ignore it
|
||||
if ((episode.EpisodeNumber == 0 && episode.SeasonNumber != 1) || episode.SeasonNumber == 0)
|
||||
{
|
||||
episodeToUpdate.Monitored = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
var season = seasons.FirstOrDefault(c => c.SeasonNumber == episode.SeasonNumber);
|
||||
episodeToUpdate.Monitored = season == null || season.Monitored;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
updateList.Add(episodeToUpdate);
|
||||
}
|
||||
|
||||
if ((episodeToUpdate.EpisodeNumber != episode.EpisodeNumber ||
|
||||
episodeToUpdate.SeasonNumber != episode.SeasonNumber) &&
|
||||
episodeToUpdate.EpisodeFileId > 0)
|
||||
{
|
||||
_logger.Debug("Un-linking episode file because the episode number has changed");
|
||||
episodeToUpdate.EpisodeFileId = 0;
|
||||
}
|
||||
|
||||
episodeToUpdate.SeriesId = series.Id;
|
||||
episodeToUpdate.TvDbEpisodeId = episode.TvDbEpisodeId;
|
||||
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
||||
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
||||
episodeToUpdate.Title = episode.Title;
|
||||
episodeToUpdate.Overview = episode.Overview;
|
||||
episodeToUpdate.AirDate = episode.AirDate;
|
||||
episodeToUpdate.AirDateUtc = episode.AirDateUtc;
|
||||
|
||||
successCount++;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.FatalException(String.Format("An error has occurred while updating episode info for series {0}. {1}", series, episode), e);
|
||||
failCount++;
|
||||
}
|
||||
}
|
||||
|
||||
var allEpisodes = new List<Episode>();
|
||||
allEpisodes.AddRange(newList);
|
||||
allEpisodes.AddRange(updateList);
|
||||
|
||||
var groups = allEpisodes.Where(c=>c.AirDateUtc.HasValue).GroupBy(e => new { e.SeriesId, e.AirDate }).Where(g => g.Count() > 1).ToList();
|
||||
|
||||
foreach (var group in groups)
|
||||
{
|
||||
int episodeCount = 0;
|
||||
foreach (var episode in group.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
||||
{
|
||||
episode.AirDateUtc = episode.AirDateUtc.Value.AddMinutes(series.Runtime * episodeCount);
|
||||
episodeCount++;
|
||||
}
|
||||
}
|
||||
|
||||
_episodeService.UpdateMany(updateList);
|
||||
_episodeService.InsertMany(newList);
|
||||
|
||||
if (newList.Any())
|
||||
{
|
||||
_messageAggregator.PublishEvent(new EpisodeInfoAddedEvent(newList, series));
|
||||
}
|
||||
|
||||
if (updateList.Any())
|
||||
{
|
||||
_messageAggregator.PublishEvent(new EpisodeInfoUpdatedEvent(updateList));
|
||||
}
|
||||
|
||||
if (failCount != 0)
|
||||
{
|
||||
_logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ",
|
||||
series.Title, successCount, failCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Info("Finished episode refresh for series: {0}.", series);
|
||||
}
|
||||
|
||||
//DeleteEpisodesNotAvailableAnymore(series, remoteEpisodes);
|
||||
}
|
||||
|
||||
|
||||
/* private void DeleteEpisodesNotAvailableAnymore(Series series, IEnumerable<Episode> onlineEpisodes)
|
||||
{
|
||||
//Todo: This will not work as currently implemented - what are we trying to do here?
|
||||
//Todo: We were trying to remove episodes that were once on tvdb but were removed, for whatever reason, instead of polluting our DB with them.
|
||||
return;
|
||||
_logger.Trace("Starting deletion of episodes that no longer exist in TVDB: {0}", series.Title.WithDefault(series.Id));
|
||||
foreach (var episode in onlineEpisodes)
|
||||
{
|
||||
_episodeRepository.Delete(episode.Id);
|
||||
}
|
||||
|
||||
_logger.Trace("Deleted episodes that no longer exist in TVDB for {0}", series.Id);
|
||||
}*/
|
||||
}
|
||||
}
|
|
@ -51,6 +51,10 @@
|
|||
<Reference Include="Moq">
|
||||
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\Newtonsoft.Json.5.0.3\lib\net35\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\NLog.2.0.1.2\lib\net40\NLog.dll</HintPath>
|
||||
|
@ -76,6 +80,7 @@
|
|||
<Compile Include="Categories\IntegrationTestAttribute.cs" />
|
||||
<Compile Include="LoggingTest.cs" />
|
||||
<Compile Include="MockerExtensions.cs" />
|
||||
<Compile Include="ObjectExtentions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ReflectionExtensions.cs" />
|
||||
<Compile Include="StringExtensions.cs" />
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Test.Common
|
||||
{
|
||||
public static class ObjectExtentions
|
||||
{
|
||||
public static T JsonClone<T>(this T source)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(source);
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using System.IO;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
|
||||
namespace NzbDrone.Test.Common
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
<package id="CommonServiceLocator" version="1.0" />
|
||||
<package id="FluentAssertions" version="2.0.1" targetFramework="net40" />
|
||||
<package id="Moq" version="4.0.10827" />
|
||||
<package id="Newtonsoft.Json" version="5.0.3" targetFramework="net40" />
|
||||
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
|
||||
<package id="NUnit" version="2.6.2" targetFramework="net40" />
|
||||
<package id="Unity" version="2.1.505.2" targetFramework="net40" />
|
||||
|
|
|
@ -106,10 +106,6 @@
|
|||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\Nancy.Owin.0.16.1\lib\net40\Nancy.Owin.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net35\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\NLog.2.0.1.2\lib\net40\NLog.dll</HintPath>
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<package id="Microsoft.Owin.Hosting" version="1.1.0-beta2" targetFramework="net40" />
|
||||
<package id="Nancy" version="0.16.1" targetFramework="net40" />
|
||||
<package id="Nancy.Owin" version="0.16.1" targetFramework="net40" />
|
||||
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
|
||||
<package id="Newtonsoft.Json" version="5.0.3" targetFramework="net40" />
|
||||
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
|
||||
<package id="NLog.Config" version="2.0.1.2" targetFramework="net40" />
|
||||
<package id="NLog.Schema" version="2.0.1.2" targetFramework="net40" />
|
||||
|
|
Loading…
Reference in New Issue