2011-06-14 05:15:55 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using NLog;
|
2012-01-13 06:22:28 +03:00
|
|
|
using Newtonsoft.Json;
|
2012-02-11 03:48:20 +03:00
|
|
|
using NzbDrone.Common;
|
2011-06-14 05:15:55 +03:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-17 09:04:01 +03:00
|
|
|
using PetaPoco;
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
2011-06-17 09:04:01 +03:00
|
|
|
public class SceneMappingProvider
|
2011-06-14 05:15:55 +03:00
|
|
|
{
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-06-17 09:04:01 +03:00
|
|
|
private readonly IDatabase _database;
|
2011-06-14 05:15:55 +03:00
|
|
|
private readonly HttpProvider _httpProvider;
|
2012-02-04 08:28:50 +03:00
|
|
|
private readonly ConfigProvider _configProvider;
|
2011-06-14 05:15:55 +03:00
|
|
|
|
2012-02-04 08:28:50 +03:00
|
|
|
public SceneMappingProvider(IDatabase database, HttpProvider httpProvider, ConfigProvider configProvider)
|
2011-06-14 05:15:55 +03:00
|
|
|
{
|
2011-06-17 09:04:01 +03:00
|
|
|
_database = database;
|
2011-06-14 05:15:55 +03:00
|
|
|
_httpProvider = httpProvider;
|
2012-02-04 08:28:50 +03:00
|
|
|
_configProvider = configProvider;
|
2011-06-14 05:15:55 +03:00
|
|
|
}
|
|
|
|
|
2011-06-17 09:04:01 +03:00
|
|
|
public SceneMappingProvider()
|
2011-06-14 08:52:12 +03:00
|
|
|
{
|
2011-07-06 10:36:49 +03:00
|
|
|
|
2011-06-14 08:52:12 +03:00
|
|
|
}
|
|
|
|
|
2011-06-14 05:15:55 +03:00
|
|
|
public virtual bool UpdateMappings()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-02-04 08:28:50 +03:00
|
|
|
var mappingsJson = _httpProvider.DownloadString(_configProvider.ServiceRootUrl + "/SceneMapping/Active");
|
2012-01-13 06:22:28 +03:00
|
|
|
var mappings = JsonConvert.DeserializeObject<List<SceneMapping>>(mappingsJson);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
Logger.Debug("Deleting all existing Scene Mappings.");
|
2011-06-17 09:04:01 +03:00
|
|
|
_database.Delete<SceneMapping>(String.Empty);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
Logger.Debug("Adding Scene Mappings");
|
2012-01-13 06:22:28 +03:00
|
|
|
_database.InsertMany(mappings);
|
2011-06-14 05:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2011-12-02 07:24:44 +03:00
|
|
|
Logger.InfoException("Failed to Update Scene Mappings:", ex);
|
2011-06-14 05:15:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual string GetSceneName(int seriesId)
|
|
|
|
{
|
2011-12-02 07:24:44 +03:00
|
|
|
UpdateIfEmpty();
|
|
|
|
|
2011-08-23 09:07:04 +03:00
|
|
|
var item = _database.FirstOrDefault<SceneMapping>("WHERE SeriesId = @0", seriesId);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return item.SceneName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual Nullable<Int32> GetSeriesId(string cleanName)
|
|
|
|
{
|
2011-12-02 07:24:44 +03:00
|
|
|
UpdateIfEmpty();
|
|
|
|
|
2011-06-17 09:04:01 +03:00
|
|
|
var item = _database.SingleOrDefault<SceneMapping>("WHERE CleanTitle = @0", cleanName);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return item.SeriesId;
|
|
|
|
}
|
2011-12-02 07:24:44 +03:00
|
|
|
|
|
|
|
public void UpdateIfEmpty()
|
|
|
|
{
|
|
|
|
var count = _database.ExecuteScalar<int>("SELECT COUNT(*) FROM SceneMappings");
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
UpdateMappings();
|
|
|
|
}
|
2012-02-27 04:45:07 +03: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;
|
|
|
|
}
|
2011-06-14 05:15:55 +03:00
|
|
|
}
|
|
|
|
}
|