mirror of
https://github.com/Radarr/Radarr.git
synced 2026-04-18 21:35:51 -04:00
Moved source code under src folder - massive change
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NLog;
|
||||
using NzbDrone.Common;
|
||||
using NzbDrone.Common.Serializer;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.DailySeries
|
||||
{
|
||||
|
||||
public interface IDailySeriesDataProxy
|
||||
{
|
||||
IEnumerable<int> GetDailySeriesIds();
|
||||
bool IsDailySeries(int tvdbid);
|
||||
}
|
||||
|
||||
public class DailySeriesDataProxy : IDailySeriesDataProxy
|
||||
{
|
||||
private readonly IHttpProvider _httpProvider;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public DailySeriesDataProxy(IHttpProvider httpProvider, Logger logger)
|
||||
{
|
||||
_httpProvider = httpProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public IEnumerable<int> GetDailySeriesIds()
|
||||
{
|
||||
try
|
||||
{
|
||||
var dailySeriesIds = _httpProvider.DownloadString(Services.RootUrl + "/v1/DailySeries");
|
||||
|
||||
var seriesIds = Json.Deserialize<List<int>>(dailySeriesIds);
|
||||
|
||||
return seriesIds;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.WarnException("Failed to get Daily Series", ex);
|
||||
return new List<int>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public bool IsDailySeries(int tvdbid)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = _httpProvider.DownloadString(Services.RootUrl + "/v1/DailySeries?seriesId=" + tvdbid);
|
||||
return Convert.ToBoolean(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.WarnException("Failed to check Daily Series status for: " + tvdbid, ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.DailySeries
|
||||
{
|
||||
public interface IDailySeriesService
|
||||
{
|
||||
void UpdateDailySeries();
|
||||
bool IsDailySeries(int tvdbid);
|
||||
}
|
||||
|
||||
public class DailySeriesService : IDailySeriesService
|
||||
{
|
||||
//TODO: add timer command
|
||||
|
||||
private readonly IDailySeriesDataProxy _proxy;
|
||||
private readonly ISeriesService _seriesService;
|
||||
|
||||
public DailySeriesService(IDailySeriesDataProxy proxy, ISeriesService seriesService)
|
||||
{
|
||||
_proxy = proxy;
|
||||
_seriesService = seriesService;
|
||||
}
|
||||
|
||||
public void UpdateDailySeries()
|
||||
{
|
||||
var dailySeries = _proxy.GetDailySeriesIds();
|
||||
|
||||
foreach (var tvdbId in dailySeries)
|
||||
{
|
||||
var series = _seriesService.FindByTvdbId(tvdbId);
|
||||
|
||||
if (series != null)
|
||||
{
|
||||
_seriesService.SetSeriesType(series.Id, SeriesTypes.Daily);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsDailySeries(int tvdbid)
|
||||
{
|
||||
return _proxy.IsDailySeries(tvdbid);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/NzbDrone.Core/DataAugmentation/Scene/SceneMapping.cs
Normal file
19
src/NzbDrone.Core/DataAugmentation/Scene/SceneMapping.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Newtonsoft.Json;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
public class SceneMapping : ModelBase
|
||||
{
|
||||
[JsonProperty("title")]
|
||||
public string ParseTerm { get; set; }
|
||||
|
||||
[JsonProperty("searchTitle")]
|
||||
public string SearchTerm { get; set; }
|
||||
|
||||
public int TvdbId { get; set; }
|
||||
|
||||
[JsonProperty("season")]
|
||||
public int SeasonNumber { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using NzbDrone.Common;
|
||||
using NzbDrone.Common.Serializer;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
public interface ISceneMappingProxy
|
||||
{
|
||||
List<SceneMapping> Fetch();
|
||||
}
|
||||
|
||||
public class SceneMappingProxy : ISceneMappingProxy
|
||||
{
|
||||
private readonly IHttpProvider _httpProvider;
|
||||
|
||||
public SceneMappingProxy(IHttpProvider httpProvider)
|
||||
{
|
||||
_httpProvider = httpProvider;
|
||||
}
|
||||
|
||||
public List<SceneMapping> Fetch()
|
||||
{
|
||||
var mappingsJson = _httpProvider.DownloadString(Services.RootUrl + "/v1/SceneMapping");
|
||||
return Json.Deserialize<List<SceneMapping>>(mappingsJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
public interface ISceneMappingRepository : IBasicRepository<SceneMapping>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class SceneMappingRepository : BasicRepository<SceneMapping>, ISceneMappingRepository
|
||||
{
|
||||
public SceneMappingRepository(IDatabase database, IEventAggregator eventAggregator)
|
||||
: base(database, eventAggregator)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
114
src/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs
Normal file
114
src/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Core.Lifecycle;
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.Parser;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
public interface ISceneMappingService
|
||||
{
|
||||
string GetSceneName(int tvdbId);
|
||||
Nullable<int> GetTvDbId(string cleanName);
|
||||
}
|
||||
|
||||
public class SceneMappingService : ISceneMappingService,
|
||||
IHandleAsync<ApplicationStartedEvent>,
|
||||
IExecute<UpdateSceneMappingCommand>
|
||||
{
|
||||
private readonly ISceneMappingRepository _repository;
|
||||
private readonly ISceneMappingProxy _sceneMappingProxy;
|
||||
private readonly Logger _logger;
|
||||
private readonly ICached<SceneMapping> _getSceneNameCache;
|
||||
private readonly ICached<SceneMapping> _gettvdbIdCache;
|
||||
|
||||
public SceneMappingService(ISceneMappingRepository repository, ISceneMappingProxy sceneMappingProxy, ICacheManger cacheManger, Logger logger)
|
||||
{
|
||||
_repository = repository;
|
||||
_sceneMappingProxy = sceneMappingProxy;
|
||||
|
||||
_getSceneNameCache = cacheManger.GetCache<SceneMapping>(GetType(), "scene_name");
|
||||
_gettvdbIdCache = cacheManger.GetCache<SceneMapping>(GetType(), "tvdb_id");
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string GetSceneName(int tvdbId)
|
||||
{
|
||||
var mapping = _getSceneNameCache.Find(tvdbId.ToString());
|
||||
|
||||
if (mapping == null) return null;
|
||||
|
||||
return mapping.SearchTerm;
|
||||
}
|
||||
|
||||
public Nullable<Int32> GetTvDbId(string cleanName)
|
||||
{
|
||||
var mapping = _gettvdbIdCache.Find(cleanName.CleanSeriesTitle());
|
||||
|
||||
if (mapping == null)
|
||||
return null;
|
||||
|
||||
return mapping.TvdbId;
|
||||
}
|
||||
|
||||
private void UpdateMappings()
|
||||
{
|
||||
_logger.Info("Updating Scene mapping");
|
||||
|
||||
try
|
||||
{
|
||||
var mappings = _sceneMappingProxy.Fetch();
|
||||
|
||||
if (mappings.Any())
|
||||
{
|
||||
_repository.Purge();
|
||||
|
||||
foreach (var sceneMapping in mappings)
|
||||
{
|
||||
sceneMapping.ParseTerm = sceneMapping.ParseTerm.CleanSeriesTitle();
|
||||
}
|
||||
|
||||
_repository.InsertMany(mappings);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Warn("Received empty list of mapping. will not update.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Failed to Update Scene Mappings:", ex);
|
||||
}
|
||||
|
||||
RefreshCache();
|
||||
}
|
||||
|
||||
private void RefreshCache()
|
||||
{
|
||||
var mappings = _repository.All();
|
||||
|
||||
_gettvdbIdCache.Clear();
|
||||
_getSceneNameCache.Clear();
|
||||
|
||||
foreach (var sceneMapping in mappings)
|
||||
{
|
||||
_getSceneNameCache.Set(sceneMapping.TvdbId.ToString(), sceneMapping);
|
||||
_gettvdbIdCache.Set(sceneMapping.ParseTerm.CleanSeriesTitle(), sceneMapping);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void HandleAsync(ApplicationStartedEvent message)
|
||||
{
|
||||
UpdateMappings();
|
||||
}
|
||||
|
||||
public void Execute(UpdateSceneMappingCommand message)
|
||||
{
|
||||
UpdateMappings();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
public class UpdateSceneMappingCommand : Command
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace NzbDrone.Core.DataAugmentation.Xem.Model
|
||||
{
|
||||
public class XemResult<T>
|
||||
{
|
||||
public string Result { get; set; }
|
||||
public T Data { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace NzbDrone.Core.DataAugmentation.Xem.Model
|
||||
{
|
||||
public class XemSceneTvdbMapping
|
||||
{
|
||||
public XemValues Scene { get; set; }
|
||||
public XemValues Tvdb { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace NzbDrone.Core.DataAugmentation.Xem.Model
|
||||
{
|
||||
public class XemValues
|
||||
{
|
||||
public int Season { get; set; }
|
||||
public int Episode { get; set; }
|
||||
public int Absolute { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Xem
|
||||
{
|
||||
public class UpdateXemMappingsCommand : Command
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
76
src/NzbDrone.Core/DataAugmentation/Xem/XemProxy.cs
Normal file
76
src/NzbDrone.Core/DataAugmentation/Xem/XemProxy.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Core.DataAugmentation.Xem.Model;
|
||||
using NzbDrone.Core.Rest;
|
||||
using RestSharp;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Xem
|
||||
{
|
||||
public interface IXemProxy
|
||||
{
|
||||
List<int> GetXemSeriesIds();
|
||||
List<XemSceneTvdbMapping> GetSceneTvdbMappings(int id);
|
||||
}
|
||||
|
||||
public class XemProxy : IXemProxy
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
private const string XEM_BASE_URL = "http://thexem.de/map/";
|
||||
|
||||
public XemProxy(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
private static RestRequest BuildRequest(string resource)
|
||||
{
|
||||
var req = new RestRequest(resource, Method.GET);
|
||||
req.AddParameter("origin", "tvdb");
|
||||
return req;
|
||||
}
|
||||
|
||||
public List<int> GetXemSeriesIds()
|
||||
{
|
||||
_logger.Trace("Fetching Series IDs from");
|
||||
|
||||
var restClient = new RestClient(XEM_BASE_URL);
|
||||
|
||||
var request = BuildRequest("havemap");
|
||||
|
||||
var response = restClient.ExecuteAndValidate<XemResult<List<int>>>(request);
|
||||
CheckForFailureResult(response);
|
||||
|
||||
return response.Data.ToList();
|
||||
}
|
||||
|
||||
public List<XemSceneTvdbMapping> GetSceneTvdbMappings(int id)
|
||||
{
|
||||
_logger.Trace("Fetching Mappings for: {0}", id);
|
||||
var url = String.Format("{0}all?id={1}&origin=tvdb", XEM_BASE_URL, id);
|
||||
|
||||
|
||||
var restClient = new RestClient(XEM_BASE_URL);
|
||||
|
||||
var request = BuildRequest("all");
|
||||
request.AddParameter("id", id);
|
||||
|
||||
var response = restClient.ExecuteAndValidate<XemResult<List<XemSceneTvdbMapping>>>(request);
|
||||
CheckForFailureResult(response);
|
||||
|
||||
return response.Data;
|
||||
}
|
||||
|
||||
private static void CheckForFailureResult<T>(XemResult<T> response)
|
||||
{
|
||||
if (response.Result.Equals("failure", StringComparison.InvariantCultureIgnoreCase) &&
|
||||
!response.Message.Contains("no show with the tvdb_id"))
|
||||
{
|
||||
throw new Exception("Error response received from Xem: " + response.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
150
src/NzbDrone.Core/DataAugmentation/Xem/XemService.cs
Normal file
150
src/NzbDrone.Core/DataAugmentation/Xem/XemService.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Core.Lifecycle;
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.Tv;
|
||||
using NzbDrone.Core.Tv.Events;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Xem
|
||||
{
|
||||
public class XemService : IExecute<UpdateXemMappingsCommand>, IHandle<SeriesUpdatedEvent>, IHandleAsync<ApplicationStartedEvent>
|
||||
{
|
||||
private readonly IEpisodeService _episodeService;
|
||||
private readonly IXemProxy _xemProxy;
|
||||
private readonly ISeriesService _seriesService;
|
||||
private readonly Logger _logger;
|
||||
private readonly ICached<bool> _cache;
|
||||
|
||||
public XemService(IEpisodeService episodeService,
|
||||
IXemProxy xemProxy,
|
||||
ISeriesService seriesService, ICacheManger cacheManger, Logger logger)
|
||||
{
|
||||
if (seriesService == null) throw new ArgumentNullException("seriesService");
|
||||
_episodeService = episodeService;
|
||||
_xemProxy = xemProxy;
|
||||
_seriesService = seriesService;
|
||||
_logger = logger;
|
||||
_logger = logger;
|
||||
_cache = cacheManger.GetCache<bool>(GetType());
|
||||
}
|
||||
|
||||
|
||||
public void Execute(UpdateXemMappingsCommand message)
|
||||
{
|
||||
UpdateMappings();
|
||||
}
|
||||
|
||||
public void Handle(SeriesUpdatedEvent message)
|
||||
{
|
||||
UpdateMappings(message.Series);
|
||||
}
|
||||
|
||||
public void HandleAsync(ApplicationStartedEvent message)
|
||||
{
|
||||
GetXemSeriesIds();
|
||||
}
|
||||
|
||||
private void UpdateMappings()
|
||||
{
|
||||
_logger.Trace("Starting scene numbering update");
|
||||
|
||||
try
|
||||
{
|
||||
var ids = GetXemSeriesIds();
|
||||
var series = _seriesService.GetAllSeries();
|
||||
var wantedSeries = series.Where(s => ids.Contains(s.TvdbId)).ToList();
|
||||
|
||||
foreach (var ser in wantedSeries)
|
||||
{
|
||||
PerformUpdate(ser);
|
||||
}
|
||||
|
||||
_logger.Trace("Completed scene numbering update");
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.WarnException("Error updating Scene Mappings", ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateMappings(Series series)
|
||||
{
|
||||
if (!_cache.Find(series.TvdbId.ToString()))
|
||||
{
|
||||
_logger.Trace("Scene numbering is not available for {0} [{1}]", series.Title, series.TvdbId);
|
||||
return;
|
||||
}
|
||||
|
||||
PerformUpdate(series);
|
||||
}
|
||||
|
||||
private void PerformUpdate(Series series)
|
||||
{
|
||||
_logger.Trace("Updating scene numbering mapping for: {0}", series);
|
||||
try
|
||||
{
|
||||
var episodesToUpdate = new List<Episode>();
|
||||
var mappings = _xemProxy.GetSceneTvdbMappings(series.TvdbId);
|
||||
|
||||
if (!mappings.Any())
|
||||
{
|
||||
_logger.Trace("Mappings for: {0} are empty, skipping", series);
|
||||
_cache.Remove(series.TvdbId.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
var episodes = _episodeService.GetEpisodeBySeries(series.Id);
|
||||
|
||||
foreach (var mapping in mappings)
|
||||
{
|
||||
_logger.Trace("Setting scene numbering mappings for {0} S{1:00}E{2:00}", series, mapping.Tvdb.Season, mapping.Tvdb.Episode);
|
||||
|
||||
var episode = episodes.SingleOrDefault(e => e.SeasonNumber == mapping.Tvdb.Season && e.EpisodeNumber == mapping.Tvdb.Episode);
|
||||
|
||||
if (episode == null)
|
||||
{
|
||||
_logger.Trace("Information hasn't been added to TheTVDB yet, skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
episode.AbsoluteEpisodeNumber = mapping.Scene.Absolute;
|
||||
episode.SceneSeasonNumber = mapping.Scene.Season;
|
||||
episode.SceneEpisodeNumber = mapping.Scene.Episode;
|
||||
episodesToUpdate.Add(episode);
|
||||
}
|
||||
|
||||
_logger.Trace("Committing scene numbering mappings to database for: {0}", series);
|
||||
_episodeService.UpdateEpisodes(episodesToUpdate);
|
||||
|
||||
_logger.Trace("Setting UseSceneMapping for {0}", series);
|
||||
series.UseSceneNumbering = true;
|
||||
_seriesService.UpdateSeries(series);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error updating scene numbering mappings for: " + series, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private List<int> GetXemSeriesIds()
|
||||
{
|
||||
_cache.Clear();
|
||||
|
||||
var ids = _xemProxy.GetXemSeriesIds();
|
||||
|
||||
foreach (var id in ids)
|
||||
{
|
||||
_cache.Set(id.ToString(), true);
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user