PlexTraktSync

repository·main·Indexed 22 days ago

https://github.com/taxel/plextraktsync

A two-way synchronization tool between trakt.tv and Plex Media Server. It allows users to sync media collections, ratings, watched status, watchlists, and liked lists without requiring Plex Pass or Trakt VIP. The tool provides a CLI for performing syncs, scrobbling plays via a watch command, and managing configurations through config.yml and servers.yml.

Tokens
9K
Snippets
16
Records
53
Agent score
79%

What's inside PlexTraktSync

  1. How to use different configurations for different libraries

    main

    To apply different sync rules to different libraries (e.g., sync ratings for Movies but not for TV Shows), you should define multiple server entries in servers.yml. Each entry represents a unique configuration setup. When running the sync, you must specify which configuration to use with the --server flag.

    # servers.yml example: Multiple server entries for different library behaviors
    servers:
      Example1:
        token: ~
        urls:
          - http://localhost:32400
        config:
          libraries:
            - "Movies"
          sync:
            plex_to_trakt:
              ratings: true
              watched_status: true
            trakt_to_plex:
              ratings: true
              watched_status: true
      Example2:
        token: ~
        urls:
          - http://localhost:32400
        config:
          libraries:
            - "TV Shows"
          sync:
            plex_to_trakt:
              ratings: true
              watched_status: false
            trakt_to_plex:
              ratings: true
              watched_status: false

    To run:

    plextraktsync --server Example1 sync

    plextraktsync --server Example2 sync

  2. Configure Trakt API and initial setup

    main

    Before running the sync, you must create a Trakt API application:

    1. Visit https://trakt.tv/oauth/applications/new.
    2. Provide a name.
    3. Set the redirect URL to urn:ietf:wg:oauth:2.0:oob.
    4. Leave Javascript origins and Permissions blank.

    Once the app is created, run plextraktsync login. The script will guide you through the credential process. If you are using Docker and cannot run interactive commands easily (e.g., on a Synology NAS), run the login process on a different device, then move the generated .env, .pytrakt.json, and servers.yml files to your mapped /app/config folder.

  3. Set up PlexTraktSync watch as a systemd service

    main

    To run the watch command as a background daemon on Linux, you can use systemd.

    System-wide Service

    Create a unit file (e.g., /etc/systemd/system/PlexTraktSync.service):

    [Unit]
    Description=PlexTraktSync watch daemon
    After=network-online.target
    
    [Service]
    ExecStart=plextraktsync watch
    Restart=on-failure
    RestartSec=10
    User=user
    Group=user
    
    [Install]
    WantedBy=multi-user.target

    Then run: sudo systemctl daemon-reload
    sudo systemctl start PlexTraktSync.service
    sudo systemctl enable PlexTraktSync.service

    User-level Service (Template)

    This allows running different services for different Plex servers using the servers.yml names. Place the file in ~/.config/systemd/user/plextraktsync@.service:

    [Unit]
    Description=PlexTraktSync watch daemon
    After=network-online.target
    
    [Service]
    ExecSearchPath=%h/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    ExecStart=plextraktsync watch --server=%i
    Restart=on-failure
    RestartSec=10
    
    [Install]
    WantedBy=default.target

    To manage instances (where SERVER_NAME is from servers.yml):

    1. systemctl --user daemon-reload
    2. systemctl --user start "plextraktsync@SERVER_NAME.service"
    3. systemctl --user enable "plextraktsync@SERVER_NAME.service"

    Note: To allow the service to run without an active login session, enable lingering with loginctl enable-linger.

    # Example systemd user service template
    [Unit]
    Description=PlexTraktSync watch daemon
    After=network-online.target
    
    [Service]
    ExecSearchPath=%h/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    ExecStart=plextraktsync watch --server=%i
    Restart=on-failure
    RestartSec=10
    
    [Install]
    WantedBy=default.target
  4. Install Plex-Trakt-Sync using Docker Compose

    main

    You can run Plex-Trakt-Sync in a Docker container using Docker Compose. It is recommended to map a local directory to /app/config to persist your configuration files. You can also enable container health monitoring by adding a healthcheck that uses the plextraktsync healthcheck command to scan logs for ERROR or CRITICAL entries.

    version: "2"
    services:
      plextraktsync:
        image: ghcr.io/taxel/plextraktsync
        command: sync
        container_name: plextraktsync
        restart: on-failure:2
        volumes:
          - ./config:/app/config
        environment:
          - PUID=1000
          - PGID=1000
          - TZ=Europe/Tallinn
        healthcheck:
          test: ["CMD", "plextraktsync", "healthcheck"]
          interval: 60s
          timeout: 10s
          start_period: 30s
          retries: 3
  5. Install Plex-Trakt-Sync via pipx

    main
    To install Plex-Trakt-Sync on your local system, use pipx. This is the recommended method for Python applications. You can install the latest version or a specific version using the --force flag. To update the installation, use the self-update command.
  6. Scrobble plays using the watch command

    main

    The watch command listens to events from your Plex Media Server and automatically scrobbles (tracks) what you are watching to Trakt. This command should run in the background.

    Recommended Configuration: To ensure only your user's activity is scrobbled, add the following to your config.yml:

    watch:
      username_filter: true

    Running watch:

    • Directly: plextraktsync watch
    • Docker: docker compose run --rm plextraktsync watch
    • Docker Compose (detached): Add command: watch to your service definition.
    # config.yml snippet
    watch:
      username_filter: true
    # docker-compose.yml snippet
    version: "2"
    services:
      plextraktsync:
        image: ghcr.io/taxel/plextraktsync
        volumes:
          - ./config:/app/config
        command: watch
  7. Schedule sync jobs using Ofelia in Docker Compose

    main

    If you are using Docker, you can automate sync jobs using the ofelia scheduler. In your docker-compose.yml, you can define jobs that run the sync command for specific servers.

    Important: If you are running only one plextraktsync container, ensure your scheduled jobs do not overlap. Ofelia will skip a scheduled run if the previous job is still active. If you have multiple containers, they can run simultaneously.

    services:
      plextraktsync:
        image: ghcr.io/taxel/plextraktsync
        command: sync
        container_name: plextraktsync
        volumes:
          - ./config:/app/config
        environment:
          - PUID=1000
          - PGID=1000
        depends_on:
          - plex
      scheduler:
        image: mcuadros/ofelia:0.3
        container_name: scheduler
        command: daemon --docker
        restart: unless-stopped
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock:ro
        labels:
          ofelia.job-run.plextraktsync.schedule: "0 6,18 * * *"
          ofelia.job-run.plextraktsync.container: "plextraktsync"
          ofelia.job-run.plextraktsync.command: "--server 'Example1' sync"
          ofelia.job-run.plextraktsync2.schedule: "0 12,0 * * *"
          ofelia.job-run.plextraktsync2.container: "plextraktsync"
          ofelia.job-run.plextraktsync2.command: "--server 'Example2' sync"
  8. Schedule Plex-Trakt-Sync with Cron (Linux/macOS)

    main

    Since Plex-Trakt-Sync does not use webhooks, it must be run manually or via a scheduler like cron. To run the sync every two hours, add a entry to your crontab. Use which plextraktsync to find the absolute path to the executable.

    # Example: run every 2 hours
    $ crontab -e
    0 */2 * * * $HOME/.local/bin/plextraktsync sync
  9. Manage Plex libraries via configuration

    main

    By default, all libraries are processed. You can control which libraries are synced using excluded-libraries or libraries keys in your configuration files.

    Global Exclusion

    In config.yml, use excluded-libraries to list names of libraries you want to skip.

    Per-Server Exclusion/Inclusion

    In servers.yml, you can define library settings for specific servers.

    • Use excluded-libraries to skip specific libraries for that server.
    • Use libraries to specify only the libraries to be processed. If you use libraries, the global excluded-libraries setting is ignored for that server.
    # servers.yml example: Excluding a library
    servers:
      Example1:
        token: ~
        urls:
          - http://localhost:32400
        config:
          excluded-libraries:
            - "Family Movies"
    
    # servers.yml example: Including only specific libraries
    servers:
      Example1:
        token: ~
        urls:
          - http://localhost:32400
        config:
          libraries:
            - "Movies"
            - "TV Shows"
  10. Configure per-server sync settings in servers.yml

    main

    You can override global config.yml settings by defining a config block within a specific server entry in servers.yml. This allows you to have different synchronization behaviors (e.g., different rating or watched status settings) for different Plex servers or different library groups.

    # servers.yml example: Overriding sync settings per server
    servers:
      Example1:
        token: ~
        urls:
          - http://localhost:32400
        config:
          sync:
            plex_to_trakt:
              collection: true
            trakt_to_plex:
              liked_lists: false
  11. Subtitle download requirements and constraints

    main

    When using the download functionality for subtitles, the following conditions must be met for a successful download:

    1. Subtitle Key: The subtitle stream must have a valid key. If sub.key is missing, the subtitle is marked as Not downloadable.
    2. File Existence: If the underlying file is not found on the Plex server, a NotFound error is caught, and the subtitle is marked as File doesn't exist: Not downloadable.
    3. Naming Convention: Subtitles are saved using a specific generated filename containing the ID, title, language, language code, and codec to ensure uniqueness and clarity.
  12. Configure logging behavior

    main

    Sync progress is recorded in plextraktsync.log. You can adjust the logging behavior in config.yml:

    • Log Level: Change the debug variable to true to enable DEBUG level logging (default is INFO).
    • Log Rotation: Set the append variable to false if you want the log file to only contain the results of the most recent run. If true (default), logs will append to the existing file.