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;
|
|
|
|
using NLog;
|
2013-02-24 22:57:33 +03:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-02-19 09:01:03 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
2011-05-21 03:23:49 +03:00
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-12-02 04:33:17 +03:00
|
|
|
using NzbDrone.Core.Providers;
|
2011-05-21 03:23:49 +03:00
|
|
|
|
2011-12-02 04:33:17 +03:00
|
|
|
namespace NzbDrone.Core.Jobs
|
2011-05-21 03:23:49 +03: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
|
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
private readonly ISeriesService _seriesService;
|
2013-02-22 03:47:09 +03:00
|
|
|
private readonly IEpisodeService _episodeService;
|
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;
|
2013-02-20 05:05:15 +03:00
|
|
|
private readonly ISeasonRepository _seasonRepository;
|
2012-10-18 07:15:42 +03:00
|
|
|
private readonly XemUpdateJob _xemUpdateJob;
|
2013-02-20 05:05:15 +03:00
|
|
|
private readonly ISeriesRepository _seriesRepository;
|
|
|
|
private readonly ISeasonService _seasonService;
|
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;
|
|
|
|
|
2013-02-22 03:47:09 +03:00
|
|
|
public ImportNewSeriesJob(ISeriesService seriesService, IEpisodeService episodeService,
|
2011-09-10 11:42:05 +03:00
|
|
|
MediaFileProvider mediaFileProvider, UpdateInfoJob updateInfoJob,
|
2012-10-18 07:15:42 +03:00
|
|
|
DiskScanJob diskScanJob, BannerDownloadJob bannerDownloadJob,
|
2013-02-20 05:05:15 +03:00
|
|
|
ISeasonRepository seasonRepository, XemUpdateJob xemUpdateJob, ISeriesRepository seriesRepository,ISeasonService seasonService)
|
2011-05-21 03:23:49 +03:00
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
_seriesService = seriesService;
|
|
|
|
_episodeService = episodeService;
|
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;
|
2013-02-20 05:05:15 +03:00
|
|
|
_seasonRepository = seasonRepository;
|
2012-10-18 07:15:42 +03:00
|
|
|
_xemUpdateJob = xemUpdateJob;
|
2013-02-20 05:05:15 +03:00
|
|
|
_seriesRepository = seriesRepository;
|
|
|
|
_seasonService = seasonService;
|
2011-05-21 03:23:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
2012-12-25 03:15:26 +03:00
|
|
|
get { return "Import New Series"; }
|
2011-05-21 03:23:49 +03:00
|
|
|
}
|
|
|
|
|
2012-01-15 05:47:23 +03:00
|
|
|
public TimeSpan DefaultInterval
|
2011-05-21 03:23:49 +03:00
|
|
|
{
|
2012-12-25 03:15:26 +03:00
|
|
|
get { return TimeSpan.FromHours(6); }
|
2011-05-21 03:23:49 +03:00
|
|
|
}
|
|
|
|
|
2012-09-10 22:04:17 +03:00
|
|
|
public void Start(ProgressNotification notification, dynamic options)
|
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)
|
|
|
|
{
|
2013-02-26 06:58:57 +03:00
|
|
|
var syncList = _seriesRepository.All().Where(s => s.LastInfoSync == null && !_attemptedSeries.Contains(s.Id)).ToList();
|
2011-05-21 03:23:49 +03:00
|
|
|
if (syncList.Count == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var currentSeries in syncList)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-02-26 06:58:57 +03:00
|
|
|
_attemptedSeries.Add(((ModelBase)currentSeries).Id);
|
2011-05-21 03:23:49 +03:00
|
|
|
notification.CurrentMessage = String.Format("Searching for '{0}'", new DirectoryInfo(currentSeries.Path).Name);
|
|
|
|
|
2013-02-26 06:58:57 +03:00
|
|
|
_updateInfoJob.Start(notification, new { SeriesId = ((ModelBase)currentSeries).Id });
|
|
|
|
_diskScanJob.Start(notification, new { SeriesId = ((ModelBase)currentSeries).Id });
|
2011-05-21 03:23:49 +03:00
|
|
|
|
2013-02-26 06:58:57 +03:00
|
|
|
var updatedSeries = _seriesRepository.Get(((ModelBase)currentSeries).Id);
|
|
|
|
AutoIgnoreSeasons(((ModelBase)updatedSeries).Id);
|
2011-05-21 03:23:49 +03:00
|
|
|
|
2011-09-10 11:42:05 +03:00
|
|
|
//Download the banner for the new series
|
2013-02-26 06:58:57 +03:00
|
|
|
_bannerDownloadJob.Start(notification, new { SeriesId = ((ModelBase)updatedSeries).Id });
|
2011-07-06 09:17:21 +03:00
|
|
|
|
2012-10-18 07:15:42 +03:00
|
|
|
//Get Scene Numbering if applicable
|
2013-02-26 06:58:57 +03:00
|
|
|
_xemUpdateJob.Start(notification, new { SeriesId = ((ModelBase)updatedSeries).Id });
|
2012-10-18 07:15:42 +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
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
var seasons = _seasonRepository.GetSeasonNumbers(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
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
_seasonService.SetIgnore(seriesId, season, true);
|
2011-06-04 04:56:53 +03:00
|
|
|
}
|
|
|
|
}
|
2011-05-21 03:23:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|