2017-02-25 05:12:24 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2017-08-13 01:35:13 +00:00
|
|
|
"""
|
|
|
|
Plex-ListTokens is a simple utility to fetch and list all known Plex
|
|
|
|
Server tokens your plex.tv account has access to. Because this information
|
|
|
|
comes from the plex.tv website, we need to ask for your username
|
|
|
|
and password. Alternatively, if you do not wish to enter your login
|
|
|
|
information below, you can retrieve the same information from plex.tv
|
|
|
|
at the URL: https://plex.tv/api/resources?includeHttps=1
|
|
|
|
"""
|
2017-08-13 05:50:40 +00:00
|
|
|
import argparse
|
2017-04-28 01:19:59 +00:00
|
|
|
from plexapi import utils
|
2017-02-25 05:12:24 +00:00
|
|
|
from plexapi.exceptions import BadRequest
|
2017-08-13 05:50:40 +00:00
|
|
|
from plexapi.myplex import _connect
|
2017-04-28 01:19:59 +00:00
|
|
|
from plexapi.server import PlexServer
|
|
|
|
|
|
|
|
SERVER = 'Plex Media Server'
|
2017-08-13 05:50:40 +00:00
|
|
|
FORMAT = '%-8s %-6s %-17s %-25s %-20s %s (%s)'
|
2017-04-28 01:19:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _list_resources(account, servers):
|
2017-08-13 05:50:40 +00:00
|
|
|
items = []
|
|
|
|
print('Finding Plex resources..')
|
|
|
|
resources = account.resources()
|
|
|
|
for r in [r for r in resources if r.accessToken]:
|
|
|
|
for connection in r.connections:
|
|
|
|
local = 'Local' if connection.local else 'Remote'
|
|
|
|
extras = [r.provides]
|
|
|
|
items.append(FORMAT % ('Resource', local, r.product, r.name, r.accessToken, connection.uri, ','.join(extras)))
|
|
|
|
items.append(FORMAT % ('Resource', local, r.product, r.name, r.accessToken, connection.httpuri, ','.join(extras)))
|
|
|
|
servers[connection.httpuri] = r.accessToken
|
|
|
|
servers[connection.uri] = r.accessToken
|
|
|
|
return items
|
2017-04-28 01:19:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _list_devices(account, servers):
|
2017-08-13 05:50:40 +00:00
|
|
|
items = []
|
|
|
|
print('Finding Plex devices..')
|
|
|
|
for d in [d for d in account.devices() if d.token]:
|
|
|
|
for connection in d.connections:
|
|
|
|
extras = [d.provides]
|
|
|
|
items.append(FORMAT % ('Device', '--', d.product, d.name, d.token, connection, ','.join(extras)))
|
|
|
|
servers[connection] = d.token
|
|
|
|
return items
|
2017-04-28 01:19:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _test_servers(servers):
|
2017-08-13 05:50:40 +00:00
|
|
|
items, seen = [], set()
|
|
|
|
print('Finding Plex clients..')
|
2017-04-28 01:19:59 +00:00
|
|
|
listargs = [[PlexServer, s, t, 5] for s,t in servers.items()]
|
|
|
|
results = utils.threaded(_connect, listargs)
|
|
|
|
for url, token, plex, runtime in results:
|
|
|
|
clients = plex.clients() if plex else []
|
|
|
|
if plex and clients:
|
|
|
|
for c in plex.clients():
|
|
|
|
if c._baseurl not in seen:
|
2017-08-13 05:50:40 +00:00
|
|
|
extras = [plex.friendlyName] + c.protocolCapabilities
|
|
|
|
items.append(FORMAT % ('Client', '--', c.product, c.title, token, c._baseurl, ','.join(extras)))
|
2017-04-28 01:19:59 +00:00
|
|
|
seen.add(c._baseurl)
|
2017-08-13 05:50:40 +00:00
|
|
|
return items
|
|
|
|
|
|
|
|
|
|
|
|
def _print_items(items, _filter=None):
|
|
|
|
if _filter:
|
|
|
|
print('Displaying items matching filter: %s' % _filter)
|
|
|
|
print()
|
|
|
|
for item in items:
|
|
|
|
filtered_out = False
|
|
|
|
for f in _filter.split():
|
|
|
|
if f.lower() not in item.lower():
|
|
|
|
filtered_out = True
|
|
|
|
if not filtered_out:
|
|
|
|
print(item)
|
|
|
|
print()
|
2017-02-25 05:12:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-08-13 05:50:40 +00:00
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
parser.add_argument('--username', help='Your Plex username')
|
|
|
|
parser.add_argument('--password', help='Your Plex password')
|
|
|
|
parser.add_argument('--filter', default='', help='Only display items containing specified filter')
|
|
|
|
opts = parser.parse_args()
|
2017-02-25 05:12:24 +00:00
|
|
|
try:
|
2017-04-28 01:19:59 +00:00
|
|
|
servers = {}
|
2017-08-13 05:50:40 +00:00
|
|
|
account = utils.getMyPlexAccount(opts)
|
|
|
|
items = _list_resources(account, servers)
|
|
|
|
items += _list_devices(account, servers)
|
|
|
|
items += _test_servers(servers)
|
|
|
|
_print_items(items, opts.filter)
|
2017-02-25 05:12:24 +00:00
|
|
|
except BadRequest as err:
|
|
|
|
print('Unable to login to plex.tv: %s' % err)
|