2011-05-21 03:23:49 +03:00
|
|
|
using System;
|
2011-05-27 05:12:28 +03:00
|
|
|
using System.Collections.Generic;
|
2011-05-21 03:23:49 +03:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2011-06-14 04:23:04 +03:00
|
|
|
using Ninject;
|
2011-05-21 03:23:49 +03:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers.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;
|
2011-06-18 07:08:17 +03:00
|
|
|
private readonly EpisodeProvider _episodeProvider;
|
2011-05-21 03:23:49 +03:00
|
|
|
private readonly MediaFileProvider _mediaFileProvider;
|
|
|
|
private readonly UpdateInfoJob _updateInfoJob;
|
|
|
|
private readonly DiskScanJob _diskScanJob;
|
2011-09-10 11:42:05 +03:00
|
|
|
private readonly BannerDownloadJob _bannerDownloadJob;
|
2011-05-21 03:23:49 +03:00
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
2011-05-27 05:12:28 +03:00
|
|
|
private List<int> _attemptedSeries;
|
|
|
|
|
2011-06-14 04:23:04 +03:00
|
|
|
[Inject]
|
2011-06-18 07:08:17 +03:00
|
|
|
public ImportNewSeriesJob(SeriesProvider seriesProvider, EpisodeProvider episodeProvider,
|
2011-09-10 11:42:05 +03:00
|
|
|
MediaFileProvider mediaFileProvider, UpdateInfoJob updateInfoJob,
|
|
|
|
DiskScanJob diskScanJob, BannerDownloadJob bannerDownloadJob)
|
2011-05-21 03:23:49 +03:00
|
|
|
{
|
|
|
|
_seriesProvider = seriesProvider;
|
2011-06-18 07:08:17 +03:00
|
|
|
_episodeProvider = episodeProvider;
|
2011-05-21 03:23:49 +03:00
|
|
|
_mediaFileProvider = mediaFileProvider;
|
2011-06-14 04:23:04 +03:00
|
|
|
_updateInfoJob = updateInfoJob;
|
2011-05-21 03:23:49 +03:00
|
|
|
_diskScanJob = diskScanJob;
|
2011-09-10 11:42:05 +03:00
|
|
|
_bannerDownloadJob = bannerDownloadJob;
|
2011-05-21 03:23:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get { return "New Series Update"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public int DefaultInterval
|
|
|
|
{
|
|
|
|
get { return 1; }
|
|
|
|
}
|
|
|
|
|
2011-08-22 03:48:37 +03:00
|
|
|
public void Start(ProgressNotification notification, int targetId, int secondaryTargetId )
|
2011-05-21 03:23:49 +03:00
|
|
|
{
|
2011-05-27 05:12:28 +03:00
|
|
|
_attemptedSeries = new List<int>();
|
2011-05-21 03:23:49 +03:00
|
|
|
ScanSeries(notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ScanSeries(ProgressNotification notification)
|
|
|
|
{
|
2011-05-27 05:12:28 +03:00
|
|
|
var syncList = _seriesProvider.GetAllSeries().Where(s => s.LastInfoSync == null && !_attemptedSeries.Contains(s.SeriesId)).ToList();
|
2011-05-21 03:23:49 +03:00
|
|
|
if (syncList.Count == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var currentSeries in syncList)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-05-27 05:12:28 +03:00
|
|
|
_attemptedSeries.Add(currentSeries.SeriesId);
|
2011-05-21 03:23:49 +03:00
|
|
|
notification.CurrentMessage = String.Format("Searching for '{0}'", new DirectoryInfo(currentSeries.Path).Name);
|
|
|
|
|
2011-08-22 03:48:37 +03:00
|
|
|
_updateInfoJob.Start(notification, currentSeries.SeriesId, 0);
|
|
|
|
_diskScanJob.Start(notification, currentSeries.SeriesId, 0);
|
2011-05-21 03:23:49 +03:00
|
|
|
|
|
|
|
var updatedSeries = _seriesProvider.GetSeries(currentSeries.SeriesId);
|
2011-06-19 23:43:33 +03:00
|
|
|
AutoIgnoreSeasons(updatedSeries.SeriesId);
|
2011-05-21 03:23:49 +03:00
|
|
|
|
2011-09-10 11:42:05 +03:00
|
|
|
//Download the banner for the new series
|
|
|
|
_bannerDownloadJob.Start(notification, updatedSeries.SeriesId, 0);
|
2011-07-06 09:17:21 +03:00
|
|
|
|
2011-09-10 11:42:05 +03:00
|
|
|
notification.CurrentMessage = String.Format("{0} was successfully imported", updatedSeries.Title);
|
2011-05-21 03:23:49 +03:00
|
|
|
}
|
2011-09-10 11:42:05 +03:00
|
|
|
|
2011-05-21 03:23:49 +03:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.ErrorException(e.Message, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Keep scanning until there no more shows left.
|
|
|
|
ScanSeries(notification);
|
|
|
|
}
|
|
|
|
|
2011-06-19 23:43:33 +03:00
|
|
|
public void AutoIgnoreSeasons(int seriesId)
|
2011-05-21 03:23:49 +03:00
|
|
|
{
|
2011-06-19 23:43:33 +03:00
|
|
|
var episodeFiles = _mediaFileProvider.GetSeriesFiles(seriesId);
|
|
|
|
|
2011-06-04 04:56:53 +03:00
|
|
|
if (episodeFiles.Count() != 0)
|
2011-05-21 03:23:49 +03:00
|
|
|
{
|
2011-06-19 23:43:33 +03:00
|
|
|
var seasons = _episodeProvider.GetSeasons(seriesId);
|
2011-06-04 04:56:53 +03:00
|
|
|
var currentSeasons = seasons.Max();
|
|
|
|
|
|
|
|
foreach (var season in seasons)
|
|
|
|
{
|
2011-06-19 23:43:33 +03:00
|
|
|
if (season != currentSeasons && !episodeFiles.Any(e => e.SeasonNumber == season))
|
2011-06-04 04:56:53 +03:00
|
|
|
{
|
2011-06-19 23:43:33 +03:00
|
|
|
_episodeProvider.SetSeasonIgnore(seriesId, season, true);
|
2011-06-04 04:56:53 +03:00
|
|
|
}
|
|
|
|
}
|
2011-05-21 03:23:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|