mirror of
https://github.com/LukeHagar/plex-api-oauth.git
synced 2025-12-09 12:37:50 +00:00
Cleanup for RFC
This commit is contained in:
@@ -4,8 +4,11 @@ import axios from "axios";
|
||||
import qs from "qs";
|
||||
// import InfiniteScroll from "react-infinite-scroller";
|
||||
import { useState, useEffect } from "react";
|
||||
import { GeneratePlexClientInformation } from "..";
|
||||
|
||||
export async function PlexLogin(plexClientInformation) {
|
||||
export async function PlexLogin(
|
||||
plexClientInformation = GeneratePlexClientInformation()
|
||||
) {
|
||||
var plexOauth = new PlexOauth(plexClientInformation);
|
||||
let data = await plexOauth.requestHostedLoginURL().catch((err) => {
|
||||
throw err;
|
||||
@@ -32,11 +35,17 @@ export async function PlexLogin(plexClientInformation) {
|
||||
} else {
|
||||
console.log("Plex Authentication Failed");
|
||||
}
|
||||
return authToken;
|
||||
return {
|
||||
plexTVAuthToken: authToken,
|
||||
plexClientInformation: plexClientInformation,
|
||||
};
|
||||
// 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
|
||||
}
|
||||
|
||||
export async function GetPlexUserData(plexClientInformation, plexTVAuthToken) {
|
||||
export async function GetPlexUserData({
|
||||
plexClientInformation,
|
||||
plexTVAuthToken,
|
||||
}) {
|
||||
let response = await axios({
|
||||
method: "GET",
|
||||
url:
|
||||
@@ -68,7 +77,10 @@ export async function GetPlexUserData(plexClientInformation, plexTVAuthToken) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function GetPlexDevices(plexClientInformation, plexAuthToken) {
|
||||
export async function GetPlexDevices({
|
||||
plexClientInformation,
|
||||
plexTVAuthToken,
|
||||
}) {
|
||||
let serverArray = [];
|
||||
let response = await axios({
|
||||
method: "GET",
|
||||
@@ -89,7 +101,10 @@ export async function GetPlexDevices(plexClientInformation, plexAuthToken) {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
||||
export async function GetPlexServers({
|
||||
plexClientInformation,
|
||||
plexTVAuthToken,
|
||||
}) {
|
||||
let serverArray = [];
|
||||
let response = await axios({
|
||||
method: "GET",
|
||||
@@ -489,12 +504,7 @@ export async function GetPlexArtists(searchParams = {}, servers, libraries) {
|
||||
return artistLibraryContent;
|
||||
}
|
||||
|
||||
export async function GetPlexAlbums(
|
||||
searchParams = {},
|
||||
servers,
|
||||
libraries,
|
||||
cancelToken
|
||||
) {
|
||||
export async function GetPlexAlbums(searchParams = {}, servers, libraries) {
|
||||
let albumLibraryContent = [];
|
||||
for (const server of servers) {
|
||||
let connectionUri = server.relayConnection;
|
||||
@@ -769,7 +779,7 @@ export function fnBrowserDetect() {
|
||||
return browserName;
|
||||
}
|
||||
|
||||
export function SavePlexSession(plexClientInformation, plexTVAuthToken) {
|
||||
export function SavePlexSession({ plexClientInformation, plexTVAuthToken }) {
|
||||
window.localStorage.setItem(
|
||||
"plexSessionData",
|
||||
JSON.stringify({
|
||||
@@ -783,30 +793,13 @@ 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",
|
||||
product = fnBrowserDetect(),
|
||||
device = navigator.userAgentData.platform,
|
||||
version = navigator.userAgentData.brands[0].version,
|
||||
forwardUrl = "",
|
||||
platform = "Web"
|
||||
platform = "Plex-API-OAuth"
|
||||
) {
|
||||
let plexClientInformation = {
|
||||
clientIdentifier: clientIdentifier,
|
||||
@@ -839,13 +832,6 @@ export function GetLibraryPages(
|
||||
setItems([]);
|
||||
}, [query, libraryType]);
|
||||
|
||||
const [cancelToken, setCancelToken] = useState();
|
||||
|
||||
function getCancelToken(event) {
|
||||
setCancelToken(event);
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
setError(false);
|
||||
@@ -863,60 +849,25 @@ export function GetLibraryPages(
|
||||
|
||||
switch (libraryType) {
|
||||
case "artists":
|
||||
data = GetPlexArtists(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
data = GetPlexArtists(searchParams, servers, libraries);
|
||||
break;
|
||||
case "albums":
|
||||
data = GetPlexAlbums(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
data = GetPlexAlbums(searchParams, servers, libraries);
|
||||
break;
|
||||
case "songs":
|
||||
data = GetPlexSongs(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
data = GetPlexSongs(searchParams, servers, libraries);
|
||||
break;
|
||||
case "shows":
|
||||
data = GetPlexShows(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
data = GetPlexShows(searchParams, servers, libraries);
|
||||
break;
|
||||
case "seasons":
|
||||
data = PlexSession.GetPlexSeasons(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
data = GetPlexSeasons(searchParams, servers, libraries);
|
||||
break;
|
||||
case "episodes":
|
||||
data = GetPlexEpisodes(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
data = GetPlexEpisodes(searchParams, servers, libraries);
|
||||
break;
|
||||
case "movies":
|
||||
data = GetPlexMovies(
|
||||
searchParams,
|
||||
servers,
|
||||
libraries,
|
||||
this.getCancelToken.bind(this)
|
||||
);
|
||||
data = GetPlexMovies(searchParams, servers, libraries);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
@@ -933,11 +884,6 @@ export function GetLibraryPages(
|
||||
setError(e);
|
||||
});
|
||||
} catch {}
|
||||
return () => {
|
||||
if (cancelToken) {
|
||||
cancelToken();
|
||||
}
|
||||
};
|
||||
}, [query, pageNumber]);
|
||||
}, [query, pageNumber, libraryType]);
|
||||
return { loading, error, items, hasMore };
|
||||
}
|
||||
|
||||
@@ -5,190 +5,170 @@ import {
|
||||
PlexLogin,
|
||||
GetPlexUserData,
|
||||
} from "../src/index.js";
|
||||
import React, { useState } from "React";
|
||||
import React from "React";
|
||||
|
||||
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 () {
|
||||
setPlexClientInformation(CreatePlexClientInformation());
|
||||
assert.notEqual(plexClientInformation, null);
|
||||
assert.notEqual(plexClientInformation, undefined);
|
||||
console.log("Plex Session");
|
||||
console.log(plexClientInformation);
|
||||
});
|
||||
it("Login", async function () {
|
||||
this.timeout(10000);
|
||||
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);
|
||||
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);
|
||||
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");
|
||||
// // 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);
|
||||
// });
|
||||
describe("Unit Tests", function () {
|
||||
let emptyArray = [];
|
||||
it("Generate ClientId and Login", async function () {
|
||||
this.timeout(10000);
|
||||
let plexSession = await PlexLogin(plexClientInformation);
|
||||
assert.notEqual(plexSession.plexTVAuthToken, null);
|
||||
assert.notEqual(plexSession.plexTVAuthToken, undefined);
|
||||
assert.notEqual(plexSession.plexClientInformation, null);
|
||||
assert.notEqual(plexSession.plexClientInformation, undefined);
|
||||
console.log("Client Info and Auth Token");
|
||||
console.log(plexSession);
|
||||
});
|
||||
}
|
||||
|
||||
UnitTests();
|
||||
it("Get Plex User Data", async function () {
|
||||
this.timeout(5000);
|
||||
let plexSession = await PlexLogin(plexClientInformation);
|
||||
let plexTVUserData = await GetPlexUserData(plexSession);
|
||||
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 plexSession = await PlexLogin(plexClientInformation);
|
||||
let plexTVUserData = await GetPlexUserData(plexSession);
|
||||
let plexServers = await PlexSession.GetPlexServers(plexSession);
|
||||
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");
|
||||
// // 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);
|
||||
// });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user