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;
|
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;
|
|
|
|
|
2011-06-17 09:04:01 +03:00
|
|
|
public SceneMappingProvider(IDatabase database, HttpProvider httpProvider)
|
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;
|
|
|
|
}
|
|
|
|
|
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-01-13 06:22:28 +03: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 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();
|
|
|
|
}
|
2011-06-14 05:15:55 +03:00
|
|
|
}
|
|
|
|
}
|