Skip to content

Commit

Permalink
#150 start caching plex media as well. refactored the availability ch…
Browse files Browse the repository at this point in the history
…ecker. NEEDS TESTING. also, we need to make the Requests hit the plex api directly rather than hitting the cache as it does now.
  • Loading branch information
Drewster727 committed Apr 9, 2016
1 parent e707837 commit 718e886
Show file tree
Hide file tree
Showing 16 changed files with 800 additions and 593 deletions.
2 changes: 2 additions & 0 deletions PlexRequests.Api.Interfaces/IPlexApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ public interface IPlexApi
PlexSearch SearchContent(string authToken, string searchTerm, Uri plexFullHost);
PlexStatus GetStatus(string authToken, Uri uri);
PlexAccount GetAccount(string authToken);
PlexLibraries GetLibrarySections(string authToken, Uri plexFullHost);
PlexSearch GetLibrary(string authToken, Uri plexFullHost, string libraryId);
}
}
22 changes: 22 additions & 0 deletions PlexRequests.Api.Models/Plex/PlexLibraries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Xml.Serialization;

namespace PlexRequests.Api.Models.Plex
{
[XmlRoot(ElementName = "MediaContainer")]
public class PlexLibraries
{
[XmlElement(ElementName = "Directory")]
public List<Directory> Directories { get; set; }
}

[XmlRoot(ElementName = "Location")]
public partial class Location
{
[XmlElement(ElementName = "id")]
public int id { get; set; }
[XmlElement(ElementName = "path")]
public string path { get; set; }
}

}
35 changes: 35 additions & 0 deletions PlexRequests.Api.Models/Plex/PlexMediaType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: PlexType.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 PlexRequests.Services
{
public enum PlexMediaType
{
Movie,
Show,
Music // double check this one
}
}
2 changes: 2 additions & 0 deletions PlexRequests.Api.Models/Plex/PlexStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class Directory
public string Key { get; set; }
[XmlAttribute(AttributeName = "title")]
public string Title { get; set; }
[XmlAttribute(AttributeName = "type")]
public string type { get; set; }
}

[XmlRoot(ElementName = "MediaContainer")]
Expand Down
2 changes: 2 additions & 0 deletions PlexRequests.Api.Models/PlexRequests.Api.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@
<Compile Include="Plex\PlexAuthentication.cs" />
<Compile Include="Plex\PlexError.cs" />
<Compile Include="Plex\PlexFriends.cs" />
<Compile Include="Plex\PlexLibraries.cs" />
<Compile Include="Plex\PlexSearch.cs" />
<Compile Include="Plex\PlexStatus.cs" />
<Compile Include="Plex\PlexMediaType.cs" />
<Compile Include="Plex\PlexUserRequest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SickRage\SickRageBase.cs" />
Expand Down
80 changes: 56 additions & 24 deletions PlexRequests.Api/PlexApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
using PlexRequests.Helpers;

using RestSharp;
using System.Xml;
using System.Collections.Generic;

namespace PlexRequests.Api
{
Expand All @@ -58,10 +60,7 @@ public PlexAuthentication SignIn(string username, string password)
Method = Method.POST
};

request.AddHeader("X-Plex-Client-Identifier", "Test213"); // TODO need something unique to the users version/installation
request.AddHeader("X-Plex-Product", "Request Plex");
request.AddHeader("X-Plex-Version", Version);
request.AddHeader("Content-Type", "application/json");
AddHeaders(ref request);

request.AddJsonBody(userModel);

Expand All @@ -76,11 +75,7 @@ public PlexFriends GetUsers(string authToken)
Method = Method.GET,
};

request.AddHeader("X-Plex-Client-Identifier", "Test213");
request.AddHeader("X-Plex-Product", "Request Plex");
request.AddHeader("X-Plex-Version", Version);
request.AddHeader("X-Plex-Token", authToken);
request.AddHeader("Content-Type", "application/xml");
AddHeaders(ref request, authToken);

var api = new ApiRequest();
var users = api.ExecuteXml<PlexFriends>(request, new Uri("https://plex.tv/pms/friends/all"));
Expand All @@ -104,11 +99,7 @@ public PlexSearch SearchContent(string authToken, string searchTerm, Uri plexFul
};

request.AddUrlSegment("searchTerm", searchTerm);
request.AddHeader("X-Plex-Client-Identifier", "Test213");
request.AddHeader("X-Plex-Product", "Request Plex");
request.AddHeader("X-Plex-Version", Version);
request.AddHeader("X-Plex-Token", authToken);
request.AddHeader("Content-Type", "application/xml");
AddHeaders(ref request, authToken);

var api = new ApiRequest();
var search = api.ExecuteXml<PlexSearch>(request, plexFullHost);
Expand All @@ -123,11 +114,7 @@ public PlexStatus GetStatus(string authToken, Uri uri)
Method = Method.GET,
};

request.AddHeader("X-Plex-Client-Identifier", "Test213");
request.AddHeader("X-Plex-Product", "Request Plex");
request.AddHeader("X-Plex-Version", Version);
request.AddHeader("X-Plex-Token", authToken);
request.AddHeader("Content-Type", "application/xml");
AddHeaders(ref request, authToken);

var api = new ApiRequest();
var users = api.ExecuteXml<PlexStatus>(request, uri);
Expand All @@ -142,17 +129,62 @@ public PlexAccount GetAccount(string authToken)
Method = Method.GET,
};

request.AddHeader("X-Plex-Client-Identifier", "Test213");
request.AddHeader("X-Plex-Product", "Request Plex");
request.AddHeader("X-Plex-Version", Version);
request.AddHeader("X-Plex-Token", authToken);
request.AddHeader("Content-Type", "application/xml");
AddHeaders(ref request, authToken);

var api = new ApiRequest();
var account = api.ExecuteXml<PlexAccount>(request, new Uri("https://plex.tv/users/account"));

return account;
}

public PlexLibraries GetLibrarySections(string authToken, Uri plexFullHost)
{
var request = new RestRequest
{
Method = Method.GET,
Resource = "library/sections"
};

AddHeaders(ref request, authToken);

var api = new ApiRequest();
var sections = api.ExecuteXml<PlexLibraries>(request, plexFullHost);

var x = GetLibrary(authToken, plexFullHost, sections.Directories[0].Key);

This comment has been minimized.

Copy link
@tidusjar

tidusjar Apr 13, 2016

Member

Why are we doing this when we are not using x?


return sections;
}

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.ToString());
AddHeaders(ref request, authToken);

var api = new ApiRequest();
var search = api.ExecuteXml<PlexSearch>(request, plexFullHost);

return search;
}

private void AddHeaders(ref RestRequest request, string authToken)
{
request.AddHeader("X-Plex-Token", authToken);
AddHeaders(ref request);
}

private void AddHeaders(ref RestRequest request)
{
request.AddHeader("X-Plex-Client-Identifier", "Test213");
request.AddHeader("X-Plex-Product", "Request Plex");
request.AddHeader("X-Plex-Version", Version);
request.AddHeader("Content-Type", "application/xml");
}
}
}

2 changes: 2 additions & 0 deletions PlexRequests.Core/CacheKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace PlexRequests.Core
{
public class CacheKeys
{
public const string PlexLibaries = "PlexLibaries";

public const string TvDbToken = "TheTvDbApiToken";

public const string SonarrQualityProfiles = "SonarrQualityProfiles";
Expand Down
Loading

0 comments on commit 718e886

Please sign in to comment.