1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/Jobs/ImportNewSeriesJob.cs

122 lines
4.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Ninject;
using NLog;
using NzbDrone.Core.Model.Notification;
2011-12-02 02:33:17 +01:00
using NzbDrone.Core.Providers;
2011-12-02 02:33:17 +01:00
namespace NzbDrone.Core.Jobs
{
/// <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 SeriesProvider _seriesProvider;
private readonly EpisodeProvider _episodeProvider;
private readonly MediaFileProvider _mediaFileProvider;
private readonly UpdateInfoJob _updateInfoJob;
private readonly DiskScanJob _diskScanJob;
private readonly BannerDownloadJob _bannerDownloadJob;
private readonly SeasonProvider _seasonProvider;
private readonly XemUpdateJob _xemUpdateJob;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private List<int> _attemptedSeries;
[Inject]
public ImportNewSeriesJob(SeriesProvider seriesProvider, EpisodeProvider episodeProvider,
MediaFileProvider mediaFileProvider, UpdateInfoJob updateInfoJob,
DiskScanJob diskScanJob, BannerDownloadJob bannerDownloadJob,
SeasonProvider seasonProvider, XemUpdateJob xemUpdateJob)
{
_seriesProvider = seriesProvider;
_episodeProvider = episodeProvider;
_mediaFileProvider = mediaFileProvider;
_updateInfoJob = updateInfoJob;
_diskScanJob = diskScanJob;
_bannerDownloadJob = bannerDownloadJob;
_seasonProvider = seasonProvider;
_xemUpdateJob = xemUpdateJob;
}
public string Name
{
get { return "New Series Update"; }
}
public TimeSpan DefaultInterval
{
get { return TimeSpan.FromMinutes(1); }
}
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 = _seriesProvider.GetAllSeries().Where(s => s.LastInfoSync == null && !_attemptedSeries.Contains(s.SeriesId)).ToList();
if (syncList.Count == 0)
{
return;
}
foreach (var currentSeries in syncList)
{
try
{
_attemptedSeries.Add(currentSeries.SeriesId);
notification.CurrentMessage = String.Format("Searching for '{0}'", new DirectoryInfo(currentSeries.Path).Name);
2012-09-10 21:04:17 +02:00
_updateInfoJob.Start(notification, new { SeriesId = currentSeries.SeriesId });
_diskScanJob.Start(notification, new { SeriesId = currentSeries.SeriesId });
var updatedSeries = _seriesProvider.GetSeries(currentSeries.SeriesId);
2011-06-19 22:43:33 +02:00
AutoIgnoreSeasons(updatedSeries.SeriesId);
//Download the banner for the new series
2012-09-10 21:04:17 +02:00
_bannerDownloadJob.Start(notification, new { SeriesId = updatedSeries.SeriesId });
2011-07-06 08:17:21 +02:00
//Get Scene Numbering if applicable
_xemUpdateJob.Start(notification, new { SeriesId = updatedSeries.SeriesId });
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)
{
2011-06-19 22:43:33 +02:00
var episodeFiles = _mediaFileProvider.GetSeriesFiles(seriesId);
if (episodeFiles.Count() != 0)
{
2011-06-19 22:43:33 +02:00
var seasons = _episodeProvider.GetSeasons(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))
{
_seasonProvider.SetIgnore(seriesId, season, true);
}
}
}
}
}
}