Fixed #850 also started #844 (Wrote the API interaction)

pull/853/head
Jamie.Rees 8 years ago
parent e17bd94990
commit 64119f57c7

@ -0,0 +1,41 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: IWatcherApi.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 Ombi.Api.Models.Watcher;
namespace Ombi.Api.Interfaces
{
{
public interface IWatcherApi
{
WatcherAddMovieResult AddMovie(string imdbId, string apiKey, Uri baseUrl);
List<WatcherListStatusResult> ListMovies(string apiKey, Uri baseUrl);
List<WatcherListStatusResult> ListMovies(string apiKey, Uri baseUrl, string imdbId);
}
}

@ -55,6 +55,7 @@
<Compile Include="IPushoverApi.cs" />
<Compile Include="ISickRageApi.cs" />
<Compile Include="ISonarrApi.cs" />
<Compile Include="IWatcherApi.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

@ -35,6 +35,10 @@
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.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" />
@ -102,6 +106,8 @@
<Compile Include="Tv\TvSearchResult.cs" />
<Compile Include="Tv\TvShow.cs" />
<Compile Include="Tv\TvShowImages.cs" />
<Compile Include="Watcher\WatcherAddMovieResult.cs" />
<Compile Include="Watcher\WatcherListStatusResult.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />

@ -0,0 +1,34 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: WatcherAddMovieResult.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.Models.Watcher
{
public class WatcherAddMovieResult
{
public string status { get; set; }
public string message { get; set; }
}
}

@ -0,0 +1,76 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: WatcherListStatusResult.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 Newtonsoft.Json;
using RestSharp.Deserializers;
namespace Ombi.Api.Models.Watcher
{
public class Quality2
{
[DeserializeAs(Name = "720P")]
public List<string> Q720P { get; set; }
[DeserializeAs(Name = "1080P")]
public List<string> Q1080P { get; set; }
[DeserializeAs( Name = "4K")]
public List<string> Q4K { get; set; }
public List<string> SD { get; set; }
}
public class Filters
{
public string preferredwords { get; set; }
public string ignoredwords { get; set; }
public string requiredwords { get; set; }
}
public class Quality
{
public Quality2 Quality { get; set; }
public Filters Filters { get; set; }
}
public class WatcherListStatusResult
{
public string status { get; set; }
public string plot { get; set; }
public string rated { get; set; }
public string title { get; set; }
public object finishedscore { get; set; }
public string predb { get; set; }
public string year { get; set; }
public string poster { get; set; }
public string tomatourl { get; set; }
public string released { get; set; }
public object finisheddate { get; set; }
public string dvd { get; set; }
public string tomatorating { get; set; }
public string imdbid { get; set; }
public Quality quality { get; set; }
}
}

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="RestSharp" version="105.2.3" targetFramework="net45" />
</packages>

@ -69,6 +69,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApiRequest.cs" />
<Compile Include="WatcherApi.cs" />
<Compile Include="MusicBrainzApi.cs" />
<Compile Include="SlackApi.cs" />
<Compile Include="PushoverApi.cs" />

@ -0,0 +1,83 @@
#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 Newtonsoft.Json.Linq;
using NLog;
using Ombi.Api.Interfaces;
using Ombi.Api.Models.Movie;
using Ombi.Api.Models.Watcher;
using Ombi.Helpers;
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)
{
return Send<WatcherAddMovieResult>("addmovie", apiKey, baseUrl, imdbId);
}
public List<WatcherListStatusResult> ListMovies(string apiKey, Uri baseUrl)
{
return Send<List<WatcherListStatusResult>>("liststatus", apiKey, baseUrl);
}
public List<WatcherListStatusResult> ListMovies(string apiKey, Uri baseUrl, string imdbId)
{
return Send<List<WatcherListStatusResult>>("liststatus", apiKey, baseUrl, imdbId);
}
private T Send<T>(string mode, string apiKey, Uri baseUrl, string imdbid = "") where T : new()
{
RestRequest request;
request = new RestRequest
{
Resource = "/api"
};
request.AddUrlSegment("apikey", apiKey);
if(!string.IsNullOrEmpty(imdbid))
{ request.AddUrlSegment("imdbid", imdbid);}
request.AddUrlSegment("mode", mode);
return Api.Execute<T>(request, baseUrl);
}
}
}

@ -36,8 +36,8 @@ namespace Ombi.Helpers
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
Formatting = Formatting.None,
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
//TypeNameHandling = TypeNameHandling.Objects,
//TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
NullValueHandling = NullValueHandling.Ignore
};

