Skip to content

Commit

Permalink
refactor(server): re-created plex bandwidth endpoint using new client
Browse files Browse the repository at this point in the history
  • Loading branch information
ADRFranklin committed May 16, 2024
1 parent 9e9986f commit a5bcfb8
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions server/src/services/plex/bandwidth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
import axios from 'axios';

import loggerMain from '@/infrastructure/logger/logger';
import { PlexClient } from '@/infrastructure/plex';
import { MediaServerEntity } from '@/resources/media-server/entity';
import { getPlexClient } from '@/services/plex/client';

const logger = loggerMain.child({ module: 'plex.bandwidth' });

export type GetBandwidthResource = {
MediaContainer: {
size: number;
Device: Array<{
id: number;
name: string;
platform: string;
clientIdentifier: string;
createdAt: number;
}>;
Account: Array<{
id: number;
key: string;
name: string;
defaultAudioLanguage: string;
autoSelectAudio: boolean;
defaultSubtitleLanguage: string;
subtitleMode: number;
thumb: string;
}>;
StatisticsBandwidth: Array<{
accountID: number;
deviceID: number;
timespan: number;
at: number;
lan: boolean;
bytes: number;
}>;
};
};

export const getBandwidthResource = (client: PlexClient) =>
client.request.request<GetBandwidthResource>({
method: 'GET',
url: '/statistics/bandwidth',
query: {
timespan: '6',
},
errors: {
400: 'Bad Request - A parameter was not specified, or was specified incorrectly.',
401: 'Unauthorized - Returned if the X-Plex-Token is missing from the header or query.',
},
});

function timeDifference(prev) {
let previous = prev;
const now = new Date();
Expand All @@ -29,12 +73,11 @@ function timeDifference(prev) {

export default async (server: MediaServerEntity) => {
try {
const res = await axios.get(
`${server.url}/statistics/bandwidth?timespan=6&X-Plex-Token=${server.token}`,
);
const client = getPlexClient(server);
const res = await getBandwidthResource(client);
const data: any = {};
const bWidth: any = [];
res.data.MediaContainer.StatisticsBandwidth.forEach((el) => {
res.MediaContainer.StatisticsBandwidth.forEach((el) => {
const type = el.lan ? 'Local' : 'Remote';
const timestamp = el.at;
if (data[timestamp]) {
Expand Down

0 comments on commit a5bcfb8

Please sign in to comment.