Hey folks, there is a new Podcast category for forums https://forums.plex.tv/categories/podcasts
If you have not already, we suggest setting your Plex username to something else rather than email which is displayed on your posts in forum. You can change the username at https://app.plex.tv/desktop#!/account
Welcome to our forums! Please take a few moments to read through our Community Guidelines (also conveniently linked in the header at the top of each page). There, you'll find guidelines on conduct, tips on getting the help you may be searching for, and more!

JBOPS - Just a Bunch Of Plex Scripts

BlacktwinBlacktwin Posts: 53Members, Plex Pass Plex Pass

Hello,

What started as an attempt to learn Python ended up with a slight obsession with creating useful and fun scripts to that interact with Plex and/or PlexPy. My favorite script so far is the ips_to_map.py.

I've posted some of these scripts on /r/plex and got some positive buzz so I thought I'd share them here as well. This repo is a collection of all the scripts I've created.

I'm adding more and am taking requests. So let me know what you think, any improvements, suggestions, requests, etc.

JBOPS - Just a Bunch Of Plex Scripts

Donate PM PM

Most of these scripts utilize a combination of PlexPy, python-plexapi, and requests.

JBOPS - Just a Bunch Of Plex Scripts

«13

Comments

  • BlacktwinBlacktwin Posts: 53Members, Plex Pass Plex Pass
    edited October 2017

    Scripts List

    Gist

    Scripts pulled from my gist profile.


    Maps
    Example File Description
    Maps Using PlexPy data, draw a map connecting Server to Clients based on IP addresses.


    Fun
    Gist File Description
    image aired_today_playlist Create a Plex Playlist with what was aired on this today's month-day, sort by oldest first. If Playlist from yesterday exists delete and create today's. If today's Playlist exists exit.
    image plexapi_haiku Create a hiaku from titles found in Plex.


    Kill stream
    Gist File Description
    wait_kill_pause_notify_main Receive session_key from PlexPy when paused. Use sub-script wait_kill_pause_notify_sub to wait for X time then check if still paused. If so, kill. Toggle whether you'd like to be notified through a PlexPy notification agent.
    watch_limit Kill streams if user has watched too much Plex Today.
    play_limit Kill streams if user has played too much Plex Today.
    kill_time Limit number of plays of TV Show episodes during time of day. Idea is to reduce continuous plays while sleeping.
    kill_trans_pause_notify Kill Plex paused video transcoding streams and receive notification.
    ip_whitelist Receive session_key and IP from PlexPy when playback starts. Use IP to check against whitelist. If not in whitelist use session_key to determine stream and kill.
    image kill_all_more_than If user has 2 or more concurrent streams kill all streams
    image kill_else_if_buffering Kill concurrent transcode streams of other users if Admin user is experiencing buffering warnings from PlexPy.
    image kill_more_than If user has 2 or more concurrent streams and the IP of the 2nd stream differs from 1st kill 2nd. If 2nd stream IP is the same as 1st stream don't kill.
    image kill_outsider_stream Kill stream if user is outside of local network.
    image kill_plex_streams Kill all Plex streams for whatever reason you want.
    image kill_session_bitrate Kill stream if bitrate is greater than 4 Mbps
    image kill_trans_exp_audio Kill Plex video transcoding streams only. All audio streams are left alone. Kill message based on platform.
    image kill_trans_pause Kill Plex paused video transcoding streams using PlexPy.


    Notify
    Gist File Description
    notify_delay Delay Notification Agent message for concurrent streams.
    image find_unwatched_notify Find what was added TFRAME ago and not watched and notify admin using PlexPy.
    image notify_added_custom Send an email with what was added to Plex in the past week using PlexPy. Email includes title (TV: Show Name: Episode Name; Movie: Movie Title), time added, image, and summary.
    image notify_fav_tv_all_movie Notify users of recently added episode to show that they have watched at least LIMIT times via email. Also notify users of new movies.
    image notify_newip If a new IP is found send notification via the Email Notification Agent. Email contains User's Avatar image, link to location, IP address, and User's Email address.
    image notify_on_added Send an Email notification when a specific show is added to Plex. Add shows to list that you want notifications for.
    image notify_user_favorites Notify users of recently added episode to show that they have watched at least LIMIT times via email.
    image notify_user_newip Notify user that their account has been accessed by a new IP. IP is cleared to make sure notification is sent again.
    image twitter_notify Post to Twitter when TV/Movie is added to Plex. Include custom message and embed poster image. Option to tweet to TWITTER_USER if title is inside TITLE_FIND.


    Utility
    Gist File Description
    plex_api_parental_control Set as cron or task for times of allowing and not allowing user access to server. Unsharing will kill any current stream from user before unsharing.
    plex_api_share Share or unshare libraries
    image bypass_auth_name Use PlexPy to pull last IP address from user and add to List of IP addresses and networks that are allowed without auth in Plex.
    image delete_watched_TV From a list of TV shows, check if users in a list has watched shows episodes. If all users in list have watched an episode of listed show, then delete episode.
    image find_plex_meta Find location of Plex metadata.
    image find_unwatched Find what was added TFRAME ago and not watched using PlexPy.
    image grab_gdrive_media Grab media (videos, pictures) from Google Drive. All videos and pictures were automatically synced from Google Photos to Google Drive. Puts media into MEDIA_TYPE/YEAR/MONTH-DAY/FILE.ext directory structure.
    image plex_api_poster_pull Pull Movie and TV Show poster images from Plex.
    image plex_imgur_dl Pull poster images from Imgur and places them inside Shows root folder.
    image plex_theme_songs Download theme songs from Plex TV Shows.
    image plexapi_delete_playlists Delete all playlists from Plex using PlexAPI.
    image plexapi_search_file Find full path for Plex items.
    image refresh_next_episode Refresh the next episode of show once current episode is watched.
    image remove_watched_movies Find Movies that have been watched by a list of users. If all users have watched movie then delete.
    image stream_limiter_ban_email This is indented to restrict a user to the LIMIT amount of concurrent streams. User will be warned, punished, and banned completely if violations continue.


    Reporting
    Gist File Description
    weekly_stats_reporting Pull library and user statistics of last week.
    image added_to_plex Find when media was added between STARTFRAME and ENDFRAME to Plex through PlexPy.
    image check_play Check if user has play a file more than 3 times but has not finished watching. Hoping to catch play failures.
    image check_plex_log Checking plex logs for debug code WARN and 'Failed to obtain a streaming resource for transcode of key /library/metadata/"titleID"'.
    image drive_check Check if drive exists. If not then notify via PlexPy notifier agent.
    image userplays_weekly_reporting Use PlexPy to count how many plays per user occurred this week and send email via PlexPy.



    Setting Up PlexPy for Custom Scripts

    Enabling Scripts in PlexPy:

    Settings > Notification Agents > Click the Scripts gear

    • [ ] Set scripts location to location of your scripts
    • [ ] Scroll down to option you want to use and select the script from the drop down menu
    • [ ] Save

    Settings > Notification Agents > Click the Bell next to Scripts

    • [ ] Check desired trigger
    • [ ] Close

    Settings > Notifications > Click Script

    • [ ] Enter in the Script Arguments
    • [ ] Save



    Common variables


    Plex
    • [ ] PLEX_HOST - Local IP to connect to Plex ('localhost', '192.168.0.x', '127.0.0.1', etc.)
    • [ ] PLEX_PORT - Port number used by Plex (default: 32400)
    • [ ] PLEX_SSL - http:// or https://? '' if http and 's' if https
    • [ ] PLEX_TOKEN - Plex or PlexPy Settings > Plex.tv Account > PMS Token


    PlexPy
    • [ ] PLEXPY_URL - Local IP to connect to PlexPy ('localhost', '192.168.0.x', '127.0.0.1', etc.)
    • [ ] PLEXPY_APIKEY - PlexPy Settings > Access Control > Enable API - API Key

    JBOPS - Just a Bunch Of Plex Scripts

  • craig91craig91 Posts: 42Members, Plex Pass Plex Pass

    Impressive collection!

  • almontefalmontef Posts: 106Members, Plex Pass Plex Pass

    @Blacktwin I am getting the following error for every internal IP when I run the ips_to_maps.py script:

    PlexPy API 'get_geoip_lookup' request failed: The address x.x.x.x is not in the database..User: John IP: x.x.x.x caused error in geo_dict.

    I get this for all internal IPs but the chart produces my external connections just find. Is there a way to exclude the internal addresses if the error is expected because these are internal IPs?

    Thanks in advance!

  • BlacktwinBlacktwin Posts: 53Members, Plex Pass Plex Pass

    @almontef You would use the REPLACEMENT_WAN_IP variable to substitute your LAN_SUBNET. The error is there to simply let you know that the local addresses aren't being mapped.

    If you don't mind could you PM me your map? Just curious how others maps turn out. I like maps. :)

    JBOPS - Just a Bunch Of Plex Scripts

  • almontefalmontef Posts: 106Members, Plex Pass Plex Pass

    Thanks for the info! Love the script. I'll PM you my map, not very interesting since I only got a few remote users, but will be sharing with more family in the upcoming weeks, but even then most connections will be from the northeast.

  • almontefalmontef Posts: 106Members, Plex Pass Plex Pass

    @Blacktwin there was a recent major update to Plexpy and the ips_to_maps script not longer works. It produces the following error:

    python ips_to_maps.py
    PlexPy API 'get_get_users_tables' request failed: No JSON object could be decoded.PlexPy API 'get_get_users_tables' request failed: No JSON object could be decoded.Traceback (most recent call last):
    File "ips_to_maps.py", line 380, in
    user_lst = sorted(get_get_users_tables('friendly_name', user_count))
    TypeError: 'NoneType' object is not iterable

    Is the script not compatible with the latest version of Plexpy? Thanks in advance.

  • BlacktwinBlacktwin Posts: 53Members, Plex Pass Plex Pass

    @almontef Thanks for the heads up. I need to go through all my scripts now that Tautulli is out of beta and update them. Some may be removed and some will be improved to work better with Tautulli.

    JBOPS - Just a Bunch Of Plex Scripts

  • almontefalmontef Posts: 106Members, Plex Pass Plex Pass

    Sounds good. I'll keep my eye out for your updates. Thanks much!!

  • BlacktwinBlacktwin Posts: 53Members, Plex Pass Plex Pass
    edited March 16

    @almontef the maps script should work fine. Check your URL and API key.

    JBOPS - Just a Bunch Of Plex Scripts

  • cw101cw101 Posts: 4Members, Plex Pass Plex Pass

    @Blacktwin I'm having issues with the kill_more_than.py script. Wasn't sure if it was a Tautulli thing or just the way I have it setup. URL and API key are setup correctly.

  • BlacktwinBlacktwin Posts: 53Members, Plex Pass Plex Pass

    @cw101 what are the issues? Please copy your logs to a gist and link it here.

    JBOPS - Just a Bunch Of Plex Scripts

  • WeapKy92WeapKy92 Posts: 5Members, Plex Pass Plex Pass

    @Blacktwin Hello, first thanks for your job :smile:
    I have problem when i run "ips_to_maps.py -l Geo -j (my json file)" i get :
    Traceback (most recent call last):
    File "ips_to_maps.py", line 449, in
    print(r.json()['html_url'])
    KeyError: 'html_url'

    And when i try run "ips_to_maps.py -l World -j (my json file)" i get :
    Traceback (most recent call last):
    File "ips_to_maps.py", line 453, in
    draw_map(opts.location, geo_json, filename, opts.headless)
    File "ips_to_maps.py", line 327, in draw_map
    x, y = m([float(data['lon']), float(SERVER_LON)], [float(data['lat']), float(SERVER_LAT)])
    ValueError: could not convert string to float:

    If you can help me, thanks :smiley:

  • BlacktwinBlacktwin Posts: 53Members, Plex Pass Plex Pass
    edited April 2

    @WeapKy92 looks like you might not be able to anonymously create gists anymore. It'll need an account. I'll need to add that then.

    JBOPS - Just a Bunch Of Plex Scripts

  • brentbarnettbrentbarnett Posts: 27Members, Plex Pass Plex Pass

    This is awesome, but I am trying to figure out what the triggers, conditions, and arguments should be for the notify_added_custom.py should be. The script is exactly what I am looking for. I setup the API key and believe I have the other settings correct. Just don't know how to fire it.

  • BlacktwinBlacktwin Posts: 53Members, Plex Pass Plex Pass

    @brentbarnett the notify_added_customer.py is not to be ran by Tautulli. Set it up as a cron or task to run on a schedule.

    JBOPS - Just a Bunch Of Plex Scripts

  • isaacrdzisaacrdz Posts: 103Members, Plex Pass Plex Pass
    edited April 10

    [ edited ]

  • UntamedGorillaUntamedGorilla Posts: 8Members, Plex Pass Plex Pass

    how do you get the posters images in the emails? Mines says image unavailable

    Server: Ryzen 1700x, 16GB, 42TB for storage, 2 256gb SSD for one for OS and one for transcode, 4 tb for "pre-plex" manipulation and game usage, GTX 1070

    Client: pretty much everything, usually Roku, XBOX, and Plex Media Player

  • BlacktwinBlacktwin Posts: 53Members, Plex Pass Plex Pass

    @diablc Need a little more information. Which script?

    JBOPS - Just a Bunch Of Plex Scripts

  • JamminRJamminR Posts: 3,535Members, Plex Pass Plex Pass

    @blacktwin - take modification requests?
    I'm looking to use your "kill more than" python script, but with Tautulli's new conditions capability, I have no want of it looking for the same IP, or (seemingly) hardcoded limit of 2 inside the code itself.
    Though I can read and basically understand what Python is doing, I don't have the experience to change it and have it working myself. (I've tried, failure has not helped teach me)

    Would you modify the script to simply look to find username's "x" number stream and kill it?
    Or perhaps create another script that does that? "kill_users_Xth_session"
    I presume this should be reasonably easy (for you) using Tautulli's {username} and {current_streams}.
    Trigger - Notify on User Concurrent Streams
    Arguments {username} {current_streams} (Custom kill message?)
    I have 2 particular users who I don't care if they use separate IPs or not, I simply want to limit them to 2 sessions at once no matter where they are (by setting my user concurrent threshold at 3, and conditional statements to 'username is blah' or 'username is blah2')
    (They do share information I think, but let them punish themselves if the same internet household tries to watch 2 streams while the shared household is already watching something)

    Bonus points for including the shutdown message to them indicating the stream limit (like the kill paused does)

    Server - HW: I7-4790K(Haswell), 16GB - SW: Win10 Pro + PMS v1.13.2.5154
    Misc. HW: HDHR Extend + 2TB RAID1(Intel Z87) + 4x4TB RAID5(QNAP TS-653Pro)
    Shares - From: 2  - To: 18(5) - Potential lifetime PlexPass data generating product customer!
    ISP: ~20mbps up/1TB monthly "cap" - (Monthly avg. xfer ~375gb(~50gb server))
    Search the Plex WIKI before asking silly questions.
    Plex stuff I favor - Tautulli - UASv3 - FMoviesPlus
  • JamminRJamminR Posts: 3,535Members, Plex Pass Plex Pass

    @blacktwin, @JonnyWong16 , @sander1 , @shopgirl284 or any other python dev, help?
    Per my previous post, I've attempted to compose a 'kill 3rd session' python script heavily based on Blacktwin's kill_more_than.py code.
    I've mangled out every section that bases on IP, instead, looking for only what I think is user and session of the video.
    I know Tautulli is set up correctly. The Email notifier emails me when I hit the 3 same user threshhold.
    When I hit 3 sessions from one user (using me only in Tautulli conditions right now), I can see on my server a QUICK python window open up, but it closes so fast I can't see any print statements or even if there were any.
    Attached is my code.
    Can you tell what I've done wrong?
    Tips on how to figure out what I've done wrong?
    Again, I don't know python out-right, I just read most code well enough to tell what it's doing.
    Occasionally I can mangle and get stuff working.
    Thanks in advance to anyone.
    (File is a .py, renamed as .txt so forum wouldn't reject)

    Server - HW: I7-4790K(Haswell), 16GB - SW: Win10 Pro + PMS v1.13.2.5154
    Misc. HW: HDHR Extend + 2TB RAID1(Intel Z87) + 4x4TB RAID5(QNAP TS-653Pro)
    Shares - From: 2  - To: 18(5) - Potential lifetime PlexPass data generating product customer!
    ISP: ~20mbps up/1TB monthly "cap" - (Monthly avg. xfer ~375gb(~50gb server))
    Search the Plex WIKI before asking silly questions.
    Plex stuff I favor - Tautulli - UASv3 - FMoviesPlus
«13
Sign In or Register to comment.