Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rules): Added new rule: Plex - [list] Labels #719

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface PlexLibraryItem {
index?: number;
parentIndex?: number;
Collection?: { tag: string }[];
Label?: { tag: string }[];
}

export interface PlexLibraryResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface PlexMetadata {
updatedAt: number;
media: Media[];
parentData?: PlexMetadata;
Label?: { tag: string }[];
}

export interface Media {
Expand Down
7 changes: 7 additions & 0 deletions server/src/modules/rules/constants/rules.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ export class RuleConstants {
mediaType: MediaType.BOTH,
type: RuleType.NUMBER,
} as Property,
{
id: 24,
name: 'labels',
humanName: '[list] Labels',
mediaType: MediaType.BOTH,
type: RuleType.TEXT,
} as Property,
],
},
{
Expand Down
47 changes: 24 additions & 23 deletions server/src/modules/rules/getter/plex-getter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../constants/rules.constants';
import { EPlexDataType } from 'src/modules/api/plex-api/enums/plex-data-type-enum';
import { RulesDto } from '../dtos/rules.dto';
import { PlexMetadata } from 'src/modules/api/plex-api/interfaces/media.interface';

@Injectable()
export class PlexGetterService {
Expand All @@ -33,6 +34,7 @@ export class PlexGetterService {
) {
try {
const prop = this.plexProperties.find((el) => el.id === id);
const metadata: PlexMetadata = await this.plexApi.getMetadata(libItem.ratingKey) // fetch metadata from cache, this data is more complete
switch (prop.name) {
case 'addDate': {
return libItem.addedAt ? new Date(+libItem.addedAt * 1000) : null;
Expand Down Expand Up @@ -75,21 +77,22 @@ export class PlexGetterService {
const count = await this.plexApi.getWatchHistory(libItem.ratingKey);
return count ? count.length : 0;
}
case 'labels': {
return metadata.Label ? metadata.Label.map(l => l.tag) : []
}
case 'collections': {
// fetch metadata because collections in plexLibrary object are wrong
return libItem.Collection
? (
await this.plexApi.getMetadata(libItem.ratingKey)
).Collection.filter(
(el) =>
el.tag.toLowerCase().trim() !==
(ruleGroup.manualCollectionName
? ruleGroup.manualCollectionName
: ruleGroup.name
)
.toLowerCase()
.trim(),
).length
return metadata.Collection
? metadata.Collection.filter(
(el) =>
el.tag.toLowerCase().trim() !==
(ruleGroup.manualCollectionName
? ruleGroup.manualCollectionName
: ruleGroup.name
)
.toLowerCase()
.trim(),
).length
: 0;
}
case 'playlists': {
Expand Down Expand Up @@ -159,10 +162,8 @@ export class PlexGetterService {
}
}
case 'collection_names': {
// fetch metadata because collections in plexLibrary object are wrong
const moreData = await this.plexApi.getMetadata(libItem.ratingKey);
return moreData.Collection
? moreData.Collection.map((el) => el.tag.trim())
return metadata.Collection
? metadata.Collection.map((el) => el.tag.trim())
: null;
}
case 'lastViewedAt': {
Expand Down Expand Up @@ -201,13 +202,13 @@ export class PlexGetterService {
const item =
libItem.type === 'episode'
? ((await this.plexApi.getMetadata(
libItem.grandparentRatingKey,
)) as unknown as PlexLibraryItem)
libItem.grandparentRatingKey,
)) as unknown as PlexLibraryItem)
: libItem.type === 'season'
? ((await this.plexApi.getMetadata(
? ((await this.plexApi.getMetadata(
libItem.parentRatingKey,
)) as unknown as PlexLibraryItem)
: libItem;
: libItem;
return item.Genre ? item.Genre.map((el) => el.tag) : null;
}
case 'sw_allEpisodesSeenBy': {
Expand Down Expand Up @@ -347,8 +348,8 @@ export class PlexGetterService {
const seasons =
libItem.type !== 'season'
? (
await this.plexApi.getChildrenMetadata(libItem.ratingKey)
).sort((a, b) => a.index - b.index)
await this.plexApi.getChildrenMetadata(libItem.ratingKey)
).sort((a, b) => a.index - b.index)
: [libItem];

const lastEpDate = await this.plexApi
Expand Down