Some plex work

This commit is contained in:
Jamie Rees 2016-02-27 20:21:08 +00:00
commit 10b22cc7ab
8 changed files with 183 additions and 83 deletions

View file

@ -1,34 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RequestPlex.Api.Models
{
public class MovieResult
{
public bool adult { get; set; }
public string backdrop_path { get; set; }
public List<object> genre_ids { get; set; }
public int id { get; set; }
public string original_language { get; set; }
public string original_title { get; set; }
public string overview { get; set; }
public string release_date { get; set; }
public string poster_path { get; set; }
public double popularity { get; set; }
public string title { get; set; }
public bool video { get; set; }
public double vote_average { get; set; }
public int vote_count { get; set; }
}
public class MovieSearch
{
public int page { get; set; }
public List<MovieResult> results { get; set; }
public int total_pages { get; set; }
public int total_results { get; set; }
}
}

View file

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace RequestPlex.Api.Models
{
public class PlexAuthentication
{
public User user { get; set; }
}
public class Subscription
{
public bool active { get; set; }
public string status { get; set; }
public object plan { get; set; }
public object features { get; set; }
}
public class Roles
{
public List<object> roles { get; set; }
}
public class User
{
public string email { get; set; }
public string joined_at { get; set; }
public string username { get; set; }
public string title { get; set; }
public string authentication_token { get; set; }
public Subscription subscription { get; set; }
public Roles roles { get; set; }
public List<string> entitlements { get; set; }
public object confirmed_at { get; set; }
public int forum_id { get; set; }
}
}

View file

@ -0,0 +1,14 @@

namespace RequestPlex.Api.Models
{
public class PlexUserRequest
{
public UserRequest user { get; set; }
}
public class UserRequest
{
public string login { get; set; }
public string password { get; set; }
}
}

View file

@ -1,43 +1,51 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Nancy.Json;
using RequestPlex.Api.Models;
using RestSharp;
namespace RequestPlex.Api
{
public class PlexApi
{
public void GetToken(string username, string password)
public PlexAuthentication GetToken(string username, string password)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes("username:password");
string auth = System.Convert.ToBase64String(plainTextBytes);
using (var client = new WebClient())
var userModel = new PlexUserRequest
{
var values = new NameValueCollection
user = new UserRequest
{
["Authorization"] = "Basic " + auth,
["X-Plex-Client-Identifier"] = "RequestPlex0001",
["X-Plex-Product"] = "Request Plex",
["X-Plex-Version"] = "0.1.0"
};
password = password,
login = username
},
};
var request = new RestRequest
{
Method = Method.POST,
};
client.Headers.Add(values);
request.AddHeader("X-Plex-Client-Identifier", "Test213");
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 response = client.UploadString("https://plex.tv/users/sign_in.json", "");
var api = new ApiRequest();
return api.Execute<PlexAuthentication>(request, new Uri("https://plex.tv/users/sign_in.json"));
}
var json = new JavaScriptSerializer();
dynamic result = json.DeserializeObject(response);
public void GetUsers(string authToken)
{
var request = new RestRequest
{
Method = Method.POST,
};
var token = result["user"]["authentication_token"];
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");
Debug.WriteLine(token);
}
}
}
}

View file

@ -61,7 +61,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApiRequest.cs" />
<Compile Include="Models\MovieSearch.cs" />
<Compile Include="Models\PlexAuthentication.cs" />
<Compile Include="Models\PlexUserRequest.cs" />
<Compile Include="PlexApi.cs" />
<Compile Include="TheMovieDbApi.cs" />
<Compile Include="MovieBase.cs" />