Metadata-Version: 2.1
Name: plexmediafixup
Version: 0.6.0
Summary: Run configurable fixups against the media database of a Plex Media Server
Home-page: https://github.com/andy-maier/plexmediafixup
Author: Andreas Maier
Author-email: andreas.r.maier@gmx.de
Maintainer: Andreas Maier
Maintainer-email: andreas.r.maier@gmx.de
License: Apache Software License 2.0
Platform: any
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: End Users/Desktop
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Multimedia
Classifier: Topic :: Utilities
Requires-Python: >=3.5
Description-Content-Type: text/x-rst
License-File: LICENSE

PlexMediaFixup - Run configurable fixups against the media database of a Plex Media Server
==========================================================================================

.. image:: https://img.shields.io/pypi/v/plexmediafixup.svg
    :target: https://pypi.python.org/pypi/plexmediafixup/
    :alt: Version on Pypi

.. image:: https://travis-ci.org/andy-maier/plexmediafixup.svg?branch=master
    :target: https://travis-ci.org/andy-maier/plexmediafixup
    :alt: Travis test status (master)

.. # .. image:: https://ci.appveyor.com/api/projects/status/i022iaeu3dao8j5x/branch/master?svg=true
.. #     :target: https://ci.appveyor.com/project/andy-maier/plexmediafixup
.. #     :alt: Appveyor test status (master)

.. # .. image:: https://img.shields.io/coveralls/plexmediafixup/plexmediafixup.svg
.. #     :target: https://coveralls.io/r/plexmediafixup/plexmediafixup
.. #     :alt: Test coverage (master)


Overview
--------

The PlexMediaFixup project provides a command named ``plexmediafixup`` that
performs certain changes in the media database of a `Plex Media Server`_.

The functionality to perform these changes is organized in plugins called
*fixups*. Each fixup can be enabled or disabled and configured as needed.

Currently supported fixups are:

* sync_title:

  This fixup walks through the movie and episode items of the configured
  library sections of the Plex Media Server, and syncs the "title" field of
  each item by setting it to the value of the title tag found in the
  corresponding media files. The ffprobe command is used to get the title
  tag from the media files.

  Use this fixup if you properly maintain the title tags in your media files
  and are not happy with the titles that get set from the metadata sites.

* sync_sort_title:

  This fixup walks through the movie, show and episode items of the configured
  library sections of the Plex Media Server, and syncs the "sort title" field of
  each item by setting it to the value of its "title" field.

  Use this fixup if you are not happy with the fact that PMS removes leading
  articles and other words from the sort title.

* video_genre_cleanup:

  This fixup walks through the movie and show items of the configured library
  sections of the Plex Media Server, and cleans up the "Genres" field of each
  item. It can consolidate differently spelled genres into a single genre,
  remove useless genres, and set a default genre if the list of genres is empty.
  These changes can be configured in the config file.

* preserve_collections:

  This fixup walks through the movie and show items of the configured library
  sections of the Plex Media Server, and preserves the collections of each item.
  This is done by syncing any present collections between PMS and a collections
  file. This solves the issue that PMS resets the collections every now and
  then, when updating the metadata of the item. Since the presence of a
  collection either in PMS or in the collections file causes it to be synced,
  the intentional removal of a collection now requires removing it from PMS and
  removing it from the collections file. The collections file is in YAML format
  and is self-explanatory. The path name of the collections file can be
  configured in the config file.

Look at the example plexmediafixup config file `example_config_file.yml`_ if
you want to see what can be configured. It describes each parameter.

This project uses the `python-PlexAPI package`_, and access to the Plex Media
Server is specified in the `PlexAPI config file`_. The ``plexmediafixup``
command supports a choice of using a direct connection to a network-accessible
Plex Media Server (which is the faster choice) or using an indirect connection
through the Plex web site (which works from anywhere but is slower).


Installation and configuration
------------------------------

1.  Install the latest released version of the plexmediafixup Python package
    into your active Python environment:

    .. code-block:: bash

        $ pip install plexmediafixup

    This will also install any prerequisite Python packages, including the
    `python-PlexAPI package`_.

2.  If this is the first time you use the `python-PlexAPI package`_, create its
    `PlexAPI config file`_ as follows.

    If you want to use direct connections to a network-accessible Plex Media
    Server, specify the following parameters:

    .. code-block:: text

        [auth]
        server_baseurl = <URL of your PMS>
        server_token = <server auth token of your PMS>

    The server auth token of your PMS can be found as described in
    `Finding your server auth token`_.

    If you want to use indirect connections through the Plex web site, specify
    the following parameters. This is simpler to set up compared to direct
    connections, but every request goes through the Plex web site and from there
    to your Plex Media Server:

    .. code-block:: text

        [auth]
        myplex_username = <your plex account username/email>
        myplex_password = <your plex account password>

    The `PlexAPI environment variables`_ can also be used.

