Skip to content

Commit

Permalink
Add support for better images resolution (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider authored May 19, 2020
1 parent 28648fc commit 39609e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Read through these two resources before posting issues to GitHub or the forums.
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/`.
2. Install the card: [Upcoming Media Card](https://github.com/custom-cards/upcoming-media-card)
3. Add the code to your `configuration.yaml` using the config options below.
4. Add the code for the card to your `ui-lovelace.yaml`.
4. Add the code for the card to your `ui-lovelace.yaml`.
5. **You will need to restart after installation for the component to start working.**

### Options
Expand All @@ -37,10 +37,11 @@ Read through these two resources before posting issues to GitHub or the forums.
| 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.
| ssl_cert | false | no | If you provide your own SSL certificate in Plex's network settings set this to true.
| section_types | movie, show | no | Allows you to specify which section types to consider [movie, show].
| 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.

#### 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.

#### 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.
#### 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.

</br></br>
**Do not just copy examples, please use config options above to build your own!**
Expand Down Expand Up @@ -88,4 +89,3 @@ Read through these two resources before posting issues to GitHub or the forums.
| line3 | $number - $rating - $runtime | "S01E12 - ★ 9.8 - 01:30"
| line4 | $genres | "Action, Adventure, Comedy" |
| icon | mdi:eye-off | https://materialdesignicons.com/icon/eye-off
22 changes: 13 additions & 9 deletions custom_components/plex_recently_added/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
CONF_MAX = 'max'
CONF_IMG_CACHE = 'img_dir'
CONF_SECTION_TYPES = 'section_types'
CONF_RESOLUTION = 'image_resolution'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
Expand All @@ -41,8 +42,9 @@
vol.Optional(CONF_DL_IMAGES, default=True): cv.boolean,
vol.Optional(CONF_HOST, default='localhost'): cv.string,
vol.Optional(CONF_PORT, default=32400): cv.port,
vol.Optional(CONF_SECTION_TYPES,
vol.Optional(CONF_SECTION_TYPES,
default=['movie', 'show']): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_RESOLUTION, default=200): cv.positive_int
vol.Optional(CONF_IMG_CACHE,
default='/upcoming-media-card-images/plex/'): cv.string
})
Expand Down Expand Up @@ -73,6 +75,7 @@ def __init__(self, hass, conf, name):
self.max_items = int(conf.get(CONF_MAX))
self.dl_images = conf.get(CONF_DL_IMAGES)
self.sections = conf.get(CONF_SECTION_TYPES)
self.resolution = conf.get(CONF_RESOLUTION)
if self.server_name:
self.server_ip, self.local_ip, self.port = get_server_ip(
self.server_name, self.token)
Expand Down Expand Up @@ -179,9 +182,9 @@ def device_state_attributes(self):
card_item['fanart'] = ''
else:
card_item['poster'] = image_url(self.url_elements,
False, poster)
False, poster, self.resolution)
card_item['fanart'] = image_url(self.url_elements,
False, fanart)
False, fanart, self.resolution)
self.card_json.append(card_item)
self.change_detected = False
attributes['data'] = self.card_json
Expand Down Expand Up @@ -219,7 +222,7 @@ def update(self):
"""Get JSON for each library, combine and sort."""
for library in sections:
sub_sec = plex.get(recently_added.format(
library, self.max_items * 2), headers=headers, timeout=10)
library, self.max_items * 2), headers=headers, timeout=10)
try:
self.api_json += sub_sec.json()['MediaContainer']['Metadata']
except:
Expand Down Expand Up @@ -275,15 +278,15 @@ def update(self):
if not os.path.isfile(fanart_jpg):
if image_url(self.url_elements, True, fanart):
image = plex.get(image_url(
self.url_elements, True, fanart),
self.url_elements, True, fanart, self.resolution),
headers=headers, timeout=10).content
open(fanart_jpg, 'wb').write(image)
else:
pass
if not os.path.isfile(poster_jpg):
if image_url(self.url_elements, True, poster):
image = plex.get(image_url(
self.url_elements, True, poster),
self.url_elements, True, poster, self.resolution),
headers=headers, timeout=10).content
open(poster_jpg, 'wb').write(image)
else:
Expand All @@ -297,7 +300,7 @@ def update(self):
self._state = '%s cannot be reached' % self.server_ip


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

0 comments on commit 39609e1

Please sign in to comment.