1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-09-20 00:11:46 +02:00
Radarr/NzbDrone.Core/Jobs/Implementations/ImportNewSeriesJob.cs

123 lines
4.6 KiB
C#
Raw Normal View History

2013-03-05 06:37:33 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NLog;
2013-02-24 20:57:33 +01:00
using NzbDrone.Core.Datastore;
2013-03-01 08:03:41 +01:00
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Model.Notification;
2013-03-05 06:37:33 +01:00
using NzbDrone.Core.Tv;
2013-03-05 06:37:33 +01:00
namespace NzbDrone.Core.Jobs.Implementations
{
/// <summary>
/// This job processes newly added jobs by downloading their info
/// from TheTVDB.org and doing a disk scan. this job should only
/// </summary>
public class ImportNewSeriesJob : IJob
{
private readonly ISeriesService _seriesService;
private readonly IEpisodeService _episodeService;
2013-03-01 08:03:41 +01:00
private readonly IMediaFileService _mediaFileService;
private readonly UpdateInfoJob _updateInfoJob;
private readonly DiskScanJob _diskScanJob;
private readonly ISeasonRepository _seasonRepository;
private readonly XemUpdateJob _xemUpdateJob;
private readonly ISeriesRepository _seriesRepository;
private readonly ISeasonService _seasonService;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private List<int> _attemptedSeries;
public ImportNewSeriesJob(ISeriesService seriesService, IEpisodeService episodeService,
2013-03-01 08:03:41 +01:00
IMediaFileService mediaFileService, UpdateInfoJob updateInfoJob,
DiskScanJob diskScanJob,
ISeasonRepository seasonRepository, XemUpdateJob xemUpdateJob, ISeriesRepository seriesRepository, ISeasonService seasonService)
{
_seriesService = seriesService;
_episodeService = episodeService;
2013-03-01 08:03:41 +01:00
_mediaFileService = mediaFileService;
_updateInfoJob = updateInfoJob;
_diskScanJob = diskScanJob;
_seasonRepository = seasonRepository;
_xemUpdateJob = xemUpdateJob;
_seriesRepository = seriesRepository;
_seasonService = seasonService;
}
public string Name
{
2012-12-25 01:15:26 +01:00
get { return "Import New Series"; }
}
public TimeSpan DefaultInterval
{
2012-12-25 01:15:26 +01:00
get { return TimeSpan.FromHours(6); }
}
2012-09-10 21:04:17 +02:00
public void Start(ProgressNotification notification, dynamic options)
{
_attemptedSeries = new List<int>();
ScanSeries(notification);
}
private void ScanSeries(ProgressNotification notification)
{
var syncList = _seriesRepository.All().Where(s => s.LastInfoSync == null && !_attemptedSeries.Contains(s.Id)).ToList();
if (syncList.Count == 0)
{
return;
}
foreach (var currentSeries in syncList)
{
try
{
_attemptedSeries.Add(((ModelBase)currentSeries).Id);
notification.CurrentMessage = String.Format("Searching for '{0}'", new DirectoryInfo(currentSeries.Path).Name);
_updateInfoJob.Start(notification, new { SeriesId = ((ModelBase)currentSeries).Id });
_diskScanJob.Start(notification, new { SeriesId = ((ModelBase)currentSeries).Id });
var updatedSeries = _seriesRepository.Get(((ModelBase)currentSeries).Id);
AutoIgnoreSeasons(((ModelBase)updatedSeries).Id);
//Get Scene Numbering if applicable
_xemUpdateJob.Start(notification, new { SeriesId = ((ModelBase)updatedSeries).Id });
notification.CurrentMessage = String.Format("{0} was successfully imported", updatedSeries.Title);
}
catch (Exception e)
{
Logger.ErrorException(e.Message, e);
}
}
//Keep scanning until there no more shows left.
ScanSeries(notification);
}
2011-06-19 22:43:33 +02:00
public void AutoIgnoreSeasons(int seriesId)
{
2013-02-27 23:29:08 +01:00
//Todo: Need to convert this over to ObjectDb
return;
2013-03-01 08:03:41 +01:00
var episodeFiles = _mediaFileService.GetFilesBySeries(seriesId);
2011-06-19 22:43:33 +02:00
if (episodeFiles.Count() != 0)
{
var seasons = _seasonRepository.GetSeasonNumbers(seriesId);
var currentSeasons = seasons.Max();
foreach (var season in seasons)
{
2011-06-19 22:43:33 +02:00
if (season != currentSeasons && !episodeFiles.Any(e => e.SeasonNumber == season))
{
_seasonService.SetIgnore(seriesId, season, true);
}
}
}
}
}
}