Compare commits

...

3 Commits

Author SHA1 Message Date
0xsysr3ll
eb08eb024f fix(plexapi): temporary remove unused PlexLibraryResponse interface 2026-02-16 21:53:14 +01:00
0xsysr3ll
b5c8e21104 feat(plexapi): add debug logs for Plex API response 2026-02-16 21:48:46 +01:00
0xsysr3ll
54baa53aac feat(plexapi): add debug logs for Plex API response 2026-02-16 21:47:01 +01:00
2 changed files with 68 additions and 15 deletions

View File

@@ -27,10 +27,13 @@ export interface PlexLibraryItem {
Media: Media[]; Media: Media[];
} }
interface PlexLibraryResponse { interface PlexLibraryResponseRaw {
MediaContainer: { MediaContainer: {
totalSize: number; totalSize?: number;
Metadata: PlexLibraryItem[]; size?: number;
Metadata?: PlexLibraryItem[];
Directory?: PlexLibraryItem[];
[key: string]: unknown;
}; };
} }
@@ -177,7 +180,7 @@ class PlexAPI extends ExternalAPI {
id: string, id: string,
{ offset = 0, size = 50 }: { offset?: number; size?: number } = {} { offset = 0, size = 50 }: { offset?: number; size?: number } = {}
): Promise<{ totalSize: number; items: PlexLibraryItem[] }> { ): Promise<{ totalSize: number; items: PlexLibraryItem[] }> {
const response = await this.get<PlexLibraryResponse>( const response = await this.get<PlexLibraryResponseRaw>(
`/library/sections/${id}/all?includeGuids=1`, `/library/sections/${id}/all?includeGuids=1`,
{ {
headers: { headers: {
@@ -187,9 +190,26 @@ class PlexAPI extends ExternalAPI {
} }
); );
const container = response.MediaContainer;
const metadataLength = container.Metadata?.length ?? 0;
const directoryLength = container.Directory?.length ?? 0;
logger.debug('Plex getLibraryContents raw response', {
label: 'Plex API',
libraryId: id,
offset,
metadataLength,
directoryLength,
totalSize: container.totalSize,
size: container.size,
keys: Object.keys(container).filter((k) =>
['Metadata', 'Directory', 'totalSize', 'size'].includes(k)
),
});
return { return {
totalSize: response.MediaContainer.totalSize, totalSize: container.totalSize ?? 0,
items: response.MediaContainer.Metadata ?? [], items: container.Metadata ?? [],
}; };
} }
@@ -221,7 +241,7 @@ class PlexAPI extends ExternalAPI {
}, },
mediaType: 'movie' | 'show' mediaType: 'movie' | 'show'
): Promise<PlexLibraryItem[]> { ): Promise<PlexLibraryItem[]> {
const response = await this.get<PlexLibraryResponse>( const response = await this.get<PlexLibraryResponseRaw>(
`/library/sections/${id}/all?type=${ `/library/sections/${id}/all?type=${
mediaType === 'show' ? '4' : '1' mediaType === 'show' ? '4' : '1'
}&sort=addedAt%3Adesc&addedAt>>=${Math.floor(options.addedAt / 1000)}`, }&sort=addedAt%3Adesc&addedAt>>=${Math.floor(options.addedAt / 1000)}`,
@@ -233,7 +253,21 @@ class PlexAPI extends ExternalAPI {
} }
); );
return response.MediaContainer.Metadata; const container = response.MediaContainer;
const items = container.Metadata ?? [];
logger.debug('Plex getRecentlyAdded raw response', {
label: 'Plex API',
libraryId: id,
mediaType,
addedAtFilter: options.addedAt,
addedAtFilterDate: new Date(options.addedAt).toISOString(),
metadataLength: container.Metadata?.length ?? 0,
directoryLength: container.Directory?.length ?? 0,
itemsReturned: items.length,
});
return items;
} }
} }

View File

@@ -90,22 +90,41 @@ class PlexScanner
if (this.isRecentOnly) { if (this.isRecentOnly) {
for (const library of this.libraries) { for (const library of this.libraries) {
this.currentLibrary = library; this.currentLibrary = library;
const addedAtOpt = library.lastScan
? {
// We remove 10 minutes from the last scan as a buffer
addedAt: library.lastScan - 1000 * 60 * 10,
}
: undefined;
this.log( this.log(
`Beginning to process recently added for library: ${library.name}`, `Beginning to process recently added for library: ${library.name}`,
'info', 'info',
{ lastScan: library.lastScan } {
lastScan: library.lastScan,
addedAtFilter: addedAtOpt?.addedAt,
addedAtFilterDate: addedAtOpt
? new Date(addedAtOpt.addedAt).toISOString()
: undefined,
}
); );
const libraryItems = await this.plexClient.getRecentlyAdded( const libraryItems = await this.plexClient.getRecentlyAdded(
library.id, library.id,
library.lastScan addedAtOpt ?? {
? { addedAt: Date.now() - 1000 * 60 * 60,
// We remove 10 minutes from the last scan as a buffer },
addedAt: library.lastScan - 1000 * 60 * 10,
}
: undefined,
library.type library.type
); );
this.log(
`Recently added fetched ${libraryItems.length} items for library: ${library.name}`,
'debug',
{
libraryId: library.id,
itemCount: libraryItems.length,
lastScanWillUpdateTo: Date.now(),
}
);
// Bundle items up by rating keys // Bundle items up by rating keys
this.items = uniqWith(libraryItems, (mediaA, mediaB) => { this.items = uniqWith(libraryItems, (mediaA, mediaB) => {
if (mediaA.grandparentRatingKey && mediaB.grandparentRatingKey) { if (mediaA.grandparentRatingKey && mediaB.grandparentRatingKey) {