@ -34,6 +34,7 @@ using Ombi.Api.Interfaces;
using Ombi.Core;
using Ombi.Core.SettingModels;
using Ombi.Core.Users;
using Ombi.Helpers.Permissions;
using Ombi.Services.Interfaces;
using Ombi.Store.Models;
using Ombi.Store.Repository;
@ -80,10 +81,13 @@ namespace Ombi.Services.Jobs
}
var plexUsers = PlexApi.GetUsers(settings.PlexAuthToken);
var userManagementSettings = UserManagementSettings.GetSettings();
var mainPlexAccount = PlexApi.GetAccount(settings.PlexAuthToken);
var requests = RequestService.GetAll().ToList();
var dbUsers = Repo.GetAll().ToList();
var localUsers = LocalUserRepository.GetAll().ToList();
// Regular users
foreach (var user in plexUsers.User)
{
var dbUser = dbUsers.FirstOrDefault(x => x.PlexUserId == user.Id);
@ -144,7 +148,7 @@ namespace Ombi.Services.Jobs
continue;
}
// Looks like it's a new user!
var m = new PlexUsers
{
@ -160,6 +164,30 @@ namespace Ombi.Services.Jobs
Repo.Insert(m);
}
// Main Plex user
var dbMainAcc = dbUsers.FirstOrDefault(x => x.Username.Equals(mainPlexAccount.Username, StringComparison.CurrentCulture));
var localMainAcc = localUsers.FirstOrDefault(x => x.UserName.Equals(mainPlexAccount.Username, StringComparison.CurrentCulture));
// Create the local admin account if it doesn't already exist
if (dbMainAcc == null && localMainAcc == null)
{
var a = new PlexUsers
{
PlexUserId = mainPlexAccount.Id,
Permissions = UserManagementHelper.GetPermissions(userManagementSettings),
Features = UserManagementHelper.GetFeatures(userManagementSettings),
UserAlias = string.Empty,
EmailAddress = mainPlexAccount.Email,
Username = mainPlexAccount.Username,
LoginId = Guid.NewGuid().ToString()
};
a.Permissions += (int) Permissions.Administrator; // Make admin
Repo.Insert(a);
}
}
catch (Exception e)
{

@ -183,7 +183,7 @@ namespace Ombi.UI.Jobs
var plexUserChecker =
TriggerBuilder.Create()
.WithIdentity("PlexUserChecker", "Plex")
.StartAt(DateBuilder.FutureDate(30, IntervalUnit.Minute))
.StartAt(DateBuilder.FutureDate(1, IntervalUnit.Minute))
.WithSimpleSchedule(x => x.WithIntervalInMinutes(s.PlexUserChecker).RepeatForever())
.Build();

@ -406,8 +406,10 @@ namespace Ombi.UI.Modules
private UserManagementUsersViewModel MapPlexUser(UserFriends plexInfo, PlexUsers dbUser, DateTime lastLoggedIn)
{
var newUser = false;
if (dbUser == null)
{
newUser = true;
dbUser = new PlexUsers();
}
var features = (Features)dbUser?.Features;
@ -416,8 +418,8 @@ namespace Ombi.UI.Modules
var m = new UserManagementUsersViewModel
{
Id = plexInfo.Id,
PermissionsFormattedString = permissions == 0 ? "None" : permissions.ToString(),
FeaturesFormattedString = features.ToString(),
PermissionsFormattedString = newUser ? "Processing..." :( permissions == 0 ? "None" : permissions.ToString()),
FeaturesFormattedString = newUser ? "Processing..." : features.ToString(),
Username = plexInfo.Username,
Type = UserType.PlexUser,
EmailAddress = plexInfo.Email,
@ -437,8 +439,10 @@ namespace Ombi.UI.Modules
private UserManagementUsersViewModel MapPlexAdmin(PlexAccount plexInfo, PlexUsers dbUser, DateTime lastLoggedIn)
{
var newUser = false;
if (dbUser == null)
{
newUser = true;
dbUser = new PlexUsers();
}
var features = (Features)dbUser?.Features;
@ -447,8 +451,8 @@ namespace Ombi.UI.Modules
var m = new UserManagementUsersViewModel
{
Id = plexInfo.Id,
PermissionsFormattedString = permissions == 0 ? "None" : permissions.ToString(),
FeaturesFormattedString = features.ToString(),
PermissionsFormattedString = newUser ? "Processing..." : (permissions == 0 ? "None" : permissions.ToString()),
FeaturesFormattedString = newUser ? "Processing..." : features.ToString(),
Username = plexInfo.Username,
Type = UserType.PlexUser,
EmailAddress = plexInfo.Email,

@ -45,6 +45,7 @@ namespace Ombi.UI.NinjectModules
Bind<IHeadphonesApi>().To<HeadphonesApi>();
Bind<ISlackApi>().To<SlackApi>();
Bind<IApiRequest>().To<ApiRequest>();
Bind<IWatcherApi>().To<WatcherApi>();
}
}
}
Loading…
Cancel
Save