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