mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-14 10:36:54 -07:00
The move!
This commit is contained in:
parent
1daf480b1b
commit
25526cc4d9
1147 changed files with 85 additions and 8524 deletions
118
Old/Ombi.Api/ApiRequest.cs
Normal file
118
Old/Ombi.Api/ApiRequest.cs
Normal file
|
@ -0,0 +1,118 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApiRequest.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Xml.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Helpers.Exceptions;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class ApiRequest : IApiRequest
|
||||
{
|
||||
private readonly JsonSerializerSettings _settings = new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore
|
||||
};
|
||||
|
||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||
/// <summary>
|
||||
/// An API request handler
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of class you want to deserialize</typeparam>
|
||||
/// <param name="request">The request.</param>
|
||||
/// <param name="baseUri">The base URI.</param>
|
||||
/// <returns>The type of class you want to deserialize</returns>
|
||||
public T Execute<T>(IRestRequest request, Uri baseUri) where T : new()
|
||||
{
|
||||
var client = new RestClient { BaseUrl = baseUri };
|
||||
var response = client.Execute<T>(request);
|
||||
Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}");
|
||||
if ((int)response.StatusCode < 300)
|
||||
return response.Data;
|
||||
else
|
||||
throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}.");
|
||||
|
||||
}
|
||||
|
||||
public IRestResponse Execute(IRestRequest request, Uri baseUri)
|
||||
{
|
||||
var client = new RestClient { BaseUrl = baseUri };
|
||||
var response = client.Execute(request);
|
||||
return response;
|
||||
}
|
||||
|
||||
public T ExecuteXml<T>(IRestRequest request, Uri baseUri) where T : class
|
||||
{
|
||||
var client = new RestClient { BaseUrl = baseUri };
|
||||
var response = client.Execute(request);
|
||||
Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}");
|
||||
if ((int)response.StatusCode < 300)
|
||||
return DeserializeXml<T>(response.Content);
|
||||
else
|
||||
throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}.");
|
||||
|
||||
}
|
||||
|
||||
public T ExecuteJson<T>(IRestRequest request, Uri baseUri) where T : new()
|
||||
{
|
||||
var client = new RestClient { BaseUrl = baseUri };
|
||||
var response = client.Execute(request);
|
||||
Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}");
|
||||
|
||||
if ((int)response.StatusCode < 300)
|
||||
return JsonConvert.DeserializeObject<T>(response.Content, _settings);
|
||||
else
|
||||
throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}.");
|
||||
}
|
||||
|
||||
private T DeserializeXml<T>(string input)
|
||||
where T : class
|
||||
{
|
||||
var ser = new XmlSerializer(typeof(T));
|
||||
|
||||
try
|
||||
{
|
||||
using (var sr = new StringReader(input))
|
||||
return (T)ser.Deserialize(sr);
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
Log.Error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
81
Old/Ombi.Api/AppveyorApi.cs
Normal file
81
Old/Ombi.Api/AppveyorApi.cs
Normal file
|
@ -0,0 +1,81 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2017 Jamie Rees
|
||||
// File: AppveyorApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Appveyor;
|
||||
using Ombi.Helpers;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class AppveyorApi : IAppveyorApi
|
||||
{
|
||||
private const string AppveyorApiUrl = "https://ci.appveyor.com/api";
|
||||
|
||||
private const string Key =
|
||||
"48Ku58C0794nBrXra8IxWav+dc6NqgkRw+PZB3/bQwbt/D0IrnJQkgtjzo0bd6nkooLMKsC8M+Ab7jyBO+ROjY14VRuxffpDopX9r0iG/fjBl6mZVvqkm+VTDNstDtzp";
|
||||
|
||||
|
||||
public AppveyorApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
private ApiRequest Api { get; set; }
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
//https://ci.appveyor.com/api/projects/tidusjar/requestplex/history?recordsNumber=10&branch=eap
|
||||
public AppveyorProjects GetProjectHistory(string branchName, int records = 10)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "projects/tidusjar/requestplex/history?recordsNumber={records}&branch={branch}",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddUrlSegment("records", records.ToString());
|
||||
request.AddUrlSegment("branch", branchName);
|
||||
AddHeaders(request);
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetProjectHistory for Appveyor, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<AppveyorProjects>(request, new Uri(AppveyorApiUrl)));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
private void AddHeaders(IRestRequest request)
|
||||
{
|
||||
request.AddHeader("Authorization", $"Bearer {Key}");
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
}
|
||||
}
|
||||
}
|
175
Old/Ombi.Api/CouchPotatoApi.cs
Normal file
175
Old/Ombi.Api/CouchPotatoApi.cs
Normal file
|
@ -0,0 +1,175 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: CouchPotatoApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Movie;
|
||||
using Ombi.Helpers;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class CouchPotatoApi : ICouchPotatoApi
|
||||
{
|
||||
public CouchPotatoApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
private ApiRequest Api { get; set; }
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public bool AddMovie(string imdbid, string apiKey, string title, Uri baseUrl, string profileId = default(string))
|
||||
{
|
||||
RestRequest request;
|
||||
request = string.IsNullOrEmpty(profileId)
|
||||
? new RestRequest { Resource = "/api/{apikey}/movie.add?title={title}&identifier={imdbid}" }
|
||||
: new RestRequest { Resource = "/api/{apikey}/movie.add?title={title}&identifier={imdbid}&profile_id={profileId}" };
|
||||
|
||||
if (!string.IsNullOrEmpty(profileId))
|
||||
{
|
||||
request.AddUrlSegment("profileId", profileId);
|
||||
}
|
||||
|
||||
request.AddUrlSegment("apikey", apiKey);
|
||||
request.AddUrlSegment("imdbid", imdbid);
|
||||
request.AddUrlSegment("title", title);
|
||||
|
||||
var obj = RetryHandler.Execute(() => Api.ExecuteJson<JObject>(request, baseUrl),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling AddMovie for CP, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (2)});
|
||||
|
||||
|
||||
if (obj.Count > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = (bool)obj["success"];
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Fatal(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the status.
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="apiKey">The API key.</param>
|
||||
/// <returns></returns>
|
||||
public CouchPotatoStatus GetStatus(Uri url, string apiKey)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "api/{apikey}/app.available/",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddUrlSegment("apikey", apiKey);
|
||||
|
||||
|
||||
var obj = RetryHandler.Execute<CouchPotatoStatus>(() => Api.Execute<CouchPotatoStatus>(request, url),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetStatus for CP, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(2),
|
||||
TimeSpan.FromSeconds(3)});
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public CouchPotatoProfiles GetProfiles(Uri url, string apiKey)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "api/{apikey}/profile.list/",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddUrlSegment("apikey", apiKey);
|
||||
|
||||
var obj = RetryHandler.Execute(() => Api.Execute<CouchPotatoProfiles>(request, url),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetProfiles for CP, Retrying {0}", timespan), null);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api/{apikey}/movie.list?status={status}",
|
||||
OnBeforeDeserialization = (x =>
|
||||
{
|
||||
x.Content = x.Content.Replace("[]", "{}");
|
||||
})
|
||||
};
|
||||
|
||||
request.AddUrlSegment("apikey", apiKey);
|
||||
request.AddUrlSegment("status", string.Join(",", status));
|
||||
try
|
||||
{
|
||||
var obj = RetryHandler.Execute(() => Api.Execute<CouchPotatoMovies>(request, baseUrl),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetMovies for CP, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(5),
|
||||
TimeSpan.FromSeconds(5)
|
||||
});
|
||||
|
||||
return obj;
|
||||
}
|
||||
catch (Exception e) // Request error is already logged in the ApiRequest class
|
||||
{
|
||||
Log.Error("Error when attempting to GetMovies.");
|
||||
Log.Error(e);
|
||||
return new CouchPotatoMovies();
|
||||
}
|
||||
}
|
||||
|
||||
public CouchPotatoApiKey GetApiKey(Uri baseUrl, string username, string password)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "getkey/?u={username}&p={password}",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddUrlSegment("username", StringHasher.CalcuateMd5Hash(username));
|
||||
request.AddUrlSegment("password", StringHasher.CalcuateMd5Hash(password));
|
||||
|
||||
var obj = RetryHandler.Execute(() => Api.Execute<CouchPotatoApiKey>(request, baseUrl),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetApiKey for CP, Retrying {0}", timespan), null);
|
||||
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
}
|
115
Old/Ombi.Api/DiscordApi.cs
Normal file
115
Old/Ombi.Api/DiscordApi.cs
Normal file
|
@ -0,0 +1,115 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2017 Jamie Rees
|
||||
// File: NetflixRouletteApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Netflix;
|
||||
using Ombi.Api.Models.Notifications;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class DiscordApi : IDiscordApi
|
||||
{
|
||||
public DiscordApi(IApiRequest req)
|
||||
{
|
||||
Api = req;
|
||||
}
|
||||
|
||||
private IApiRequest Api { get; }
|
||||
private Uri Endpoint => new Uri("https://discordapp.com/api/"); //webhooks/270828242636636161/lLysOMhJ96AFO1kvev0bSqP-WCZxKUh1UwfubhIcLkpS0DtM3cg4Pgeraw3waoTXbZii
|
||||
|
||||
|
||||
public void SendMessage(string message, string webhookId, string webhookToken, string username = null)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "webhooks/{webhookId}/{webhookToken}",
|
||||
Method = Method.POST
|
||||
};
|
||||
|
||||
request.AddUrlSegment("webhookId", webhookId);
|
||||
request.AddUrlSegment("webhookToken", webhookToken);
|
||||
|
||||
var body = new DiscordWebhookRequest
|
||||
{
|
||||
content = message,
|
||||
username = username
|
||||
};
|
||||
request.AddJsonBody(body);
|
||||
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
Api.Execute(request, Endpoint);
|
||||
}
|
||||
|
||||
public async Task SendMessageAsync(string message, string webhookId, string webhookToken, string username = null)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "webhooks/{webhookId}/{webhookToken}",
|
||||
Method = Method.POST
|
||||
};
|
||||
|
||||
request.AddUrlSegment("webhookId", webhookId);
|
||||
request.AddUrlSegment("webhookToken", webhookToken);
|
||||
|
||||
var body = new DiscordWebhookRequest
|
||||
{
|
||||
content = message,
|
||||
username = username
|
||||
};
|
||||
request.AddJsonBody(body);
|
||||
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
await Task.Run(
|
||||
() =>
|
||||
{
|
||||
Api.Execute(request, Endpoint);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
public NetflixMovieResult CheckNetflix(string title, string year = null)
|
||||
{
|
||||
var request = new RestRequest();
|
||||
request.AddQueryParameter("title", title);
|
||||
if (!string.IsNullOrEmpty(year))
|
||||
{
|
||||
request.AddQueryParameter("year", year);
|
||||
}
|
||||
var result = Api.Execute(request, Endpoint);
|
||||
|
||||
return JsonConvert.DeserializeObject<NetflixMovieResult>(result.Content);
|
||||
}
|
||||
}
|
||||
}
|
313
Old/Ombi.Api/EmbyApi.cs
Normal file
313
Old/Ombi.Api/EmbyApi.cs
Normal file
|
@ -0,0 +1,313 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2017 Jamie Rees
|
||||
// File: EmbyApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Emby;
|
||||
using Ombi.Helpers;
|
||||
using Polly;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class EmbyApi : IEmbyApi
|
||||
{
|
||||
public EmbyApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
|
||||
private ApiRequest Api { get; }
|
||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <summary>
|
||||
/// Returns all users from the Emby Instance
|
||||
/// </summary>
|
||||
/// <param name="baseUri"></param>
|
||||
/// <param name="apiKey"></param>
|
||||
public List<EmbyUser> GetUsers(Uri baseUri, string apiKey)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "emby/users",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
AddHeaders(request, apiKey);
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetUsers for Emby, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<List<EmbyUser>>(request, baseUri));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public EmbySystemInfo GetSystemInformation(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "emby/System/Info",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
AddHeaders(request, apiKey);
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetSystemInformation for Emby, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(5)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<EmbySystemInfo>(request, baseUrl));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public EmbyItemContainer<EmbyLibrary> ViewLibrary(string apiKey, string userId, Uri baseUri)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "emby/users/{userId}/items",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddUrlSegment("userId", userId);
|
||||
AddHeaders(request, apiKey);
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling ViewLibrary for Emby, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(5)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<EmbyItemContainer<EmbyLibrary>>(request, baseUri));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public EmbyItemContainer<EmbyMovieItem> GetAllMovies(string apiKey, string userId, Uri baseUri)
|
||||
{
|
||||
return GetAll<EmbyMovieItem>("Movie", apiKey, userId, baseUri);
|
||||
}
|
||||
|
||||
public EmbyItemContainer<EmbyEpisodeItem> GetAllEpisodes(string apiKey, string userId, Uri baseUri)
|
||||
{
|
||||
return GetAll<EmbyEpisodeItem>("Episode", apiKey, userId, baseUri);
|
||||
}
|
||||
|
||||
public EmbyItemContainer<EmbyMovieInformation> GetCollection(string mediaId, string apiKey, string userId, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "emby/users/{userId}/items?parentId={mediaId}",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddUrlSegment("userId", userId);
|
||||
request.AddUrlSegment("mediaId", mediaId);
|
||||
|
||||
AddHeaders(request, apiKey);
|
||||
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetCollections for Emby, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(5)
|
||||
});
|
||||
return policy.Execute(() => Api.ExecuteJson<EmbyItemContainer<EmbyMovieInformation>>(request, baseUrl));
|
||||
}
|
||||
|
||||
public EmbyInformation GetInformation(string mediaId, EmbyMediaType type, string apiKey, string userId, Uri baseUri)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "emby/users/{userId}/items/{mediaId}",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddUrlSegment("userId", userId);
|
||||
request.AddUrlSegment("mediaId", mediaId);
|
||||
|
||||
AddHeaders(request, apiKey);
|
||||
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetAll<T>({1}) for Emby, Retrying {0}", timespan, type), new[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(5)
|
||||
});
|
||||
|
||||
IRestResponse response = null;
|
||||
try
|
||||
{
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case EmbyMediaType.Movie:
|
||||
response = policy.Execute(() => Api.Execute(request, baseUri));
|
||||
break;
|
||||
|
||||
case EmbyMediaType.Series:
|
||||
response = policy.Execute(() => Api.Execute(request, baseUri));
|
||||
break;
|
||||
case EmbyMediaType.Music:
|
||||
break;
|
||||
case EmbyMediaType.Episode:
|
||||
response = policy.Execute(() => Api.Execute(request, baseUri));
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(type), type, null);
|
||||
}
|
||||
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case EmbyMediaType.Movie:
|
||||
return new EmbyInformation
|
||||
{
|
||||
MovieInformation = JsonConvert.DeserializeObject<EmbyMovieInformation>(response.Content)
|
||||
};
|
||||
case EmbyMediaType.Series:
|
||||
return new EmbyInformation
|
||||
{
|
||||
SeriesInformation = JsonConvert.DeserializeObject<EmbySeriesInformation>(response.Content)
|
||||
};
|
||||
case EmbyMediaType.Music:
|
||||
break;
|
||||
case EmbyMediaType.Episode:
|
||||
return new EmbyInformation
|
||||
{
|
||||
EpisodeInformation = JsonConvert.DeserializeObject<EmbyEpisodeInformation>(response.Content)
|
||||
};
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(type), type, null);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error("Could not get the media item's information");
|
||||
Log.Error(e);
|
||||
Log.Debug("ResponseContent");
|
||||
Log.Debug(response?.Content ?? "Empty");
|
||||
Log.Debug("ResponseStatusCode");
|
||||
Log.Debug(response?.StatusCode ?? HttpStatusCode.PreconditionFailed);
|
||||
|
||||
Log.Debug("ResponseError");
|
||||
Log.Debug(response?.ErrorMessage ?? "No Error");
|
||||
Log.Debug("ResponseException");
|
||||
Log.Debug(response?.ErrorException ?? new Exception());
|
||||
|
||||
|
||||
|
||||
throw;
|
||||
}
|
||||
return new EmbyInformation();
|
||||
}
|
||||
|
||||
|
||||
public EmbyItemContainer<EmbySeriesItem> GetAllShows(string apiKey, string userId, Uri baseUri)
|
||||
{
|
||||
return GetAll<EmbySeriesItem>("Series", apiKey, userId, baseUri);
|
||||
}
|
||||
|
||||
public EmbyUser LogIn(string username, string password, string apiKey, Uri baseUri)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "emby/users/authenticatebyname",
|
||||
Method = Method.POST
|
||||
};
|
||||
|
||||
var body = new
|
||||
{
|
||||
username,
|
||||
password = StringHasher.GetSha1Hash(password).ToLower(),
|
||||
passwordMd5 = StringHasher.CalcuateMd5Hash(password)
|
||||
};
|
||||
|
||||
request.AddJsonBody(body);
|
||||
|
||||
request.AddHeader("X-Emby-Authorization",
|
||||
$"MediaBrowser Client=\"Ombi\", Device=\"Ombi\", DeviceId=\"{AssemblyHelper.GetProductVersion()}\", Version=\"{AssemblyHelper.GetAssemblyVersion()}\"");
|
||||
AddHeaders(request, apiKey);
|
||||
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling LogInfor Emby, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (1)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.Execute(request, baseUri));
|
||||
|
||||
if (obj.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return JsonConvert.DeserializeObject<EmbyUserLogin>(obj.Content)?.User;
|
||||
}
|
||||
|
||||
private EmbyItemContainer<T> GetAll<T>(string type, string apiKey, string userId, Uri baseUri)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "emby/users/{userId}/items",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddUrlSegment("userId", userId);
|
||||
request.AddQueryParameter("Recursive", true.ToString());
|
||||
request.AddQueryParameter("IncludeItemTypes", type);
|
||||
|
||||
AddHeaders(request, apiKey);
|
||||
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetAll<T>({1}) for Emby, Retrying {0}", timespan, type), new[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(5)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<EmbyItemContainer<T>>(request, baseUri));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
private static void AddHeaders(IRestRequest req, string apiKey)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
req.AddHeader("X-MediaBrowser-Token", apiKey);
|
||||
}
|
||||
req.AddHeader("Accept", "application/json");
|
||||
req.AddHeader("Content-Type", "application/json");
|
||||
req.AddHeader("Device", "Ombi");
|
||||
}
|
||||
}
|
||||
}
|
201
Old/Ombi.Api/HeadphonesApi.cs
Normal file
201
Old/Ombi.Api/HeadphonesApi.cs
Normal file
|
@ -0,0 +1,201 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: HeadphonesApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Music;
|
||||
using Ombi.Helpers;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class HeadphonesApi : IHeadphonesApi
|
||||
{
|
||||
public HeadphonesApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
private ApiRequest Api { get; }
|
||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public async Task<bool> AddAlbum(string apiKey, Uri baseUrl, string albumId)
|
||||
{
|
||||
Log.Trace("Adding album: {0}", albumId);
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api?cmd=addAlbum&id={albumId}",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddQueryParameter("apikey", apiKey);
|
||||
request.AddUrlSegment("albumId", albumId);
|
||||
|
||||
try
|
||||
{
|
||||
var result = await Task.Run(() => Api.Execute(request, baseUrl)).ConfigureAwait(false);
|
||||
Log.Trace("Add Album Result: {0}", result.DumpJson());
|
||||
|
||||
var albumResult = result.Content.Equals("OK", StringComparison.CurrentCultureIgnoreCase);
|
||||
Log.Info("Album add result {0}", albumResult);
|
||||
|
||||
return albumResult;
|
||||
}
|
||||
catch (Exception jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
return false; // If there is no matching result we do not get returned a JSON string, it just returns "false".
|
||||
}
|
||||
}
|
||||
public async Task<List<HeadphonesGetIndex>> GetIndex(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddQueryParameter("apikey", apiKey);
|
||||
request.AddQueryParameter("cmd", "getIndex");
|
||||
|
||||
try
|
||||
{
|
||||
var result = await Task.Run(() => Api.ExecuteJson<List<HeadphonesGetIndex>>(request, baseUrl)).ConfigureAwait(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
return new List<HeadphonesGetIndex>();
|
||||
}
|
||||
}
|
||||
public async Task<bool> AddArtist(string apiKey, Uri baseUrl, string artistId)
|
||||
{
|
||||
Log.Trace("Adding Artist: {0}", artistId);
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddQueryParameter("apikey", apiKey);
|
||||
request.AddQueryParameter("cmd", "addArtist");
|
||||
request.AddQueryParameter("id", artistId);
|
||||
|
||||
try
|
||||
{
|
||||
var result = await Task.Run(() => Api.Execute(request, baseUrl)).ConfigureAwait(false);
|
||||
Log.Info("Add Artist Result: {0}", result.Content);
|
||||
Log.Trace("Add Artist Result: {0}", result.DumpJson());
|
||||
return result.Content.Equals("OK", StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
return false; // If there is no matching result we do not get returned a JSON string, it just returns "false".
|
||||
}
|
||||
}
|
||||
public async Task<bool> QueueAlbum(string apiKey, Uri baseUrl, string albumId)
|
||||
{
|
||||
Log.Trace("Queing album: {0}", albumId);
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api?cmd=queueAlbum&id={albumId}",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddQueryParameter("apikey", apiKey);
|
||||
request.AddUrlSegment("albumId", albumId);
|
||||
|
||||
try
|
||||
{
|
||||
var result = await Task.Run(() => Api.Execute(request, baseUrl)).ConfigureAwait(false);
|
||||
Log.Info("Queue Result: {0}", result.Content);
|
||||
Log.Trace("Queue Result: {0}", result.DumpJson());
|
||||
return result.Content.Equals("OK", StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
return false; // If there is no matching result we do not get returned a JSON string, it just returns "false".
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> RefreshArtist(string apiKey, Uri baseUrl, string artistId)
|
||||
{
|
||||
Log.Trace("Refreshing artist: {0}", artistId);
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddQueryParameter("apikey", apiKey);
|
||||
request.AddQueryParameter("cmd", "queueAlbum");
|
||||
request.AddQueryParameter("id", artistId);
|
||||
|
||||
try
|
||||
{
|
||||
var result = await Task.Run(() => Api.Execute(request, baseUrl)).ConfigureAwait(false);
|
||||
Log.Info("Artist refresh Result: {0}", result.Content);
|
||||
Log.Trace("Artist refresh Result: {0}", result.DumpJson());
|
||||
return result.Content.Equals("OK", StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
return false; // If there is no matching result we do not get returned a JSON string, it just returns "false".
|
||||
}
|
||||
}
|
||||
|
||||
public HeadphonesVersion GetVersion(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api",
|
||||
Method = Method.GET
|
||||
};
|
||||
|
||||
request.AddQueryParameter("apikey", apiKey);
|
||||
request.AddQueryParameter("cmd", "getVersion");
|
||||
|
||||
try
|
||||
{
|
||||
return Api.ExecuteJson<HeadphonesVersion>(request, baseUrl);
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
return new HeadphonesVersion(); // If there is no matching result we do not get returned a JSON string, it just returns "false".
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
Old/Ombi.Api/MovieBase.cs
Normal file
39
Old/Ombi.Api/MovieBase.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: TvBase.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using Ombi.Helpers;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public abstract class MovieBase
|
||||
{
|
||||
private static readonly string Encrypted = "0T3QNSseexLO7n7UPiJvl70Y+KKnvbeTlsusl7Kwq0hPH0BHOuFNGwksNCjkwqWedyDdI/MJeUR4wtL4bIl0Z+//uHXEaYM/4H2pjeLbH5EWdUe5TTj1AhaIR5PQweamvcienRyFD/3YPCC/+qL5mHkKXBkPumMod3Zb/4yN0Ik=";
|
||||
private string _apiKey;
|
||||
|
||||
protected string ApiKey => _apiKey ?? (_apiKey = StringCipher.Decrypt(Encrypted, "ApiKey"));
|
||||
}
|
||||
}
|
113
Old/Ombi.Api/MusicBrainzApi.cs
Normal file
113
Old/Ombi.Api/MusicBrainzApi.cs
Normal file
|
@ -0,0 +1,113 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: MusicBrainzApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Music;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class MusicBrainzApi : IMusicBrainzApi
|
||||
{
|
||||
public MusicBrainzApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
private ApiRequest Api { get; }
|
||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private readonly Uri BaseUri = new Uri("http://musicbrainz.org/ws/2/");
|
||||
|
||||
public MusicBrainzSearchResults SearchAlbum(string searchTerm)
|
||||
{
|
||||
Log.Trace("Searching for album: {0}", searchTerm);
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "release/?query={searchTerm}&fmt=json",
|
||||
Method = Method.GET
|
||||
};
|
||||
request.AddUrlSegment("searchTerm", searchTerm);
|
||||
|
||||
try
|
||||
{
|
||||
return Api.ExecuteJson<MusicBrainzSearchResults>(request, BaseUri);
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Warn(jse);
|
||||
return new MusicBrainzSearchResults(); // If there is no matching result we do not get returned a JSON string, it just returns "false".
|
||||
}
|
||||
}
|
||||
|
||||
public MusicBrainzReleaseInfo GetAlbum(string releaseId)
|
||||
{
|
||||
Log.Trace("Getting album: {0}", releaseId);
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "release/{albumId}",
|
||||
Method = Method.GET
|
||||
};
|
||||
request.AddUrlSegment("albumId", releaseId);
|
||||
request.AddQueryParameter("fmt", "json");
|
||||
request.AddQueryParameter("inc", "artists");
|
||||
|
||||
try
|
||||
{
|
||||
return Api.ExecuteJson<MusicBrainzReleaseInfo>(request, BaseUri);
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Warn(jse);
|
||||
return new MusicBrainzReleaseInfo(); // If there is no matching result we do not get returned a JSON string, it just returns "false".
|
||||
}
|
||||
}
|
||||
|
||||
public MusicBrainzCoverArt GetCoverArt(string releaseId)
|
||||
{
|
||||
Log.Trace("Getting cover art for release: {0}", releaseId);
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "release/{releaseId}",
|
||||
Method = Method.GET
|
||||
};
|
||||
request.AddUrlSegment("releaseId", releaseId);
|
||||
|
||||
try
|
||||
{
|
||||
return Api.Execute<MusicBrainzCoverArt>(request, new Uri("http://coverartarchive.org/"));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Warn(e);
|
||||
return new MusicBrainzCoverArt(); // If there is no matching result we do not get returned a JSON string, it just returns "false".
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
60
Old/Ombi.Api/NetflixRouletteApi.cs
Normal file
60
Old/Ombi.Api/NetflixRouletteApi.cs
Normal file
|
@ -0,0 +1,60 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2017 Jamie Rees
|
||||
// File: NetflixRouletteApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Netflix;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class NetflixRouletteApi : INetflixApi
|
||||
{
|
||||
public NetflixRouletteApi(IApiRequest req)
|
||||
{
|
||||
Api = req;
|
||||
}
|
||||
|
||||
private IApiRequest Api { get; }
|
||||
private Uri Endpoint => new Uri("http://netflixroulette.net/api/api.php");
|
||||
|
||||
public NetflixMovieResult CheckNetflix(string title, string year = null)
|
||||
{
|
||||
var request = new RestRequest();
|
||||
request.AddQueryParameter("title", title);
|
||||
if (!string.IsNullOrEmpty(year))
|
||||
{
|
||||
request.AddQueryParameter("year", year);
|
||||
}
|
||||
var result = Api.Execute(request, Endpoint);
|
||||
|
||||
return JsonConvert.DeserializeObject<NetflixMovieResult>(result.Content);
|
||||
}
|
||||
}
|
||||
}
|
130
Old/Ombi.Api/Ombi.Api.csproj
Normal file
130
Old/Ombi.Api/Ombi.Api.csproj
Normal file
|
@ -0,0 +1,130 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{8CB8D235-2674-442D-9C6A-35FCAEEB160D}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Ombi.Api</RootNamespace>
|
||||
<AssemblyName>Ombi.Api</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Dapper, Version=1.50.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Dapper.1.50.0-beta8\lib\net45\Dapper.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NLog.4.3.6\lib\net45\NLog.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Polly, Version=4.3.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Polly-Signed.4.3.0\lib\net45\Polly.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\RestSharp.105.2.3\lib\net45\RestSharp.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="Nancy, Version=1.4.2.0, Culture=neutral, PublicKeyToken=null">
|
||||
<HintPath>..\packages\Nancy.1.4.3\lib\net40\Nancy.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="TMDbLib, Version=0.9.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\TMDbLib.0.9.0.0-alpha\lib\net45\TMDbLib.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="TraktApiSharp, Version=0.8.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\TraktApiSharp.0.8.0\lib\portable-net45+netcore45+wpa81\TraktApiSharp.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="WebSocket4Net, Version=0.14.1.0, Culture=neutral, PublicKeyToken=eb4e154b696bf72a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\WebSocket4Net.0.14.1\lib\net45\WebSocket4Net.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ApiRequest.cs" />
|
||||
<Compile Include="AppveyorApi.cs" />
|
||||
<Compile Include="DiscordApi.cs" />
|
||||
<Compile Include="EmbyApi.cs" />
|
||||
<Compile Include="NetflixRouletteApi.cs" />
|
||||
<Compile Include="RadarrApi.cs" />
|
||||
<Compile Include="TraktApi.cs" />
|
||||
<Compile Include="WatcherApi.cs" />
|
||||
<Compile Include="MusicBrainzApi.cs" />
|
||||
<Compile Include="SlackApi.cs" />
|
||||
<Compile Include="PushoverApi.cs" />
|
||||
<Compile Include="PushbulletApi.cs" />
|
||||
<Compile Include="SickrageApi.cs" />
|
||||
<Compile Include="HeadphonesApi.cs" />
|
||||
<Compile Include="SonarrApi.cs" />
|
||||
<Compile Include="CouchPotatoApi.cs" />
|
||||
<Compile Include="MovieBase.cs" />
|
||||
<Compile Include="PlexApi.cs" />
|
||||
<Compile Include="TheMovieDbApi.cs" />
|
||||
<Compile Include="TvBase.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TheTvDbApi.cs" />
|
||||
<Compile Include="TvMazeApi.cs" />
|
||||
<Compile Include="TvMazeBase.cs" />
|
||||
<Compile Include="RetryHandler.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ombi.Api.Interfaces\Ombi.Api.Interfaces.csproj">
|
||||
<Project>{95834072-A675-415D-AA8F-877C91623810}</Project>
|
||||
<Name>Ombi.Api.Interfaces</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Ombi.Api.Models\Ombi.Api.Models.csproj">
|
||||
<Project>{CB37A5F8-6DFC-4554-99D3-A42B502E4591}</Project>
|
||||
<Name>Ombi.Api.Models</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Ombi.Helpers\Ombi.Helpers.csproj">
|
||||
<Project>{1252336D-42A3-482A-804C-836E60173DFA}</Project>
|
||||
<Name>Ombi.Helpers</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
427
Old/Ombi.Api/PlexApi.cs
Normal file
427
Old/Ombi.Api/PlexApi.cs
Normal file
|
@ -0,0 +1,427 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: PlexApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Plex;
|
||||
using Ombi.Helpers;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class PlexApi : IPlexApi
|
||||
{
|
||||
static PlexApi()
|
||||
{
|
||||
Version = AssemblyHelper.GetAssemblyVersion();
|
||||
}
|
||||
|
||||
public PlexApi (IApiRequest api)
|
||||
{
|
||||
Api = api;
|
||||
}
|
||||
|
||||
private IApiRequest Api { get; }
|
||||
|
||||
private const string SignInUri = "https://plex.tv/users/sign_in.json";
|
||||
private const string FriendsUri = "https://plex.tv/pms/friends/all";
|
||||
private const string GetAccountUri = "https://plex.tv/users/account";
|
||||
private const string ServerUri = "https://plex.tv/pms/servers.xml";
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private static string Version { get; }
|
||||
|
||||
public PlexAuthentication SignIn(string username, string password)
|
||||
{
|
||||
var userModel = new PlexUserRequest
|
||||
{
|
||||
user = new UserRequest
|
||||
{
|
||||
password = password,
|
||||
login = username
|
||||
}
|
||||
};
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.POST
|
||||
};
|
||||
|
||||
AddHeaders(ref request, false);
|
||||
|
||||
request.AddJsonBody(userModel);
|
||||
|
||||
var obj = Api.Execute<PlexAuthentication> (request, new Uri(SignInUri));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public PlexFriends GetUsers(string authToken)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
};
|
||||
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
var users = RetryHandler.Execute(() => Api.ExecuteXml<PlexFriends> (request, new Uri(FriendsUri)),
|
||||
(exception, timespan) => Log.Error (exception, "Exception when calling GetUsers for Plex, Retrying {0}", timespan), null);
|
||||
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the users.
|
||||
/// </summary>
|
||||
/// <param name="authToken">The authentication token.</param>
|
||||
/// <param name="searchTerm">The search term.</param>
|
||||
/// <param name="plexFullHost">The full plex host.</param>
|
||||
/// <returns></returns>
|
||||
public PlexSearch SearchContent(string authToken, string searchTerm, Uri plexFullHost)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "search?query={searchTerm}"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("searchTerm", searchTerm);
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
var search = RetryHandler.Execute(() => Api.ExecuteXml<PlexSearch> (request, plexFullHost),
|
||||
(exception, timespan) => Log.Error (exception, "Exception when calling SearchContent for Plex, Retrying {0}", timespan), null);
|
||||
|
||||
return search;
|
||||
}
|
||||
|
||||
public PlexStatus GetStatus(string authToken, Uri uri)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
};
|
||||
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
var users = RetryHandler.Execute(() => Api.ExecuteXml<PlexStatus> (request, uri),
|
||||
(exception, timespan) => Log.Error (exception, "Exception when calling GetStatus for Plex, Retrying {0}", timespan), null);
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
public PlexAccount GetAccount(string authToken)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
};
|
||||
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
var account = RetryHandler.Execute(() => Api.ExecuteXml<PlexAccount> (request, new Uri(GetAccountUri)),
|
||||
(exception, timespan) => Log.Error (exception, "Exception when calling GetAccount for Plex, Retrying {0}", timespan), null);
|
||||
|
||||
return account;
|
||||
}
|
||||
|
||||
public PlexLibraries GetLibrarySections(string authToken, Uri plexFullHost)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "library/sections"
|
||||
};
|
||||
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
try
|
||||
{
|
||||
var lib = RetryHandler.Execute(() => Api.ExecuteXml<PlexLibraries> (request, plexFullHost),
|
||||
(exception, timespan) => Log.Error (exception, "Exception when calling GetLibrarySections for Plex, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (5),
|
||||
TimeSpan.FromSeconds(10),
|
||||
TimeSpan.FromSeconds(30)
|
||||
});
|
||||
|
||||
return lib;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e,"There has been a API Exception when attempting to get the Plex Libraries");
|
||||
return new PlexLibraries();
|
||||
}
|
||||
}
|
||||
|
||||
public PlexSearch GetLibrary(string authToken, Uri plexFullHost, string libraryId)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "library/sections/{libraryId}/all"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("libraryId", libraryId);
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
try
|
||||
{
|
||||
var lib = RetryHandler.Execute(() => Api.ExecuteXml<PlexSearch> (request, plexFullHost),
|
||||
(exception, timespan) => Log.Error (exception, "Exception when calling GetLibrary for Plex, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (5),
|
||||
TimeSpan.FromSeconds(10),
|
||||
TimeSpan.FromSeconds(30)
|
||||
});
|
||||
|
||||
return lib;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e,"There has been a API Exception when attempting to get the Plex Library");
|
||||
return new PlexSearch();
|
||||
}
|
||||
}
|
||||
|
||||
public PlexEpisodeMetadata GetEpisodeMetaData(string authToken, Uri host, string ratingKey)
|
||||
{
|
||||
|
||||
// 192.168.1.69:32400/library/metadata/3662/allLeaves
|
||||
// The metadata ratingkey should be in the Cache
|
||||
// Search for it and then call the above with the Directory.RatingKey
|
||||
// THEN! We need the episode metadata using result.Vide.Key ("/library/metadata/3664")
|
||||
// We then have the GUID which contains the TVDB ID plus the season and episode number: guid="com.plexapp.agents.thetvdb://269586/2/8?lang=en"
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "/library/metadata/{ratingKey}"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("ratingKey", ratingKey);
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
try
|
||||
{
|
||||
var lib = RetryHandler.Execute(() => Api.ExecuteXml<PlexEpisodeMetadata>(request, host),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetEpisodeMetaData for Plex, Retrying {0}", timespan));
|
||||
|
||||
return lib;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been a API Exception when attempting to get GetEpisodeMetaData");
|
||||
return new PlexEpisodeMetadata();
|
||||
}
|
||||
}
|
||||
|
||||
public PlexSearch GetAllEpisodes(string authToken, Uri host, string section, int startPage, int returnCount)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "/library/sections/{section}/all"
|
||||
};
|
||||
|
||||
request.AddQueryParameter("type", 4.ToString());
|
||||
AddLimitHeaders(ref request, startPage, returnCount);
|
||||
request.AddUrlSegment("section", section);
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
try
|
||||
{
|
||||
var lib = RetryHandler.Execute(() => Api.ExecuteXml<PlexSearch>(request, host),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetAllEpisodes for Plex, Retrying {0}", timespan));
|
||||
|
||||
return lib;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been a API Exception when attempting to get GetAllEpisodes");
|
||||
return new PlexSearch();
|
||||
}
|
||||
}
|
||||
|
||||
public PlexMetadata GetMetadata(string authToken, Uri plexFullHost, string itemId)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "library/metadata/{itemId}"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("itemId", itemId);
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
try
|
||||
{
|
||||
var lib = RetryHandler.Execute(() => Api.ExecuteXml<PlexMetadata>(request, plexFullHost),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetMetadata for Plex, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (5),
|
||||
TimeSpan.FromSeconds(10),
|
||||
TimeSpan.FromSeconds(30)
|
||||
});
|
||||
|
||||
return lib;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been a API Exception when attempting to get the Plex GetMetadata");
|
||||
return new PlexMetadata();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public PlexSeasonMetadata GetSeasons(string authToken, Uri plexFullHost, string ratingKey)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "library/metadata/{ratingKey}/children"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("ratingKey", ratingKey);
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
try
|
||||
{
|
||||
var lib = RetryHandler.Execute(() => Api.ExecuteXml<PlexSeasonMetadata>(request, plexFullHost),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetMetadata for Plex, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (5),
|
||||
TimeSpan.FromSeconds(10),
|
||||
TimeSpan.FromSeconds(30)
|
||||
});
|
||||
|
||||
return lib;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been a API Exception when attempting to get the Plex GetMetadata");
|
||||
return new PlexSeasonMetadata();
|
||||
}
|
||||
}
|
||||
|
||||
public PlexServer GetServer(string authToken)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
};
|
||||
|
||||
AddHeaders(ref request, authToken, false);
|
||||
|
||||
var servers = RetryHandler.Execute(() => Api.ExecuteXml<PlexServer>(request, new Uri(ServerUri)),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetServer for Plex, Retrying {0}", timespan));
|
||||
|
||||
|
||||
return servers;
|
||||
}
|
||||
|
||||
public RecentlyAddedModelOld RecentlyAddedOld(string authToken, Uri plexFullHost, string sectionId)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "library/sections/{sectionId}/recentlyAdded"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("sectionId", sectionId);
|
||||
AddHeaders(ref request, authToken, true);
|
||||
AddLimitHeaders(ref request, 0, 25);
|
||||
|
||||
try
|
||||
{
|
||||
var lib = RetryHandler.Execute(() => Api.ExecuteJson<RecentlyAddedModelOld>(request, plexFullHost),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling RecentlyAddedModel for Plex, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (5),
|
||||
TimeSpan.FromSeconds(10),
|
||||
TimeSpan.FromSeconds(30)
|
||||
});
|
||||
|
||||
return lib;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been a API Exception when attempting to get the Plex RecentlyAddedModel");
|
||||
return new RecentlyAddedModelOld();
|
||||
}
|
||||
}
|
||||
|
||||
public PlexRecentlyAddedModel RecentlyAdded(string authToken, Uri plexFullHost, string sectionId)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "library/sections/{sectionId}/recentlyAdded"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("sectionId", sectionId);
|
||||
AddHeaders(ref request, authToken, true);
|
||||
AddLimitHeaders(ref request, 0, 25);
|
||||
|
||||
try
|
||||
{
|
||||
var lib = RetryHandler.Execute(() => Api.ExecuteJson<PlexRecentlyAddedModel>(request, plexFullHost),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling PlexRecentlyAddedModel for Plex, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (5),
|
||||
TimeSpan.FromSeconds(10),
|
||||
TimeSpan.FromSeconds(30)
|
||||
});
|
||||
|
||||
return lib;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been a API Exception when attempting to get the Plex RecentlyAddedModel");
|
||||
return new PlexRecentlyAddedModel();
|
||||
}
|
||||
}
|
||||
|
||||
private void AddLimitHeaders(ref RestRequest request, int from, int to)
|
||||
{
|
||||
request.AddHeader("X-Plex-Container-Start", from.ToString());
|
||||
request.AddHeader("X-Plex-Container-Size", to.ToString());
|
||||
}
|
||||
|
||||
private void AddHeaders(ref RestRequest request, string authToken, bool json)
|
||||
{
|
||||
request.AddHeader("X-Plex-Token", authToken);
|
||||
AddHeaders(ref request, json);
|
||||
}
|
||||
|
||||
private void AddHeaders(ref RestRequest request, bool json)
|
||||
{
|
||||
request.AddHeader("X-Plex-Client-Identifier", $"Ombi{Version}");
|
||||
request.AddHeader("X-Plex-Product", "Ombi");
|
||||
request.AddHeader("X-Plex-Version", Version);
|
||||
request.AddHeader("Content-Type", json ? "application/json" : "application/xml");
|
||||
request.AddHeader("Accept", json ? "application/json" : "application/xml");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
37
Old/Ombi.Api/Properties/AssemblyInfo.cs
Normal file
37
Old/Ombi.Api/Properties/AssemblyInfo.cs
Normal file
|
@ -0,0 +1,37 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Ombi.Api")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Ombi.Api")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2016")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("8cb8d235-2674-442d-9c6a-35fcaeeb160d")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
[assembly: AssemblyInformationalVersionAttribute("1.0.0.0")]
|
63
Old/Ombi.Api/PushbulletApi.cs
Normal file
63
Old/Ombi.Api/PushbulletApi.cs
Normal file
|
@ -0,0 +1,63 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: PlexApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Notifications;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class PushbulletApi : IPushbulletApi
|
||||
{
|
||||
public async Task<PushbulletResponse> PushAsync(string accessToken, string title, string message, string deviceIdentifier = default(string))
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.POST,
|
||||
|
||||
};
|
||||
|
||||
request.AddHeader("Access-Token", accessToken);
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var push = new PushbulletPush { title = title, body = message, type = "note"};
|
||||
|
||||
if (!string.IsNullOrEmpty(deviceIdentifier))
|
||||
{
|
||||
push.device_iden = deviceIdentifier;
|
||||
}
|
||||
|
||||
request.AddJsonBody(push);
|
||||
|
||||
var api = new ApiRequest();
|
||||
return await Task.Run(() => api.ExecuteJson<PushbulletResponse>(request, new Uri("https://api.pushbullet.com/v2/pushes")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
56
Old/Ombi.Api/PushoverApi.cs
Normal file
56
Old/Ombi.Api/PushoverApi.cs
Normal file
|
@ -0,0 +1,56 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: PlexApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Notifications;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class PushoverApi : IPushoverApi
|
||||
{
|
||||
public async Task<PushoverResponse> PushAsync(string accessToken, string message, string userToken)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.POST,
|
||||
Resource = "messages.json?token={token}&user={user}&message={message}"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("token", accessToken);
|
||||
request.AddUrlSegment("message", message);
|
||||
request.AddUrlSegment("user", userToken);
|
||||
|
||||
|
||||
var api = new ApiRequest();
|
||||
return await Task.Run(() => api.ExecuteJson<PushoverResponse>(request, new Uri("https://api.pushover.net/1")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
171
Old/Ombi.Api/RadarrApi.cs
Normal file
171
Old/Ombi.Api/RadarrApi.cs
Normal file
|
@ -0,0 +1,171 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: CouchPotatoApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Radarr;
|
||||
using Ombi.Api.Models.Sonarr;
|
||||
using Ombi.Helpers;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class RadarrApi : IRadarrApi
|
||||
{
|
||||
public RadarrApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
private ApiRequest Api { get; set; }
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public List<SonarrProfile> GetProfiles(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/profile", Method = Method.GET };
|
||||
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetProfiles for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (2),
|
||||
TimeSpan.FromSeconds(5),
|
||||
TimeSpan.FromSeconds(10)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<List<SonarrProfile>>(request, baseUrl));
|
||||
|
||||
return obj;
|
||||
}
|
||||
public List<SonarrRootFolder> GetRootFolders(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/rootfolder", Method = Method.GET };
|
||||
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetRootFolders for Radarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(2)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<List<SonarrRootFolder>>(request, baseUrl));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public RadarrAddMovie AddMovie(int tmdbId, string title, int year, int qualityId, string rootPath, string apiKey, Uri baseUrl, bool searchNow = false)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api/movie",
|
||||
Method = Method.POST
|
||||
};
|
||||
|
||||
var options = new RadarrAddMovie
|
||||
{
|
||||
title = title,
|
||||
tmdbId = tmdbId,
|
||||
qualityProfileId = qualityId,
|
||||
rootFolderPath = rootPath,
|
||||
titleSlug = title,
|
||||
monitored = true,
|
||||
year = year
|
||||
};
|
||||
|
||||
if (searchNow)
|
||||
{
|
||||
options.addOptions = new RadarrAddOptions
|
||||
{
|
||||
searchForMovie = true
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
request.AddJsonBody(options);
|
||||
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (2)
|
||||
});
|
||||
|
||||
var response = policy.Execute(() => Api.Execute(request, baseUrl));
|
||||
if (response.Content.Contains("\"message\":"))
|
||||
{
|
||||
var error = JsonConvert.DeserializeObject < RadarrError>(response.Content);
|
||||
return new RadarrAddMovie {Error = error};
|
||||
}
|
||||
if (response.Content.Contains("\"errorMessage\":"))
|
||||
{
|
||||
var error = JsonConvert.DeserializeObject<List<SonarrError>>(response.Content).FirstOrDefault();
|
||||
return new RadarrAddMovie {Error = new RadarrError {message = error?.errorMessage}};
|
||||
}
|
||||
return JsonConvert.DeserializeObject < RadarrAddMovie>(response.Content);
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public SystemStatus SystemStatus(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/system/status", Method = Method.GET };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling SystemStatus for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (2),
|
||||
TimeSpan.FromSeconds(5),
|
||||
TimeSpan.FromSeconds(10)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<SystemStatus>(request, baseUrl));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
public List<RadarrMovieResponse> GetMovies(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/movie", Method = Method.GET };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling SystemStatus for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (2),
|
||||
TimeSpan.FromSeconds(5),
|
||||
TimeSpan.FromSeconds(10)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.Execute(request, baseUrl));
|
||||
|
||||
return JsonConvert.DeserializeObject<List<RadarrMovieResponse>>(obj.Content);
|
||||
}
|
||||
}
|
||||
}
|
93
Old/Ombi.Api/RetryHandler.cs
Normal file
93
Old/Ombi.Api/RetryHandler.cs
Normal file
|
@ -0,0 +1,93 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: RetryHandler.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using Polly;
|
||||
using Polly.Retry;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public static class RetryHandler
|
||||
{
|
||||
private static readonly TimeSpan[] DefaultRetryTime = { TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5) };
|
||||
|
||||
public static T Execute<T>(Func<T> action, TimeSpan[] timeSpan = null)
|
||||
{
|
||||
if (timeSpan == null)
|
||||
{
|
||||
timeSpan = DefaultRetryTime;
|
||||
}
|
||||
var policy = RetryAndWaitPolicy(timeSpan);
|
||||
|
||||
return policy.Execute(action);
|
||||
}
|
||||
|
||||
public static T Execute<T>(Func<T> func, Action<Exception, TimeSpan> action, TimeSpan[] timeSpan = null)
|
||||
{
|
||||
if (timeSpan == null)
|
||||
{
|
||||
timeSpan = DefaultRetryTime;
|
||||
}
|
||||
var policy = RetryAndWaitPolicy(action, timeSpan);
|
||||
|
||||
return policy.Execute(func);
|
||||
}
|
||||
|
||||
public static RetryPolicy RetryAndWaitPolicy(Action action, TimeSpan[] timeSpan = null)
|
||||
{
|
||||
if (timeSpan == null)
|
||||
{
|
||||
timeSpan = DefaultRetryTime;
|
||||
}
|
||||
var policy = Policy.Handle<Exception>().WaitAndRetry(timeSpan, (e, ts) => action());
|
||||
|
||||
return policy;
|
||||
}
|
||||
|
||||
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] timeSpan)
|
||||
{
|
||||
if (timeSpan == null)
|
||||
{
|
||||
timeSpan = DefaultRetryTime;
|
||||
}
|
||||
var policy = Policy.Handle<Exception>().WaitAndRetry(timeSpan);
|
||||
|
||||
return policy;
|
||||
}
|
||||
|
||||
public static RetryPolicy RetryAndWaitPolicy(Action<Exception, TimeSpan> action, TimeSpan[] timeSpan = null)
|
||||
{
|
||||
if (timeSpan == null)
|
||||
{
|
||||
timeSpan = DefaultRetryTime;
|
||||
}
|
||||
var policy = Policy.Handle<Exception>().WaitAndRetry(timeSpan, action);
|
||||
|
||||
return policy;
|
||||
}
|
||||
}
|
||||
}
|
214
Old/Ombi.Api/SickrageApi.cs
Normal file
214
Old/Ombi.Api/SickrageApi.cs
Normal file
|
@ -0,0 +1,214 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: SickrageApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.SickRage;
|
||||
using Ombi.Helpers;
|
||||
using Ombi.Helpers.Exceptions;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class SickrageApi : ISickRageApi
|
||||
{
|
||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public SickrageApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
|
||||
private ApiRequest Api { get; }
|
||||
|
||||
public SickRageSeasonList VerifyShowHasLoaded(int tvdbId, string apiKey, Uri baseUrl)
|
||||
{
|
||||
Log.Trace("Entered `VerifyShowHasLoaded({0} <- id)`", tvdbId);
|
||||
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=show.seasonlist", Method = Method.GET };
|
||||
request.AddUrlSegment("apiKey", apiKey);
|
||||
request.AddQueryParameter("tvdbid", tvdbId.ToString());
|
||||
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling VerifyShowHasLoaded for SR, Retrying {0}", timespan), null);
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<SickRageSeasonList>(request, baseUrl));
|
||||
return obj;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
return new SickRageSeasonList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<SickRageTvAdd> AddSeries(int tvdbId, int seasonCount, int[] seasons, string quality, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var futureStatus = seasons.Length > 0 && !seasons.Any(x => x == seasonCount) ? SickRageStatus.Skipped : SickRageStatus.Wanted;
|
||||
var status = seasons.Length > 0 ? SickRageStatus.Skipped : SickRageStatus.Wanted;
|
||||
|
||||
Log.Trace("Future Status: {0}", futureStatus);
|
||||
Log.Trace("Current Status: {0}", status);
|
||||
|
||||
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=show.addnew", Method = Method.GET };
|
||||
request.AddUrlSegment("apiKey", apiKey);
|
||||
request.AddQueryParameter("tvdbid", tvdbId.ToString());
|
||||
request.AddQueryParameter("status", status);
|
||||
request.AddQueryParameter("future_status", futureStatus);
|
||||
if (!quality.Equals("default", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
Log.Trace("Settings quality to {0}", quality);
|
||||
request.AddQueryParameter("initial", quality);
|
||||
}
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for SR, Retrying {0}", timespan), null);
|
||||
|
||||
var obj = policy.Execute(() => Api.Execute<SickRageTvAdd>(request, baseUrl));
|
||||
Log.Trace("obj Result:");
|
||||
Log.Trace(obj.DumpJson());
|
||||
|
||||
if (obj.result != "failure")
|
||||
{
|
||||
var sw = new Stopwatch();
|
||||
sw.Start();
|
||||
|
||||
var seasonIncrement = 0;
|
||||
var seasonList = new SickRageSeasonList();
|
||||
try
|
||||
{
|
||||
while (seasonIncrement < seasonCount)
|
||||
{
|
||||
seasonList = VerifyShowHasLoaded(tvdbId, apiKey, baseUrl);
|
||||
if (seasonList.result.Equals("failure"))
|
||||
{
|
||||
Thread.Sleep(3000);
|
||||
continue;
|
||||
}
|
||||
seasonIncrement = seasonList.Data?.Length ?? 0;
|
||||
Log.Trace("New seasonIncrement -> {0}", seasonIncrement);
|
||||
|
||||
if (sw.ElapsedMilliseconds > 30000) // Break out after 30 seconds, it's not going to get added
|
||||
{
|
||||
Log.Warn("Couldn't find out if the show had been added after 10 seconds. I doubt we can change the status to wanted.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
sw.Stop();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error("Exception thrown when getting the seasonList");
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
Log.Trace("seasons.Length > 0 where seasons.Len -> {0}", seasons.Length);
|
||||
try
|
||||
{
|
||||
if (seasons.Length > 0)
|
||||
{
|
||||
//handle the seasons requested
|
||||
foreach (var s in seasons)
|
||||
{
|
||||
Log.Trace("Adding season {0}", s);
|
||||
|
||||
var result = await AddSeason(tvdbId, s, apiKey, baseUrl);
|
||||
Log.Trace("SickRage adding season results: ");
|
||||
Log.Trace(result.DumpJson());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Trace("Exception when adding seasons:");
|
||||
Log.Error(e);
|
||||
throw;
|
||||
}
|
||||
|
||||
Log.Trace("Finished with the API, returning the obj");
|
||||
return obj;
|
||||
}
|
||||
|
||||
public SickRagePing Ping(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=sb.ping", Method = Method.GET };
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling Ping for SR, Retrying {0}", timespan), null);
|
||||
|
||||
request.AddUrlSegment("apiKey", apiKey);
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<SickRagePing>(request, baseUrl));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public async Task<SickRageTvAdd> AddSeason(int tvdbId, int season, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=episode.setstatus", Method = Method.GET };
|
||||
request.AddUrlSegment("apiKey", apiKey);
|
||||
request.AddQueryParameter("tvdbid", tvdbId.ToString());
|
||||
request.AddQueryParameter("season", season.ToString());
|
||||
request.AddQueryParameter("status", SickRageStatus.Wanted);
|
||||
|
||||
await Task.Run(() => Thread.Sleep(2000));
|
||||
return await Task.Run(
|
||||
() =>
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeason for SR, Retrying {0}", timespan), null);
|
||||
|
||||
var result = policy.Execute(() => Api.Execute<SickRageTvAdd>(request, baseUrl));
|
||||
|
||||
return result;
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<SickrageShows> GetShows(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=shows", Method = Method.GET };
|
||||
request.AddUrlSegment("apiKey", apiKey);
|
||||
|
||||
return await Task.Run(
|
||||
() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetShows for SR, Retrying {0}", timespan), new[] { TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(30) });
|
||||
|
||||
return policy.Execute(() => Api.Execute<SickrageShows>(request, baseUrl));
|
||||
}
|
||||
catch (ApiRequestException)
|
||||
{
|
||||
Log.Error("There has been a API exception when Getting the Sickrage shows");
|
||||
return null;
|
||||
}
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
61
Old/Ombi.Api/SlackApi.cs
Normal file
61
Old/Ombi.Api/SlackApi.cs
Normal file
|
@ -0,0 +1,61 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: PlexApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Notifications;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class SlackApi : ISlackApi
|
||||
{
|
||||
public async Task<string> PushAsync(string team, string token, string service, SlackNotificationBody message)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.POST,
|
||||
Resource = "/services/{team}/{service}/{token}"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("team", team);
|
||||
request.AddUrlSegment("service", service);
|
||||
request.AddUrlSegment("token", token);
|
||||
request.AddJsonBody(message);
|
||||
|
||||
var api = new ApiRequest();
|
||||
return await Task.Run(
|
||||
() =>
|
||||
{
|
||||
var result = api.Execute(request, new Uri("https://hooks.slack.com/"));
|
||||
return result.Content;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
539
Old/Ombi.Api/SonarrApi.cs
Normal file
539
Old/Ombi.Api/SonarrApi.cs
Normal file
|
@ -0,0 +1,539 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: CouchPotatoApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Sonarr;
|
||||
using Ombi.Helpers;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class SonarrApi : ISonarrApi
|
||||
{
|
||||
public SonarrApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
private ApiRequest Api { get; set; }
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public List<SonarrProfile> GetProfiles(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/profile", Method = Method.GET };
|
||||
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetProfiles for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(2),
|
||||
TimeSpan.FromSeconds(5)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<List<SonarrProfile>>(request, baseUrl));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public List<SonarrRootFolder> GetRootFolders(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/rootfolder", Method = Method.GET };
|
||||
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetRootFolders for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (1),
|
||||
TimeSpan.FromSeconds(2),
|
||||
TimeSpan.FromSeconds(5)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<List<SonarrRootFolder>>(request, baseUrl));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int seasonCount, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false)
|
||||
{
|
||||
Log.Debug("Adding series {0}", title);
|
||||
Log.Debug("Seasons = {0}, out of {1} seasons", seasons.DumpJson(), seasonCount);
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api/Series?",
|
||||
Method = Method.POST
|
||||
};
|
||||
|
||||
var options = new SonarrAddSeries
|
||||
{
|
||||
seasonFolder = seasonFolders,
|
||||
title = title,
|
||||
qualityProfileId = qualityId,
|
||||
tvdbId = tvdbId,
|
||||
titleSlug = title,
|
||||
seasons = new List<Season>(),
|
||||
rootFolderPath = rootPath,
|
||||
monitored = monitor
|
||||
};
|
||||
|
||||
if (!searchForMissingEpisodes)
|
||||
{
|
||||
options.addOptions = new AddOptions
|
||||
{
|
||||
searchForMissingEpisodes = false,
|
||||
ignoreEpisodesWithFiles = true,
|
||||
ignoreEpisodesWithoutFiles = true
|
||||
};
|
||||
}
|
||||
|
||||
for (var i = 1; i <= seasonCount; i++)
|
||||
{
|
||||
var season = new Season
|
||||
{
|
||||
seasonNumber = i,
|
||||
// ReSharper disable once SimplifyConditionalTernaryExpression
|
||||
monitored = monitor ? seasons.Length == 0 || seasons.Any(x => x == i) : false
|
||||
};
|
||||
options.seasons.Add(season);
|
||||
}
|
||||
|
||||
Log.Debug("Sonarr API Options:");
|
||||
Log.Debug(options.DumpJson());
|
||||
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
request.AddJsonBody(options);
|
||||
|
||||
SonarrAddSeries result;
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (2)
|
||||
});
|
||||
|
||||
result = policy.Execute(() => Api.ExecuteJson<SonarrAddSeries>(request, baseUrl));
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
var error = Api.ExecuteJson<List<SonarrError>>(request, baseUrl);
|
||||
var messages = error?.Select(x => x.errorMessage).ToList();
|
||||
messages?.ForEach(x => Log.Error(x));
|
||||
result = new SonarrAddSeries { ErrorMessages = messages };
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public SonarrAddSeries AddSeries(SonarrAddSeries series,string apiKey, Uri baseUrl)
|
||||
{
|
||||
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api/Series?",
|
||||
Method = Method.POST
|
||||
};
|
||||
|
||||
Log.Debug("Sonarr API Options:");
|
||||
Log.Debug(series.DumpJson());
|
||||
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
request.AddJsonBody(series);
|
||||
|
||||
SonarrAddSeries result;
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (2)
|
||||
});
|
||||
|
||||
result = policy.Execute(() => Api.ExecuteJson<SonarrAddSeries>(request, baseUrl));
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
var error = Api.ExecuteJson<List<SonarrError>>(request, baseUrl);
|
||||
var messages = error?.Select(x => x.errorMessage).ToList();
|
||||
messages?.ForEach(x => Log.Error(x));
|
||||
result = new SonarrAddSeries { ErrorMessages = messages };
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public SonarrAddSeries AddSeriesNew(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Resource = "/api/Series?",
|
||||
Method = Method.POST
|
||||
};
|
||||
|
||||
var options = new SonarrAddSeries
|
||||
{
|
||||
seasonFolder = seasonFolders,
|
||||
title = title,
|
||||
qualityProfileId = qualityId,
|
||||
tvdbId = tvdbId,
|
||||
titleSlug = title,
|
||||
seasons = new List<Season>(),
|
||||
rootFolderPath = rootPath,
|
||||
monitored = monitor
|
||||
};
|
||||
|
||||
if (!searchForMissingEpisodes)
|
||||
{
|
||||
options.addOptions = new AddOptions
|
||||
{
|
||||
searchForMissingEpisodes = false,
|
||||
ignoreEpisodesWithFiles = true,
|
||||
ignoreEpisodesWithoutFiles = true
|
||||
};
|
||||
}
|
||||
|
||||
for (var i = 1; i <= seasons.Length; i++)
|
||||
{
|
||||
var season = new Season
|
||||
{
|
||||
seasonNumber = i,
|
||||
// ReSharper disable once SimplifyConditionalTernaryExpression
|
||||
monitored = true
|
||||
};
|
||||
options.seasons.Add(season);
|
||||
}
|
||||
|
||||
Log.Debug("Sonarr API Options:");
|
||||
Log.Debug(options.DumpJson());
|
||||
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
request.AddJsonBody(options);
|
||||
|
||||
SonarrAddSeries result;
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (2)
|
||||
});
|
||||
|
||||
result = policy.Execute(() => Api.ExecuteJson<SonarrAddSeries>(request, baseUrl));
|
||||
}
|
||||
catch (JsonSerializationException jse)
|
||||
{
|
||||
Log.Error(jse);
|
||||
var error = Api.ExecuteJson<List<SonarrError>>(request, baseUrl);
|
||||
var messages = error?.Select(x => x.errorMessage).ToList();
|
||||
messages?.ForEach(x => Log.Error(x));
|
||||
result = new SonarrAddSeries { ErrorMessages = messages };
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public SystemStatus SystemStatus(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/system/status", Method = Method.GET };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling SystemStatus for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (2),
|
||||
TimeSpan.FromSeconds(5),
|
||||
TimeSpan.FromSeconds(10)
|
||||
});
|
||||
|
||||
var obj = policy.Execute(() => Api.ExecuteJson<SystemStatus>(request, baseUrl));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public List<Series> GetSeries(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/series", Method = Method.GET };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetSeries for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
|
||||
TimeSpan.FromSeconds (5),
|
||||
TimeSpan.FromSeconds(5),
|
||||
TimeSpan.FromSeconds(5)
|
||||
});
|
||||
|
||||
var series = policy.Execute(() => Api.ExecuteJson<List<Series>>(request, baseUrl));
|
||||
|
||||
// Remove the 'specials from the object'
|
||||
foreach (var s in series)
|
||||
{
|
||||
var seasonToRemove = s.seasons.FirstOrDefault(x => x.seasonNumber == 0);
|
||||
if (seasonToRemove != null)
|
||||
{
|
||||
s.seasons.Remove(seasonToRemove);
|
||||
}
|
||||
}
|
||||
return series;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when getting the Sonarr Series");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Series GetSeries(string seriesId, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/series/{seriesId}", Method = Method.GET };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
request.AddUrlSegment("seriesId", seriesId);
|
||||
try
|
||||
{
|
||||
var policy =
|
||||
RetryHandler.RetryAndWaitPolicy(
|
||||
(exception, timespan) =>
|
||||
Log.Error(exception, "Exception when calling GetSeries by ID for Sonarr, Retrying {0}",
|
||||
timespan));
|
||||
|
||||
var series = policy.Execute(() => Api.ExecuteJson<Series>(request, baseUrl));
|
||||
|
||||
// Remove the specials season
|
||||
var toRemove = series.seasons.FirstOrDefault(x => x.seasonNumber == 0);
|
||||
if (toRemove != null)
|
||||
{
|
||||
series.seasons.Remove(toRemove);
|
||||
}
|
||||
return series;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when getting the Sonarr Series by ID");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all episodes for the given series.
|
||||
/// </summary>
|
||||
/// <param name="seriesId">The series identifier.</param>
|
||||
/// <param name="apiKey">The API key.</param>
|
||||
/// <param name="baseUrl">The base URL.</param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<SonarrEpisodes> GetEpisodes(string seriesId, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/Episode", Method = Method.GET };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
request.AddQueryParameter("seriesId", seriesId);
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
|
||||
Log.Error(exception, "Exception when calling GetEpisodes for Sonarr, Retrying {0}", timespan));
|
||||
|
||||
return policy.Execute(() => Api.ExecuteJson<List<SonarrEpisodes>>(request, baseUrl));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when getting the Sonarr GetEpisodes");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the episode with the matching id.
|
||||
/// </summary>
|
||||
/// <param name="episodeId">The episode identifier.</param>
|
||||
/// <param name="apiKey">The API key.</param>
|
||||
/// <param name="baseUrl">The base URL.</param>
|
||||
/// <returns></returns>
|
||||
public SonarrEpisode GetEpisode(string episodeId, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/Episode/{episodeId}", Method = Method.GET };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
request.AddUrlSegment("episodeId", episodeId);
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
|
||||
Log.Error(exception, "Exception when calling GetEpisode by ID for Sonarr, Retrying {0}", timespan));
|
||||
|
||||
return policy.Execute(() => Api.ExecuteJson<SonarrEpisode>(request, baseUrl));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when getting the Sonarr GetEpisode by ID");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the given episodes, currently only monitored is changed, all other modifications are ignored.
|
||||
/// Required: All parameters (you should perform a GET/{id} and submit the full body with the changes, as other values may be editable in the future.
|
||||
/// </summary>
|
||||
/// <param name="episodeInfo">The episode information.</param>
|
||||
/// <param name="apiKey">The API key.</param>
|
||||
/// <param name="baseUrl">The base URL.</param>
|
||||
/// <returns></returns>
|
||||
public SonarrEpisode UpdateEpisode(SonarrEpisode episodeInfo, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/Episode", Method = Method.PUT };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
request.AddJsonBody(episodeInfo);
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
|
||||
Log.Error(exception, "Exception when calling UpdateEpisode for Sonarr, Retrying {0}", timespan));
|
||||
|
||||
return policy.Execute(() => Api.ExecuteJson<SonarrEpisode>(request, baseUrl));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when put the Sonarr UpdateEpisode");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public SonarrEpisodes UpdateEpisode(SonarrEpisodes episodeInfo, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/Episode", Method = Method.PUT };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
request.AddJsonBody(episodeInfo);
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
|
||||
Log.Error(exception, "Exception when calling UpdateEpisode for Sonarr, Retrying {0}", timespan));
|
||||
|
||||
return policy.Execute(() => Api.ExecuteJson<SonarrEpisodes>(request, baseUrl));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when put the Sonarr UpdateEpisode");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Search for one or more episodes
|
||||
/// </summary>
|
||||
/// <param name="episodeIds">The episode ids.</param>
|
||||
/// <param name="apiKey">The API key.</param>
|
||||
/// <param name="baseUrl">The base URL.</param>
|
||||
/// <returns></returns>
|
||||
public SonarrAddEpisodeResult SearchForEpisodes(int[] episodeIds, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/Command", Method = Method.POST };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
|
||||
var body = new SonarrAddEpisodeBody
|
||||
{
|
||||
name = "EpisodeSearch",
|
||||
episodeIds = episodeIds
|
||||
};
|
||||
|
||||
request.AddJsonBody(body);
|
||||
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
|
||||
Log.Error(exception, "Exception when calling SearchForEpisodes for Sonarr, Retrying {0}", timespan));
|
||||
|
||||
return policy.Execute(() => Api.ExecuteJson<SonarrAddEpisodeResult>(request, baseUrl));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when put the Sonarr SearchForEpisodes");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Series UpdateSeries(Series series, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/Series", Method = Method.PUT };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
|
||||
request.AddJsonBody(series);
|
||||
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
|
||||
Log.Error(exception, "Exception when calling UpdateSeries for Sonarr, Retrying {0}", timespan));
|
||||
|
||||
return policy.Execute(() => Api.ExecuteJson<Series>(request, baseUrl));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when put the Sonarr UpdateSeries");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public SonarrSeasonSearchResult SearchForSeason(int seriesId, int seasonNumber, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/Command", Method = Method.POST };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
|
||||
var body = new SonarrSearchCommand
|
||||
{
|
||||
name = "SeasonSearch",
|
||||
seriesId = seriesId,
|
||||
seasonNumber = seasonNumber
|
||||
};
|
||||
request.AddJsonBody(body);
|
||||
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
|
||||
Log.Error(exception, "Exception when calling SearchForSeason for Sonarr, Retrying {0}", timespan));
|
||||
|
||||
return policy.Execute(() => Api.ExecuteJson<SonarrSeasonSearchResult>(request, baseUrl));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when put the Sonarr SearchForSeason");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public SonarrSeriesSearchResult SearchForSeries(int seriesId, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var request = new RestRequest { Resource = "/api/Command", Method = Method.POST };
|
||||
request.AddHeader("X-Api-Key", apiKey);
|
||||
|
||||
var body = new SonarrSearchCommand
|
||||
{
|
||||
name = "SeriesSearch",
|
||||
seriesId = seriesId
|
||||
};
|
||||
request.AddJsonBody(body);
|
||||
|
||||
try
|
||||
{
|
||||
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
|
||||
Log.Error(exception, "Exception when calling SearchForSeries for Sonarr, Retrying {0}", timespan));
|
||||
|
||||
return policy.Execute(() => Api.ExecuteJson<SonarrSeriesSearchResult>(request, baseUrl));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been an API exception when put the Sonarr SearchForSeries");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
155
Old/Ombi.Api/TheMovieDbApi.cs
Normal file
155
Old/Ombi.Api/TheMovieDbApi.cs
Normal file
|
@ -0,0 +1,155 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: TheMovieDbApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using NLog.Fluent;
|
||||
using Ombi.Api.Models.Movie;
|
||||
using RestSharp;
|
||||
using TMDbLib.Client;
|
||||
using TMDbLib.Objects.General;
|
||||
using TMDbLib.Objects.Movies;
|
||||
using TMDbLib.Objects.Search;
|
||||
using Movie = TMDbLib.Objects.Movies.Movie;
|
||||
using TMDbLib.Objects.People;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class TheMovieDbApi : MovieBase
|
||||
{
|
||||
public TheMovieDbApi()
|
||||
{
|
||||
Client = new TMDbClient(ApiKey);
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
|
||||
private ApiRequest Api { get; }
|
||||
public TMDbClient Client { get; set; }
|
||||
private const string BaseUrl = "https://api.themoviedb.org/3/";
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
public async Task<List<SearchMovie>> SearchMovie(string searchTerm)
|
||||
{
|
||||
var results = await Client.SearchMovie(searchTerm);
|
||||
return results?.Results ?? new List<SearchMovie>();
|
||||
}
|
||||
|
||||
public async Task<List<MovieResult>> GetCurrentPlayingMovies()
|
||||
{
|
||||
var movies = await Client.GetMovieList(MovieListType.NowPlaying);
|
||||
return movies?.Results ?? new List<MovieResult>();
|
||||
}
|
||||
public async Task<List<MovieResult>> GetUpcomingMovies()
|
||||
{
|
||||
var movies = await Client.GetMovieList(MovieListType.Upcoming);
|
||||
return movies?.Results ?? new List<MovieResult>();
|
||||
}
|
||||
|
||||
private async Task<Movie> GetMovie(int id)
|
||||
{
|
||||
return await Client.GetMovie(id);
|
||||
}
|
||||
|
||||
public TmdbMovieDetails GetMovieInformationWithVideos(int tmdbId)
|
||||
{
|
||||
var request = new RestRequest { Resource = "movie/{movieId}", Method = Method.GET };
|
||||
request.AddUrlSegment("movieId", tmdbId.ToString());
|
||||
request.AddQueryParameter("api_key", ApiKey);
|
||||
request.AddQueryParameter("append_to_response", "videos"); // Get the videos
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
var obj = Api.ExecuteJson<TmdbMovieDetails>(request, new Uri(BaseUrl));
|
||||
return obj;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Movie> GetMovieInformation(int tmdbId)
|
||||
{
|
||||
var movies = await Client.GetMovie(tmdbId);
|
||||
return movies;
|
||||
}
|
||||
|
||||
public async Task<Movie> GetMovieInformation(string imdbId)
|
||||
{
|
||||
var movies = await Client.GetMovie(imdbId);
|
||||
return movies ?? new Movie();
|
||||
}
|
||||
|
||||
public async Task<List<Movie>> SearchPerson(string searchTerm)
|
||||
{
|
||||
return await SearchPerson(searchTerm, null);
|
||||
}
|
||||
|
||||
public async Task<List<Movie>> SearchPerson(string searchTerm, Func<int, string, string, Task<bool>> alreadyAvailable)
|
||||
{
|
||||
SearchContainer<SearchPerson> result = await Client.SearchPerson(searchTerm);
|
||||
|
||||
var people = result?.Results ?? new List<SearchPerson>();
|
||||
var person = (people.Count != 0 ? people[0] : null);
|
||||
var movies = new List<Movie>();
|
||||
var counter = 0;
|
||||
try
|
||||
{
|
||||
if (person != null)
|
||||
{
|
||||
var credits = await Client.GetPersonMovieCredits(person.Id);
|
||||
|
||||
// grab results from both cast and crew, prefer items in cast. we can handle directors like this.
|
||||
List<Movie> movieResults = (from MovieRole role in credits.Cast select new Movie() { Id = role.Id, Title = role.Title, ReleaseDate = role.ReleaseDate }).ToList();
|
||||
movieResults.AddRange((from MovieJob job in credits.Crew select new Movie() { Id = job.Id, Title = job.Title, ReleaseDate = job.ReleaseDate }).ToList());
|
||||
|
||||
//only get the first 10 movies and delay a bit between each request so we don't overload the API
|
||||
foreach (var m in movieResults)
|
||||
{
|
||||
if (counter == 10)
|
||||
break;
|
||||
if (alreadyAvailable == null || !(await alreadyAvailable(m.Id, m.Title, m.ReleaseDate.Value.Year.ToString())))
|
||||
{
|
||||
movies.Add(await GetMovie(m.Id));
|
||||
counter++;
|
||||
}
|
||||
await Task.Delay(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Log(LogLevel.Error, e);
|
||||
}
|
||||
return movies;
|
||||
}
|
||||
}
|
||||
}
|
143
Old/Ombi.Api/TheTvDbApi.cs
Normal file
143
Old/Ombi.Api/TheTvDbApi.cs
Normal file
|
@ -0,0 +1,143 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: TheTvDbApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using Ombi.Api.Models.Tv;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
[Obsolete("Use TVMazeAPP")]
|
||||
public class TheTvDbApi : TvBase
|
||||
{
|
||||
public TheTvDbApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
private ApiRequest Api { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Authenticates against TheTVDB.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string Authenticate()
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.POST,
|
||||
Resource = "login",
|
||||
RequestFormat = DataFormat.Json,
|
||||
|
||||
};
|
||||
var apiKey = new { apikey = ApiKey };
|
||||
|
||||
request.AddBody(apiKey);
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
return Api.Execute<Authentication>(request, Url).token;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the token.
|
||||
/// </summary>
|
||||
/// <param name="oldToken">The old token.</param>
|
||||
/// <returns></returns>
|
||||
public Authentication RefreshToken(string oldToken)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.POST,
|
||||
Resource = "refresh_token"
|
||||
};
|
||||
request.AddHeader("Authorization", $"Bearer {oldToken}");
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
return Api.Execute<Authentication>(request, Url);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches for a tv series.
|
||||
/// </summary>
|
||||
/// <param name="searchTerm">The search term.</param>
|
||||
/// <param name="token">The token.</param>
|
||||
/// <returns></returns>
|
||||
public TvSearchResult SearchTv(string searchTerm, string token)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "search/series?name={searchTerm}"
|
||||
};
|
||||
request.AddUrlSegment("searchTerm", searchTerm);
|
||||
request.AddHeader("Authorization", $"Bearer {token}");
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
return Api.Execute<TvSearchResult>(request, Url);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tv images.
|
||||
/// </summary>
|
||||
/// <param name="seriesId">The series identifier.</param>
|
||||
/// <param name="token">The token.</param>
|
||||
/// <returns></returns>
|
||||
public TvShowImages GetTvImages(int seriesId, string token)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "/series/{id}/images/query?keyType=poster"
|
||||
};
|
||||
request.AddUrlSegment("id", seriesId.ToString());
|
||||
request.AddHeader("Authorization", $"Bearer {token}");
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
return Api.Execute<TvShowImages>(request, Url);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the information for a TV Series.
|
||||
/// </summary>
|
||||
/// <param name="tvdbId">The TVDB identifier.</param>
|
||||
/// <param name="token">The token.</param>
|
||||
/// <returns></returns>
|
||||
public TvShowInformation GetInformation(int tvdbId, string token)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "series/{id}"
|
||||
};
|
||||
request.AddUrlSegment("id", tvdbId.ToString());
|
||||
request.AddHeader("Authorization", $"Bearer {token}");
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
return Api.Execute<TvShowInformation>(request, Url);
|
||||
}
|
||||
}
|
||||
}
|
51
Old/Ombi.Api/TraktApi.cs
Normal file
51
Old/Ombi.Api/TraktApi.cs
Normal file
|
@ -0,0 +1,51 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Helpers;
|
||||
using TraktApiSharp;
|
||||
using TraktApiSharp.Enums;
|
||||
using TraktApiSharp.Objects.Get.Shows;
|
||||
using TraktApiSharp.Objects.Get.Shows.Common;
|
||||
using TraktApiSharp.Requests.Params;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class TraktApi : ITraktApi
|
||||
{
|
||||
private TraktClient Client { get; }
|
||||
|
||||
private static readonly string Encrypted = "134e568350f7427511e257a6534026660480cf9b93c77f7378f340603b67381d";
|
||||
private readonly string _apiKey = StringCipher.Decrypt(Encrypted, "ApiKey");
|
||||
public TraktApi()
|
||||
{
|
||||
Client = new TraktClient(_apiKey);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TraktShow>> GetPopularShows(int? page = null, int? limitPerPage = null)
|
||||
{
|
||||
var popular = await Client.Shows.GetPopularShowsAsync(new TraktExtendedInfo { Full = true }, null, page ?? 1, limitPerPage ?? 10);
|
||||
return popular.Items;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TraktTrendingShow>> GetTrendingShows(int? page = null, int? limitPerPage = null)
|
||||
{
|
||||
var trendingShowsTop10 = await Client.Shows.GetTrendingShowsAsync(new TraktExtendedInfo { Full = true }, null, page ?? 1, limitPerPage ?? 10);
|
||||
return trendingShowsTop10.Items;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TraktMostAnticipatedShow>> GetAnticipatedShows(int? page = null, int? limitPerPage = null)
|
||||
{
|
||||
var anticipatedShows = await Client.Shows.GetMostAnticipatedShowsAsync(new TraktExtendedInfo { Full = true }, null, page ?? 1, limitPerPage ?? 10);
|
||||
return anticipatedShows.Items;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TraktMostWatchedShow>> GetMostWatchesShows(TraktTimePeriod period = null, int? page = null, int? limitPerPage = null)
|
||||
{
|
||||
var anticipatedShows = await Client.Shows.GetMostWatchedShowsAsync(period ?? TraktTimePeriod.Monthly, new TraktExtendedInfo { Full = true }, null, page ?? 1, limitPerPage ?? 10);
|
||||
return anticipatedShows.Items;
|
||||
}
|
||||
}
|
||||
}
|
39
Old/Ombi.Api/TvBase.cs
Normal file
39
Old/Ombi.Api/TvBase.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: MovieBase.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using Ombi.Helpers;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public abstract class TvBase
|
||||
{
|
||||
private static readonly string Encrypted = "AVdhrVK6XX8anvrQgEyN/qNr9rk8ZPwy7/r1t5t5cKyUEzxcyk0L1v6dSxgE7hTCxvITUX2cWa6VlFMlTMgJWyuPZml7fN3csCHntgd/VGYro6VfNf24snZ/rQ3mf005";
|
||||
protected string ApiKey = StringCipher.Decrypt(Encrypted, "ApiKey");
|
||||
protected Uri Url = new Uri("https://api-beta.thetvdb.com/");
|
||||
}
|
||||
}
|
140
Old/Ombi.Api/TvMazeApi.cs
Normal file
140
Old/Ombi.Api/TvMazeApi.cs
Normal file
|
@ -0,0 +1,140 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: TvMazeApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Ombi.Api.Models.Tv;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class TvMazeApi : TvMazeBase
|
||||
{
|
||||
public TvMazeApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
private ApiRequest Api { get; }
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
public List<TvMazeSearch> Search(string searchTerm)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "search/shows?q={searchTerm}"
|
||||
};
|
||||
request.AddUrlSegment("searchTerm", searchTerm);
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
return Api.Execute<List<TvMazeSearch>>(request, new Uri(Uri));
|
||||
}
|
||||
|
||||
public TvMazeShow ShowLookup(int showId)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "shows/{id}"
|
||||
};
|
||||
request.AddUrlSegment("id", showId.ToString());
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
return Api.Execute<TvMazeShow>(request, new Uri(Uri));
|
||||
}
|
||||
|
||||
public IEnumerable<TvMazeEpisodes> EpisodeLookup(int showId)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "shows/{id}/episodes"
|
||||
};
|
||||
request.AddUrlSegment("id", showId.ToString());
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
return Api.Execute<List<TvMazeEpisodes>>(request, new Uri(Uri));
|
||||
}
|
||||
|
||||
public TvMazeShow ShowLookupByTheTvDbId(int theTvDbId)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "lookup/shows?thetvdb={id}"
|
||||
};
|
||||
request.AddUrlSegment("id", theTvDbId.ToString());
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
try
|
||||
{
|
||||
var result = Api.Execute(request, new Uri(Uri));
|
||||
var obj = JsonConvert.DeserializeObject<TvMazeShow>(result.Content);
|
||||
|
||||
var episodes = EpisodeLookup(obj.id).ToList();
|
||||
|
||||
foreach (var e in episodes)
|
||||
{
|
||||
obj.Season.Add(new TvMazeCustomSeason
|
||||
{
|
||||
SeasonNumber = e.season,
|
||||
EpisodeNumber = e.number
|
||||
});
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public List<TvMazeSeasons> GetSeasons(int id)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "shows/{id}/seasons"
|
||||
};
|
||||
request.AddUrlSegment("id", id.ToString());
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
return Api.Execute<List<TvMazeSeasons>>(request, new Uri(Uri));
|
||||
}
|
||||
|
||||
public int GetSeasonCount(int id)
|
||||
{
|
||||
var obj = GetSeasons(id);
|
||||
var seasons = obj.Select(x => x.number > 0);
|
||||
return seasons.Count();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
33
Old/Ombi.Api/TvMazeBase.cs
Normal file
33
Old/Ombi.Api/TvMazeBase.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: TvMazeBase.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class TvMazeBase
|
||||
{
|
||||
protected string Uri = "http://api.tvmaze.com";
|
||||
}
|
||||
}
|
151
Old/Ombi.Api/WatcherApi.cs
Normal file
151
Old/Ombi.Api/WatcherApi.cs
Normal file
|
@ -0,0 +1,151 @@
|
|||
#region Copyright
|
||||
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: WatcherApi.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Ombi.Api.Interfaces;
|
||||
using Ombi.Api.Models.Watcher;
|
||||
using RestSharp;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class WatcherApi : IWatcherApi
|
||||
{
|
||||
public WatcherApi()
|
||||
{
|
||||
Api = new ApiRequest();
|
||||
}
|
||||
|
||||
private ApiRequest Api { get; set; }
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public WatcherAddMovieResult AddMovie(string imdbId, string apiKey, Uri baseUrl)
|
||||
{
|
||||
var response = Send("addmovie", apiKey, baseUrl, imdbId);
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject<WatcherAddMovieResult>(response.Content);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
return new WatcherAddMovieResult
|
||||
{
|
||||
Error = true,
|
||||
ErrorMessage = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
public WatcherVersion Version(string apiKey, Uri baseUri)
|
||||
{
|
||||
var response = Send("version", apiKey, baseUri);
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject<WatcherVersion>(response.Content);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
return new WatcherVersion
|
||||
{
|
||||
response = false,
|
||||
ErrorMessage = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public WatcherListStatusResultContainer ListMovies(string apiKey, Uri baseUrl)
|
||||
{
|
||||
var response = Send("liststatus", apiKey, baseUrl);
|
||||
try
|
||||
{
|
||||
if (response.Content.Contains("No movies found"))
|
||||
{
|
||||
return new WatcherListStatusResultContainer();
|
||||
}
|
||||
var items = JsonConvert.DeserializeObject<WatcherListStatusResultContainer>(response.Content);
|
||||
return items;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
return new WatcherListStatusResultContainer
|
||||
{
|
||||
Error = true,
|
||||
ErrorMessage = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public WatcherListStatusResultContainer ListMovies(string apiKey, Uri baseUrl, string imdbId)
|
||||
{
|
||||
var response = Send("liststatus", apiKey, baseUrl, imdbId);
|
||||
try
|
||||
{
|
||||
if (response.Content.Contains("No movies found"))
|
||||
{
|
||||
return new WatcherListStatusResultContainer();
|
||||
}
|
||||
return JsonConvert.DeserializeObject<WatcherListStatusResultContainer>(response.Content);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
return new WatcherListStatusResultContainer
|
||||
{
|
||||
Error = true,
|
||||
ErrorMessage = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private IRestResponse Send(string mode, string apiKey, Uri baseUrl, string imdbid = "")
|
||||
{
|
||||
RestRequest request;
|
||||
request = new RestRequest
|
||||
{
|
||||
Resource = string.IsNullOrEmpty(imdbid) ? "/api?apikey={apikey}&mode={mode}" : "/api?apikey={apikey}&mode={mode}&imdbid={imdbid}"
|
||||
};
|
||||
|
||||
|
||||
request.AddUrlSegment("apikey", apiKey);
|
||||
if (!string.IsNullOrEmpty(imdbid))
|
||||
{
|
||||
request.AddUrlSegment("imdbid", imdbid);
|
||||
}
|
||||
request.AddUrlSegment("mode", mode);
|
||||
|
||||
return Api.Execute(request, baseUrl);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
11
Old/Ombi.Api/app.config
Normal file
11
Old/Ombi.Api/app.config
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /></startup></configuration>
|
13
Old/Ombi.Api/packages.config
Normal file
13
Old/Ombi.Api/packages.config
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Dapper" version="1.50.0-beta8" targetFramework="net45" />
|
||||
<package id="Nancy" version="1.4.3" targetFramework="net45" />
|
||||
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
|
||||
<package id="NLog" version="4.3.6" targetFramework="net45" />
|
||||
<package id="Polly-Signed" version="4.3.0" targetFramework="net45" />
|
||||
<package id="RestSharp" version="105.2.3" targetFramework="net45" />
|
||||
<package id="System.Net.Http" version="4.0.0" targetFramework="net45" />
|
||||
<package id="TMDbLib" version="0.9.0.0-alpha" targetFramework="net45" />
|
||||
<package id="TraktApiSharp" version="0.8.0" targetFramework="net45" />
|
||||
<package id="WebSocket4Net" version="0.14.1" targetFramework="net45" />
|
||||
</packages>
|
Loading…
Add table
Add a link
Reference in a new issue