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

[Feature]: Remove dependency on Plex database #17

Closed
JonnyWong16 opened this issue Jul 14, 2023 · 0 comments
Closed

[Feature]: Remove dependency on Plex database #17

JonnyWong16 opened this issue Jul 14, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@JonnyWong16
Copy link

JonnyWong16 commented Jul 14, 2023

Describe the Feature Request

The resource folder path can be found purely using the Plex API.

Example code:

from hashlib import sha1
from pathlib import Path
from plexapi.server import PlexServer


PMS_DIRECTORY = Path('Plex Media Server')
METADATA_DIRECTORY = PMS_DIRECTORY / 'Metadata'
METADATA_FOLDER_MAP = {
    'movie': 'Movies',
    'show': 'TV Shows',
    'season': 'TV Shows',
    'episode': 'TV Shows',
    'artist': 'Artists',
    'album': 'Albums',
    'track': 'Albums',
    'photo': 'Photos',
    'collection': 'Collections',
    'playlist': 'Playlists'
}


def get_items(section):
    for item in section.all():
        yield item

        if item.type in ('show', 'artist'):
            for child in item:
                yield child

                if child.type in ('season', 'album'):
                    for grandchild in child:
                        yield grandchild

    if section.type != 'photo':
        for item in section.collections():
            yield item

    for item in section.playlists():
        yield item


def get_resources(item):
    for method in ('posters', 'arts', 'themes'):
        for resource in getattr(item, method, lambda: [])():
            yield resource


def build_resource_path(item, resource):
    if item.type == 'season':
        guid = item.parentGuid
    elif item.type == 'episode':
        guid = item.grandparentGuid
    elif item.type == 'track':
        guid = item.parentGuid
    else:
        guid = item.guid

    guid_hash = sha1(guid.encode('utf-8')).hexdigest()
    resource_path = resource.ratingKey.split('://')[-1]

    return (
        METADATA_DIRECTORY / METADATA_FOLDER_MAP[item.type]
        / guid_hash[0] / f"{guid_hash[1:]}.bundle"
        / 'Uploads' / resource_path
    )


def process_resource(item, resource):
    resource_path = build_resource_path(item, resource)

    # Do whatever you want with the resource (delete, move, etc.)
    print(item, resource_path)


if __name__ == '__main__':
    plex = PlexServer()

    for section in plex.library.sections():
        for item in get_items(section):
            for resource in get_resources(item):
                # Uploaded resource and not currently selected
                if resource.ratingKey.startswith('upload://') and not resource.selected:
                    process_resource(item, resource)
@JonnyWong16 JonnyWong16 added the enhancement New feature or request label Jul 14, 2023
meisnate12 added a commit that referenced this issue Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants