You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/NzbDrone.Core/Providers/SeriesProvider.cs

75 lines
2.6 KiB

using System.IO;
14 years ago
using System.Linq;
using log4net;
using NzbDrone.Core.Repository;
using SubSonic.Repository;
using TvdbLib.Data;
namespace NzbDrone.Core.Providers
14 years ago
{
public class SeriesProvider : ISeriesProvider
14 years ago
{
private readonly IConfigProvider _config;
private readonly IDiskProvider _diskProvider;
14 years ago
private readonly ILog _logger;
private readonly IRepository _sonioRepo;
private readonly ITvDbProvider _tvDb;
14 years ago
public SeriesProvider(ILog logger, IDiskProvider diskProvider, IConfigProvider configProvider, IRepository dataRepository, ITvDbProvider tvDbProvider)
14 years ago
{
_logger = logger;
_diskProvider = diskProvider;
_config = configProvider;
14 years ago
_sonioRepo = dataRepository;
_tvDb = tvDbProvider;
14 years ago
}
#region ISeriesProvider Members
14 years ago
public IQueryable<Series> GetSeries()
{
return _sonioRepo.All<Series>();
}
public Series GetSeries(int tvdbId)
{
return _sonioRepo.Single<Series>(s => s.TvdbId == tvdbId.ToString());
}
14 years ago
public void SyncSeriesWithDisk()
{
foreach (string seriesFolder in _diskProvider.GetDirectories(_config.SeriesRoot))
14 years ago
{
var cleanPath = DiskProvider.CleanPath(new DirectoryInfo(seriesFolder).FullName);
if (!_sonioRepo.Exists<Series>(s => s.Path == cleanPath))
14 years ago
{
_logger.InfoFormat("Folder '{0} isn't mapped to a series in the database. Trying to map it.'", cleanPath);
AddShow(cleanPath);
14 years ago
}
}
}
#endregion
private void AddShow(string path)
{
var searchResults = _tvDb.SearchSeries(new DirectoryInfo(path).Name);
if (searchResults.Count != 0 && !_sonioRepo.Exists<Series>(s => s.TvdbId == searchResults[0].Id.ToString()))
AddShow(path, _tvDb.GetSeries(searchResults[0].Id, searchResults[0].Language));
14 years ago
}
private void AddShow(string path, TvdbSeries series)
{
var repoSeries = new Series();
repoSeries.TvdbId = series.Id.ToString();
repoSeries.SeriesName = series.SeriesName;
repoSeries.AirTimes = series.AirsTime;
repoSeries.AirsDayOfWeek = series.AirsDayOfWeek;
repoSeries.Overview = series.Overview;
repoSeries.Status = series.Status;
repoSeries.Language = series.Language != null ? series.Language.Abbriviation : string.Empty;
repoSeries.Path = path;
_sonioRepo.Add(repoSeries);
14 years ago
}
}
}