2011-06-14 02:15:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-17 06:04:01 +00:00
|
|
|
|
using PetaPoco;
|
2011-06-14 02:15:55 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
2011-06-17 06:04:01 +00:00
|
|
|
|
public class SceneMappingProvider
|
2011-06-14 02:15:55 +00:00
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-06-17 06:04:01 +00:00
|
|
|
|
private readonly IDatabase _database;
|
2011-06-14 02:15:55 +00:00
|
|
|
|
private readonly HttpProvider _httpProvider;
|
|
|
|
|
|
2011-06-17 06:04:01 +00:00
|
|
|
|
public SceneMappingProvider(IDatabase database, HttpProvider httpProvider)
|
2011-06-14 02:15:55 +00:00
|
|
|
|
{
|
2011-06-17 06:04:01 +00:00
|
|
|
|
_database = database;
|
2011-06-14 02:15:55 +00:00
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-17 06:04:01 +00:00
|
|
|
|
public SceneMappingProvider()
|
2011-06-14 05:52:12 +00:00
|
|
|
|
{
|
2011-07-06 07:36:49 +00:00
|
|
|
|
|
2011-06-14 05:52:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-14 02:15:55 +00:00
|
|
|
|
public virtual bool UpdateMappings()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-06-17 06:04:01 +00:00
|
|
|
|
var mapping = _httpProvider.DownloadString("http://vps.nzbdrone.com/SceneMappings.csv");
|
|
|
|
|
var newMaps = new List<SceneMapping>();
|
2011-06-14 02:15:55 +00:00
|
|
|
|
|
|
|
|
|
using (var reader = new StringReader(mapping))
|
|
|
|
|
{
|
|
|
|
|
string line;
|
|
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
var split = line.Split(',');
|
2011-07-06 07:36:49 +00:00
|
|
|
|
int seriesId;
|
2011-06-14 02:15:55 +00:00
|
|
|
|
Int32.TryParse(split[1], out seriesId);
|
|
|
|
|
|
2011-06-17 06:04:01 +00:00
|
|
|
|
var map = new SceneMapping();
|
|
|
|
|
map.CleanTitle = split[0];
|
2011-06-14 02:15:55 +00:00
|
|
|
|
map.SeriesId = seriesId;
|
|
|
|
|
map.SceneName = split[2];
|
|
|
|
|
|
|
|
|
|
newMaps.Add(map);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Debug("Deleting all existing Scene Mappings.");
|
2011-06-17 06:04:01 +00:00
|
|
|
|
_database.Delete<SceneMapping>(String.Empty);
|
2011-06-14 02:15:55 +00:00
|
|
|
|
|
|
|
|
|
Logger.Debug("Adding Scene Mappings");
|
2011-06-17 06:04:01 +00:00
|
|
|
|
_database.InsertMany(newMaps);
|
2011-06-14 02:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.InfoException("Failed to Update Scene Mappings", ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string GetSceneName(int seriesId)
|
|
|
|
|
{
|
2011-08-23 06:07:04 +00:00
|
|
|
|
var item = _database.FirstOrDefault<SceneMapping>("WHERE SeriesId = @0", seriesId);
|
2011-06-14 02:15:55 +00:00
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return item.SceneName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Nullable<Int32> GetSeriesId(string cleanName)
|
|
|
|
|
{
|
2011-06-17 06:04:01 +00:00
|
|
|
|
var item = _database.SingleOrDefault<SceneMapping>("WHERE CleanTitle = @0", cleanName);
|
2011-06-14 02:15:55 +00:00
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return item.SeriesId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|