Skip to content

Commit

Permalink
add analyze video option
Browse files Browse the repository at this point in the history
  • Loading branch information
yajrendrag committed Feb 7, 2025
1 parent cca08e5 commit 40944bc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
3 changes: 3 additions & 0 deletions source/notify_plex_partial/changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

**<span style="color:#56adda">0.0.4</span>**
- add option to analyze media file

**<span style="color:#56adda">0.0.3</span>**
- add test to see if destination file exists - otherwise plex can't be notified by destination path

Expand Down
6 changes: 3 additions & 3 deletions source/notify_plex_partial/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ These two approaches will likely yield different tokens, but both should work.

- 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 a mapping that shows your host media path relative to your unmanic library media path. The mapping should only go include where the paths are
different, e.g., /media:/library. This means that Plex sees your media at the path /media and unmanic sees it at /library. Below these paths the paths should be identical.
- Enter as a single string, the mapping that shows your host media path relative to your unmanic library media path. The mapping should only include the unique component of the paths, e.g., /media:/library. This means that Plex sees your media at the path /media and unmanic sees it at /library. Below these paths the paths should be identical.
You should have a unique mapping for each library in which this plugin is deployed, e.g., TVShows, Movies, etc.
- enter True or False for whether this plugin should run an update if the task for the file failed.
- Enter True or False for whether this plugin should run an update if the task for the file failed.
- Enter True or False for whether this plugin should run an analyze on the media file

:::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
Expand Down
2 changes: 1 addition & 1 deletion source/notify_plex_partial/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"on_postprocessor_task_results": 0
},
"tags": "post-processor",
"version": "0.0.3"
"version": "0.0.4"
}
42 changes: 42 additions & 0 deletions source/notify_plex_partial/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import requests
import re
import urllib.parse
from plexapi.server import PlexServer
import os
import PTN

# Configure plugin logger
logger = logging.getLogger("Unmanic.Plugin.notify_plex_partial")
Expand All @@ -35,6 +38,7 @@ class Settings(PluginSettings):
'Plex Token': '',
'Unmanic Library Mapping': '',
'Notify on Task Failure?': False,
'Analyze Video': False,
}

def get_section(media_dir, plex_url, plex_token):
Expand All @@ -59,6 +63,37 @@ def update_plex(plex_url, plex_token, media_dir, section_id):
else:
logger.error("Error requesting Plex to update: '{}'".format(media_dir))

def analyze_video(media_dir, plex_url, plex_token):
headers = {'Accept': 'application/json'}
plex = PlexServer(plex_url, plex_token)
basename = os.path.splitext(os.path.basename(media_dir))[0]
basename = re.sub(r' \(\d\d\d\d\)','', basename)
parsed_info = PTN.parse(basename, standardise = False)
try:
video = parsed_info['episodeName']
except KeyError:
video = parsed_info['title']
query_url = plex_url + '/search/?X-Plex-Token=' + plex_token + '&query=' + video
response = requests.get(query_url, headers=headers)
if response.status_code == 200:
videos = response.json()['MediaContainer']['Metadata']
for vid in range(len(videos)):
if basename in videos[vid]['Media'][0]['Part'][0]['file']:
if videos[vid]['type'] == 'movie':
lib = plex.library.section(videos[vid]['librarySectionTitle'])
movie=lib.get(video)
logger.debug(f"analyzing movie {basename} in library {videos[vid]['librarySectionTitle']}")
movie.analyze()
else:
show_title = parsed_info['title']
if videos[vid]['title'] == video:
lib = plex.library.section(videos[vid]['librarySectionTitle'])
episodes = lib.get(show_title).episodes()
for k in range(len(episodes)):
if episodes[k].title == video:
logger.debug(f"analyzing show {basename} in library {videos[vid]['librarySectionTitle']}")
episodes[k].analyze()

def on_postprocessor_task_results(data):
"""
Runner function - provides a means for additional postprocessor functions based on the task success.
Expand Down Expand Up @@ -88,6 +123,9 @@ def on_postprocessor_task_results(data):
logger.error("Non-existent destination file - plex cannot be notified.")
return data

analyze = settings.get_setting('Analyze Video')

# Get host mapping of library folder
lib_map = settings.get_setting('Unmanic Library Mapping')
unmanic_dir=re.search(r'.*:(.*$)', lib_map)
if unmanic_dir:
Expand All @@ -113,4 +151,8 @@ def on_postprocessor_task_results(data):
return data
update_plex(plex_url, plex_token, media_dir, section_id)

# Analyze Video
if analyze:
analyze_video(media_dir, plex_url, plex_token)

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

0 comments on commit 40944bc

Please sign in to comment.