mirror of
https://github.com/LukeHagar/plex-api-oauth.git
synced 2025-12-10 12:37:49 +00:00
RFC Compat testing
This commit is contained in:
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "plex-api-oauth",
|
||||
"version": "1.1.10",
|
||||
"version": "1.1.46",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "plex-api-oauth",
|
||||
"version": "1.1.10",
|
||||
"version": "1.1.46",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"axios": "^0.27.2",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "plex-api-oauth",
|
||||
"version": "1.1.46",
|
||||
"version": "1.1.58",
|
||||
"description": "An NPM Module designed to make Plex Media Server and plex.tv API calls easier to implement in JavaScript and React projects",
|
||||
"main": "./src/index.js",
|
||||
"type": "module",
|
||||
|
||||
@@ -2,193 +2,11 @@ import { PlexOauth } from "plex-oauth";
|
||||
import { v4 } from "uuid";
|
||||
import axios from "axios";
|
||||
import qs from "qs";
|
||||
import InfiniteScroll from "react-infinite-scroller";
|
||||
// import InfiniteScroll from "react-infinite-scroller";
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
export class PlexAPIOAuth {
|
||||
plexClientInformation;
|
||||
clientId;
|
||||
product;
|
||||
device;
|
||||
version;
|
||||
forwardUrl;
|
||||
platform;
|
||||
plexTVAuthToken;
|
||||
plexTVUserData;
|
||||
plexServers;
|
||||
plexLibraries;
|
||||
plexDevices;
|
||||
cancelToken;
|
||||
constructor(
|
||||
clientId = "",
|
||||
product = "Plex-API-OAuth",
|
||||
device = "Web-Client",
|
||||
version = "1",
|
||||
forwardUrl = "",
|
||||
platform = "Web",
|
||||
plexTVAuthToken = "",
|
||||
plexTVUserData = {},
|
||||
plexServers = [],
|
||||
plexLibraries = [],
|
||||
plexDevices = [],
|
||||
cancelToken = null
|
||||
) {
|
||||
this.clientId = clientId;
|
||||
this.product = product;
|
||||
this.device = device;
|
||||
this.version = version;
|
||||
this.forwardUrl = forwardUrl;
|
||||
this.platform = platform;
|
||||
this.plexTVAuthToken = plexTVAuthToken;
|
||||
this.plexTVUserData = plexTVUserData;
|
||||
this.plexServers = plexServers;
|
||||
this.plexDevices = plexDevices;
|
||||
this.plexLibraries = plexLibraries;
|
||||
this.cancelToken = cancelToken;
|
||||
|
||||
this.plexClientInformation = {
|
||||
clientIdentifier: this.clientId, // This is a unique identifier used to identify your app with Plex. - If none is provided a new one is generated and saved locally
|
||||
product: this.product, // Name of your application - Defaults to Plex-API-OAuth
|
||||
device: this.device, // The type of device your application is running on - Defaults to "Web Client"
|
||||
version: this.version, // Version of your application - Defaults to 1
|
||||
forwardUrl: this.forwardUrl, // Url to forward back to after signing in - Defaults to an empty string
|
||||
platform: this.platform, // Platform your application runs on - Defaults to 'Web'
|
||||
};
|
||||
}
|
||||
|
||||
onlyUnique(value, index, self) {
|
||||
return self.indexOf(value) === index;
|
||||
}
|
||||
|
||||
PlexLogout() {
|
||||
this.clientId = "";
|
||||
this.product = "Plex-API-OAuth";
|
||||
this.device = "Web-Client";
|
||||
this.version = "1";
|
||||
this.forwardUrl = "";
|
||||
this.platform = "Web";
|
||||
this.plexTVAuthToken = "";
|
||||
this.plexTVUserData = {};
|
||||
this.plexServers = [];
|
||||
this.plexLibraries = [];
|
||||
this.plexDevices = [];
|
||||
this.plexClientInformation = {
|
||||
clientIdentifier: this.clientId, // This is a unique identifier used to identify your app with Plex. - If none is provided a new one is generated and saved locally
|
||||
product: this.product, // Name of your application - Defaults to Plex-API-OAuth
|
||||
device: this.device, // The type of device your application is running on - Defaults to "Web Client"
|
||||
version: this.version, // Version of your application - Defaults to 1
|
||||
forwardUrl: this.forwardUrl, // Url to forward back to after signing in - Defaults to an empty string
|
||||
platform: this.platform, // Platform your application runs on - Defaults to 'Web'
|
||||
};
|
||||
}
|
||||
|
||||
SetPlexSession({
|
||||
clientId = "",
|
||||
product = "Plex-API-OAuth",
|
||||
device = "Web-Client",
|
||||
version = "1",
|
||||
forwardUrl = "",
|
||||
platform = "Web",
|
||||
plexTVAuthToken = "",
|
||||
plexTVUserData = {},
|
||||
plexServers = [],
|
||||
plexDevices = [],
|
||||
plexLibraries = [],
|
||||
plexLibraryContent = [],
|
||||
}) {
|
||||
this.plexTVAuthToken = plexTVAuthToken;
|
||||
this.plexTVUserData = plexTVUserData;
|
||||
this.clientId = clientId;
|
||||
this.product = product;
|
||||
this.device = device;
|
||||
this.version = version;
|
||||
this.forwardUrl = forwardUrl;
|
||||
this.platform = platform;
|
||||
this.plexServers = plexServers;
|
||||
this.plexDevices = plexDevices;
|
||||
this.plexLibraries = plexLibraries;
|
||||
this.plexClientInformation = {
|
||||
clientIdentifier: this.clientId, // This is a unique identifier used to identify your app with Plex. - If none is provided a new one is generated and saved locally
|
||||
product: this.product, // Name of your application - Defaults to Plex-API-OAuth
|
||||
device: this.device, // The type of device your application is running on - Defaults to "Web Client"
|
||||
version: this.version, // Version of your application - Defaults to 1
|
||||
forwardUrl: this.forwardUrl, // Url to forward back to after signing in - Defaults to an empty string
|
||||
platform: this.platform, // Platform your application runs on - Defaults to 'Web'
|
||||
};
|
||||
}
|
||||
|
||||
SavePlexSession() {
|
||||
window.localStorage.setItem(
|
||||
"plexSessionData",
|
||||
JSON.stringify({
|
||||
plexTVAuthToken: this.plexTVAuthToken,
|
||||
plexTVUserData: this.plexTVUserData,
|
||||
clientId: this.clientId,
|
||||
product: this.product,
|
||||
device: this.device,
|
||||
version: this.version,
|
||||
forwardUrl: this.forwardUrl,
|
||||
platform: this.platform,
|
||||
plexServers: this.plexServers,
|
||||
plexDevices: this.plexDevices,
|
||||
plexLibraries: this.plexLibraries,
|
||||
plexClientInformation: this.plexClientInformation,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
LoadPlexSession() {
|
||||
this.SetPlexSession(
|
||||
JSON.parse(window.localStorage?.getItem("plexSessionData") || "{}")
|
||||
);
|
||||
}
|
||||
|
||||
fnBrowserDetect() {
|
||||
let userAgent = navigator.userAgent;
|
||||
let browserName;
|
||||
|
||||
if (userAgent.match(/chrome|chromium|crios/i)) {
|
||||
browserName = "chrome";
|
||||
} else if (userAgent.match(/firefox|fxios/i)) {
|
||||
browserName = "firefox";
|
||||
} else if (userAgent.match(/safari/i)) {
|
||||
browserName = "safari";
|
||||
} else if (userAgent.match(/opr\//i)) {
|
||||
browserName = "opera";
|
||||
} else if (userAgent.match(/edg/i)) {
|
||||
browserName = "edge";
|
||||
} else {
|
||||
browserName = "No browser detection";
|
||||
}
|
||||
return browserName;
|
||||
}
|
||||
|
||||
GenerateClientId() {
|
||||
this.clientId = v4();
|
||||
this.plexClientInformation.clientIdentifier = this.clientId;
|
||||
}
|
||||
|
||||
GenerateClientInformation() {
|
||||
if (typeof navigator !== "undefined") {
|
||||
this.clientId = v4();
|
||||
this.product = this.fnBrowserDetect;
|
||||
this.device = navigator.userAgentData.platform;
|
||||
this.version = navigator.userAgentData.brands[0].version;
|
||||
this.plexClientInformation = {
|
||||
clientIdentifier: this.clientId,
|
||||
product: this.product,
|
||||
device: this.device,
|
||||
version: this.version,
|
||||
forwardUrl: this.forwardUrl,
|
||||
platform: this.platform,
|
||||
};
|
||||
console.log("Client Information generated successfully");
|
||||
}
|
||||
throw "Unable to detect Client";
|
||||
}
|
||||
|
||||
async PlexLogin() {
|
||||
var plexOauth = new PlexOauth(this.plexClientInformation);
|
||||
export async function PlexLogin(plexClientInformation) {
|
||||
var plexOauth = new PlexOauth(plexClientInformation);
|
||||
let data = await plexOauth.requestHostedLoginURL().catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
@@ -203,15 +21,6 @@ export class PlexAPIOAuth {
|
||||
window.focus();
|
||||
}
|
||||
|
||||
/*
|
||||
* You can now navigate the user's browser to the 'hostedUILink'. This will include the forward URL
|
||||
* for your application, so when they have finished signing into Plex, they will be redirected back
|
||||
* to the specified URL. From there, you just need to perform a query to check for the auth token.
|
||||
* (See Below)
|
||||
*/
|
||||
|
||||
// Check for the auth token, once returning to the application
|
||||
|
||||
let authToken = await plexOauth
|
||||
.checkForAuthToken(pinId, 1000, 10)
|
||||
.catch((err) => {
|
||||
@@ -219,26 +28,23 @@ export class PlexAPIOAuth {
|
||||
});
|
||||
|
||||
if (authToken !== null) {
|
||||
this.plexTVAuthToken = authToken;
|
||||
console.log("Plex Authentication Successful");
|
||||
return authToken;
|
||||
} else {
|
||||
console.log("Plex Authentication Failed");
|
||||
}
|
||||
return authToken;
|
||||
}
|
||||
// An auth token will only be null if the user never signs into the hosted UI, or you stop checking for a new one before they can log in
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexUserData() {
|
||||
export async function GetPlexUserData(plexClientInformation, plexTVAuthToken) {
|
||||
let response = await axios({
|
||||
method: "GET",
|
||||
url:
|
||||
"https://plex.tv/api/v2/user?" +
|
||||
qs.stringify({
|
||||
"X-Plex-Product": this.plexClientInformation.product,
|
||||
"X-Plex-Client-Identifier":
|
||||
this.plexClientInformation.clientIdentifier,
|
||||
"X-Plex-Token": this.plexTVAuthToken,
|
||||
"X-Plex-Product": plexClientInformation.product,
|
||||
"X-Plex-Client-Identifier": plexClientInformation.clientIdentifier,
|
||||
"X-Plex-Token": plexTVAuthToken,
|
||||
}),
|
||||
headers: { accept: "application/json" },
|
||||
}).catch(function (error) {
|
||||
@@ -254,45 +60,56 @@ export class PlexAPIOAuth {
|
||||
console.log(error.config);
|
||||
});
|
||||
|
||||
if (response.status === 200) {
|
||||
this.plexTVUserData = response.data;
|
||||
return this.plexTVUserData;
|
||||
if (response?.status === 200) {
|
||||
return response.data;
|
||||
}
|
||||
if (response.status === 401) {
|
||||
if (response?.status === 401) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexServers(searchParams = {}, filter = {}) {
|
||||
export async function GetPlexDevices(plexClientInformation, plexAuthToken) {
|
||||
let serverArray = [];
|
||||
let response = await axios({
|
||||
method: "GET",
|
||||
url:
|
||||
"https://plex.tv/api/v2/resources?" +
|
||||
qs.stringify({
|
||||
...searchParams,
|
||||
includeHttps: 1,
|
||||
includeRelay: 1,
|
||||
includeIPv6: 1,
|
||||
"X-Plex-Product": this.plexClientInformation.product,
|
||||
"X-Plex-Client-Identifier":
|
||||
this.plexClientInformation.clientIdentifier,
|
||||
"X-Plex-Token": this.plexTVAuthToken,
|
||||
"X-Plex-Product": plexClientInformation.product,
|
||||
"X-Plex-Client-Identifier": plexClientInformation.clientIdentifier,
|
||||
"X-Plex-Token": plexTVAuthToken,
|
||||
}),
|
||||
headers: { accept: "application/json" },
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
this.plexDevices = response.data;
|
||||
for (const server of response.data
|
||||
.filter((Obj) => Obj.product === "Plex Media Server")
|
||||
.filter((Obj) => {
|
||||
for (var key in filter) {
|
||||
if (Obj[key] === filter[key]) {
|
||||
return Obj;
|
||||
}
|
||||
}
|
||||
})) {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
||||
let serverArray = [];
|
||||
let response = await axios({
|
||||
method: "GET",
|
||||
url:
|
||||
"https://plex.tv/api/v2/resources?" +
|
||||
qs.stringify({
|
||||
includeHttps: 1,
|
||||
includeRelay: 1,
|
||||
includeIPv6: 1,
|
||||
"X-Plex-Product": plexClientInformation.product,
|
||||
"X-Plex-Client-Identifier": plexClientInformation.clientIdentifier,
|
||||
"X-Plex-Token": plexTVAuthToken,
|
||||
}),
|
||||
headers: { accept: "application/json" },
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
for (const server of response.data.filter(
|
||||
(Obj) => Obj.product === "Plex Media Server"
|
||||
)) {
|
||||
let localConnection = null;
|
||||
let serverCapabilities = null;
|
||||
let preferredConnection = server.connections.filter(
|
||||
@@ -350,12 +167,14 @@ export class PlexAPIOAuth {
|
||||
connections: server.connections,
|
||||
});
|
||||
}
|
||||
this.plexServers = serverArray;
|
||||
return serverArray;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexLibraries(servers = this.plexServers) {
|
||||
export async function GetPlexLibraries(servers) {
|
||||
let libraryArray = [];
|
||||
if (typeof servers === Object) {
|
||||
console.log("Single Object Detected");
|
||||
}
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
if (server.localConnection) {
|
||||
@@ -400,11 +219,10 @@ export class PlexAPIOAuth {
|
||||
});
|
||||
}
|
||||
}
|
||||
this.plexLibraries = libraryArray;
|
||||
return this.plexLibraries;
|
||||
}
|
||||
return libraryArray;
|
||||
}
|
||||
|
||||
async GetPlexMovieLibraries(servers = this.plexServers) {
|
||||
export async function GetPlexMovieLibraries(servers) {
|
||||
let libraryArray = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -453,9 +271,9 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
}
|
||||
return libraryArray;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexMusicLibraries(servers = this.plexServers) {
|
||||
export async function GetPlexMusicLibraries(servers) {
|
||||
let libraryArray = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -504,9 +322,9 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
}
|
||||
return libraryArray;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexTVShowLibraries(servers = this.plexServers) {
|
||||
export async function GetPlexTVShowLibraries(servers) {
|
||||
let libraryArray = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -555,13 +373,9 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
}
|
||||
return libraryArray;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexMovies(
|
||||
searchParams = {},
|
||||
servers = this.plexServers,
|
||||
libraries = this.plexLibraries
|
||||
) {
|
||||
export async function GetPlexMovies(searchParams = {}, servers, libraries) {
|
||||
let movieLibraryContent = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -581,7 +395,7 @@ export class PlexAPIOAuth {
|
||||
...searchParams,
|
||||
"X-Plex-Token": server?.accessToken,
|
||||
},
|
||||
cancelToken: axios.CancelToken((c) => (this.cancelToken = c)),
|
||||
cancelToken: axios.CancelToken((c) => (cancelToken = c)),
|
||||
headers: { accept: "application/json" },
|
||||
}).catch((e) => {
|
||||
if (axios.isCancel(e)) return;
|
||||
@@ -623,13 +437,9 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
}
|
||||
return movieLibraryContent;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexArtists(
|
||||
searchParams = {},
|
||||
servers = this.plexServers,
|
||||
libraries = this.plexLibraries
|
||||
) {
|
||||
export async function GetPlexArtists(searchParams = {}, servers, libraries) {
|
||||
let artistLibraryContent = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -650,7 +460,7 @@ export class PlexAPIOAuth {
|
||||
...searchParams,
|
||||
"X-Plex-Token": server.accessToken,
|
||||
},
|
||||
cancelToken: axios.CancelToken((c) => (this.cancelToken = c)),
|
||||
cancelToken: axios.CancelToken((c) => (cancelToken = c)),
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
@@ -677,13 +487,14 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
|
||||
return artistLibraryContent;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexAlbums(
|
||||
export async function GetPlexAlbums(
|
||||
searchParams = {},
|
||||
servers = this.plexServers,
|
||||
libraries = this.plexLibraries
|
||||
) {
|
||||
servers,
|
||||
libraries,
|
||||
cancelToken
|
||||
) {
|
||||
let albumLibraryContent = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -704,7 +515,7 @@ export class PlexAPIOAuth {
|
||||
...searchParams,
|
||||
"X-Plex-Token": server.accessToken,
|
||||
},
|
||||
cancelToken: axios.CancelToken((c) => (this.cancelToken = c)),
|
||||
cancelToken: axios.CancelToken((c) => this.props.getCancelToken(c)),
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
@@ -736,13 +547,9 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
}
|
||||
return albumLibraryContent;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexSongs(
|
||||
searchParams = {},
|
||||
servers = this.plexServers,
|
||||
libraries = this.plexLibraries
|
||||
) {
|
||||
export async function GetPlexSongs(searchParams = {}, servers, libraries) {
|
||||
let songLibraryContent = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -763,7 +570,7 @@ export class PlexAPIOAuth {
|
||||
...searchParams,
|
||||
"X-Plex-Token": server.accessToken,
|
||||
},
|
||||
cancelToken: axios.CancelToken((c) => (this.cancelToken = c)),
|
||||
cancelToken: axios.CancelToken((c) => (cancelToken = c)),
|
||||
}).catch((err) => {
|
||||
if (axios.isCancel(err)) return;
|
||||
throw err;
|
||||
@@ -805,13 +612,9 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
}
|
||||
return songLibraryContent;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexShows(
|
||||
searchParams = {},
|
||||
servers = this.plexServers,
|
||||
libraries = this.plexLibraries
|
||||
) {
|
||||
export async function GetPlexShows(searchParams = {}, servers, libraries) {
|
||||
let tvShowLibraryContent = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -826,17 +629,14 @@ export class PlexAPIOAuth {
|
||||
let response = await axios({
|
||||
method: "GET",
|
||||
url:
|
||||
connectionUri.uri +
|
||||
"/library/sections/" +
|
||||
showLibrary?.key +
|
||||
"/all",
|
||||
connectionUri.uri + "/library/sections/" + showLibrary?.key + "/all",
|
||||
headers: { accept: "application/json" },
|
||||
params: {
|
||||
type: 2,
|
||||
...searchParams,
|
||||
"X-Plex-Token": server?.accessToken,
|
||||
},
|
||||
cancelToken: axios.CancelToken((c) => (this.cancelToken = c)),
|
||||
cancelToken: axios.CancelToken((c) => (cancelToken = c)),
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
@@ -879,13 +679,9 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
}
|
||||
return tvShowLibraryContent;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexSeasons(
|
||||
searchParams = {},
|
||||
servers = this.plexServers,
|
||||
libraries = this.plexLibraries
|
||||
) {
|
||||
export async function GetPlexSeasons(searchParams = {}, servers, libraries) {
|
||||
let seasonArray = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -900,17 +696,14 @@ export class PlexAPIOAuth {
|
||||
let response = await axios({
|
||||
method: "GET",
|
||||
url:
|
||||
connectionUri.uri +
|
||||
"/library/sections/" +
|
||||
showLibrary?.key +
|
||||
"/all",
|
||||
connectionUri.uri + "/library/sections/" + showLibrary?.key + "/all",
|
||||
headers: { accept: "application/json" },
|
||||
params: {
|
||||
type: 3,
|
||||
...searchParams,
|
||||
"X-Plex-Token": server?.accessToken,
|
||||
},
|
||||
cancelToken: axios.CancelToken((c) => (this.cancelToken = c)),
|
||||
cancelToken: axios.CancelToken((c) => (cancelToken = c)),
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
@@ -920,13 +713,9 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
}
|
||||
return seasonArray;
|
||||
}
|
||||
}
|
||||
|
||||
async GetPlexEpisodes(
|
||||
searchParams = {},
|
||||
servers = this.plexServers,
|
||||
libraries = this.plexLibraries
|
||||
) {
|
||||
export async function GetPlexEpisodes(searchParams = {}, servers, libraries) {
|
||||
let episodeLibrary = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -941,17 +730,14 @@ export class PlexAPIOAuth {
|
||||
let response = await axios({
|
||||
method: "GET",
|
||||
url:
|
||||
connectionUri.uri +
|
||||
"/library/sections/" +
|
||||
showLibrary?.key +
|
||||
"/all",
|
||||
connectionUri.uri + "/library/sections/" + showLibrary?.key + "/all",
|
||||
headers: { accept: "application/json" },
|
||||
params: {
|
||||
type: 4,
|
||||
...searchParams,
|
||||
"X-Plex-Token": server?.accessToken,
|
||||
},
|
||||
cancelToken: axios.CancelToken((c) => (this.cancelToken = c)),
|
||||
cancelToken: axios.CancelToken((c) => (cancelToken = c)),
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
@@ -961,14 +747,87 @@ export class PlexAPIOAuth {
|
||||
}
|
||||
}
|
||||
return episodeLibrary;
|
||||
}
|
||||
}
|
||||
|
||||
GetLibraryPages(
|
||||
export function fnBrowserDetect() {
|
||||
let userAgent = navigator.userAgent;
|
||||
let browserName;
|
||||
|
||||
if (userAgent.match(/chrome|chromium|crios/i)) {
|
||||
browserName = "chrome";
|
||||
} else if (userAgent.match(/firefox|fxios/i)) {
|
||||
browserName = "firefox";
|
||||
} else if (userAgent.match(/safari/i)) {
|
||||
browserName = "safari";
|
||||
} else if (userAgent.match(/opr\//i)) {
|
||||
browserName = "opera";
|
||||
} else if (userAgent.match(/edg/i)) {
|
||||
browserName = "edge";
|
||||
} else {
|
||||
browserName = "No browser detection";
|
||||
}
|
||||
return browserName;
|
||||
}
|
||||
|
||||
export function SavePlexSession(plexClientInformation, plexTVAuthToken) {
|
||||
window.localStorage.setItem(
|
||||
"plexSessionData",
|
||||
JSON.stringify({
|
||||
plexClientInformation: plexClientInformation,
|
||||
plexTVAuthToken: plexTVAuthToken,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export function LoadPlexSession() {
|
||||
return JSON.parse(window.localStorage?.getItem("plexSessionData") || "{}");
|
||||
}
|
||||
|
||||
export function GeneratePlexClientInformation(forwardUrl = "") {
|
||||
if (typeof navigator !== "undefined") {
|
||||
let plexClientInformation = {
|
||||
clientIdentifier: v4(),
|
||||
product: fnBrowserDetect(),
|
||||
device: navigator.userAgentData.platform,
|
||||
version: navigator.userAgentData.brands[0].version,
|
||||
forwardUrl: forwardUrl,
|
||||
platform: "Plex-API-OAuth",
|
||||
};
|
||||
console.log("Client Information generated successfully");
|
||||
return plexClientInformation;
|
||||
} else {
|
||||
throw "Unable to detect Client";
|
||||
}
|
||||
}
|
||||
|
||||
export function CreatePlexClientInformation(
|
||||
clientIdentifier = v4(),
|
||||
product = "Plex-API-OAuth",
|
||||
device = "Web-Client",
|
||||
version = "1",
|
||||
forwardUrl = "",
|
||||
platform = "Web"
|
||||
) {
|
||||
let plexClientInformation = {
|
||||
clientIdentifier: clientIdentifier,
|
||||
product: product,
|
||||
device: device,
|
||||
version: version,
|
||||
forwardUrl: forwardUrl,
|
||||
platform: platform,
|
||||
};
|
||||
console.log("Client Information generated successfully");
|
||||
return plexClientInformation;
|
||||
}
|
||||
|
||||
export function GetLibraryPages(
|
||||
servers,
|
||||
libraries,
|
||||
libraryType = "",
|
||||
query = null,
|
||||
pageNumber = 0,
|
||||
chunkSize = 50
|
||||
) {
|
||||
) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(false);
|
||||
const [items, setItems] = useState([]);
|
||||
@@ -980,6 +839,13 @@ export class PlexAPIOAuth {
|
||||
setItems([]);
|
||||
}, [query, libraryType]);
|
||||
|
||||
const [cancelToken, setCancelToken] = useState();
|
||||
|
||||
function getCancelToken(event) {
|
||||
setCancelToken(event);
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
setError(false);
|
||||
@@ -997,25 +863,60 @@ export class PlexAPIOAuth {
|
||||
|
||||
switch (libraryType) {
|
||||
case "artists":
|
||||
data = this.GetPlexArtists(searchParams);
|
||||
data = GetPlexArtists(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
break;
|
||||
case "albums":
|
||||
data = this.GetPlexAlbums(searchParams);
|
||||
data = GetPlexAlbums(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
break;
|
||||
case "songs":
|
||||
data = this.GetPlexSongs(searchParams);
|
||||
data = GetPlexSongs(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
break;
|
||||
case "shows":
|
||||
data = this.GetPlexShows(searchParams);
|
||||
data = GetPlexShows(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
break;
|
||||
case "seasons":
|
||||
data = this.GetPlexSeasons(searchParams);
|
||||
data = PlexSession.GetPlexSeasons(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
break;
|
||||
case "episodes":
|
||||
data = this.GetPlexEpisodes(searchParams);
|
||||
data = GetPlexEpisodes(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
break;
|
||||
case "movies":
|
||||
data = this.GetPlexMovies(searchParams);
|
||||
data = GetPlexMovies(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
@@ -1033,11 +934,10 @@ export class PlexAPIOAuth {
|
||||
});
|
||||
} catch {}
|
||||
return () => {
|
||||
if (this.cancelToken) {
|
||||
this.cancelToken();
|
||||
if (cancelToken) {
|
||||
cancelToken();
|
||||
}
|
||||
};
|
||||
}, [query, pageNumber]);
|
||||
return { loading, error, items, hasMore };
|
||||
}
|
||||
}
|
||||
|
||||
23
src/index.js
23
src/index.js
@@ -1 +1,22 @@
|
||||
export { PlexAPIOAuth } from "./PlexAPIOAuth/PlexAPIOAuth.js";
|
||||
export {
|
||||
GeneratePlexClientInformation,
|
||||
CreatePlexClientInformation,
|
||||
PlexLogin,
|
||||
GetPlexUserData,
|
||||
GetPlexServers,
|
||||
GetPlexMovies,
|
||||
GetPlexShows,
|
||||
GetPlexSeasons,
|
||||
GetPlexEpisodes,
|
||||
GetPlexMovieLibraries,
|
||||
GetPlexMusicLibraries,
|
||||
GetPlexTVShowLibraries,
|
||||
GetPlexArtists,
|
||||
GetPlexAlbums,
|
||||
GetPlexSongs,
|
||||
GetPlexLibraries,
|
||||
GetPlexDevices,
|
||||
GetLibraryPages,
|
||||
LoadPlexSession,
|
||||
SavePlexSession,
|
||||
} from "./PlexAPIOAuth/PlexAPIOAuth.js";
|
||||
|
||||
@@ -1,177 +1,194 @@
|
||||
import { PlexAPIOAuth } from "../src/index.js";
|
||||
import { strict as assert } from "assert";
|
||||
import {
|
||||
CreatePlexClientInformation,
|
||||
GeneratePlexClientInformation,
|
||||
PlexLogin,
|
||||
GetPlexUserData,
|
||||
} from "../src/index.js";
|
||||
import React, { useState } from "React";
|
||||
|
||||
describe("Unit Tests", function () {
|
||||
let PlexSession = new PlexAPIOAuth();
|
||||
function UnitTests() {
|
||||
const [plexClientInformation, setPlexClientInformation] = useState();
|
||||
const [plexTVAuthToken, setPlexTVAuthToken] = useState();
|
||||
const [plexTVUserData, setPlexUserData] = useState();
|
||||
const [plexServers, setPlexServers] = useState();
|
||||
describe("Unit Tests", function () {
|
||||
let emptyArray = [];
|
||||
it("Generate ClientId", function () {
|
||||
PlexSession.GenerateClientId();
|
||||
assert.notEqual(PlexSession.clientId, null);
|
||||
assert.notEqual(PlexSession.clientId, undefined);
|
||||
// console.log("Plex Session");
|
||||
// console.log(PlexSession);
|
||||
setPlexClientInformation(CreatePlexClientInformation());
|
||||
assert.notEqual(plexClientInformation, null);
|
||||
assert.notEqual(plexClientInformation, undefined);
|
||||
console.log("Plex Session");
|
||||
console.log(plexClientInformation);
|
||||
});
|
||||
it("Login", async function () {
|
||||
this.timeout(10000);
|
||||
let response = await PlexSession.PlexLogin();
|
||||
assert.notEqual(PlexSession.plexTVAuthToken, undefined);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Auth Token");
|
||||
// console.log(PlexSession.plexTVAuthToken);
|
||||
setPlexClientInformation(await PlexLogin(plexClientInformation));
|
||||
assert.notEqual(plexTVAuthToken, null);
|
||||
assert.notEqual(plexTVAuthToken, undefined);
|
||||
console.log("Auth Token");
|
||||
console.log(plexTVAuthToken);
|
||||
});
|
||||
it("Get Plex User Data", async function () {
|
||||
this.timeout(5000);
|
||||
let response = await PlexSession.GetPlexUserData();
|
||||
assert.notEqual(PlexSession.plexTVUserData, undefined);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("User Data");
|
||||
// console.log(PlexSession.plexTVUserData);
|
||||
setPlexUserData(
|
||||
await GetPlexUserData(plexClientInformation, plexTVAuthToken)
|
||||
);
|
||||
assert.notEqual(plexTVUserData, undefined);
|
||||
assert.notEqual(plexTVUserData, null);
|
||||
console.log("User Data");
|
||||
console.log(plexTVUserData);
|
||||
});
|
||||
it("Get Plex Servers", async function () {
|
||||
this.timeout(12000);
|
||||
let response = await PlexSession.GetPlexServers({}, { owned: true });
|
||||
assert.notEqual(PlexSession.plexServers, emptyArray);
|
||||
assert.notEqual(PlexSession.plexServers, null);
|
||||
assert.notEqual(PlexSession.plexServers, undefined);
|
||||
assert.notEqual(PlexSession.plexDevices, emptyArray);
|
||||
assert.notEqual(PlexSession.plexDevices, null);
|
||||
assert.notEqual(PlexSession.plexDevices, undefined);
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Servers");
|
||||
// console.log(PlexSession.plexServers);
|
||||
setPlexServers(
|
||||
await PlexSession.GetPlexServers(
|
||||
plexClientInformation,
|
||||
plexTVAuthToken,
|
||||
{
|
||||
owned: true,
|
||||
}
|
||||
)
|
||||
);
|
||||
assert.notEqual(plexServers, emptyArray);
|
||||
assert.notEqual(plexServers, null);
|
||||
assert.notEqual(plexServers, undefined);
|
||||
console.log("Plex Servers");
|
||||
console.log(plexServers);
|
||||
});
|
||||
it("Get Plex Libraries", async function () {
|
||||
this.timeout(10000);
|
||||
let response = await PlexSession.GetPlexLibraries();
|
||||
assert.notEqual(PlexSession.plexLibraries, null);
|
||||
assert.notEqual(PlexSession.plexLibraries, undefined);
|
||||
assert.notEqual(PlexSession.plexLibraries, emptyArray);
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
//console.log("Plex Libraries");
|
||||
//console.log(PlexSession.plexLibraries);
|
||||
});
|
||||
it("Get Plex Movie Libraries", async function () {
|
||||
this.timeout(20000);
|
||||
let response = await PlexSession.GetPlexMovieLibraries();
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Movie Libraries");
|
||||
// it("Get Plex Libraries", async function () {
|
||||
// this.timeout(10000);
|
||||
// let response = await PlexSession.GetPlexLibraries();
|
||||
// assert.notEqual(PlexSession.plexLibraries, null);
|
||||
// assert.notEqual(PlexSession.plexLibraries, undefined);
|
||||
// assert.notEqual(PlexSession.plexLibraries, emptyArray);
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// //console.log("Plex Libraries");
|
||||
// //console.log(PlexSession.plexLibraries);
|
||||
// });
|
||||
// it("Get Plex Movie Libraries", async function () {
|
||||
// this.timeout(20000);
|
||||
// let response = await PlexSession.GetPlexMovieLibraries();
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex Movie Libraries");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex Music Libraries", async function () {
|
||||
// this.timeout(10000);
|
||||
// let response = await PlexSession.GetPlexMusicLibraries();
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex Music Libraries");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex TV Libraries", async function () {
|
||||
// this.timeout(10000);
|
||||
// let response = await PlexSession.GetPlexTVShowLibraries();
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex TV Libraries");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex Movies", async function () {
|
||||
// this.timeout(10000);
|
||||
// let response = await PlexSession.GetPlexMovies({
|
||||
// "X-Plex-Container-Start": 0,
|
||||
// "X-Plex-Container-Size": 2,
|
||||
// });
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex Movies");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex Shows", async function () {
|
||||
// this.timeout(10000);
|
||||
// let response = await PlexSession.GetPlexShows({
|
||||
// "X-Plex-Container-Start": 0,
|
||||
// "X-Plex-Container-Size": 2,
|
||||
// });
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex Shows");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex Seasons", async function () {
|
||||
// this.timeout(10000);
|
||||
// let response = await PlexSession.GetPlexSeasons({
|
||||
// "X-Plex-Container-Start": 0,
|
||||
// "X-Plex-Container-Size": 2,
|
||||
// });
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex Seasons");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex Episodes", async function () {
|
||||
// this.timeout(20000);
|
||||
// let response = await PlexSession.GetPlexEpisodes({
|
||||
// "X-Plex-Container-Start": 0,
|
||||
// "X-Plex-Container-Size": 2,
|
||||
// });
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex Episodes");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex Artists", async function () {
|
||||
// this.timeout(10000);
|
||||
// let response = await PlexSession.GetPlexArtists({
|
||||
// "X-Plex-Container-Start": 0,
|
||||
// "X-Plex-Container-Size": 2,
|
||||
// });
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex Artists");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex Albums", async function () {
|
||||
// this.timeout(10000);
|
||||
// let response = await PlexSession.GetPlexAlbums({
|
||||
// "X-Plex-Container-Start": 0,
|
||||
// "X-Plex-Container-Size": 2,
|
||||
// });
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex Albums");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex Songs", async function () {
|
||||
// this.timeout(20000);
|
||||
// let response = await PlexSession.GetPlexSongs({
|
||||
// "X-Plex-Container-Start": 0,
|
||||
// "X-Plex-Container-Size": 2,
|
||||
// });
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// // console.log("Plex Songs");
|
||||
// // console.log(response);
|
||||
// });
|
||||
// it("Get Plex Songs Paged", async function () {
|
||||
// this.timeout(20000);
|
||||
// let response = GetLibraryPages(PlexSession, "songs");
|
||||
// assert.notEqual(response, emptyArray);
|
||||
// assert.notEqual(response, null);
|
||||
// assert.notEqual(response, undefined);
|
||||
// console.log("Plex Songs Paged");
|
||||
// console.log(response);
|
||||
// });
|
||||
});
|
||||
it("Get Plex Music Libraries", async function () {
|
||||
this.timeout(10000);
|
||||
let response = await PlexSession.GetPlexMusicLibraries();
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Music Libraries");
|
||||
// console.log(response);
|
||||
});
|
||||
it("Get Plex TV Libraries", async function () {
|
||||
this.timeout(10000);
|
||||
let response = await PlexSession.GetPlexTVShowLibraries();
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex TV Libraries");
|
||||
// console.log(response);
|
||||
});
|
||||
it("Get Plex Movies", async function () {
|
||||
this.timeout(10000);
|
||||
let response = await PlexSession.GetPlexMovies({
|
||||
"X-Plex-Container-Start": 0,
|
||||
"X-Plex-Container-Size": 2,
|
||||
});
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Movies");
|
||||
// console.log(response);
|
||||
});
|
||||
it("Get Plex Shows", async function () {
|
||||
this.timeout(10000);
|
||||
let response = await PlexSession.GetPlexShows({
|
||||
"X-Plex-Container-Start": 0,
|
||||
"X-Plex-Container-Size": 2,
|
||||
});
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Shows");
|
||||
// console.log(response);
|
||||
});
|
||||
it("Get Plex Seasons", async function () {
|
||||
this.timeout(10000);
|
||||
let response = await PlexSession.GetPlexSeasons({
|
||||
"X-Plex-Container-Start": 0,
|
||||
"X-Plex-Container-Size": 2,
|
||||
});
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Seasons");
|
||||
// console.log(response);
|
||||
});
|
||||
it("Get Plex Episodes", async function () {
|
||||
this.timeout(20000);
|
||||
let response = await PlexSession.GetPlexEpisodes({
|
||||
"X-Plex-Container-Start": 0,
|
||||
"X-Plex-Container-Size": 2,
|
||||
});
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Episodes");
|
||||
// console.log(response);
|
||||
});
|
||||
it("Get Plex Artists", async function () {
|
||||
this.timeout(10000);
|
||||
let response = await PlexSession.GetPlexArtists({
|
||||
"X-Plex-Container-Start": 0,
|
||||
"X-Plex-Container-Size": 2,
|
||||
});
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Artists");
|
||||
// console.log(response);
|
||||
});
|
||||
it("Get Plex Albums", async function () {
|
||||
this.timeout(10000);
|
||||
let response = await PlexSession.GetPlexAlbums({
|
||||
"X-Plex-Container-Start": 0,
|
||||
"X-Plex-Container-Size": 2,
|
||||
});
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Albums");
|
||||
// console.log(response);
|
||||
});
|
||||
it("Get Plex Songs", async function () {
|
||||
this.timeout(20000);
|
||||
let response = await PlexSession.GetPlexSongs({
|
||||
"X-Plex-Container-Start": 0,
|
||||
"X-Plex-Container-Size": 2,
|
||||
});
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
// console.log("Plex Songs");
|
||||
// console.log(response);
|
||||
});
|
||||
it("Get Plex Songs Paged", async function () {
|
||||
this.timeout(20000);
|
||||
let response = PlexSession.GetLibraryPages("songs");
|
||||
assert.notEqual(response, emptyArray);
|
||||
assert.notEqual(response, null);
|
||||
assert.notEqual(response, undefined);
|
||||
//console.log("Plex Songs Paged");
|
||||
//console.log(response);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
UnitTests();
|
||||
|
||||
Reference in New Issue
Block a user