Plex friends api

pull/13/head
Jamie Rees 9 years ago
parent 10b22cc7ab
commit 59d61b3a0f

@ -27,9 +27,7 @@ namespace RequestPlex.Api
var response = client.Execute<T>(request);
if (response.ErrorException != null)
{
{
var message = "Error retrieving response. Check inner details for more info.";
throw new ApplicationException(message, response.ErrorException);
}

@ -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<UserFriends> 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; }
}
}

@ -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<PlexAuthentication>(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<PlexFriends>(request, new Uri("https://plex.tv/pms/friends/all"));
return users;
}
}
}

@ -62,6 +62,7 @@
<ItemGroup>
<Compile Include="ApiRequest.cs" />
<Compile Include="Models\PlexAuthentication.cs" />
<Compile Include="Models\PlexFriends.cs" />
<Compile Include="Models\PlexUserRequest.cs" />
<Compile Include="PlexApi.cs" />
<Compile Include="TheMovieDbApi.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/"] = _ =>
{
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;
}
Get["admin/"] = _ => Admin();
Post["admin/"] = _ => SaveAdmin();
Post["admin/requestauth"] = _ => RequestAuthToken();
Get["admin/getusers"] = _ => GetUsers();
Get["admin/couchpotato"] = _ => CouchPotato();
}
return View["/Admin/Settings", model];
};
Post["admin/"] = _ =>
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)
{
var model = this.Bind<SettingsModel>();
model.Port = settings.Port;
model.PlexAuthToken = settings.PlexAuthToken;
}
return View["/Admin/Settings", model];
}
var s = new SettingsService();
s.SaveSettings(model);
private Response SaveAdmin()
{
var model = this.Bind<SettingsModel>();
var s = new SettingsService();
s.SaveSettings(model);
return Context.GetRedirect("~/admin");
};
return Context.GetRedirect("~/admin");
}
Post["admin/requestauth"] = _ =>
private Response RequestAuthToken()
{
var user = this.Bind<PlexAuth>();
if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password))
{
var user = this.Bind<PlexAuth>();
return Context.GetRedirect("~/admin?error=true");
}
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 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
{
var newModel = new SettingsModel
{
PlexAuthToken = model.user.authentication_token
};
s.SaveSettings(newModel);
}
PlexAuthToken = model.user.authentication_token
};
s.SaveSettings(newModel);
}
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.
}
return Context.GetRedirect("~/admin");
};
private Response CouchPotato()
{
dynamic model = new ExpandoObject();
Get["admin/getusers"] = _ =>
{
var api = new PlexApi();
return View["/Admin/Settings"];
};
return View["/Admin/CouchPotato", model];
}
}
}

@ -60,7 +60,7 @@
</div>
</div>
<small>Current users that are allowed to authenticate: </small>
<select id="users" multiple="" class="form-control">
<select id="users" multiple="" class="form-control col-lg-10 col-lg-offset-2">
</select>
<div class="form-group">

Loading…
Cancel
Save