Last active
September 5, 2017 12:18
-
-
Save lad1337/72880debc1dcc6922f5bf39eaad3072c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
# run this: pip install git+https://github.com/lad1337/python-plexapi@feature/download-progress | |
USAGE = "usage: python pcp.py <username> [<password>]" | |
from plexapi.myplex import MyPlexAccount | |
from plexapi.video import Episode | |
from plexapi.video import Movie | |
from plexapi.video import Show | |
def choose(msg, items, attr_name): | |
for index, i in enumerate(items): | |
if callable(attr_name): | |
name = attr_name(i) | |
else: | |
name = getattr(i, attr_name) | |
print("{index}: {name}".format(index=index, name=name)) | |
picked = map(int, (input("{}: ".format(msg))).split(',')) | |
if len(picked) == 1: | |
return items[picked[0]] | |
else: | |
return [items[p] for p in picked] | |
def search(server): | |
query = input("What are you looking for?: ") | |
return choose("Choose result", server.search(query), lambda x: repr(x)) | |
def download(item): | |
print("Loading...") | |
files = wanted_item.download('./') | |
print("Complete.") | |
for f in files: | |
print("- {}".format(f)) | |
def main(): | |
args = sys.argv[1:] | |
if len(args) == 2: | |
user, pw = args | |
elif len(args) == 1: | |
user = args[0] | |
import getpass | |
pw = getpass.getpass() | |
else: | |
print(USAGE) | |
exit(1) | |
account = MyPlexAccount(user, pw) | |
res = choose("Choose a server", account.resources(), 'name') | |
print("Connecting...") | |
server = res.connect() | |
print("Connected!") | |
found_item = search(server) | |
if isinstance(found_item, Show): | |
wanted_item = choose("Choose episode", found_item.episodes(), lambda x: x._prettyfilename()) | |
elif isinstance(found_item, (Movie, Episode)): | |
wanted_item = found_item | |
else: | |
print("dont know what to to with {}".format(found_item)) | |
exit(1) | |
if isinstance(wanted_item, list): | |
print("downloading {} items".format(len(wanted_item))) | |
for wanted_i in wanted_item: | |
download(wanted_i) | |
else: | |
download(wanted_item) | |
if __name__ == "__main__": | |
main() |
no, sure go ahead, i would be honoured :)
Thanks! -- I changed the formating slightly and added an option to download from a PlexWeb url. You can use that by calling plex-download.py --url=<URL>
(just make sure to only copy everything after the ! in the url). I love your choose function, I'll be using that very often in the future.
https://github.com/pkkid/python-plexapi/blob/master/tools/plex-download.py
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey lad1337. This looks quite useful. Do you mind if I include this in the python-plexapi tools directory?