Resource API
Resources are files bundled inside a plugin under Contents/Resources/. The
Resource API gives your code two ways to work with them:
Reference a resource file by URL so that PMS or a client can fetch its contents — without reading the file yourself.
Load a resource file’s raw bytes or text into memory so your code can process them at runtime.
ExternalPath(itemname) → str
Returns a URL that Plex Media Server will use to serve the named resource file to clients. The file is not read by this call; it is only located on disk to verify existence and obtain its modification time.
The returned path has the form:
/:/plugins/{identifier}/resources/{itemname}?t={mtime}
The ?t= query parameter is the file’s modification time in seconds. It acts
as a cache-buster: when a resource file changes, its mtime changes and therefore
the URL changes, causing clients to re-fetch it instead of serving a stale
cached copy.
Typical use — assigning image resources to object attributes:
DirectoryObject(
title='My Channel',
thumb=Resource.ExternalPath('icon-default.png'),
art=Resource.ExternalPath('art-default.jpg'),
)
Note
R(itemname) is a published alias for this method and is the shorthand
form used in most plugins. See R below.
Load(itemname, binary=True) → str
Opens the named resource file and returns its contents. The file is read from disk on every call — there is no caching.
Parameter |
Type |
Description |
|---|---|---|
|
|
Filename relative to |
|
|
If |
# Load a JSON config file as text
raw = Resource.Load('config.json', binary=False)
config = JSON.ObjectFromString(raw)
# Load a binary file
data = Resource.Load('template.dat')
GuessMimeType(path) → str
Returns the MIME type string inferred from the file extension of path using
Python’s built-in mimetypes module. No file I/O is performed.
Resource.GuessMimeType('video.mp4') # → 'video/mp4'
Resource.GuessMimeType('thumb.jpg') # → 'image/jpeg'
Resource.GuessMimeType('data.json') # → 'application/json'
AddMimeType(mimetype, extension)
Registers a custom MIME type for the given file extension. This is a one-time registration that affects the entire process for the rest of its lifetime.
Note
Available in legacy and bundle plugins only. Not available under the modern
(ModernPolicy) sandbox.
Resource.AddMimeType('application/x-custom', '.myext')
ContentsOfURLWithFallback(url, fallback=None, hosted_fallback=None) → str
Generates a callback URL that, when fetched by PMS, tries to reach a remote URL and falls back to a bundled or hosted resource if the remote URL is unavailable. This is useful for remote images that can fall back to a local placeholder.
This method does not make any network request itself. It returns an opaque callback URL string that PMS evaluates lazily.
Parameter |
Type |
Description |
|---|---|---|
|
|
One URL, or a list of URLs to try in order. PMS will redirect to the first URL that responds successfully. |
|
|
Filename of a bundled resource (inside |
|
|
A 2- or 3-element tuple |
# Prefer a remote image; fall back to a bundled placeholder
thumb = Resource.ContentsOfURLWithFallback(
url='https://example.com/poster.jpg',
fallback='missing-poster.png',
)
# Try multiple remote URLs
thumb = Resource.ContentsOfURLWithFallback(
url=['https://cdn1.example.com/img.jpg', 'https://cdn2.example.com/img.jpg'],
fallback='missing-poster.png',
)
Hosted(type, group, identifier=None) → HostedResource
Returns a HostedResource object that resolves to a URL on Plex’s CDN
(resources-cdn.plexapp.com). When coerced to a string (e.g. assigned to
an object attribute), it produces a URL of the form:
http://resources-cdn.plexapp.com/{type}/{group}/{filename}?h={hash}
Parameter |
Type |
Description |
|---|---|---|
|
|
Resource type, typically |
|
|
Resource group, e.g. |
|
|
Plugin bundle identifier. Defaults to the current plugin’s identifier. |
Used internally by the framework to supply default art and thumbnails from Plex’s hosted resource set when a plugin does not provide its own.