Skip to content

Commit

Permalink
Merge pull request #719 from jorenn92/fix/#701
Browse files Browse the repository at this point in the history
feat(rules): Added new rule: Plex - [list] Labels
  • Loading branch information
jorenn92 authored Jan 7, 2024
2 parents ce690fb + 1c5a89a commit 308aafe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
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

0 comments on commit 308aafe

Please sign in to comment.