Skip to main content

Python-PlexAPI

This unofficial app installation guide is provided for your convenience. The guide is provided as-is and may not be updated or maintained by Ultra.cc. Unofficial support may be offered via Discord only and at the sole discretion of Ultra.cc staff. Use at your own risk and only proceed if you are comfortable managing the application on your own.

Python-PlexAPI is a utility tool with intent of performing actions on your Plex instance by interacting with the Plex API. Included features are library actions such as scan, analyze, empty trash among many other things.

  • More information can be found here.

Prerequisites

  • Python version 3.8.15 or above. See here.
  • Plex instance installed on your service.

Installation

Do note, Python-PlexAPI requires some familiarity with the Linux terminal and scripting in general. If you are new to this, you should start by reading Your Ultra.cc Shell - A Beginner's Guide.

  • Connect to your Ultra.cc service via SSH
  • Once connected, execute the following command:
pip install plexapi
  • Once the installation has completed, you can begin using the utility tool.
  • In this guide, we will create a script that lists all your media titles in a .txt file.
    • However, this is just one of many usecases. Other examples can be found here.

  • Start by creating a Python script file and make it executable.
touch plexapi.py && chmod +x plexapi.py
  • Next, open the file in the Nano editor.
nano plexapi.py
  • Paste the following script into the editor and edit the plex_url and plex_token.
    • You can find your Plex IP in the Plex webUI. Navigate to Settings > Remote Access and copy the Private IP. It will be in the format of 172.17.0.xxx. Do note, if Plex is restarted/upgraded, the private IP might change, and you will need to update your script with the new IP.
    • You can find your Plex token by following the instructions in this link.
    • Additionally, you must edit the script to reflect your library names instead of 4K Movies, 1080p Movies and TV Shows.
from plexapi.server import PlexServer

# Replace 'http://YOUR-PLEX-IP:32400' and 'YOUR-PLEX-TOKEN' with your Plex server details
plex_url = 'http://YOUR-PLEX-IP:32400'
plex_token = 'YOUR-PLEX-TOKEN'

# Connect to the Plex server
plex = PlexServer(plex_url, plex_token)

# Output file name
output_file = 'plex_media_titles_with_year.txt'

# Replace library names to align with your Plex server's library names.
with open(output_file, 'w', encoding='utf-8') as file:
    file.write("4K Movies:\n")
    for movie in plex.library.section('4K Movies').all():
        file.write(f"{movie.title} ({movie.year})\n")

    file.write("\n1080p Movies:\n")
    for movie in plex.library.section('1080p Movies').all():
        file.write(f"{movie.title} ({movie.year})\n")

    file.write("\nTV Shows:\n")
    for show in plex.library.section('TV Shows').all():
        file.write(f"{show.title} ({show.year})\n")

print(f"Media titles with release year have been written to {output_file}")
  • Once you have edited the script with your Plex server details and library names, you can save and exit the editor.
    • To save and exit, press CTRL+X and Y, then press ENTER to confirm.
  • To run the script, execute the following command:
python plexapi.py
  • Depending on the size of your library, it may take some time to execute.
  • If the execution of the script was successful, you should see the following output in your terminal.
ultradocs@spica:~$ python plexapi.py
Media titles with release year have been written to plex_media_titles_with_year.txt