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;
|
|
|
|
private readonly MediaFileProvider _mediaFileProvider;
|
|
|
|
private readonly UpdateInfoJob _updateInfoJob;
|
|
|
|
private readonly DiskScanJob _diskScanJob;
|
|
|
|
|
|
|
|
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-04 04:56:53 +03:00
|
|
|
public ImportNewSeriesJob(SeriesProvider seriesProvider,
|
2011-05-21 03:23:49 +03:00
|
|
|
MediaFileProvider mediaFileProvider, UpdateInfoJob updateInfoJob, DiskScanJob diskScanJob)
|
|
|
|
{
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
_mediaFileProvider = mediaFileProvider;
|
2011-06-14 04:23:04 +03:00
|
|
|
_updateInfoJob = updateInfoJob;
|
2011-05-21 03:23:49 +03:00
|
|
|
_diskScanJob = diskScanJob;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get { return "New Series Update"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public int DefaultInterval
|
|
|
|
{
|
|
|
|
get { return 1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(ProgressNotification notification, int targetId)
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
_updateInfoJob.Start(notification, currentSeries.SeriesId);
|
|
|
|
_diskScanJob.Start(notification, currentSeries.SeriesId);
|
|
|
|
|
|
|
|
var updatedSeries = _seriesProvider.GetSeries(currentSeries.SeriesId);
|
|
|
|
AutoIgnoreSeasons(updatedSeries);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.ErrorException(e.Message, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Keep scanning until there no more shows left.
|
|
|
|
ScanSeries(notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AutoIgnoreSeasons(Series updatedSeries)
|
|
|
|
{
|
2011-06-04 04:56:53 +03:00
|
|
|
var episodeFiles = _mediaFileProvider.GetSeriesFiles(updatedSeries.SeriesId);
|
|
|
|
var episodes = updatedSeries.Episodes;
|
|
|
|
if (episodeFiles.Count() != 0)
|
2011-05-21 03:23:49 +03:00
|
|
|
{
|
2011-06-04 04:56:53 +03:00
|
|
|
var seasons = episodes.Select(c => c.SeasonNumber).Distinct();
|
|
|
|
var currentSeasons = seasons.Max();
|
|
|
|
|
|
|
|
foreach (var season in seasons)
|
|
|
|
{
|
2011-06-14 04:23:04 +03:00
|
|
|
if (season != currentSeasons)
|
2011-06-04 04:56:53 +03:00
|
|
|
{
|
2011-06-14 04:23:04 +03:00
|
|
|
|
2011-06-04 04:56:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-14 04:23:04 +03:00
|
|
|
/* Logger.Debug("Looking for seasons to ignore");
|
|
|
|
foreach (var season in updatedSeries.Seasons)
|
|
|
|
{
|
|
|
|
if (season.SeasonNumber != updatedSeries.Seasons.Max(s => s.SeasonNumber) && _mediaFileProvider.GetSeasonFiles(season.SeasonId).Count() == 0)
|
|
|
|
{
|
|
|
|
Logger.Info("Season {0} of {1} doesn't have any files on disk. season will not be monitored.", season.SeasonNumber, updatedSeries.Title);
|
|
|
|
season.Monitored = false;
|
|
|
|
_seasonProvider.SaveSeason(season);
|
|
|
|
}
|
|
|
|
}*/
|
2011-05-21 03:23:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|