Compare commits
3 Commits
develop
...
0xsysr3ll/
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb08eb024f | ||
|
|
b5c8e21104 | ||
|
|
54baa53aac |
@@ -27,10 +27,13 @@ export interface PlexLibraryItem {
|
||||
Media: Media[];
|
||||
}
|
||||
|
||||
interface PlexLibraryResponse {
|
||||
interface PlexLibraryResponseRaw {
|
||||
MediaContainer: {
|
||||
totalSize: number;
|
||||
Metadata: PlexLibraryItem[];
|
||||
totalSize?: number;
|
||||
size?: number;
|
||||
Metadata?: PlexLibraryItem[];
|
||||
Directory?: PlexLibraryItem[];
|
||||
[key: string]: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -177,7 +180,7 @@ class PlexAPI extends ExternalAPI {
|
||||
id: string,
|
||||
{ offset = 0, size = 50 }: { offset?: number; size?: number } = {}
|
||||
): Promise<{ totalSize: number; items: PlexLibraryItem[] }> {
|
||||
const response = await this.get<PlexLibraryResponse>(
|
||||
const response = await this.get<PlexLibraryResponseRaw>(
|
||||
`/library/sections/${id}/all?includeGuids=1`,
|
||||
{
|
||||
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 {
|
||||
totalSize: response.MediaContainer.totalSize,
|
||||
items: response.MediaContainer.Metadata ?? [],
|
||||
totalSize: container.totalSize ?? 0,
|
||||
items: container.Metadata ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -221,7 +241,7 @@ class PlexAPI extends ExternalAPI {
|
||||
},
|
||||
mediaType: 'movie' | 'show'
|
||||
): Promise<PlexLibraryItem[]> {
|
||||
const response = await this.get<PlexLibraryResponse>(
|
||||
const response = await this.get<PlexLibraryResponseRaw>(
|
||||
`/library/sections/${id}/all?type=${
|
||||
mediaType === 'show' ? '4' : '1'
|
||||
}&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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -90,22 +90,41 @@ class PlexScanner
|
||||
if (this.isRecentOnly) {
|
||||
for (const library of this.libraries) {
|
||||
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(
|
||||
`Beginning to process recently added for library: ${library.name}`,
|
||||
'info',
|
||||
{ lastScan: library.lastScan }
|
||||
{
|
||||
lastScan: library.lastScan,
|
||||
addedAtFilter: addedAtOpt?.addedAt,
|
||||
addedAtFilterDate: addedAtOpt
|
||||
? new Date(addedAtOpt.addedAt).toISOString()
|
||||
: undefined,
|
||||
}
|
||||
);
|
||||
const libraryItems = await this.plexClient.getRecentlyAdded(
|
||||
library.id,
|
||||
library.lastScan
|
||||
? {
|
||||
// We remove 10 minutes from the last scan as a buffer
|
||||
addedAt: library.lastScan - 1000 * 60 * 10,
|
||||
}
|
||||
: undefined,
|
||||
addedAtOpt ?? {
|
||||
addedAt: Date.now() - 1000 * 60 * 60,
|
||||
},
|
||||
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
|
||||
this.items = uniqWith(libraryItems, (mediaA, mediaB) => {
|
||||
if (mediaA.grandparentRatingKey && mediaB.grandparentRatingKey) {
|
||||
|
||||
Reference in New Issue
Block a user