Last active
December 22, 2021 18:18
-
-
Save mikaeldui/86b1e06215fb23c45472bd4c5bdea8db to your computer and use it in GitHub Desktop.
.NET Client for getting Plex session from a local server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Net.Http.Json; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml; | |
using System.Xml.Serialization; | |
namespace Plex | |
{ | |
// Doesn't support authentication. The client must be allowed to access the server unauthenticated. | |
internal class PlexClient : IDisposable | |
{ | |
private HttpClient _httpClient; | |
public PlexClient(Uri uri) | |
{ | |
// The handler isn't needed if your certificate is configured properly, which it probably isn't | |
var handler = new HttpClientHandler(); | |
handler.ClientCertificateOptions = ClientCertificateOption.Manual; | |
handler.ServerCertificateCustomValidationCallback = | |
(httpRequestMessage, cert, cetChain, policyErrors) => | |
{ | |
return true; | |
}; | |
_httpClient = new HttpClient(handler) | |
{ | |
BaseAddress = uri | |
}; | |
// The XML parser ended up being a p.i.t.a. (I never use it) so I went for JSON instead of the XML default | |
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
} | |
public async Task<dynamic[]> GetSessionsAsync() => await _httpClient.GetFromJsonAsync<PlexSessions>("status/sessions"); | |
public void Dispose() => _httpClient.Dispose(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Plex | |
{ | |
public class PlexSessions | |
{ | |
public Mediacontainer MediaContainer { get; set; } | |
} | |
public class Mediacontainer | |
{ | |
public int size { get; set; } | |
public Metadata[] Metadata { get; set; } | |
} | |
public class Metadata | |
{ | |
public int addedAt { get; set; } | |
public string art { get; set; } | |
public float audienceRating { get; set; } | |
public string audienceRatingImage { get; set; } | |
public int duration { get; set; } | |
public string guid { get; set; } | |
public string key { get; set; } | |
public string librarySectionID { get; set; } | |
public string librarySectionKey { get; set; } | |
public string librarySectionTitle { get; set; } | |
public string originallyAvailableAt { get; set; } | |
public string ratingKey { get; set; } | |
public string sessionKey { get; set; } | |
public string studio { get; set; } | |
public string summary { get; set; } | |
public string thumb { get; set; } | |
public string title { get; set; } | |
public string type { get; set; } | |
public int updatedAt { get; set; } | |
public int viewOffset { get; set; } | |
public int year { get; set; } | |
public Medium[] Media { get; set; } | |
public Genre[] Genre { get; set; } | |
public Director[] Director { get; set; } | |
public Writer[] Writer { get; set; } | |
public Producer[] Producer { get; set; } | |
public Country[] Country { get; set; } | |
public Role[] Role { get; set; } | |
public User User { get; set; } | |
public Player Player { get; set; } | |
public Session Session { get; set; } | |
public Transcodesession TranscodeSession { get; set; } | |
public string grandparentTitle { get; set; } | |
public string grandparentArt { get; set; } | |
} | |
public class User | |
{ | |
public string id { get; set; } | |
public string thumb { get; set; } | |
public string title { get; set; } | |
} | |
public class Player | |
{ | |
public string address { get; set; } | |
public string device { get; set; } | |
public string machineIdentifier { get; set; } | |
public string platform { get; set; } | |
public string platformVersion { get; set; } | |
public string product { get; set; } | |
public string profile { get; set; } | |
public string remotePublicAddress { get; set; } | |
public string state { get; set; } | |
public string title { get; set; } | |
public string version { get; set; } | |
public bool local { get; set; } | |
public bool relayed { get; set; } | |
public bool secure { get; set; } | |
public int userID { get; set; } | |
} | |
public class Session | |
{ | |
public string id { get; set; } | |
public int bandwidth { get; set; } | |
public string location { get; set; } | |
} | |
public class Transcodesession | |
{ | |
public string key { get; set; } | |
public bool throttled { get; set; } | |
public bool complete { get; set; } | |
public float progress { get; set; } | |
public int size { get; set; } | |
public float speed { get; set; } | |
public bool error { get; set; } | |
public int duration { get; set; } | |
public int remaining { get; set; } | |
public string context { get; set; } | |
public string sourceVideoCodec { get; set; } | |
public string sourceAudioCodec { get; set; } | |
public string videoDecision { get; set; } | |
public string audioDecision { get; set; } | |
public string protocol { get; set; } | |
public string container { get; set; } | |
public string videoCodec { get; set; } | |
public string audioCodec { get; set; } | |
public int audioChannels { get; set; } | |
public bool transcodeHwRequested { get; set; } | |
public float timeStamp { get; set; } | |
public float maxOffsetAvailable { get; set; } | |
public float minOffsetAvailable { get; set; } | |
} | |
public class Medium | |
{ | |
public string id { get; set; } | |
public string videoProfile { get; set; } | |
public int audioChannels { get; set; } | |
public string audioCodec { get; set; } | |
public int bitrate { get; set; } | |
public string container { get; set; } | |
public int duration { get; set; } | |
public int height { get; set; } | |
public string videoCodec { get; set; } | |
public string videoFrameRate { get; set; } | |
public string videoResolution { get; set; } | |
public int width { get; set; } | |
public bool selected { get; set; } | |
public Part[] Part { get; set; } | |
} | |
public class Part | |
{ | |
public string id { get; set; } | |
public string videoProfile { get; set; } | |
public int bitrate { get; set; } | |
public string container { get; set; } | |
public int duration { get; set; } | |
public int height { get; set; } | |
public int width { get; set; } | |
public string decision { get; set; } | |
public bool selected { get; set; } | |
public Stream[] Stream { get; set; } | |
} | |
public class Stream | |
{ | |
public int bitrate { get; set; } | |
public string codec { get; set; } | |
public string displayTitle { get; set; } | |
public string extendedDisplayTitle { get; set; } | |
public float frameRate { get; set; } | |
public int height { get; set; } | |
public string id { get; set; } | |
public int streamType { get; set; } | |
public int width { get; set; } | |
public string decision { get; set; } | |
public string location { get; set; } | |
public string bitrateMode { get; set; } | |
public int channels { get; set; } | |
public bool selected { get; set; } | |
} | |
public class Genre | |
{ | |
public string count { get; set; } | |
public string filter { get; set; } | |
public string id { get; set; } | |
public string tag { get; set; } | |
} | |
public class Director | |
{ | |
public string count { get; set; } | |
public string filter { get; set; } | |
public string id { get; set; } | |
public string tag { get; set; } | |
} | |
public class Writer | |
{ | |
public string count { get; set; } | |
public string filter { get; set; } | |
public string id { get; set; } | |
public string tag { get; set; } | |
} | |
public class Producer | |
{ | |
public string count { get; set; } | |
public string filter { get; set; } | |
public string id { get; set; } | |
public string tag { get; set; } | |
} | |
public class Country | |
{ | |
public string count { get; set; } | |
public string filter { get; set; } | |
public string id { get; set; } | |
public string tag { get; set; } | |
} | |
public class Role | |
{ | |
public string count { get; set; } | |
public string filter { get; set; } | |
public string id { get; set; } | |
public string role { get; set; } | |
public string tag { get; set; } | |
public string thumb { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment