bexem / PlexCache

Automate Plex media management: Efficiently transfer media from the On Deck/Watchlist to the cache, and seamlessly move watched media back to their respective locations.
58 stars 7 forks source link

Feature request #17

Closed ezek1el closed 11 months ago

ezek1el commented 12 months ago

Would it be possible to let the script generate an Unraid Notification after each run?

bexem commented 12 months ago

How are you running the script? Directly on unraid or are you using Chronos?

I did read your message earlier and I've been trying to integrate the functionality as I like the idea! Problem is I'm having issues using the notify command from Chronos, but if the script is run directly on unraid then it's a different deal and should be ready, it just needs to be tested.

ezek1el commented 12 months ago

Unraid through user scripts on a daily basis. I would gladly test it.

bexem commented 12 months ago

Check the updated one, it should work, let me know. https://github.com/bexem/PlexCache/blob/main/plexcache.py

Have a look at the variables inside, you will notice

notification = "unraid" # Unraid, Webhook or Both
unraid_level = "warning"  # Set the desired logging level for the notifications. Leave empty for notifications only on ERROR. (Options: debug, info, warning, error, critical)
webhook_level = ""  # Set the desired logging level for the notifications. Leave empty for notifications only on ERROR. (Options: debug, info, warning, error, critical)

webhook_url = ""  # Your webhook URL, leave empty for no notifications.
webhook_headers = {} # Leave empty for Discord, otherwise edit it accordingly. (Slack example: "Content-Type": "application/json" "Authorization": "Bearer YOUR_SLACK_TOKEN" })

Let me know if it works for you

ezek1el commented 12 months ago

Notification works great. But, notification_level "info" just throws out every cached item. Can there be a flag for like a summary? Like the script outputs?

Total size of media files to be moved to array: 3.37 GB
Free space on the array: 38.62 TB
Moving media to array...
Moving media files to array...
Finished moving files with 0 errors.
Editing file paths...
Total size of media files to be moved to cache: 12.67 GB
Free space on the cache: 801.39 GB
Moving media to cache...
Moving media files to cache...
Finished moving files with 0 errors.
Thank you for using bexem's script: 
bexem commented 12 months ago

I can add that, I will keep you posted!

ezek1el commented 12 months ago

Another thing: this is what the notification looks like:

2023-07-21_235001

bexem commented 12 months ago

Done both (theoretically). https://github.com/bexem/PlexCache/blob/main/plexcache.py The summary is still bare-bone but it's there.

Please let me know for any issues or further improvements.

bexem commented 12 months ago

So I've fixed a couple of things and also added a total execution time to the script. The only minor issue is that to get some useful information in the unraid web interface I need to use a field that it gets then duplicated when sending it via the Agents (in my case Discord)

image

But otherwise it works. I will likely continue to work on it tomorrow (well... today)

ezek1el commented 12 months ago

Discord notifications work very well.

2023-07-22_074304

Unraid WebUI notifications still the same. They should just reflect the status of the script anyway. Like worked with 0 Errors or something like that, right?

bexem commented 12 months ago

I'm not doing "It works on my end" because I want to understand why it doesn't on yours and fix it, but this how it show on my unraid (6.12.3)

image

Try on a unraid terminal to execute this? /usr/local/emhttp/webGui/scripts/notify -e "Test" -s "Test2" -m "Test3" -d "Test4" -i normal

The script basically runs the exact same command: notify_cmd = f'{self.notify_cmd_base} -e "PlexCache: Summary" -s "{record.name}" -m "{record.msg}" -d "{record.msg}" -i "{icon}"'

with "notify_cmd_base": self.notify_cmd_base = "/usr/local/emhttp/webGui/scripts/notify"

ezek1el commented 12 months ago

the Webui:

2023-07-22_075739

discord

2023-07-22_075752

do you ever sleep? πŸ˜„

bexem commented 12 months ago

I only work nights, I keep the schedule also during my days off. Although I'm about to go. 😴

So it works fine in your end too, but not when run by the script? That is just so odd, they are the same commands, I will need to investigate.

ezek1el commented 12 months ago

Ok, let me now if you want anything else tested. Good night!

bexem commented 12 months ago

I can't say if it is fixed because I can't seem to be able to replicate the same behaviour, but I've changed a couple of things, let me know if it's any better.

ezek1el commented 12 months ago

