Skip to content

Commit

Permalink
[notify_plex_partial] v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
yajrendrag committed Oct 25, 2024
1 parent 3b55b60 commit 10d5188
Show file tree
Hide file tree
Showing 9 changed files with 826 additions and 0 deletions.
4 changes: 4 additions & 0 deletions source/notify_plex_partial/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/__pycache__
*.py[cod]
**/site-packages
settings.json
674 changes: 674 additions & 0 deletions source/notify_plex_partial/LICENSE

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions source/notify_plex_partial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Notify Plex Partial
Plugin for [Unmanic](https://github.com/Unmanic)

---

### Information:

- [Description](description.md)
- [Changelog](changelog.md)
4 changes: 4 additions & 0 deletions source/notify_plex_partial/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

**<span style="color:#56adda">0.0.1</span>**
- Initial version
- based on v0.0.5 of notify_plex
19 changes: 19 additions & 0 deletions source/notify_plex_partial/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

To find your Plex token, follow the instructions found here:
- https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/

Alternatively, navigate to the Plex data folder on your Plex server and open the Preferences.xml file and look for the value of "PlexOnlineToken".

These two approaches will likely yield different tokens, but both should work.

#### Configuration

- Enter the url for your Plex Server using your local IP address (ie, not the plex.tv cloud address)
- Enter the Plex token you found as per above
- Enter as a single string, the Library Mapping that you entered in your docker compose or on the docker run line for the library in which this plugin is installed,
e.g., /media/TVShows:/library
- enter True or False for whether this plugin should run an update if the task for the file failed.

:::note
If you are not running Unmanic in docker, then for the above library mapping, just enter the mapping to be identical on both sides of the colon, e.g., /media/TVShows:/media/TVShows
:::
Binary file added source/notify_plex_partial/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions source/notify_plex_partial/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"author": "yajrendrag",
"compatibility": [
1,
2
],
"description": "partial pex library update based on folder with changed files.",
"icon": "https://raw.githubusercontent.com/Josh5/unmanic.plugin.notify_plex/master/icon.png",
"id": "notify_plex_partial",
"name": "Notify Plex Partial",
"platform": [
"all"
],
"priorities": {
"on_postprocessor_task_results": 0
},
"tags": "post-processor",
"version": "0.0.5"
}
96 changes: 96 additions & 0 deletions source/notify_plex_partial/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Written by: yajrendrag <[email protected]>
Date: 25 October 2024, (11:00 AM)
Copyright:
Copyright (C) 2024 Jay Gardner
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <https://www.gnu.org/licenses/>.
"""
import logging
from unmanic.libs.unplugins.settings import PluginSettings
import requests
import re
import urllib.parse

# Configure plugin logger
logger = logging.getLogger("Unmanic.Plugin.notify_plex_partial")


class Settings(PluginSettings):
settings = {
'Plex URL': 'http://localhost:32400',
'Plex Token': '',
'Unmanic Library Mapping': '',
'Notify on Task Failure?': False,
}


def update_plex(plex_url, plex_token, media_dir):
# Call to Plex to trigger an update
plex_url = plex_url + '/library/sections/all/refresh/?path=' + urllib.parse.quote(media_dir, safe='') + '&X-Plex-Token=' + plex_token
response = requests.get(plex_url)
if response.status_code == 200:
logger.info("Notifying Plex ({}) to update its library.".format(plex_url))
else:
logger.error("Error requesting Plex to update: '{}'".format(media_dir))

def on_postprocessor_task_results(data):
"""
Runner function - provides a means for additional postprocessor functions based on the task success.
The 'data' object argument includes:
task_processing_success - Boolean, did all task processes complete successfully.
file_move_processes_success - Boolean, did all postprocessor movement tasks complete successfully.
destination_files - List containing all file paths created by postprocessor file movements.
source_data - Dictionary containing data pertaining to the original source file.
:param data:
:return:
"""
# Configure settings object (maintain compatibility with v1 plugins)
if data.get('library_id'):
settings = Settings(library_id=data.get('library_id'))
else:
settings = Settings()

if not data.get('task_processing_success') and not settings.get_setting('Notify on Task Failure?'):
return data

media_dir = data.get('destination_files')[0]
lib_map = settings.get_setting('Unmanic Library Mapping')
unmanic_dir=re.search(r'.*:(.*$)', lib_map)
if unmanic_dir:
unmanic_dir = unmanic_dir.group(1)
else:
logger.error("unable to find identify unmanic dir from mapping: '{}'".format(lib_map))
return data
host_dir=re.search(r'(.*):', lib_map)
if host_dir:
host_dir = host_dir.group(1)
else:
logger.error("unable to find identify host dir from mapping: '{}'".format(lib_map))
return data
plex_url = settings.get_setting('Plex URL')
plex_token = settings.get_setting('Plex Token')
try:
media_dir = media_dir.replace(unmanic_dir,host_dir)
except:
logger.error("cannot form host media directory path - unmanic_dir: '{}', host_dir: '{}', media_dir: '{}'".format(unmanic_dir, host_dir, media_dir))
return data
update_plex(plex_url, plex_token, media_dir)

return data
1 change: 1 addition & 0 deletions source/notify_plex_partial/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plexapi==4.15.16

0 comments on commit 10d5188

Please sign in to comment.