mirror of
https://github.com/LukeHagar/plex-api-oauth.git
synced 2025-12-07 04:20:42 +00:00
Organization, formatting, and standardizing responses
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "plex-api-oauth",
|
"name": "plex-api-oauth",
|
||||||
"version": "1.1.126",
|
"version": "1.2.4",
|
||||||
"description": "An NPM Module designed to make Plex Media Server and plex.tv API calls easier to implement in JavaScript and React projects",
|
"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",
|
"main": "./src/index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
|||||||
@@ -3,6 +3,26 @@ import { v4 } from "uuid";
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import qs from "qs";
|
import qs from "qs";
|
||||||
|
|
||||||
|
export function CreatePlexClientInformation(
|
||||||
|
clientIdentifier = v4(),
|
||||||
|
product = FnBrowserDetect(),
|
||||||
|
device = navigator.userAgentData.platform,
|
||||||
|
version = navigator.userAgentData.brands[0].version,
|
||||||
|
forwardUrl = "",
|
||||||
|
platform = "Plex-API-OAuth"
|
||||||
|
) {
|
||||||
|
let plexClientInformation = {
|
||||||
|
clientIdentifier: clientIdentifier,
|
||||||
|
product: product,
|
||||||
|
device: device,
|
||||||
|
version: version,
|
||||||
|
forwardUrl: forwardUrl,
|
||||||
|
platform: platform,
|
||||||
|
};
|
||||||
|
console.debug("Client Information generated successfully");
|
||||||
|
return plexClientInformation;
|
||||||
|
}
|
||||||
|
|
||||||
export async function PlexLogin(plexClientInformation) {
|
export async function PlexLogin(plexClientInformation) {
|
||||||
var plexOauth = new PlexOauth(plexClientInformation);
|
var plexOauth = new PlexOauth(plexClientInformation);
|
||||||
let data = await plexOauth.requestHostedLoginURL().catch((err) => {
|
let data = await plexOauth.requestHostedLoginURL().catch((err) => {
|
||||||
@@ -84,7 +104,75 @@ export async function GetPlexDevices(plexClientInformation, plexTVAuthToken) {
|
|||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
return response.data;
|
console.debug("Plex Devices Reponse Query:");
|
||||||
|
console.debug(response.data);
|
||||||
|
for (const server of response.data) {
|
||||||
|
let localConnection = {};
|
||||||
|
let serverCapabilities = {};
|
||||||
|
let preferredConnection = {};
|
||||||
|
let relayConnection = {};
|
||||||
|
if (server.connections) {
|
||||||
|
if (server.connections.length > 0) {
|
||||||
|
relayConnection = server.connections.filter(
|
||||||
|
(connection) => connection.relay === true
|
||||||
|
)[0];
|
||||||
|
preferredConnection = server.connections.filter(
|
||||||
|
(connection) => connection.relay === true
|
||||||
|
)[0];
|
||||||
|
for (const connection of server.connections.filter(
|
||||||
|
(entry) => entry.local === true
|
||||||
|
)) {
|
||||||
|
if (localConnection === null && serverCapabilities === null) {
|
||||||
|
try {
|
||||||
|
let capabilitiesresponse = await axios({
|
||||||
|
method: "GET",
|
||||||
|
url:
|
||||||
|
connection.uri +
|
||||||
|
"/?" +
|
||||||
|
qs.stringify({ "X-Plex-Token": server.accessToken }),
|
||||||
|
timeout: 1000,
|
||||||
|
});
|
||||||
|
localConnection = connection;
|
||||||
|
serverCapabilities = capabilitiesresponse.data.MediaContainer;
|
||||||
|
preferredConnection = connection;
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serverArray.push({
|
||||||
|
name: server.name,
|
||||||
|
product: server.product,
|
||||||
|
productVersion: server.productVersion,
|
||||||
|
platform: server.platform,
|
||||||
|
platformVersion: server.platformVersion,
|
||||||
|
device: server.device,
|
||||||
|
clientIdentifier: server.clientIdentifier,
|
||||||
|
createdAt: server.createdAt,
|
||||||
|
lastSeenAt: server.lastSeenAt,
|
||||||
|
localConnection: localConnection,
|
||||||
|
preferredConnection: preferredConnection,
|
||||||
|
provides: server.provides,
|
||||||
|
ownerId: server.ownerId,
|
||||||
|
sourceTitle: server.sourceTitle,
|
||||||
|
publicAddress: server.publicAddress,
|
||||||
|
accessToken: server.accessToken,
|
||||||
|
owned: server.owned,
|
||||||
|
home: server.home,
|
||||||
|
synced: server.synced,
|
||||||
|
relay: server.relay,
|
||||||
|
relayConnection: relayConnection,
|
||||||
|
serverCapabilities: serverCapabilities,
|
||||||
|
presence: server.presence,
|
||||||
|
httpsRequired: server.httpsRequired,
|
||||||
|
publicAddressMatches: server.publicAddressMatches,
|
||||||
|
dnsRebindingProtection: server.dnsRebindingProtection,
|
||||||
|
natLoopbackSupported: server.natLoopbackSupported,
|
||||||
|
connections: server.connections,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return serverArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
||||||
@@ -105,6 +193,8 @@ export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
|||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
|
console.debug("Plex Servers Reponse Query:");
|
||||||
|
console.debug(response.data);
|
||||||
for (const server of response.data.filter(
|
for (const server of response.data.filter(
|
||||||
(Obj) => Obj.product === "Plex Media Server"
|
(Obj) => Obj.product === "Plex Media Server"
|
||||||
)) {
|
)) {
|
||||||
@@ -118,7 +208,7 @@ export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
|||||||
)) {
|
)) {
|
||||||
if (localConnection === null && serverCapabilities === null) {
|
if (localConnection === null && serverCapabilities === null) {
|
||||||
try {
|
try {
|
||||||
let response = await axios({
|
let capabilitiesresponse = await axios({
|
||||||
method: "GET",
|
method: "GET",
|
||||||
url:
|
url:
|
||||||
connection.uri +
|
connection.uri +
|
||||||
@@ -127,7 +217,7 @@ export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
|||||||
timeout: 1000,
|
timeout: 1000,
|
||||||
});
|
});
|
||||||
localConnection = connection;
|
localConnection = connection;
|
||||||
serverCapabilities = response.data.MediaContainer;
|
serverCapabilities = capabilitiesresponse.data.MediaContainer;
|
||||||
preferredConnection = connection;
|
preferredConnection = connection;
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
@@ -168,7 +258,7 @@ export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
|||||||
return serverArray;
|
return serverArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GetPlexLibraries(plexServers, plexLibraries) {
|
export async function GetPlexLibraries(plexServers) {
|
||||||
let libraryArray = [];
|
let libraryArray = [];
|
||||||
for (const server of plexServers) {
|
for (const server of plexServers) {
|
||||||
let response = await axios({
|
let response = await axios({
|
||||||
@@ -864,26 +954,6 @@ export function LoadPlexSession() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CreatePlexClientInformation(
|
|
||||||
clientIdentifier = v4(),
|
|
||||||
product = FnBrowserDetect(),
|
|
||||||
device = navigator.userAgentData.platform,
|
|
||||||
version = navigator.userAgentData.brands[0].version,
|
|
||||||
forwardUrl = "",
|
|
||||||
platform = "Plex-API-OAuth"
|
|
||||||
) {
|
|
||||||
let plexClientInformation = {
|
|
||||||
clientIdentifier: clientIdentifier,
|
|
||||||
product: product,
|
|
||||||
device: device,
|
|
||||||
version: version,
|
|
||||||
forwardUrl: forwardUrl,
|
|
||||||
platform: platform,
|
|
||||||
};
|
|
||||||
console.debug("Client Information generated successfully");
|
|
||||||
return plexClientInformation;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GetLibraryPages(
|
export async function GetLibraryPages(
|
||||||
plexServers,
|
plexServers,
|
||||||
plexLibraries,
|
plexLibraries,
|
||||||
@@ -1076,15 +1146,12 @@ export async function GetArtistPage(
|
|||||||
let albums = [];
|
let albums = [];
|
||||||
let response = {};
|
let response = {};
|
||||||
let songs = [];
|
let songs = [];
|
||||||
let relatedAlbums = [];
|
let relatedArtists = [];
|
||||||
for (const server of plexServers.filter(
|
//.filter((Obj) => Obj.clientIdentifier === artistObject.server)
|
||||||
(Obj) => Obj.clientIdentifier === artistObject.server
|
//&& Obj.uuid === artistObject.library;
|
||||||
)) {
|
for (const server of plexServers) {
|
||||||
for (const musicLibrary of plexLibraries.filter(
|
for (const musicLibrary of plexLibraries.filter(
|
||||||
(Obj) =>
|
(Obj) => Obj.server === server.clientIdentifier && Obj.type === "artist"
|
||||||
Obj.server === server.clientIdentifier &&
|
|
||||||
Obj.type === "artist" &&
|
|
||||||
Obj.uuid === artistObject.library
|
|
||||||
)) {
|
)) {
|
||||||
response = await axios({
|
response = await axios({
|
||||||
method: "GET",
|
method: "GET",
|
||||||
@@ -1105,7 +1172,7 @@ export async function GetArtistPage(
|
|||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
console.debug(response.data);
|
console.warn(response.data);
|
||||||
if (response.data.MediaContainer.size > 0) {
|
if (response.data.MediaContainer.size > 0) {
|
||||||
for (const album of response.data.MediaContainer.Metadata) {
|
for (const album of response.data.MediaContainer.Metadata) {
|
||||||
albums.push({
|
albums.push({
|
||||||
@@ -1218,7 +1285,7 @@ export async function GetArtistPage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let relatedAlbumsResponse = await axios({
|
let relatedArtistsResponse = await axios({
|
||||||
method: "GET",
|
method: "GET",
|
||||||
url:
|
url:
|
||||||
server.preferredConnection.uri +
|
server.preferredConnection.uri +
|
||||||
@@ -1238,11 +1305,20 @@ export async function GetArtistPage(
|
|||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
console.debug(relatedAlbumsResponse.data);
|
console.debug(relatedArtistsResponse.data);
|
||||||
if (relatedAlbumsResponse.data.MediaContainer.size > 0) {
|
if (
|
||||||
for (const album of relatedAlbumsResponse.data.MediaContainer.Hub[6]
|
relatedArtistsResponse.data.MediaContainer.Hub.some(function (Object) {
|
||||||
.Metadata) {
|
return Object.title === "Similar Artists";
|
||||||
relatedAlbums.push({
|
})
|
||||||
|
) {
|
||||||
|
let relatedArtistHub =
|
||||||
|
relatedArtistsResponse.data.MediaContainer.Hub.filter(
|
||||||
|
(Obj) => Obj.title === "Similar Artists"
|
||||||
|
);
|
||||||
|
console.warn(relatedArtistHub);
|
||||||
|
for (const hubAlbum of relatedArtistHub) {
|
||||||
|
for (const album of hubAlbum.Metadata) {
|
||||||
|
relatedArtists.push({
|
||||||
server: server.clientIdentifier,
|
server: server.clientIdentifier,
|
||||||
library: musicLibrary.uuid,
|
library: musicLibrary.uuid,
|
||||||
Director: album.Director,
|
Director: album.Director,
|
||||||
@@ -1283,6 +1359,7 @@ export async function GetArtistPage(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let songResponse = await axios({
|
let songResponse = await axios({
|
||||||
method: "GET",
|
method: "GET",
|
||||||
@@ -1302,6 +1379,8 @@ export async function GetArtistPage(
|
|||||||
sort: "ratingCount:desc",
|
sort: "ratingCount:desc",
|
||||||
type: 10,
|
type: 10,
|
||||||
includeUserState: 1,
|
includeUserState: 1,
|
||||||
|
includeCollections: 1,
|
||||||
|
includeExternalMedia: 1,
|
||||||
"X-Plex-Container-Start": 0,
|
"X-Plex-Container-Start": 0,
|
||||||
"X-Plex-Container-Size": 20,
|
"X-Plex-Container-Size": 20,
|
||||||
"X-Plex-Product": plexClientInformation.product,
|
"X-Plex-Product": plexClientInformation.product,
|
||||||
@@ -1313,6 +1392,7 @@ export async function GetArtistPage(
|
|||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
console.debug(songResponse.data);
|
console.debug(songResponse.data);
|
||||||
|
if (songResponse.data.MediaContainer.size > 0) {
|
||||||
for (const song of songResponse.data.MediaContainer.Metadata) {
|
for (const song of songResponse.data.MediaContainer.Metadata) {
|
||||||
songs.push({
|
songs.push({
|
||||||
server: server.clientIdentifier,
|
server: server.clientIdentifier,
|
||||||
@@ -1356,12 +1436,20 @@ export async function GetArtistPage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const albumArray = albums
|
||||||
|
.map((item) => {
|
||||||
|
return Object.fromEntries(Object.entries(item).sort());
|
||||||
|
})
|
||||||
|
.map(JSON.stringify);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
inputObject: artistObject,
|
inputObject: artistObject,
|
||||||
response: response.data,
|
response: response.data,
|
||||||
albums,
|
albums: [...new Set(albumArray)].map(JSON.parse),
|
||||||
songs,
|
songs,
|
||||||
relatedAlbums,
|
relatedArtists,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user