2011-06-14 04:15:55 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using NLog;
|
2012-01-13 04:22:28 +01:00
|
|
|
|
using Newtonsoft.Json;
|
2012-02-11 01:48:20 +01:00
|
|
|
|
using NzbDrone.Common;
|
2011-06-14 04:15:55 +02:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-17 08:04:01 +02:00
|
|
|
|
using PetaPoco;
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
2011-06-17 08:04:01 +02:00
|
|
|
|
public class SceneMappingProvider
|
2011-06-14 04:15:55 +02:00
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-06-17 08:04:01 +02:00
|
|
|
|
private readonly IDatabase _database;
|
2011-06-14 04:15:55 +02:00
|
|
|
|
private readonly HttpProvider _httpProvider;
|
2012-02-04 06:28:50 +01:00
|
|
|
|
private readonly ConfigProvider _configProvider;
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
2012-02-04 06:28:50 +01:00
|
|
|
|
public SceneMappingProvider(IDatabase database, HttpProvider httpProvider, ConfigProvider configProvider)
|
2011-06-14 04:15:55 +02:00
|
|
|
|
{
|
2011-06-17 08:04:01 +02:00
|
|
|
|
_database = database;
|
2011-06-14 04:15:55 +02:00
|
|
|
|
_httpProvider = httpProvider;
|
2012-02-04 06:28:50 +01:00
|
|
|
|
_configProvider = configProvider;
|
2011-06-14 04:15:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-17 08:04:01 +02:00
|
|
|
|
public SceneMappingProvider()
|
2011-06-14 07:52:12 +02:00
|
|
|
|
{
|
2011-07-06 09:36:49 +02:00
|
|
|
|
|
2011-06-14 07:52:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-14 04:15:55 +02:00
|
|
|
|
public virtual bool UpdateMappings()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-02-04 06:28:50 +01:00
|
|
|
|
var mappingsJson = _httpProvider.DownloadString(_configProvider.ServiceRootUrl + "/SceneMapping/Active");
|
2012-01-13 04:22:28 +01:00
|
|
|
|
var mappings = JsonConvert.DeserializeObject<List<SceneMapping>>(mappingsJson);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
Logger.Debug("Deleting all existing Scene Mappings.");
|
2011-06-17 08:04:01 +02:00
|
|
|
|
_database.Delete<SceneMapping>(String.Empty);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
Logger.Debug("Adding Scene Mappings");
|
2012-01-13 04:22:28 +01:00
|
|
|
|
_database.InsertMany(mappings);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2011-12-02 05:24:44 +01:00
|
|
|
|
Logger.InfoException("Failed to Update Scene Mappings:", ex);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string GetSceneName(int seriesId)
|
|
|
|
|
{
|
2011-12-02 05:24:44 +01:00
|
|
|
|
UpdateIfEmpty();
|
|
|
|
|
|
2011-08-23 08:07:04 +02:00
|
|
|
|
var item = _database.FirstOrDefault<SceneMapping>("WHERE SeriesId = @0", seriesId);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return item.SceneName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Nullable<Int32> GetSeriesId(string cleanName)
|
|
|
|
|
{
|
2011-12-02 05:24:44 +01:00
|
|
|
|
UpdateIfEmpty();
|
|
|
|
|
|
2011-06-17 08:04:01 +02:00
|
|
|
|
var item = _database.SingleOrDefault<SceneMapping>("WHERE CleanTitle = @0", cleanName);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return item.SeriesId;
|
|
|
|
|
}
|
2011-12-02 05:24:44 +01:00
|
|
|
|
|
|
|
|
|
public void UpdateIfEmpty()
|
|
|
|
|
{
|
|
|
|
|
var count = _database.ExecuteScalar<int>("SELECT COUNT(*) FROM SceneMappings");
|
|
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
|
UpdateMappings();
|
|
|
|
|
}
|
2012-02-27 02:45:07 +01:00
|
|
|
|
|
|
|
|
|
public virtual bool SubmitMapping(int id, string postTitle)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Parsing example post");
|
|
|
|
|
var episodeParseResult = Parser.ParseTitle(postTitle);
|
|
|
|
|
var cleanTitle = episodeParseResult.CleanTitle;
|
|
|
|
|
var title = episodeParseResult.SeriesTitle.Replace('.', ' ');
|
|
|
|
|
Logger.Trace("Example post parsed. CleanTitle: {0}, Title: {1}", cleanTitle, title);
|
|
|
|
|
|
|
|
|
|
var newMapping = String.Format("/SceneMapping/AddPending?cleanTitle={0}&id={1}&title={2}", cleanTitle, id, title);
|
|
|
|
|
var response = _httpProvider.DownloadString(_configProvider.ServiceRootUrl + newMapping);
|
|
|
|
|
|
|
|
|
|
if (JsonConvert.DeserializeObject<String>(response).Equals("Ok"))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-12-19 17:41:51 +01:00
|
|
|
|
|
|
|
|
|
public virtual string GetCleanName(int seriesId)
|
|
|
|
|
{
|
|
|
|
|
var item = _database.FirstOrDefault<SceneMapping>("WHERE SeriesId = @0", seriesId);
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return item.CleanTitle;
|
|
|
|
|
}
|
2011-06-14 04:15:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|