Moved API Sln dir

This commit is contained in:
tidusjar 2016-12-19 20:10:09 +00:00
commit b5855f2644
28 changed files with 1 additions and 301 deletions

145
Ombi.Api/ApiRequest.cs Normal file
View file

@ -0,0 +1,145 @@
#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.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 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("Api Content Response:");
Log.Trace(response.Content);
if (response.ErrorException != null)
{
var message = "Error retrieving response. Check inner details for more info.";
Log.Error(response.ErrorException);
throw new ApiRequestException(message, response.ErrorException);
}
return response.Data;
}
public IRestResponse Execute(IRestRequest request, Uri baseUri)
{
var client = new RestClient { BaseUrl = baseUri };
var response = client.Execute(request);
if (response.ErrorException != null)
{
Log.Error(response.ErrorException);
var message = "Error retrieving response. Check inner details for more info.";
throw new ApiRequestException(message, response.ErrorException);
}
return response;
}
public T ExecuteXml<T>(IRestRequest request, Uri baseUri) where T : class
{
var client = new RestClient { BaseUrl = baseUri };
var response = client.Execute(request);
if (response.ErrorException != null)
{
Log.Error(response.ErrorException);
var message = "Error retrieving response. Check inner details for more info.";
throw new ApiRequestException(message, response.ErrorException);
}
var result = DeserializeXml<T>(response.Content);
return result;}
public T ExecuteJson<T>(IRestRequest request, Uri baseUri) where T : new()
{
var client = new RestClient { BaseUrl = baseUri };
var response = client.Execute(request);
Log.Trace("Api Content Response:");
Log.Trace(response.Content);
if (response.ErrorException != null)
{
Log.Error(response.ErrorException);
var message = "Error retrieving response. Check inner details for more info.";
throw new ApiRequestException(message, response.ErrorException);
}
Log.Trace("Deserialzing Object");
var json = JsonConvert.DeserializeObject<T>(response.Content, _settings);
Log.Trace("Finished Deserialzing Object");
return json;
}
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;
}
}
}
}

175
Ombi.Api/CouchPotatoApi.cs Normal file
View 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 (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)});
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 (5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
});
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;
}
}
}

201
Ombi.Api/HeadphonesApi.cs Normal file
View 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".
}
}
}
}

37
Ombi.Api/MovieBase.cs Normal file
View file

@ -0,0 +1,37 @@
#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=";
protected string ApiKey = StringCipher.Decrypt(Encrypted, "ApiKey");
}
}

113
Ombi.Api/MusicBrainzApi.cs Normal file
View 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".
}
}
}
}

116
Ombi.Api/Ombi.Api.csproj Normal file
View file

@ -0,0 +1,116 @@
<?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>
</ItemGroup>
<ItemGroup>
<Compile Include="ApiRequest.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="..\PlexRequests.Api.Interfaces\Ombi.Api.Interfaces.csproj">
<Project>{95834072-A675-415D-AA8F-877C91623810}</Project>
<Name>Ombi.Api.Interfaces</Name>
</ProjectReference>
<ProjectReference Include="..\PlexRequests.Api.Models\Ombi.Api.Models.csproj">
<Project>{CB37A5F8-6DFC-4554-99D3-A42B502E4591}</Project>
<Name>Ombi.Api.Models</Name>
</ProjectReference>
<ProjectReference Include="..\PlexRequests.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>

428
Ombi.Api/PlexApi.cs Normal file
View file

@ -0,0 +1,428 @@
#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 = RetryHandler.Execute<PlexAuthentication>(() => Api.Execute<PlexAuthentication> (request, new Uri(SignInUri)),
(exception, timespan) => Log.Error (exception, "Exception when calling SignIn for Plex, Retrying {0}", timespan));
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");
}
}
}

View 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
Ombi.Api/PushbulletApi.cs Normal file
View 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
Ombi.Api/PushoverApi.cs Normal file
View 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")));
}
}
}

93
Ombi.Api/RetryHandler.cs Normal file
View 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
Ombi.Api/SickrageApi.cs Normal file
View 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
Ombi.Api/SlackApi.cs Normal file
View 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;
});
}
}
}

468
Ombi.Api/SonarrApi.cs Normal file
View file

@ -0,0 +1,468 @@
#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 (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
});
var obj = policy.Execute(() => Api.ExecuteJson<List<SonarrProfile>>(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 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)
});
return policy.Execute(() => Api.ExecuteJson<List<Series>>(request, baseUrl));
}
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));
return policy.Execute(() => Api.ExecuteJson<Series>(request, baseUrl));
}
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;
}
}
}
}

74
Ombi.Api/TheMovieDbApi.cs Normal file
View file

@ -0,0 +1,74 @@
#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.Collections.Generic;
using System.Threading.Tasks;
using TMDbLib.Client;
using TMDbLib.Objects.General;
using TMDbLib.Objects.Movies;
using TMDbLib.Objects.Search;
namespace Ombi.Api
{
public class TheMovieDbApi : MovieBase
{
public TheMovieDbApi()
{
Client = new TMDbClient(ApiKey);
}
public TMDbClient Client { get; set; }
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>();
}
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();
}
}
}

143
Ombi.Api/TheTvDbApi.cs Normal file
View 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);
}
}
}

39
Ombi.Api/TvBase.cs Normal file
View 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/");
}
}

131
Ombi.Api/TvMazeApi.cs Normal file
View file

@ -0,0 +1,131 @@
#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 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");
var obj = Api.Execute<TvMazeShow>(request, new Uri(Uri));
var episodes = EpisodeLookup(obj.id).ToList();
foreach (var e in episodes)
{
obj.Season.Add(new TvMazeCustomSeason
{
SeasonNumber = e.season,
EpisodeNumber = e.number
});
}
return obj;
}
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
Ombi.Api/TvMazeBase.cs Normal file
View 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";
}
}

11
Ombi.Api/app.config Normal file
View 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>

11
Ombi.Api/packages.config Normal file
View file

@ -0,0 +1,11 @@
<?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" />
</packages>