2011-05-21 00:23:49 +00:00
|
|
|
|
using System;
|
2011-05-27 02:12:28 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-05-21 00:23:49 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-02-24 19:57:33 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-03-01 07:03:41 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles;
|
2013-02-19 06:01:03 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2011-05-21 00:23:49 +00:00
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-12-02 01:33:17 +00:00
|
|
|
|
using NzbDrone.Core.Providers;
|
2011-05-21 00:23:49 +00:00
|
|
|
|
|
2011-12-02 01:33:17 +00:00
|
|
|
|
namespace NzbDrone.Core.Jobs
|
2011-05-21 00:23:49 +00: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 02:05:15 +00:00
|
|
|
|
private readonly ISeriesService _seriesService;
|
2013-02-22 00:47:09 +00:00
|
|
|
|
private readonly IEpisodeService _episodeService;
|
2013-03-01 07:03:41 +00:00
|
|
|
|
private readonly IMediaFileService _mediaFileService;
|
2011-05-21 00:23:49 +00:00
|
|
|
|
private readonly UpdateInfoJob _updateInfoJob;
|
|
|
|
|
private readonly DiskScanJob _diskScanJob;
|
2011-09-10 08:42:05 +00:00
|
|
|
|
private readonly BannerDownloadJob _bannerDownloadJob;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
private readonly ISeasonRepository _seasonRepository;
|
2012-10-18 04:15:42 +00:00
|
|
|
|
private readonly XemUpdateJob _xemUpdateJob;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
private readonly ISeriesRepository _seriesRepository;
|
|
|
|
|
private readonly ISeasonService _seasonService;
|
2011-05-21 00:23:49 +00:00
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-05-27 02:12:28 +00:00
|
|
|
|
private List<int> _attemptedSeries;
|
|
|
|
|
|
2013-02-22 00:47:09 +00:00
|
|
|
|
public ImportNewSeriesJob(ISeriesService seriesService, IEpisodeService episodeService,
|
2013-03-01 07:03:41 +00:00
|
|
|
|
IMediaFileService mediaFileService, UpdateInfoJob updateInfoJob,
|
2012-10-18 04:15:42 +00:00
|
|
|
|
DiskScanJob diskScanJob, BannerDownloadJob bannerDownloadJob,
|
2013-02-20 02:05:15 +00:00
|
|
|
|
ISeasonRepository seasonRepository, XemUpdateJob xemUpdateJob, ISeriesRepository seriesRepository,ISeasonService seasonService)
|
2011-05-21 00:23:49 +00:00
|
|
|
|
{
|
2013-02-20 02:05:15 +00:00
|
|
|
|
_seriesService = seriesService;
|
|
|
|
|
_episodeService = episodeService;
|
2013-03-01 07:03:41 +00:00
|
|
|
|
_mediaFileService = mediaFileService;
|
2011-06-14 01:23:04 +00:00
|
|
|
|
_updateInfoJob = updateInfoJob;
|
2011-05-21 00:23:49 +00:00
|
|
|
|
_diskScanJob = diskScanJob;
|
2011-09-10 08:42:05 +00:00
|
|
|
|
_bannerDownloadJob = bannerDownloadJob;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
_seasonRepository = seasonRepository;
|
2012-10-18 04:15:42 +00:00
|
|
|
|
_xemUpdateJob = xemUpdateJob;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
_seriesRepository = seriesRepository;
|
|
|
|
|
_seasonService = seasonService;
|
2011-05-21 00:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
2012-12-25 00:15:26 +00:00
|
|
|
|
get { return "Import New Series"; }
|
2011-05-21 00:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-15 02:47:23 +00:00
|
|
|
|
public TimeSpan DefaultInterval
|
2011-05-21 00:23:49 +00:00
|
|
|
|
{
|
2012-12-25 00:15:26 +00:00
|
|
|
|
get { return TimeSpan.FromHours(6); }
|
2011-05-21 00:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-10 19:04:17 +00:00
|
|
|
|
public void Start(ProgressNotification notification, dynamic options)
|
2011-05-21 00:23:49 +00:00
|
|
|
|
{
|
2011-05-27 02:12:28 +00:00
|
|
|
|
_attemptedSeries = new List<int>();
|
2011-05-21 00:23:49 +00:00
|
|
|
|
ScanSeries(notification);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ScanSeries(ProgressNotification notification)
|
|
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
|
var syncList = _seriesRepository.All().Where(s => s.LastInfoSync == null && !_attemptedSeries.Contains(s.Id)).ToList();
|
2011-05-21 00:23:49 +00:00
|
|
|
|
if (syncList.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var currentSeries in syncList)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
|
_attemptedSeries.Add(((ModelBase)currentSeries).Id);
|
2011-05-21 00:23:49 +00:00
|
|
|
|
notification.CurrentMessage = String.Format("Searching for '{0}'", new DirectoryInfo(currentSeries.Path).Name);
|
|
|
|
|
|
2013-02-26 03:58:57 +00:00
|
|
|
|
_updateInfoJob.Start(notification, new { SeriesId = ((ModelBase)currentSeries).Id });
|
|
|
|
|
_diskScanJob.Start(notification, new { SeriesId = ((ModelBase)currentSeries).Id });
|
2011-05-21 00:23:49 +00:00
|
|
|
|
|
2013-02-26 03:58:57 +00:00
|
|
|
|
var updatedSeries = _seriesRepository.Get(((ModelBase)currentSeries).Id);
|
|
|
|
|
AutoIgnoreSeasons(((ModelBase)updatedSeries).Id);
|
2011-05-21 00:23:49 +00:00
|
|
|
|
|
2011-09-10 08:42:05 +00:00
|
|
|
|
//Download the banner for the new series
|
2013-02-26 03:58:57 +00:00
|
|
|
|
_bannerDownloadJob.Start(notification, new { SeriesId = ((ModelBase)updatedSeries).Id });
|
2011-07-06 06:17:21 +00:00
|
|
|
|
|
2012-10-18 04:15:42 +00:00
|
|
|
|
//Get Scene Numbering if applicable
|
2013-02-26 03:58:57 +00:00
|
|
|
|
_xemUpdateJob.Start(notification, new { SeriesId = ((ModelBase)updatedSeries).Id });
|
2012-10-18 04:15:42 +00:00
|
|
|
|
|
2011-09-10 08:42:05 +00:00
|
|
|
|
notification.CurrentMessage = String.Format("{0} was successfully imported", updatedSeries.Title);
|
2011-05-21 00:23:49 +00:00
|
|
|
|
}
|
2011-09-10 08:42:05 +00:00
|
|
|
|
|
2011-05-21 00:23:49 +00:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException(e.Message, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Keep scanning until there no more shows left.
|
|
|
|
|
ScanSeries(notification);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-19 20:43:33 +00:00
|
|
|
|
public void AutoIgnoreSeasons(int seriesId)
|
2011-05-21 00:23:49 +00:00
|
|
|
|
{
|
2013-02-27 22:29:08 +00:00
|
|
|
|
//Todo: Need to convert this over to ObjectDb
|
|
|
|
|
return;
|
2013-03-01 07:03:41 +00:00
|
|
|
|
var episodeFiles = _mediaFileService.GetFilesBySeries(seriesId);
|
2011-06-19 20:43:33 +00:00
|
|
|
|
|
2011-06-04 01:56:53 +00:00
|
|
|
|
if (episodeFiles.Count() != 0)
|
2011-05-21 00:23:49 +00:00
|
|
|
|
{
|
2013-02-20 02:05:15 +00:00
|
|
|
|
var seasons = _seasonRepository.GetSeasonNumbers(seriesId);
|
2011-06-04 01:56:53 +00:00
|
|
|
|
var currentSeasons = seasons.Max();
|
|
|
|
|
|
|
|
|
|
foreach (var season in seasons)
|
|
|
|
|
{
|
2011-06-19 20:43:33 +00:00
|
|
|
|
if (season != currentSeasons && !episodeFiles.Any(e => e.SeasonNumber == season))
|
2011-06-04 01:56:53 +00:00
|
|
|
|
{
|
2013-02-20 02:05:15 +00:00
|
|
|
|
_seasonService.SetIgnore(seriesId, season, true);
|
2011-06-04 01:56:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-21 00:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|