Skip to content

Commit 39609e1

Browse files
authored
Add support for better images resolution (#35)
1 parent 28648fc commit 39609e1

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Read through these two resources before posting issues to GitHub or the forums.
2020
1. Install this component by copying [these files](https://github.com/custom-components/sensor.plex_recently_added/tree/master/custom_components/plex_recently_added) to `custom_components/plex_recently_added/`.
2121
2. Install the card: [Upcoming Media Card](https://github.com/custom-cards/upcoming-media-card)
2222
3. Add the code to your `configuration.yaml` using the config options below.
23-
4. Add the code for the card to your `ui-lovelace.yaml`.
23+
4. Add the code for the card to your `ui-lovelace.yaml`.
2424
5. **You will need to restart after installation for the component to start working.**
2525

2626
### Options
@@ -37,10 +37,11 @@ Read through these two resources before posting issues to GitHub or the forums.
3737
| img_dir | '/custom-lovelace/upcoming-media-card/images/plex/' | no | This option allows you to choose a custom directory to store images in if you enable download_images.
3838
| ssl_cert | false | no | If you provide your own SSL certificate in Plex's network settings set this to true.
3939
| section_types | movie, show | no | Allows you to specify which section types to consider [movie, show].
40+
| image_resolution | 200 | no | Allows you to change the resolution of the generated images (in px), useful to display higher quality images as a background somewhere.
4041

4142
#### By default this addon automatically downloads images from Plex to your /www/custom-lovelace/upcoming-media-card/ directory. The directory is automatically created & only images reported in the upcoming list are downloaded. Images are small in size and are removed automatically when no longer needed. Currently & unfortunately, this may not work on all systems.
4243

43-
#### If you prefer to not download the images you may set download_images to false, but you either have to set "Secure connections" to "preferred" or "disabled" (no SSL) or have a custom certificate set (these options are found in your Plex server's network settings). This is needed because the default SSL certificate supplied by Plex is for their own domain and not for your Plex server. Your server also needs to be "fully accessible outside your network" if you wish to be able to see images remotely. If your Plex server provides it's own certificate you only need to set ssl_cert to true and download_images to false.
44+
#### If you prefer to not download the images you may set download_images to false, but you either have to set "Secure connections" to "preferred" or "disabled" (no SSL) or have a custom certificate set (these options are found in your Plex server's network settings). This is needed because the default SSL certificate supplied by Plex is for their own domain and not for your Plex server. Your server also needs to be "fully accessible outside your network" if you wish to be able to see images remotely. If your Plex server provides it's own certificate you only need to set ssl_cert to true and download_images to false.
4445

4546
</br></br>
4647
**Do not just copy examples, please use config options above to build your own!**
@@ -88,4 +89,3 @@ Read through these two resources before posting issues to GitHub or the forums.
8889
| line3 | $number - $rating - $runtime | "S01E12 - ★ 9.8 - 01:30"
8990
| line4 | $genres | "Action, Adventure, Comedy" |
9091
| icon | mdi:eye-off | https://materialdesignicons.com/icon/eye-off
91-

custom_components/plex_recently_added/sensor.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
CONF_MAX = 'max'
3131
CONF_IMG_CACHE = 'img_dir'
3232
CONF_SECTION_TYPES = 'section_types'
33+
CONF_RESOLUTION = 'image_resolution'
3334

3435
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
3536
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
@@ -41,8 +42,9 @@
4142
vol.Optional(CONF_DL_IMAGES, default=True): cv.boolean,
4243
vol.Optional(CONF_HOST, default='localhost'): cv.string,
4344
vol.Optional(CONF_PORT, default=32400): cv.port,
44-
vol.Optional(CONF_SECTION_TYPES,
45+
vol.Optional(CONF_SECTION_TYPES,
4546
default=['movie', 'show']): vol.All(cv.ensure_list, [cv.string]),
47+
vol.Optional(CONF_RESOLUTION, default=200): cv.positive_int
4648
vol.Optional(CONF_IMG_CACHE,
4749
default='/upcoming-media-card-images/plex/'): cv.string
4850
})
@@ -73,6 +75,7 @@ def __init__(self, hass, conf, name):
7375
self.max_items = int(conf.get(CONF_MAX))
7476
self.dl_images = conf.get(CONF_DL_IMAGES)
7577
self.sections = conf.get(CONF_SECTION_TYPES)
78+
self.resolution = conf.get(CONF_RESOLUTION)
7679
if self.server_name:
7780
self.server_ip, self.local_ip, self.port = get_server_ip(
7881
self.server_name, self.token)
@@ -179,9 +182,9 @@ def device_state_attributes(self):
179182
card_item['fanart'] = ''
180183
else:
181184
card_item['poster'] = image_url(self.url_elements,
182-
False, poster)
185+
False, poster, self.resolution)
183186
card_item['fanart'] = image_url(self.url_elements,
184-
False, fanart)
187+
False, fanart, self.resolution)
185188
self.card_json.append(card_item)
186189
self.change_detected = False
187190
attributes['data'] = self.card_json
@@ -219,7 +222,7 @@ def update(self):
219222
"""Get JSON for each library, combine and sort."""
220223
for library in sections:
221224
sub_sec = plex.get(recently_added.format(
222-
library, self.max_items * 2), headers=headers, timeout=10)
225+
library, self.max_items * 2), headers=headers, timeout=10)
223226
try:
224227
self.api_json += sub_sec.json()['MediaContainer']['Metadata']
225228
except:
@@ -275,15 +278,15 @@ def update(self):
275278
if not os.path.isfile(fanart_jpg):
276279
if image_url(self.url_elements, True, fanart):
277280
image = plex.get(image_url(
278-
self.url_elements, True, fanart),
281+
self.url_elements, True, fanart, self.resolution),
279282
headers=headers, timeout=10).content
280283
open(fanart_jpg, 'wb').write(image)
281284
else:
282285
pass
283286
if not os.path.isfile(poster_jpg):
284287
if image_url(self.url_elements, True, poster):
285288
image = plex.get(image_url(
286-
self.url_elements, True, poster),
289+
self.url_elements, True, poster, self.resolution),
287290
headers=headers, timeout=10).content
288291
open(poster_jpg, 'wb').write(image)
289292
else:
@@ -297,7 +300,7 @@ def update(self):
297300
self._state = '%s cannot be reached' % self.server_ip
298301

299302

300-
def image_url(url_elements, cert_check, img):
303+
def image_url(url_elements, cert_check, img, resolution=200):
301304
"""Plex can resize images with a long & partially % encoded url."""
302305
from urllib.parse import quote
303306
ssl, host, local, port, token, self_cert, dl_images = url_elements
@@ -311,9 +314,10 @@ def image_url(url_elements, cert_check, img):
311314
img,
312315
token),
313316
safe='')
314-
url = ('http{0}://{1}:{2}/photo/:/transcode?width=200&height=200'
317+
url = ('http{0}://{1}:{2}/photo/:/transcode?width={5}&height={5}'
315318
'&minSize=1&url={3}&X-Plex-Token={4}').format(ssl, host, port,
316-
encoded, token)
319+
encoded, token,
320+
resolution)
317321
"""Check if image exists"""
318322
if not self_cert:
319323
r = requests.head(url, verify=False)

0 commit comments

Comments
 (0)