mirror of
https://github.com/LukeHagar/plex-api-oauth.git
synced 2025-12-06 20:47:45 +00:00
Organization, formatting, and standardizing responses
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"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",
|
||||
"main": "./src/index.js",
|
||||
"type": "module",
|
||||
|
||||
@@ -3,6 +3,26 @@ import { v4 } from "uuid";
|
||||
import axios from "axios";
|
||||
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) {
|
||||
var plexOauth = new PlexOauth(plexClientInformation);
|
||||
let data = await plexOauth.requestHostedLoginURL().catch((err) => {
|
||||
@@ -84,7 +104,75 @@ export async function GetPlexDevices(plexClientInformation, plexTVAuthToken) {
|
||||
}).catch((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) {
|
||||
@@ -105,6 +193,8 @@ export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
console.debug("Plex Servers Reponse Query:");
|
||||
console.debug(response.data);
|
||||
for (const server of response.data.filter(
|
||||
(Obj) => Obj.product === "Plex Media Server"
|
||||
)) {
|
||||
@@ -118,7 +208,7 @@ export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
||||
)) {
|
||||
if (localConnection === null && serverCapabilities === null) {
|
||||
try {
|
||||
let response = await axios({
|
||||
let capabilitiesresponse = await axios({
|
||||
method: "GET",
|
||||
url:
|
||||
connection.uri +
|
||||
@@ -127,7 +217,7 @@ export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
||||
timeout: 1000,
|
||||
});
|
||||
localConnection = connection;
|
||||
serverCapabilities = response.data.MediaContainer;
|
||||
serverCapabilities = capabilitiesresponse.data.MediaContainer;
|
||||
preferredConnection = connection;
|
||||
} catch {}
|
||||
}
|
||||
@@ -168,7 +258,7 @@ export async function GetPlexServers(plexClientInformation, plexTVAuthToken) {
|
||||
return serverArray;
|
||||
}
|
||||
|
||||
export async function GetPlexLibraries(plexServers, plexLibraries) {
|
||||
export async function GetPlexLibraries(plexServers) {
|
||||
let libraryArray = [];
|
||||
for (const server of plexServers) {
|
||||
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(
|
||||
plexServers,
|
||||
plexLibraries,
|
||||
@@ -1076,15 +1146,12 @@ export async function GetArtistPage(
|
||||
let albums = [];
|
||||
let response = {};
|
||||
let songs = [];
|
||||
let relatedAlbums = [];
|
||||
for (const server of plexServers.filter(
|
||||
(Obj) => Obj.clientIdentifier === artistObject.server
|
||||
)) {
|
||||
let relatedArtists = [];
|
||||
//.filter((Obj) => Obj.clientIdentifier === artistObject.server)
|
||||
//&& Obj.uuid === artistObject.library;
|
||||
for (const server of plexServers) {
|
||||
for (const musicLibrary of plexLibraries.filter(
|
||||
(Obj) =>
|
||||
Obj.server === server.clientIdentifier &&
|
||||
Obj.type === "artist" &&
|
||||
Obj.uuid === artistObject.library
|
||||
(Obj) => Obj.server === server.clientIdentifier && Obj.type === "artist"
|
||||
)) {
|
||||
response = await axios({
|
||||
method: "GET",
|
||||
@@ -1105,7 +1172,7 @@ export async function GetArtistPage(
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
console.debug(response.data);
|
||||
console.warn(response.data);
|
||||
if (response.data.MediaContainer.size > 0) {
|
||||
for (const album of response.data.MediaContainer.Metadata) {
|
||||
albums.push({
|
||||
@@ -1218,7 +1285,7 @@ export async function GetArtistPage(
|
||||
}
|
||||
}
|
||||
|
||||
let relatedAlbumsResponse = await axios({
|
||||
let relatedArtistsResponse = await axios({
|
||||
method: "GET",
|
||||
url:
|
||||
server.preferredConnection.uri +
|
||||
@@ -1238,11 +1305,20 @@ export async function GetArtistPage(
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
console.debug(relatedAlbumsResponse.data);
|
||||
if (relatedAlbumsResponse.data.MediaContainer.size > 0) {
|
||||
for (const album of relatedAlbumsResponse.data.MediaContainer.Hub[6]
|
||||
.Metadata) {
|
||||
relatedAlbums.push({
|
||||
console.debug(relatedArtistsResponse.data);
|
||||
if (
|
||||
relatedArtistsResponse.data.MediaContainer.Hub.some(function (Object) {
|
||||
return Object.title === "Similar Artists";
|
||||
})
|
||||
) {
|
||||
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,
|
||||
library: musicLibrary.uuid,
|
||||
Director: album.Director,
|
||||
@@ -1283,6 +1359,7 @@ export async function GetArtistPage(
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let songResponse = await axios({
|
||||
method: "GET",
|
||||
@@ -1302,6 +1379,8 @@ export async function GetArtistPage(
|
||||
sort: "ratingCount:desc",
|
||||
type: 10,
|
||||
includeUserState: 1,
|
||||
includeCollections: 1,
|
||||
includeExternalMedia: 1,
|
||||
"X-Plex-Container-Start": 0,
|
||||
"X-Plex-Container-Size": 20,
|
||||
"X-Plex-Product": plexClientInformation.product,
|
||||
@@ -1313,6 +1392,7 @@ export async function GetArtistPage(
|
||||
throw err;
|
||||
});
|
||||
console.debug(songResponse.data);
|
||||
if (songResponse.data.MediaContainer.size > 0) {
|
||||
for (const song of songResponse.data.MediaContainer.Metadata) {
|
||||
songs.push({
|
||||
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 {
|
||||
inputObject: artistObject,
|
||||
response: response.data,
|
||||
albums,
|
||||
albums: [...new Set(albumArray)].map(JSON.parse),
|
||||
songs,
|
||||
relatedAlbums,
|
||||
relatedArtists,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user