Last active 1644869771

a script to get direct download urls from a plex host. pass numbers as arguments to preselect choices instead of entering when prompted

Revision df589f2840aed025b6f57653de70e4672368e73c

plexbrowse.py Raw
1#!/usr/bin/env python3
2import requests
3from sys import stderr, stdout, argv
4from os import environ
5
6host = environ.get("PLEX_HOST", "")
7token = environ.get("PLEX_TOKEN", "")
8
9args = argv[1:]
10
11def prompt(text):
12 global args
13 print(text, file=stderr, end="")
14 if args:
15 picked = args.pop(0)
16 print(picked, file=stderr)
17 return picked
18 return input()
19
20def get(endpoint):
21 url = host + ("/" if endpoint[0] != "/" else "") + endpoint
22 r = requests.get(url, params={"X-Plex-Token": token}, headers={"Accept": "application/json"})
23 return r.json()
24
25def main():
26 libraries = [d for d in get("/library/sections")["MediaContainer"]["Directory"]]
27 for n,l in enumerate(libraries):
28 print(f"[{l['key']}] {l['title']}", file=stderr)
29 choice = prompt("Pick library: ")
30 library = [l for l in libraries if l["key"] == choice][0]
31 lib_list = [i for i in get(f"/library/sections/{library['key']}/all")["MediaContainer"]["Metadata"]]
32 for n,i in enumerate(lib_list):
33 print(f"[{n}] {i['title']}", file=stderr)
34 picked = lib_list[int(prompt("Pick item: "))]
35 if picked["type"] == "show":
36 episodes = [e for e in get(picked["key"].replace("children", "allLeaves"))["MediaContainer"]["Metadata"]]
37 for n,e in enumerate(episodes):
38 print(f"[{n}] S{e['parentIndex']:02d}E{e['index']:02d} {e['title']}", file=stderr)
39 picked = episodes[int(prompt("Pick episode: "))]
40 print(f"{host}{picked['Media'][0]['Part'][0]['key']}?X-Plex-Token={token}")
41 return 0
42
43if __name__ == "__main__":
44 from sys import exit
45 exit(main())