Skip to content

Instantly share code, notes, and snippets.

@mog54
Forked from felixbuenemann/plexwatcher.py
Last active February 8, 2020 17:22
Show Gist options
  • Select an option

  • Save mog54/03159ba55f1cf77d84f12cd03a0304d0 to your computer and use it in GitHub Desktop.

Select an option

Save mog54/03159ba55f1cf77d84f12cd03a0304d0 to your computer and use it in GitHub Desktop.

Revisions

  1. mog54 revised this gist Feb 8, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions plexwatcher.service
    Original file line number Diff line number Diff line change
    @@ -8,12 +8,12 @@ Requires=plexdrive.service
    User=plex
    Group=plex
    Type=simple
    ExecStartPre=/bin/sh -c 'while [ ! -d /mnt/plexdrive/Media ]; do /bin/sleep 5; done'
    ExecStartPre=/bin/sh -c 'while [ ! -d /mnt/plexdrive ]; do /bin/sleep 5; done'
    TimeoutStartSec=60
    TimeoutStopSec=60
    # Environment=LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1
    # watcher is from https://github.com/radovskyb/watcher
    ExecStart=/usr/local/bin/watcher -interval=60s -dotfiles=false -pipe=true "-cmd=/usr/bin/python3 /var/lib/plexmediaserver/plexwatcher.py" /mnt/plexdrive/Media
    ExecStart=/usr/local/bin/watcher -interval=60s -dotfiles=false -pipe=true "-cmd=/usr/bin/python3 /var/lib/plexmediaserver/plexwatcher.py" /mnt/plexdrive
    Restart=on-abort

    [Install]
  2. @felixbuenemann felixbuenemann created this gist Feb 1, 2020.
    51 changes: 51 additions & 0 deletions plexwatcher.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/usr/bin/env python3
    #
    # plexwatcher.py - parse watcher events to partially scan plex libraries
    # relevant paths and sections are discovered directly from the plex database
    #
    # Author: Felix Buenemann - https://github.com/felixbuenemann
    import os, sys, re, sqlite3, subprocess
    from os.path import dirname

    # reload(sys)
    # sys.setdefaultencoding('utf8')

    pms = '/usr/lib/plexmediaserver/Plex Media Scanner'
    pdb = '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db'

    conn = sqlite3.connect(pdb)
    c = conn.cursor()

    c.execute('SELECT id, name FROM library_sections')
    sections = dict(c.fetchall())
    c.execute('SELECT library_section_id, root_path FROM section_locations')
    dirs = c.fetchall()

    # Log format for parsing https://github.com/radovskyb/watcher events:
    log = re.compile(r'^(?P<type>[A-Z]+) "(?P<name>[^"]+)" (?P<action>[A-Z]+) \[(?P<path>[^\]]+)\]$')

    os.environ['LD_LIBRARY_PATH'] = '/usr/lib/plexmediaserver/lib'

    for line in sys.stdin:
    m = log.match(line)
    type = m.group('type')
    name = m.group('name')
    action = m.group('action')
    paths = m.group('path').split(' -> ')
    if (type in ('FILE', 'DIRECTORY') and action in ('CREATE', 'MOVE', 'REMOVE')):
    for library_section_id, root_path in dirs:
    for path in paths:
    if path.startswith(root_path + '/'):
    if type == 'DIRECTORY':
    directory = path
    else:
    directory = dirname(path)
    print(u'Update "%s" in section "%s"' % (directory, sections[library_section_id]))
    subprocess.call([pms,
    '--scan',
    '--refresh',
    '--no-thumbs',
    '--section', str(library_section_id),
    '--directory', directory
    ])

    20 changes: 20 additions & 0 deletions plexwatcher.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # /lib/systemd/system/plexwatcher.service
    [Unit]
    Description=Plex Library Updater
    After=plexdrive.service
    Requires=plexdrive.service

    [Service]
    User=plex
    Group=plex
    Type=simple
    ExecStartPre=/bin/sh -c 'while [ ! -d /mnt/plexdrive/Media ]; do /bin/sleep 5; done'
    TimeoutStartSec=60
    TimeoutStopSec=60
    # Environment=LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1
    # watcher is from https://github.com/radovskyb/watcher
    ExecStart=/usr/local/bin/watcher -interval=60s -dotfiles=false -pipe=true "-cmd=/usr/bin/python3 /var/lib/plexmediaserver/plexwatcher.py" /mnt/plexdrive/Media
    Restart=on-abort

    [Install]
    WantedBy=default.target