Skip to content

Commit

Permalink
Fixed #553
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Oct 19, 2016
1 parent 1a0e7cb commit 512d213
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 125 deletions.
2 changes: 1 addition & 1 deletion PlexRequests.Api.Interfaces/IPlexApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public interface IPlexApi
PlexSearch GetAllEpisodes(string authToken, Uri host, string section, int startPage, int returnCount);
PlexServer GetServer(string authToken);
PlexSeasonMetadata GetSeasons(string authToken, Uri plexFullHost, string ratingKey);
RecentlyAdded RecentlyAdded(string authToken, Uri plexFullHost);
RecentlyAddedModel RecentlyAdded(string authToken, Uri plexFullHost, string sectionId);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: RecentlyAdded.cs
// File: RecentlyAddedModel.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
Expand Down Expand Up @@ -32,53 +32,102 @@ namespace PlexRequests.Api.Models.Plex
public class RecentlyAddedChild
{
public string _elementType { get; set; }
public string allowSync { get; set; }
public string librarySectionID { get; set; }
public string librarySectionTitle { get; set; }
public string librarySectionUUID { get; set; }
public int ratingKey { get; set; }
public string key { get; set; }
public int parentRatingKey { get; set; }
public int grandparentRatingKey { get; set; }
public string type { get; set; }
public string title { get; set; }
public string grandparentKey { get; set; }
public string parentKey { get; set; }
public string parentTitle { get; set; }
public string parentSummary { get; set; }
public string grandparentTitle { get; set; }
public string summary { get; set; }
public int index { get; set; }
public int parentIndex { get; set; }
public string thumb { get; set; }
public string art { get; set; }
public string parentThumb { get; set; }
public int leafCount { get; set; }
public int viewedLeafCount { get; set; }
public string grandparentThumb { get; set; }
public string grandparentArt { get; set; }
public int duration { get; set; }
public int addedAt { get; set; }
public int updatedAt { get; set; }
public List<object> _children { get; set; }
public string studio { get; set; }
public string chapterSource { get; set; }
public List<Child2> _children { get; set; }
public string contentRating { get; set; }
public string rating { get; set; }
public int? viewCount { get; set; }
public int? lastViewedAt { get; set; }
public int? year { get; set; }
public int? duration { get; set; }
public string parentThumb { get; set; }
public string grandparentTheme { get; set; }
public string originallyAvailableAt { get; set; }
public string chapterSource { get; set; }
public string parentTheme { get; set; }
public string titleSort { get; set; }
public string tagline { get; set; }
public int? viewCount { get; set; }
public int? lastViewedAt { get; set; }
public int? viewOffset { get; set; }
public string rating { get; set; }
public string studio { get; set; }
public string tagline { get; set; }
public string originalTitle { get; set; }
public string audienceRating { get; set; }
public string audienceRatingImage { get; set; }
public string ratingImage { get; set; }
}
public class Child3
{
public string _elementType { get; set; }
public string id { get; set; }
public string key { get; set; }
public double duration { get; set; }
public string file { get; set; }
public double size { get; set; }
public string audioProfile { get; set; }
public string container { get; set; }
public string videoProfile { get; set; }
public string deepAnalysisVersion { get; set; }
public string requiredBandwidths { get; set; }
public string hasThumbnail { get; set; }
public bool? has64bitOffsets { get; set; }
public bool? optimizedForStreaming { get; set; }
public bool? hasChapterTextStream { get; set; }
}

public class RecentlyAdded
public class Child2
{
public string _elementType { get; set; }
public string videoResolution { get; set; }
public int id { get; set; }
public int duration { get; set; }
public int bitrate { get; set; }
public int width { get; set; }
public int height { get; set; }
public string aspectRatio { get; set; }
public int audioChannels { get; set; }
public string audioCodec { get; set; }
public string videoCodec { get; set; }
public string container { get; set; }
public string videoFrameRate { get; set; }
public string audioProfile { get; set; }
public string videoProfile { get; set; }
public List<Child3> _children { get; set; }
public string tag { get; set; }
}

public class RecentlyAddedModel
{
public string _elementType { get; set; }
public string allowSync { get; set; }
public string art { get; set; }
public string identifier { get; set; }
public string librarySectionID { get; set; }
public string librarySectionTitle { get; set; }
public string librarySectionUUID { get; set; }
public string mediaTagPrefix { get; set; }
public string mediaTagVersion { get; set; }
public string mixedParents { get; set; }
public string nocache { get; set; }
public string thumb { get; set; }
public string title1 { get; set; }
public string title2 { get; set; }
public string viewGroup { get; set; }
public string viewMode { get; set; }
public List<RecentlyAddedChild> _children { get; set; }
}
}
2 changes: 1 addition & 1 deletion PlexRequests.Api.Models/PlexRequests.Api.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<Compile Include="Plex\PlexStatus.cs" />
<Compile Include="Plex\PlexMediaType.cs" />
<Compile Include="Plex\PlexUserRequest.cs" />
<Compile Include="Plex\RecentlyAdded.cs" />
<Compile Include="Plex\RecentlyAddedModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SickRage\SickRageBase.cs" />
<Compile Include="SickRage\SickrageShows.cs" />
Expand Down
65 changes: 34 additions & 31 deletions PlexRequests.Api/PlexApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public PlexAuthentication SignIn(string username, string password)
Method = Method.POST
};

AddHeaders(ref request);
AddHeaders(ref request, false);

request.AddJsonBody(userModel);

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

AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

var users = RetryHandler.Execute(() => Api.ExecuteXml<PlexFriends> (request, new Uri(FriendsUri)),
(exception, timespan) => Log.Error (exception, "Exception when calling GetUsers for Plex, Retrying {0}", timespan), null);
Expand All @@ -118,7 +118,7 @@ public PlexSearch SearchContent(string authToken, string searchTerm, Uri plexFul
};