I tested the script with the unraid console and it works there. However, no notifications are sent. Neither to discord nor to Unraid UI. Furthermore, I get the following error when I run the script (I assume it's still this one: https://github.com/bexem/PlexCache/blob/main/plexcache.py) via Unraid User Scripts:

Script location: /tmp/user.scripts/tmpScripts/Plex Cache/script
Note/tmp/user.scripts/tmpScripts/Plex Cache/script: line 2: import: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 3: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 4: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 5: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 6: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 7: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 8: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 9: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 10: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 12: syntax error near unexpected token `"*** PlexCache ***"'
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 12: `print("*** PlexCache ***")'
bexem commented 12 months ago

I tested the script with the unraid console and it works there. However, no notifications are sent. Neither to discord nor to Unraid UI. Furthermore, I get the following error when I run the script (I assume it's still this one: https://github.com/bexem/PlexCache/blob/main/plexcache.py) via Unraid User Scripts:

Script location: /tmp/user.scripts/tmpScripts/Plex Cache/script
Note/tmp/user.scripts/tmpScripts/Plex Cache/script: line 2: import: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 3: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 4: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 5: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 6: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 7: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 8: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 9: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 10: from: command not found
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 12: syntax error near unexpected token `"*** PlexCache ***"'
/tmp/user.scripts/tmpScripts/Plex Cache/script: line 12: `print("*** PlexCache ***")'

How are you trying to execute it in Unraid user Scripts?

ezek1el commented 12 months ago

add the script as user script and press the run script button.

bexem commented 12 months ago

Uhm no, you need to execute the python scrpt from a script in user script. See https://github.com/bexem/PlexCache/wiki/UserScripts#option-2-user-scripts-plugin

ezek1el commented 12 months ago

Oh, yeah, ofc, that makes so much more sense...

bexem commented 12 months ago

Yeah I know...As far as I know, user script can only run bash/sh scripts, but my project is done with python, so it needs to run through User Script rather than by User Script πŸ˜…

ezek1el commented 12 months ago

ok that makes more sense of course. However, the notifications still don't work. Neither Discord nor WebUI

bexem commented 12 months ago

Can you show me how you have the script set?

image
ezek1el commented 12 months ago

uhh...oh..i have to confess... i haven't set webhooks up.

ezek1el commented 12 months ago

Ok. Now it's working. WebUi works great. But Discords notifications gets send twice.

2023-07-24_012247

bexem commented 12 months ago

Set Notification = "unraid" or Notification = "system"

The webhook is useful if you are running it on a system that doesn't use the same 'notify' service or for example, in Chronos as it cannot easily access and run the host executables.

ezek1el commented 12 months ago

Yep, that did the trick. Now it works flawlessly! Thank you very much!

ezek1el commented 12 months ago

maybe a tiny little detail, but could the discord summary be formatted so each entry gets its own line?

this looks so clean and neat

2023-07-24_013749

this, not so much

2023-07-24_013833

I know its nitpicking....😁

bexem commented 12 months ago

Unfortunately is how unraid handles the formatting when forwarding the notification to discord. One alternative is to have each result as unique message, but then it would not be a summary anymore.

So I have now modified in a way that it's more coherent, have a look at the new script (be aware of your settings).

ezek1el commented 12 months ago

It's beautiful! Thank you so much!

ezek1el commented 12 months ago

Hi, me again. I really thought everything would be perfect with your script. Unfortunately, I get the following error messages via email:

Event: PlexCache
Subject: CRITICAL
Description: Error checking free space and moving media files to the array: name 'summary_message' is not defined
Importance: alert
Event: PlexCache
Subject: ERROR
Description: Error: Failed to Fetch one-of-my-users-username onDeck media. Error: (401) unauthorized; http://plex.ip.adress/ <html><head><script>window.location = window.location.href.match(/(^.+\/)[^\/]*$/)[1] + 'web/index.html';</script><title>Unauthorized</title></head><body><h1>401 Unauthorized</h1></body></html>
Importance: alert

and last but not least:

Event: PlexCache
Subject: ERROR
Description: Failed to get access token for %s
Importance: alert
bexem commented 12 months ago

Apologies! It has been working fine so far but only now I noticed this mistake and actually started giving me the same error. It should be fixed now.

Regarding the onDeck media error, it seems to be an error with the token. Try and re-run it, see if it still has the error, please send me the log. (Keep an eye for sensitive info)

ezek1el commented 12 months ago

No Apologies needed! You doing great work. The script works so far without any errors. I ran it several times without any errors and will report back if any errors occur.

bexem commented 12 months ago

Great! Please let me know of any errors or anything you would like to be implemented ☺️

ezek1el commented 11 months ago

20 hours later still no errors. Looks like everything working ok. Does your script involve any measures to verify the file transfers complete without any file corruption?

ezek1el commented 11 months ago

Can the unraid ui notificationFinished moving files with 0 errors. changed from a warning to a notice?

bexem commented 11 months ago

Can the unraid ui notificationFinished moving files with 0 errors. changed from a warning to a notice?

Done, just changed it, it was never meant to be a warning to be fair!

20 hours later still no errors. Looks like everything working ok. Does your script involve any measures to verify the file transfers complete without any file corruption?

I'm glad it is working fine! The script doesn't implement any measure to verify the files, yet. It is a good idea and I will very likely add it.

I'm currently working on a rewrite of the whole script, separating each part so it will be easier to work on it, and one day might also expand the compatibility to jellyfin.

bexem commented 11 months ago

I'm closing this because it has been implemented and no errors have been encountered so far. ☺️

ezek1el commented 11 months ago

Hello, I wasn't sure if I should create a new ticket or just attach my problem here... I was wondering why I didn't get any more notifications and ran the script via console today. I get the following error:

File "/mnt/user/appdata/plexcache/plexcache.py", line 1, in
import os, json, logging, glob, socket, platform, shutil, ntpath, posixpath, re, requests, subprocess, time
ModuleNotFoundError: No module named 'requests'
bexem commented 11 months ago

Have you tried installing the module again? pip install requests

ezek1el commented 11 months ago

pip install requests and plexapi fixed it. Thanks again. Next time new issue?

bexem commented 11 months ago

Probably some plugin or unraid update must have removed the necessary modules...something I will need to keep in mind...

pip install requests and plexapi fixed it. Thanks again. Next time new issue?

Yes please, I was wondering where was your message at first because when I went on 'issues' there was only one open, but I did receive an email notification of your message πŸ˜…

bexem commented 11 months ago

A reboot apparently removes the modules. Keep it in mind. I will probably will add a script to run a system reboot to install the necessary modules in the instruction, until a better solution comes up in my mind!