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;
|
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;
|
|
|
|
|
|
2011-06-17 08:04:01 +02:00
|
|
|
|
public SceneMappingProvider(IDatabase database, HttpProvider httpProvider)
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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-01-13 04:22:28 +01:00
|
|
|
|
const string url = "http://services.nzbdrone.com/SceneMapping/Active";
|
|
|
|
|
|
|
|
|
|
var mappingsJson = _httpProvider.DownloadString(url);
|
|
|
|
|
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();
|
|
|
|
|
}
|
2011-06-14 04:15:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|