3.  Create a plexmediafixup config file.

    A plexmediafixup config file is in `YAML format`_ and specifies some general
    parameters (such as whether to use direct or indirect connections, or the
    location of the `PlexAPI config file`_) and the fixups that should be run,
    including any parameters for the fixups.

    An example plexmediafixup config file is provided as
    `example_config_file.yml`_. This example config file contains descriptions
    for all parameters, and describes all supported fixups.

    Create your plexmediafixup config file by copying that example file and
    modifying it as needed.

    The copy of the example file should work without further modifications.
    By default, it uses a direct connection and has all fixups enabled and
    configured to process all items. However, please review your copy
    carefully and make changes as needed.

4. Install the ffprobe command

   This command is only used by the sync_title fixup.

   On Windows with Chocolatey, on MacOS with HomeBrew, and on most Linux
   distros, the package containing this command is named ``ffmpeg``.
   Alternatively, download it from https://ffmpeg.org/download.html.


Usage
-----

To explore the command line options and general usage of the ``plexmediafixup``
command, invoke:

.. code-block:: bash

    $ plexmediafixup --help

The following commands assume that ``my_config_file.yml`` is the plexmediafixup
config file you have created.

First, invoke the ``plexmediafixup`` command in dry-run mode. This shows you
what would be changed in a real run without actually doing any changes:

.. code-block:: bash

    $ plexmediafixup my_config_file.yml --verbose --dryrun

If you are satisfied with what you see, perform the changes:

.. code-block:: bash

    $ plexmediafixup my_config_file.yml --verbose


Simplified setup and run script
-------------------------------

If you want to run the ``plexmediafixup`` command regularly, this GitHub repo
contains ``run_plexmediafixup.sh`` and ``run_plexmediafixup.bat`` scripts that
simplify the setup somewhat.
Copy the script for your OS (.bat for native Windows, .sh otherwise) to a
directory in your PATH and modify it as needed.

These scripts have the following prerequisites:

* The plexmediafixup config file exists as ``$HOME/.config/plexmediafixup.yml``
  or, on native Windows as ``%HOMEDRIVE%%HOMEPATH%\.config\plexmediafixup.yml``
* The `PlexAPI config file`_ exists
* The ``ffprobe`` and ``virtualenv`` commands are available in the PATH

These scripts perform all the setup that is needed (such as checking if the
``virtualenv`` and ``ffprobe`` commands are available, creating a Python virtual
environment, activating it, installing the plexmediafixup package into that
virtual environment, checking if the plexmediafixup config file exists), and
finally run the ``plexmediafixup`` command with that config file.


Trouble shooting
----------------

HTTP status 500 Internal Server Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If the ``video_genre_cleanup`` fixup of the ``plexmediafixup`` command fails as follows:

.. code-block:: text

    Error: Cannot change the genres field of movie 'Star Wars VW Werbung' from [] to ['<keins>']:
      (500) internal_server_error; http://192.168.0.12:32400/library/sections/None/all?type=1&
      id=53458&genre%5B%5D.tag.tag-=&genre%5B0%5D.tag.tag=%3Ckeins%3E
      <html><head><title>Internal Server Error</title></head><body><h1>500 Internal Server Error</h1></body></html>
      (raised plexapi.exceptions.BadRequest after 0s)
    Error: Fixup video_genre_cleanup has encountered errors - aborting

Then one possible reason is that the server auth token is invalid.

To fix this, update the ``server_token`` parameter in the `PlexAPI config file`_
to the current server auth token (see `Finding your server auth token`_).


Bugs and features
-----------------

Please report any bugs and request features via the `issue tracker`_.


Contributing
------------

Contributions to the PlexMediaFixup project are welcome; for details see
`Development`_.


License
-------

The PlexMediaFixup project is provided under the
`Apache Software License 2.0`_.


.. _Plex Media Server: https://en.wikipedia.org/wiki/Plex_(software)
.. _python-PlexAPI package: https://python-plexapi.readthedocs.io/en/latest/introduction.html
.. _PlexAPI config file: https://python-plexapi.readthedocs.io/en/latest/configuration.html
.. _PlexAPI environment variables: https://python-plexapi.readthedocs.io/en/latest/configuration.html#environment-variables
.. _Finding your server auth token: https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
.. _YAML format: https://yaml.org/start.html

.. _issue tracker: https://github.com/andy-maier/plexmediafixup/issues
.. _example_config_file.yml: https://github.com/andy-maier/plexmediafixup/blob/master/example_config_file.yml
.. _Apache Software License 2.0: https://github.com/andy-maier/plexmediafixup/blob/master/LICENSE
.. _Development: https://github.com/andy-maier/plexmediafixup/blob/master/DEVELOPMENT.rst