request.AddUrlSegment("searchTerm", searchTerm);
AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

var search = RetryHandler.Execute(() => Api.ExecuteXml<PlexSearch> (request, plexFullHost),
(exception, timespan) => Log.Error (exception, "Exception when calling SearchContent for Plex, Retrying {0}", timespan), null);
Expand All @@ -133,7 +133,7 @@ public PlexStatus GetStatus(string authToken, Uri uri)
Method = Method.GET,
};

AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

var users = RetryHandler.Execute(() => Api.ExecuteXml<PlexStatus> (request, uri),
(exception, timespan) => Log.Error (exception, "Exception when calling GetStatus for Plex, Retrying {0}", timespan), null);
Expand All @@ -148,7 +148,7 @@ public PlexAccount GetAccount(string authToken)
Method = Method.GET,
};

AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

var account = RetryHandler.Execute(() => Api.ExecuteXml<PlexAccount> (request, new Uri(GetAccountUri)),
(exception, timespan) => Log.Error (exception, "Exception when calling GetAccount for Plex, Retrying {0}", timespan), null);
Expand All @@ -164,7 +164,7 @@ public PlexLibraries GetLibrarySections(string authToken, Uri plexFullHost)
Resource = "library/sections"
};

AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

try
{
Expand Down Expand Up @@ -193,7 +193,7 @@ public PlexSearch GetLibrary(string authToken, Uri plexFullHost, string libraryI
};

request.AddUrlSegment("libraryId", libraryId);
AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

try
{
Expand Down Expand Up @@ -228,7 +228,7 @@ public PlexEpisodeMetadata GetEpisodeMetaData(string authToken, Uri host, string
};

request.AddUrlSegment("ratingKey", ratingKey);
AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

try
{
Expand All @@ -253,10 +253,9 @@ public PlexSearch GetAllEpisodes(string authToken, Uri host, string section, int
};

request.AddQueryParameter("type", 4.ToString());
request.AddQueryParameter("X-Plex-Container-Start", startPage.ToString());
request.AddQueryParameter("X-Plex-Container-Size", returnCount.ToString());
AddLimitHeaders(ref request, startPage, returnCount);
request.AddUrlSegment("section", section);
AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

try
{
Expand All @@ -281,7 +280,7 @@ public PlexMetadata GetMetadata(string authToken, Uri plexFullHost, string itemI
};

request.AddUrlSegment("itemId", itemId);
AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

try
{
Expand Down Expand Up @@ -311,7 +310,7 @@ public PlexSeasonMetadata GetSeasons(string authToken, Uri plexFullHost, string
};

request.AddUrlSegment("ratingKey", ratingKey);
AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

try
{
Expand All @@ -338,7 +337,7 @@ public PlexServer GetServer(string authToken)
Method = Method.GET,
};

AddHeaders(ref request, authToken);
AddHeaders(ref request, authToken, false);

var servers = RetryHandler.Execute(() => Api.ExecuteXml<PlexServer>(request, new Uri(ServerUri)),
(exception, timespan) => Log.Error(exception, "Exception when calling GetServer for Plex, Retrying {0}", timespan));
Expand All @@ -347,25 +346,22 @@ public PlexServer GetServer(string authToken)
return servers;
}

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

request.AddHeader("X-Plex-Token", authToken);
request.AddHeader("X-Plex-Client-Identifier", $"PlexRequests.Net{Version}");
request.AddHeader("X-Plex-Product", "Plex Requests .Net");
request.AddHeader("X-Plex-Version", Version);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");

request.AddUrlSegment("sectionId", sectionId);
AddHeaders(ref request, authToken, true);
AddLimitHeaders(ref request, 0, 25);

try
{
var lib = RetryHandler.Execute(() => Api.ExecuteJson<RecentlyAdded>(request, plexFullHost),
(exception, timespan) => Log.Error(exception, "Exception when calling RecentlyAdded for Plex, Retrying {0}", timespan), new[] {
var lib = RetryHandler.Execute(() => Api.ExecuteJson<RecentlyAddedModel>(request, plexFullHost),
(exception, timespan) => Log.Error(exception, "Exception when calling RecentlyAddedModel for Plex, Retrying {0}", timespan), new[] {
TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
Expand All @@ -375,23 +371,30 @@ public RecentlyAdded RecentlyAdded(string authToken, Uri plexFullHost)
}
catch (Exception e)
{
Log.Error(e, "There has been a API Exception when attempting to get the Plex RecentlyAdded");
return new RecentlyAdded();
Log.Error(e, "There has been a API Exception when attempting to get the Plex RecentlyAddedModel");
return new RecentlyAddedModel();
}
}

private void AddHeaders(ref RestRequest request, string authToken)
private void AddLimitHeaders(ref RestRequest request, int from, int to)
{
request.AddHeader("X-Plex-Container-Start", from.ToString());
request.AddHeader("X-Plex-Container-Size", to.ToString());
}

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

private void AddHeaders(ref RestRequest request)
private void AddHeaders(ref RestRequest request, bool json)
{
request.AddHeader("X-Plex-Client-Identifier", $"PlexRequests.Net{Version}");
request.AddHeader("X-Plex-Product", "Plex Requests .Net");
request.AddHeader("X-Plex-Version", Version);
request.AddHeader("Content-Type", "application/xml");
request.AddHeader("Content-Type", json ? "application/json" : "application/xml");
request.AddHeader("Accept", json ? "application/json" : "application/xml");
}
}
}
Expand Down
Loading

0 comments on commit 512d213

Please sign in to comment.