diff --git a/RequestPlex.Api/ApiRequest.cs b/RequestPlex.Api/ApiRequest.cs index 74837f64c..974e4e9c5 100644 --- a/RequestPlex.Api/ApiRequest.cs +++ b/RequestPlex.Api/ApiRequest.cs @@ -27,9 +27,7 @@ namespace RequestPlex.Api var response = client.Execute(request); if (response.ErrorException != null) - { - - + { var message = "Error retrieving response. Check inner details for more info."; throw new ApplicationException(message, response.ErrorException); } diff --git a/RequestPlex.Api/Models/PlexFriends.cs b/RequestPlex.Api/Models/PlexFriends.cs new file mode 100644 index 000000000..77bafa637 --- /dev/null +++ b/RequestPlex.Api/Models/PlexFriends.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Serialization; +using RestSharp.Deserializers; + +namespace RequestPlex.Api.Models +{ + [XmlRoot(ElementName = "Server")] + public class Server + { + [XmlAttribute(AttributeName = "id")] + public string Id { get; set; } + [XmlAttribute(AttributeName = "serverId")] + public string ServerId { get; set; } + [XmlAttribute(AttributeName = "machineIdentifier")] + public string MachineIdentifier { get; set; } + [XmlAttribute(AttributeName = "name")] + public string Name { get; set; } + [XmlAttribute(AttributeName = "lastSeenAt")] + public string LastSeenAt { get; set; } + [XmlAttribute(AttributeName = "numLibraries")] + public string NumLibraries { get; set; } + [XmlAttribute(AttributeName = "owned")] + public string Owned { get; set; } + } + + [XmlRoot(ElementName = "User")] + public class UserFriends + { + [XmlElement(ElementName = "Server")] + public Server Server { get; set; } + [XmlAttribute(AttributeName = "id")] + public string Id { get; set; } + [XmlAttribute(AttributeName = "title")] + public string Title { get; set; } + [XmlAttribute(AttributeName = "username")] + public string Username { get; set; } + [XmlAttribute(AttributeName = "email")] + public string Email { get; set; } + [XmlAttribute(AttributeName = "recommendationsPlaylistId")] + public string RecommendationsPlaylistId { get; set; } + [XmlAttribute(AttributeName = "thumb")] + public string Thumb { get; set; } + } + + [XmlRoot(ElementName = "MediaContainer")] + public class PlexFriends + { + [XmlElement(ElementName = "User")] + public List User { get; set; } + [XmlAttribute(AttributeName = "friendlyName")] + public string FriendlyName { get; set; } + [XmlAttribute(AttributeName = "identifier")] + public string Identifier { get; set; } + [XmlAttribute(AttributeName = "machineIdentifier")] + public string MachineIdentifier { get; set; } + [XmlAttribute(AttributeName = "totalSize")] + public string TotalSize { get; set; } + [XmlAttribute(AttributeName = "size")] + public string Size { get; set; } + } + +} diff --git a/RequestPlex.Api/PlexApi.cs b/RequestPlex.Api/PlexApi.cs index 79690949e..efa7c2727 100644 --- a/RequestPlex.Api/PlexApi.cs +++ b/RequestPlex.Api/PlexApi.cs @@ -1,6 +1,10 @@ using System; +using System.IO; +using System.Runtime.Remoting.Messaging; using RequestPlex.Api.Models; using RestSharp; +using RestSharp.Deserializers; +using RestSharp.Serializers; namespace RequestPlex.Api { @@ -25,26 +29,30 @@ namespace RequestPlex.Api request.AddHeader("X-Plex-Product", "Request Plex"); request.AddHeader("X-Plex-Version", "0.0.1"); request.AddHeader("Content-Type", "application/json"); - + request.AddJsonBody(userModel); var api = new ApiRequest(); return api.Execute(request, new Uri("https://plex.tv/users/sign_in.json")); } - public void GetUsers(string authToken) + public PlexFriends GetUsers(string authToken) { var request = new RestRequest { - Method = Method.POST, + Method = Method.GET, }; request.AddHeader("X-Plex-Client-Identifier", "Test213"); request.AddHeader("X-Plex-Product", "Request Plex"); request.AddHeader("X-Plex-Version", "0.0.1"); request.AddHeader("X-Plex-Token", authToken); - request.AddHeader("Content-Type", "application/json"); + request.AddHeader("Content-Type", "application/xml"); + var api = new ApiRequest(); + var users = api.Execute(request, new Uri("https://plex.tv/pms/friends/all")); + + return users; } } } diff --git a/RequestPlex.Api/RequestPlex.Api.csproj b/RequestPlex.Api/RequestPlex.Api.csproj index c04ba5870..4afe2f00a 100644 --- a/RequestPlex.Api/RequestPlex.Api.csproj +++ b/RequestPlex.Api/RequestPlex.Api.csproj @@ -62,6 +62,7 @@ + diff --git a/RequestPlex.UI/Modules/AdminModule.cs b/RequestPlex.UI/Modules/AdminModule.cs index 1aa0a6392..b94b30a61 100644 --- a/RequestPlex.UI/Modules/AdminModule.cs +++ b/RequestPlex.UI/Modules/AdminModule.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Dynamic; using System.Linq; +using System.Web.UI; using Nancy; using Nancy.Extensions; using Nancy.ModelBinding; @@ -23,73 +24,93 @@ namespace RequestPlex.UI.Modules #if !DEBUG this.RequiresAuthentication(); #endif - Get["admin/"] = _ => + Get["admin/"] = _ => Admin(); + + Post["admin/"] = _ => SaveAdmin(); + + Post["admin/requestauth"] = _ => RequestAuthToken(); + + Get["admin/getusers"] = _ => GetUsers(); + + Get["admin/couchpotato"] = _ => CouchPotato(); + } + + + private Response Admin() + { + dynamic model = new ExpandoObject(); + model.Errored = Request.Query.error.HasValue; + model.Port = null; + var s = new SettingsService(); + var settings = s.GetSettings(); + if (settings != null) { - dynamic model = new ExpandoObject(); - model.Errored = Request.Query.error.HasValue; - model.Port = null; - var s = new SettingsService(); - var settings = s.GetSettings(); - if (settings != null) - { - model.Port = settings.Port; - model.PlexAuthToken = settings.PlexAuthToken; - } + model.Port = settings.Port; + model.PlexAuthToken = settings.PlexAuthToken; + } - return View["/Admin/Settings", model]; - }; + return View["/Admin/Settings", model]; + } - Post["admin/"] = _ => + private Response SaveAdmin() + { + var model = this.Bind(); + + var s = new SettingsService(); + s.SaveSettings(model); + + + return Context.GetRedirect("~/admin"); + } + + private Response RequestAuthToken() + { + var user = this.Bind(); + + if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password)) { - var model = this.Bind(); + return Context.GetRedirect("~/admin?error=true"); + } - var s = new SettingsService(); - s.SaveSettings(model); - - - return Context.GetRedirect("~/admin"); - }; - - Post["admin/requestauth"] = _ => + var plex = new PlexApi(); + var model = plex.GetToken(user.username, user.password); + var s = new SettingsService(); + var oldSettings = s.GetSettings(); + if (oldSettings != null) { - var user = this.Bind(); - - if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password)) - { - return Context.GetRedirect("~/admin?error=true"); - } - - var plex = new PlexApi(); - var model = plex.GetToken(user.username, user.password); - var s = new SettingsService(); - var oldSettings = s.GetSettings(); - if (oldSettings != null) - { - oldSettings.PlexAuthToken = model.user.authentication_token; - s.SaveSettings(oldSettings); - } - else - { - var newModel = new SettingsModel - { - PlexAuthToken = model.user.authentication_token - }; - s.SaveSettings(newModel); - } - - - - return Context.GetRedirect("~/admin"); - }; - - Get["admin/getusers"] = _ => + oldSettings.PlexAuthToken = model.user.authentication_token; + s.SaveSettings(oldSettings); + } + else { - var api = new PlexApi(); - + var newModel = new SettingsModel + { + PlexAuthToken = model.user.authentication_token + }; + s.SaveSettings(newModel); + } - return View["/Admin/Settings"]; - }; + return Context.GetRedirect("~/admin"); + } + + private Response GetUsers() + { + var s = new SettingsService(); + var token = s.GetSettings().PlexAuthToken; + var api = new PlexApi(); + var users = api.GetUsers(token); + var usernames = users.User.Select(x => x.Username); + return Response.AsJson(usernames); //TODO usernames are not populated. + } + + private Response CouchPotato() + { + dynamic model = new ExpandoObject(); + + + + return View["/Admin/CouchPotato", model]; } } } \ No newline at end of file diff --git a/RequestPlex.UI/Views/Admin/Settings.cshtml b/RequestPlex.UI/Views/Admin/Settings.cshtml index df14a4ed9..0c38cf5f8 100644 --- a/RequestPlex.UI/Views/Admin/Settings.cshtml +++ b/RequestPlex.UI/Views/Admin/Settings.cshtml @@ -60,7 +60,7 @@ Current users that are allowed to authenticate: -