mirror of
https://github.com/pkkid/python-plexapi
synced 2024-11-23 04:03:05 +00:00
67 lines
2.5 KiB
Python
Executable file
67 lines
2.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Allows downloading a Plex media item from a local or shared library. You
|
|
may specify the item by the PlexWeb url (everything after !) or by
|
|
manually searching the items from the command line wizard.
|
|
|
|
Original contribution by lad1337.
|
|
"""
|
|
import argparse, re
|
|
from plexapi import utils
|
|
from plexapi.compat import unquote
|
|
from plexapi.video import Episode, Movie, Show
|
|
|
|
|
|
def choose(msg, items, attr):
|
|
print()
|
|
for index, i in enumerate(items):
|
|
name = attr(i) if callable(attr) else getattr(i, attr)
|
|
print(' %s: %s' % (index, name))
|
|
number = int(input('\n%s: ' % msg))
|
|
return items[number]
|
|
|
|
|
|
def search_for_item(url=None):
|
|
if url: return get_item_from_url(opts.url)
|
|
server = choose('Choose a Server', account.resources(), 'name').connect()
|
|
query = input('What are you looking for?: ')
|
|
item = choose('Choose result', server.search(query), lambda x: repr(x))
|
|
if isinstance(item, Show):
|
|
item = choose('Choose episode', item.episodes(), lambda x: x._prettyfilename())
|
|
if not isinstance(item, (Movie, Episode)):
|
|
raise SystemExit('Unable to download %s' % item.__class__.__name__)
|
|
return item
|
|
|
|
|
|
def get_item_from_url(url):
|
|
# Parse the ClientID and Key from the URL
|
|
clientid = re.findall('[a-f0-9]{40}', url)
|
|
key = re.findall('key=(.*?)(&.*)?$', url)
|
|
if not clientid or not key:
|
|
raise SystemExit('Cannot parse URL: %s' % url)
|
|
clientid = clientid[0]
|
|
key = unquote(key[0][0])
|
|
# Connect to the server and fetch the item
|
|
servers = [r for r in account.resources() if r.clientIdentifier == clientid]
|
|
if len(servers) != 1:
|
|
raise SystemExit('Unknown or ambiguous client id: %s' % clientid)
|
|
server = servers[0].connect()
|
|
return server.fetchItem(key)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# Command line parser
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--username', help='Your Plex username')
|
|
parser.add_argument('--password', help='Your Plex password')
|
|
parser.add_argument('--url', default=None, help='Download from URL (only paste after !)')
|
|
opts = parser.parse_args()
|
|
# Search item to download
|
|
account = utils.getMyPlexAccount(opts)
|
|
item = search_for_item(opts.url)
|
|
# Download the item
|
|
print("Downloading '%s' from %s.." % (item._prettyfilename(), item._server.friendlyName))
|
|
filepaths = item.download('./')
|
|
for filepath in filepaths:
|
|
print(' %s' % filepath)
|