2019-10-16 02:23:25 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2022-02-27 03:26:08 +00:00
|
|
|
The script is used to bootstrap a the test environment for plexapi
|
2020-04-18 19:50:04 +00:00
|
|
|
with all the libraries required for testing.
|
|
|
|
|
|
|
|
By default this uses a docker.
|
|
|
|
|
|
|
|
It can be used manually using:
|
|
|
|
python plex-bootraptest.py --no-docker --server-name name_of_server --account Hellowlol --password yourpassword
|
|
|
|
|
2018-09-14 18:03:23 +00:00
|
|
|
"""
|
2018-09-08 15:27:34 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
2020-04-29 21:23:22 +00:00
|
|
|
import shutil
|
2019-10-16 02:23:25 +00:00
|
|
|
import socket
|
|
|
|
import time
|
2018-09-08 15:27:34 +00:00
|
|
|
from glob import glob
|
2020-05-12 21:26:29 +00:00
|
|
|
from os import makedirs
|
|
|
|
from shutil import copyfile, which
|
2018-09-08 15:27:34 +00:00
|
|
|
from subprocess import call
|
2019-10-16 02:23:25 +00:00
|
|
|
from uuid import uuid4
|
2020-04-18 19:50:04 +00:00
|
|
|
|
|
|
|
import plexapi
|
2018-09-14 18:03:23 +00:00
|
|
|
from plexapi.exceptions import BadRequest, NotFound
|
|
|
|
from plexapi.myplex import MyPlexAccount
|
2018-09-15 08:42:42 +00:00
|
|
|
from plexapi.server import PlexServer
|
2020-04-18 19:50:04 +00:00
|
|
|
from plexapi.utils import SEARCHTYPES
|
|
|
|
from tqdm import tqdm
|
2018-09-08 15:27:34 +00:00
|
|
|
|
|
|
|
DOCKER_CMD = [
|
2020-04-18 19:50:04 +00:00
|
|
|
"docker",
|
|
|
|
"run",
|
|
|
|
"-d",
|
|
|
|
"--name",
|
|
|
|
"plex-test-%(container_name_extra)s%(image_tag)s",
|
|
|
|
"--restart",
|
|
|
|
"on-failure",
|
|
|
|
"-p",
|
|
|
|
"32400:32400/tcp",
|
|
|
|
"-p",
|
|
|
|
"3005:3005/tcp",
|
|
|
|
"-p",
|
|
|
|
"8324:8324/tcp",
|
|
|
|
"-p",
|
|
|
|
"32469:32469/tcp",
|
|
|
|
"-p",
|
|
|
|
"1900:1900/udp",
|
|
|
|
"-p",
|
|
|
|
"32410:32410/udp",
|
|
|
|
"-p",
|
|
|
|
"32412:32412/udp",
|
|
|
|
"-p",
|
|
|
|
"32413:32413/udp",
|
|
|
|
"-p",
|
|
|
|
"32414:32414/udp",
|
|
|
|
"-e",
|
|
|
|
"PLEX_CLAIM=%(claim_token)s",
|
|
|
|
"-e",
|
|
|
|
"ADVERTISE_IP=http://%(advertise_ip)s:32400/",
|
2021-08-26 01:05:49 +00:00
|
|
|
"-e",
|
|
|
|
"TZ=%(timezone)s",
|
|
|
|
"-e",
|
|
|
|
"LANG=%(language)s",
|
2020-04-18 19:50:04 +00:00
|
|
|
"-h",
|
|
|
|
"%(hostname)s",
|
|
|
|
"-v",
|
|
|
|
"%(destination)s/db:/config",
|
|
|
|
"-v",
|
|
|
|
"%(destination)s/transcode:/transcode",
|
|
|
|
"-v",
|
|
|
|
"%(destination)s/media:/data",
|
|
|
|
"plexinc/pms-docker:%(image_tag)s",
|
2018-09-08 15:27:34 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-04-18 19:50:04 +00:00
|
|
|
BASE_DIR_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
STUB_MOVIE_PATH = os.path.join(BASE_DIR_PATH, "tests", "data", "video_stub.mp4")
|
2020-04-29 21:23:22 +00:00
|
|
|
STUB_MP3_PATH = os.path.join(BASE_DIR_PATH, "tests", "data", "audio_stub.mp3")
|
2020-04-18 19:50:04 +00:00
|
|
|
STUB_IMAGE_PATH = os.path.join(BASE_DIR_PATH, "tests", "data", "cute_cat.jpg")
|
|
|
|
|
|
|
|
|
|
|
|
def check_ext(path, ext):
|
|
|
|
"""I hate glob so much."""
|
|
|
|
result = []
|
|
|
|
for root, dirs, fil in os.walk(path):
|
|
|
|
for f in fil:
|
|
|
|
fp = os.path.join(root, f)
|
|
|
|
if fp.endswith(ext):
|
|
|
|
result.append(fp)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2020-04-29 21:23:22 +00:00
|
|
|
class ExistingSection(Exception):
|
|
|
|
"""This server has sections, exiting"""
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
raise SystemExit("This server has sections exiting")
|
|
|
|
|
|
|
|
|
|
|
|
def clean_pms(server, path):
|
|
|
|
for section in server.library.sections():
|
|
|
|
print("Deleting %s" % section.title)
|
|
|
|
section.delete()
|
|
|
|
|
|
|
|
server.library.cleanBundles()
|
|
|
|
server.library.optimize()
|
|
|
|
print("optimized db and removed any bundles")
|
|
|
|
|
|
|
|
shutil.rmtree(path, ignore_errors=False, onerror=None)
|
|
|
|
print("Deleted %s" % path)
|
|
|
|
|
|
|
|
|
2022-12-21 19:32:43 +00:00
|
|
|
def setup_music(music_path, docker=False):
|
2021-05-24 03:39:59 +00:00
|
|
|
print("Setup files for the Music section..")
|
2020-04-18 19:50:04 +00:00
|
|
|
makedirs(music_path, exist_ok=True)
|
|
|
|
|
2020-04-29 21:23:22 +00:00
|
|
|
all_music = {
|
|
|
|
|
|
|
|
"Broke for free": {
|
|
|
|
"Layers": [
|
|
|
|
"1 - As Colorful As Ever.mp3",
|
2023-08-29 03:29:39 +00:00
|
|
|
# "02 - Knock Knock.mp3",
|
|
|
|
# "03 - Only Knows.mp3",
|
|
|
|
# "04 - If.mp3",
|
|
|
|
# "05 - Note Drop.mp3",
|
|
|
|
# "06 - Murmur.mp3",
|
|
|
|
# "07 - Spellbound.mp3",
|
|
|
|
# "08 - The Collector.mp3",
|
|
|
|
# "09 - Quit Bitching.mp3",
|
|
|
|
# "10 - A Year.mp3",
|
2020-04-18 19:50:04 +00:00
|
|
|
]
|
|
|
|
},
|
2020-04-29 21:23:22 +00:00
|
|
|
|
2020-04-18 19:50:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 19:32:43 +00:00
|
|
|
m3u_file = open(os.path.join(music_path, "playlist.m3u"), "w")
|
|
|
|
|
2020-04-18 19:50:04 +00:00
|
|
|
for artist, album in all_music.items():
|
|
|
|
for k, v in album.items():
|
2020-04-29 21:23:22 +00:00
|
|
|
artist_album = os.path.join(music_path, artist, k)
|
2020-04-18 19:50:04 +00:00
|
|
|
makedirs(artist_album, exist_ok=True)
|
|
|
|
for song in v:
|
2022-12-21 19:32:43 +00:00
|
|
|
trackpath = os.path.join(artist_album, song)
|
|
|
|
copyfile(STUB_MP3_PATH, trackpath)
|
|
|
|
|
|
|
|
if docker:
|
|
|
|
reltrackpath = os.path.relpath(trackpath, os.path.dirname(music_path))
|
|
|
|
m3u_file.write(os.path.join("/data", reltrackpath) + "\n")
|
|
|
|
else:
|
|
|
|
m3u_file.write(trackpath + "\n")
|
|
|
|
|
|
|
|
m3u_file.close()
|
2020-04-18 19:50:04 +00:00
|
|
|
|
|
|
|
return len(check_ext(music_path, (".mp3")))
|
|
|
|
|
|
|
|
|
|
|
|
def setup_movies(movies_path):
|
|
|
|
print("Setup files for the Movies section..")
|
|
|
|
makedirs(movies_path, exist_ok=True)
|
|
|
|
if len(glob(movies_path + "/*.mkv", recursive=True)) == 4:
|
|
|
|
return 4
|
|
|
|
|
|
|
|
required_movies = {
|
|
|
|
"Elephants Dream": 2006,
|
|
|
|
"Sita Sings the Blues": 2008,
|
|
|
|
"Big Buck Bunny": 2008,
|
|
|
|
"Sintel": 2010,
|
|
|
|
}
|
|
|
|
expected_media_count = 0
|
|
|
|
for name, year in required_movies.items():
|
|
|
|
expected_media_count += 1
|
|
|
|
if not os.path.isfile(get_movie_path(movies_path, name, year)):
|
|
|
|
copyfile(STUB_MOVIE_PATH, get_movie_path(movies_path, name, year))
|
|
|
|
|
|
|
|
return expected_media_count
|
|
|
|
|
|
|
|
|
|
|
|
def setup_images(photos_path):
|
|
|
|
print("Setup files for the Photos section..")
|
|
|
|
|
|
|
|
makedirs(photos_path, exist_ok=True)
|
|
|
|
# expected_photo_count = 0
|
|
|
|
folders = {
|
|
|
|
("Cats",): 3,
|
|
|
|
("Cats", "Cats in bed"): 7,
|
|
|
|
("Cats", "Cats not in bed"): 1,
|
|
|
|
("Cats", "Not cats in bed"): 1,
|
|
|
|
}
|
|
|
|
has_photos = 0
|
|
|
|
for folder_path, required_cnt in folders.items():
|
|
|
|
folder_path = os.path.join(photos_path, *folder_path)
|
|
|
|
makedirs(folder_path, exist_ok=True)
|
|
|
|
photos_in_folder = len(glob(os.path.join(folder_path, "/*.jpg")))
|
|
|
|
while photos_in_folder < required_cnt:
|
|
|
|
# Dunno why this is need got permission error on photo0.jpg
|
|
|
|
photos_in_folder += 1
|
|
|
|
full_path = os.path.join(folder_path, "photo%d.jpg" % photos_in_folder)
|
|
|
|
copyfile(STUB_IMAGE_PATH, full_path)
|
|
|
|
has_photos += photos_in_folder
|
|
|
|
|
|
|
|
return len(check_ext(photos_path, (".jpg")))
|
|
|
|
|
|
|
|
|
|
|
|
def setup_show(tvshows_path):
|
|
|
|
print("Setup files for the TV-Shows section..")
|
|
|
|
makedirs(tvshows_path, exist_ok=True)
|
|
|
|
makedirs(os.path.join(tvshows_path, "Game of Thrones"), exist_ok=True)
|
|
|
|
makedirs(os.path.join(tvshows_path, "The 100"), exist_ok=True)
|
|
|
|
required_tv_shows = {
|
|
|
|
"Game of Thrones": [list(range(1, 11)), list(range(1, 11))],
|
|
|
|
"The 100": [list(range(1, 14)), list(range(1, 17))],
|
|
|
|
}
|
|
|
|
expected_media_count = 0
|
|
|
|
for show_name, seasons in required_tv_shows.items():
|
|
|
|
for season_id, episodes in enumerate(seasons, start=1):
|
|
|
|
for episode_id in episodes:
|
|
|
|
expected_media_count += 1
|
|
|
|
episode_path = get_tvshow_path(
|
|
|
|
tvshows_path, show_name, season_id, episode_id
|
|
|
|
)
|
|
|
|
if not os.path.isfile(episode_path):
|
|
|
|
copyfile(STUB_MOVIE_PATH, episode_path)
|
|
|
|
|
|
|
|
return expected_media_count
|
|
|
|
|
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
def get_default_ip():
|
|
|
|
""" Return the first IP address of the current machine if available. """
|
2020-04-18 19:50:04 +00:00
|
|
|
available_ips = list(
|
|
|
|
set(
|
|
|
|
[
|
|
|
|
i[4][0]
|
|
|
|
for i in socket.getaddrinfo(socket.gethostname(), None)
|
|
|
|
if i[4][0] not in ("127.0.0.1", "::1")
|
|
|
|
and not i[4][0].startswith("fe80:")
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2019-10-16 02:23:25 +00:00
|
|
|
return available_ips[0] if len(available_ips) else None
|
|
|
|
|
|
|
|
|
|
|
|
def get_plex_account(opts):
|
2022-02-27 03:26:08 +00:00
|
|
|
""" Authenticate with Plex using the command line options. """
|
2019-10-16 02:23:25 +00:00
|
|
|
if not opts.unclaimed:
|
|
|
|
if opts.token:
|
|
|
|
return MyPlexAccount(token=opts.token)
|
|
|
|
return plexapi.utils.getMyPlexAccount(opts)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-04-18 19:50:04 +00:00
|
|
|
def get_movie_path(movies_path, name, year):
|
2019-10-16 02:23:25 +00:00
|
|
|
""" Return a movie path given its title and year. """
|
2020-04-18 19:50:04 +00:00
|
|
|
return os.path.join(movies_path, "%s (%d).mp4" % (name, year))
|
2019-10-16 02:23:25 +00:00
|
|
|
|
|
|
|
|
2020-04-18 19:50:04 +00:00
|
|
|
def get_tvshow_path(tvshows_path, name, season, episode):
|
2019-10-16 02:23:25 +00:00
|
|
|
""" Return a TV show path given its title, season, and episode. """
|
2020-04-18 19:50:04 +00:00
|
|
|
return os.path.join(tvshows_path, name, "S%02dE%02d.mp4" % (season, episode))
|
2018-09-08 15:27:34 +00:00
|
|
|
|
|
|
|
|
2019-10-16 02:44:10 +00:00
|
|
|
def add_library_section(server, section):
|
2019-10-16 02:40:01 +00:00
|
|
|
""" Add the specified section to our Plex instance. This tends to be a bit
|
|
|
|
flaky, so we retry a few times here.
|
|
|
|
"""
|
|
|
|
start = time.time()
|
|
|
|
runtime = 0
|
2019-10-16 02:44:10 +00:00
|
|
|
while runtime < 60:
|
2019-10-16 02:40:01 +00:00
|
|
|
try:
|
|
|
|
server.library.add(**section)
|
|
|
|
return True
|
|
|
|
except BadRequest as err:
|
2020-04-18 19:50:04 +00:00
|
|
|
if "server is still starting up. Please retry later" in str(err):
|
2019-10-16 02:40:01 +00:00
|
|
|
time.sleep(1)
|
|
|
|
continue
|
|
|
|
raise
|
|
|
|
runtime = time.time() - start
|
2020-04-18 19:50:04 +00:00
|
|
|
raise SystemExit("Timeout adding section to Plex instance.")
|
2019-10-16 02:40:01 +00:00
|
|
|
|
|
|
|
|
2023-08-29 03:29:39 +00:00
|
|
|
def create_section(server, section, opts): # noqa: C901
|
2018-09-14 18:03:23 +00:00
|
|
|
processed_media = 0
|
2020-04-18 19:50:04 +00:00
|
|
|
expected_media_count = section.pop("expected_media_count", 0)
|
|
|
|
expected_media_type = (section["type"],)
|
2021-05-31 01:02:29 +00:00
|
|
|
if section["type"] == "show":
|
|
|
|
expected_media_type = ("show", "season", "episode")
|
2020-04-18 19:50:04 +00:00
|
|
|
if section["type"] == "artist":
|
|
|
|
expected_media_type = ("artist", "album", "track")
|
2018-09-14 18:03:23 +00:00
|
|
|
expected_media_type = tuple(SEARCHTYPES[t] for t in expected_media_type)
|
|
|
|
|
|
|
|
def alert_callback(data):
|
2019-10-16 02:57:04 +00:00
|
|
|
""" Listen to the Plex notifier to determine when metadata scanning is complete. """
|
2018-09-14 18:03:23 +00:00
|
|
|
global processed_media
|
2020-04-18 19:50:04 +00:00
|
|
|
if data["type"] == "timeline":
|
|
|
|
for entry in data["TimelineEntry"]:
|
|
|
|
if (
|
|
|
|
entry.get("identifier", "com.plexapp.plugins.library")
|
|
|
|
== "com.plexapp.plugins.library"
|
|
|
|
):
|
2018-09-14 18:03:23 +00:00
|
|
|
# Missed mediaState means that media was processed (analyzed & thumbnailed)
|
2020-04-18 19:50:04 +00:00
|
|
|
if (
|
|
|
|
"mediaState" not in entry
|
|
|
|
and entry["type"] in expected_media_type
|
|
|
|
):
|
2018-09-14 18:03:23 +00:00
|
|
|
# state=5 means record processed, applicable only when metadata source was set
|
2020-04-18 19:50:04 +00:00
|
|
|
if entry["state"] == 5:
|
2019-10-16 03:02:26 +00:00
|
|
|
cnt = 1
|
2020-04-18 19:50:04 +00:00
|
|
|
if entry["type"] == SEARCHTYPES["show"]:
|
|
|
|
show = server.library.sectionByID(
|
2021-03-11 21:27:08 +00:00
|
|
|
entry["sectionID"]
|
2020-04-18 19:50:04 +00:00
|
|
|
).get(entry["title"])
|
2019-10-16 03:02:26 +00:00
|
|
|
cnt = show.leafCount
|
|
|
|
bar.update(cnt)
|
2019-10-16 03:49:27 +00:00
|
|
|
processed_media += cnt
|
2018-09-14 18:03:23 +00:00
|
|
|
# state=1 means record processed, when no metadata source was set
|
2020-04-18 19:50:04 +00:00
|
|
|
elif (
|
|
|
|
entry["state"] == 1
|
|
|
|
and entry["type"] == SEARCHTYPES["photo"]
|
|
|
|
):
|
2018-09-14 18:03:23 +00:00
|
|
|
bar.update()
|
2019-10-16 03:49:27 +00:00
|
|
|
processed_media += 1
|
2018-09-14 18:03:23 +00:00
|
|
|
|
2019-10-16 02:40:01 +00:00
|
|
|
runtime = 0
|
|
|
|
start = time.time()
|
2020-04-18 19:50:04 +00:00
|
|
|
bar = tqdm(desc="Scanning section " + section["name"], total=expected_media_count)
|
2018-09-14 18:03:23 +00:00
|
|
|
notifier = server.startAlertListener(alert_callback)
|
2019-11-12 01:28:25 +00:00
|
|
|
time.sleep(3)
|
2019-10-16 02:44:10 +00:00
|
|
|
add_library_section(server, section)
|
2019-10-16 04:04:41 +00:00
|
|
|
while bar.n < bar.total:
|
|
|
|
if runtime >= 120:
|
2020-04-18 19:50:04 +00:00
|
|
|
print("Metadata scan taking too long, but will continue anyway..")
|
2019-10-16 03:31:13 +00:00
|
|
|
break
|
2019-10-16 02:23:25 +00:00
|
|
|
time.sleep(3)
|
2019-11-12 01:28:25 +00:00
|
|
|
runtime = int(time.time() - start)
|
2018-09-14 18:03:23 +00:00
|
|
|
bar.close()
|
|
|
|
notifier.stop()
|
2018-09-08 15:27:34 +00:00
|
|
|
|
|
|
|
|
2023-08-29 03:29:39 +00:00
|
|
|
if __name__ == "__main__": # noqa: C901
|
2019-10-16 02:23:25 +00:00
|
|
|
default_ip = get_default_ip()
|
2018-09-08 15:27:34 +00:00
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
2019-10-16 02:23:25 +00:00
|
|
|
# Authentication arguments
|
2018-09-15 08:42:42 +00:00
|
|
|
mg = parser.add_mutually_exclusive_group()
|
|
|
|
g = mg.add_argument_group()
|
2020-04-18 19:50:04 +00:00
|
|
|
g.add_argument("--username", help="Your Plex username")
|
|
|
|
g.add_argument("--password", help="Your Plex password")
|
|
|
|
mg.add_argument(
|
|
|
|
"--token",
|
|
|
|
help="Plex.tv authentication token",
|
|
|
|
default=plexapi.CONFIG.get("auth.server_token"),
|
|
|
|
)
|
|
|
|
mg.add_argument(
|
|
|
|
"--unclaimed",
|
|
|
|
help="Do not claim the server",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
)
|
2019-10-16 02:23:25 +00:00
|
|
|
# Test environment arguments
|
2020-04-18 19:50:04 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--no-docker", help="Use docker", default=False, action="store_true"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--timezone", help="Timezone to set inside plex", default="UTC"
|
|
|
|
) # noqa
|
2021-08-26 01:05:49 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--language", help="Language to set inside plex", default="en_US.UTF-8"
|
|
|
|
) # noqa
|
2020-04-18 19:50:04 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--destination",
|
|
|
|
help="Local path where to store all the media",
|
|
|
|
default=os.path.join(os.getcwd(), "plex"),
|
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
|
|
|
"--advertise-ip",
|
|
|
|
help="IP address which should be advertised by new Plex instance",
|
|
|
|
required=default_ip is None,
|
|
|
|
default=default_ip,
|
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
|
|
|
"--docker-tag", help="Docker image tag to install", default="latest"
|
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
|
|
|
"--bootstrap-timeout",
|
|
|
|
help="Timeout for each step of bootstrap, in seconds (default: %(default)s)",
|
|
|
|
default=180,
|
|
|
|
type=int,
|
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
|
|
|
"--server-name",
|
|
|
|
help="Name for the new server",
|
|
|
|
default="plex-test-docker-%s" % str(uuid4()),
|
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
2022-02-27 03:26:08 +00:00
|
|
|
"--accept-eula", help="Accept Plex's EULA", default=False, action="store_true"
|
2020-04-18 19:50:04 +00:00
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
|
|
|
"--without-movies",
|
|
|
|
help="Do not create Movies section",
|
|
|
|
default=True,
|
|
|
|
dest="with_movies",
|
|
|
|
action="store_false",
|
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
|
|
|
"--without-shows",
|
|
|
|
help="Do not create TV Shows section",
|
|
|
|
default=True,
|
|
|
|
dest="with_shows",
|
|
|
|
action="store_false",
|
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
|
|
|
"--without-music",
|
|
|
|
help="Do not create Music section",
|
|
|
|
default=True,
|
|
|
|
dest="with_music",
|
|
|
|
action="store_false",
|
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
|
|
|
"--without-photos",
|
|
|
|
help="Do not create Photos section",
|
|
|
|
default=True,
|
|
|
|
dest="with_photos",
|
|
|
|
action="store_false",
|
|
|
|
) # noqa
|
|
|
|
parser.add_argument(
|
|
|
|
"--show-token",
|
|
|
|
help="Display access token after bootstrap",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
) # noqa
|
2020-12-02 20:42:51 +00:00
|
|
|
opts, _ = parser.parse_known_args()
|
2019-10-16 02:23:25 +00:00
|
|
|
|
|
|
|
account = get_plex_account(opts)
|
2018-09-08 15:27:34 +00:00
|
|
|
path = os.path.realpath(os.path.expanduser(opts.destination))
|
2020-04-18 19:50:04 +00:00
|
|
|
media_path = os.path.join(path, "media")
|
|
|
|
makedirs(media_path, exist_ok=True)
|
|
|
|
|
|
|
|
# Download the Plex Docker image
|
|
|
|
if opts.no_docker is False:
|
|
|
|
print(
|
|
|
|
"Creating Plex instance named %s with advertised ip %s"
|
|
|
|
% (opts.server_name, opts.advertise_ip)
|
|
|
|
)
|
|
|
|
if which("docker") is None:
|
|
|
|
print("Docker is required to be available")
|
|
|
|
exit(1)
|
|
|
|
if call(["docker", "pull", "plexinc/pms-docker:%s" % opts.docker_tag]) != 0:
|
|
|
|
print("Got an error when executing docker pull!")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
# Start the Plex Docker container
|
|
|
|
|
|
|
|
arg_bindings = {
|
|
|
|
"destination": path,
|
|
|
|
"hostname": opts.server_name,
|
|
|
|
"claim_token": account.claimToken() if account else "",
|
|
|
|
"timezone": opts.timezone,
|
2021-08-26 01:05:49 +00:00
|
|
|
"language": opts.language,
|
2020-04-18 19:50:04 +00:00
|
|
|
"advertise_ip": opts.advertise_ip,
|
|
|
|
"image_tag": opts.docker_tag,
|
|
|
|
"container_name_extra": "" if account else "unclaimed-",
|
|
|
|
}
|
|
|
|
docker_cmd = [c % arg_bindings for c in DOCKER_CMD]
|
|
|
|
exit_code = call(docker_cmd)
|
|
|
|
if exit_code != 0:
|
|
|
|
raise SystemExit(
|
|
|
|
"Error %s while starting the Plex docker container" % exit_code
|
|
|
|
)
|
2018-09-08 15:27:34 +00:00
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
# Wait for the Plex container to start
|
2020-04-18 19:50:04 +00:00
|
|
|
print("Waiting for the Plex to start..")
|
2019-10-16 02:23:25 +00:00
|
|
|
start = time.time()
|
|
|
|
runtime = 0
|
2018-09-08 15:27:34 +00:00
|
|
|
server = None
|
2019-10-16 02:23:25 +00:00
|
|
|
while not server and (runtime < opts.bootstrap_timeout):
|
2018-09-08 15:27:34 +00:00
|
|
|
try:
|
2018-09-15 08:42:42 +00:00
|
|
|
if account:
|
2019-10-16 02:23:25 +00:00
|
|
|
server = account.device(opts.server_name).connect()
|
2018-09-15 08:42:42 +00:00
|
|
|
else:
|
2020-04-18 19:50:04 +00:00
|
|
|
server = PlexServer("http://%s:32400" % opts.advertise_ip)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
except Exception as err:
|
|
|
|
print(err)
|
|
|
|
time.sleep(1)
|
2020-12-16 04:26:58 +00:00
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
runtime = time.time() - start
|
2020-12-16 04:26:58 +00:00
|
|
|
|
2018-09-08 15:27:34 +00:00
|
|
|
if not server:
|
2020-04-18 19:50:04 +00:00
|
|
|
raise SystemExit(
|
2022-02-27 03:26:08 +00:00
|
|
|
"Server didn't appear in your account after %ss" % opts.bootstrap_timeout
|
2020-04-18 19:50:04 +00:00
|
|
|
)
|
2020-12-16 04:26:58 +00:00
|
|
|
|
2022-02-20 01:16:37 +00:00
|
|
|
print("Plex container started after %ss" % int(runtime))
|
|
|
|
print("Plex server version %s" % server.version)
|
2018-09-08 15:27:34 +00:00
|
|
|
|
2020-12-16 04:26:58 +00:00
|
|
|
if opts.accept_eula:
|
|
|
|
server.settings.get("acceptedEULA").set(True)
|
|
|
|
# Disable settings for background tasks when using the test server.
|
|
|
|
# These tasks won't work on the test server since we are using fake media files
|
|
|
|
if not opts.unclaimed and account and account.subscriptionActive:
|
|
|
|
server.settings.get("GenerateIntroMarkerBehavior").set("never")
|
2023-03-09 21:32:17 +00:00
|
|
|
server.settings.get("GenerateCreditsMarkerBehavior").set("never")
|
2020-12-16 04:26:58 +00:00
|
|
|
server.settings.get("GenerateBIFBehavior").set("never")
|
|
|
|
server.settings.get("GenerateChapterThumbBehavior").set("never")
|
|
|
|
server.settings.get("LoudnessAnalysisBehavior").set("never")
|
|
|
|
server.settings.save()
|
|
|
|
|
2018-09-08 15:27:34 +00:00
|
|
|
sections = []
|
2020-04-18 19:50:04 +00:00
|
|
|
|
2022-02-27 03:26:08 +00:00
|
|
|
# Lets add a check here do somebody don't mess up
|
2020-04-18 19:50:04 +00:00
|
|
|
# there normal server if they run manual tests.
|
2020-04-29 21:23:22 +00:00
|
|
|
# Like i did....
|
|
|
|
if len(server.library.sections()) and opts.no_docker is True:
|
|
|
|
ans = input(
|
|
|
|
"The server has %s sections, do you wish to remove it?\n> "
|
|
|
|
% len(server.library.sections())
|
2020-04-18 19:50:04 +00:00
|
|
|
)
|
2020-04-29 21:23:22 +00:00
|
|
|
if ans in ("y", "Y", "Yes"):
|
|
|
|
ans = input(
|
|
|
|
"Are you really sure you want to delete %s libraries? There is no way back\n> "
|
|
|
|
% len(server.library.sections())
|
|
|
|
)
|
|
|
|
if ans in ("y", "Y", "Yes"):
|
|
|
|
clean_pms(server, path)
|
|
|
|
else:
|
|
|
|
raise ExistingSection()
|
|
|
|
else:
|
|
|
|
raise ExistingSection()
|
2020-04-18 19:50:04 +00:00
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
# Prepare Movies section
|
2018-09-08 15:27:34 +00:00
|
|
|
if opts.with_movies:
|
2020-04-18 19:50:04 +00:00
|
|
|
movies_path = os.path.join(media_path, "Movies")
|
|
|
|
num_movies = setup_movies(movies_path)
|
|
|
|
sections.append(
|
|
|
|
dict(
|
|
|
|
name="Movies",
|
|
|
|
type="movie",
|
2020-04-29 12:51:55 +00:00
|
|
|
location="/data/Movies" if opts.no_docker is False else movies_path,
|
2021-01-25 02:03:58 +00:00
|
|
|
agent="tv.plex.agents.movie",
|
|
|
|
scanner="Plex Movie",
|
2021-05-10 04:27:52 +00:00
|
|
|
language="en-US",
|
2020-04-18 19:50:04 +00:00
|
|
|
expected_media_count=num_movies,
|
|
|
|
)
|
|
|
|
)
|
2018-09-08 15:27:34 +00:00
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
# Prepare TV Show section
|
2018-09-08 15:27:34 +00:00
|
|
|
if opts.with_shows:
|
2020-04-18 19:50:04 +00:00
|
|
|
tvshows_path = os.path.join(media_path, "TV-Shows")
|
|
|
|
num_ep = setup_show(tvshows_path)
|
|
|
|
|
|
|
|
sections.append(
|
|
|
|
dict(
|
|
|
|
name="TV Shows",
|
|
|
|
type="show",
|
2020-04-29 12:51:55 +00:00
|
|
|
location="/data/TV-Shows" if opts.no_docker is False else tvshows_path,
|
2021-05-10 04:27:52 +00:00
|
|
|
agent="tv.plex.agents.series",
|
|
|
|
scanner="Plex TV Series",
|
|
|
|
language="en-US",
|
2020-04-18 19:50:04 +00:00
|
|
|
expected_media_count=num_ep,
|
|
|
|
)
|
|
|
|
)
|
2018-09-08 15:27:34 +00:00
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
# Prepare Music section
|
2018-09-08 15:27:34 +00:00
|
|
|
if opts.with_music:
|
2020-04-18 19:50:04 +00:00
|
|
|
music_path = os.path.join(media_path, "Music")
|
2022-12-21 19:32:43 +00:00
|
|
|
song_c = setup_music(music_path, docker=not opts.no_docker)
|
2020-04-18 19:50:04 +00:00
|
|
|
|
|
|
|
sections.append(
|
|
|
|
dict(
|
|
|
|
name="Music",
|
|
|
|
type="artist",
|
2020-04-29 12:51:55 +00:00
|
|
|
location="/data/Music" if opts.no_docker is False else music_path,
|
2020-09-16 22:58:36 +00:00
|
|
|
agent="tv.plex.agents.music",
|
|
|
|
scanner="Plex Music",
|
2023-07-21 19:56:58 +00:00
|
|
|
language="en-US",
|
2020-04-18 19:50:04 +00:00
|
|
|
expected_media_count=song_c,
|
|
|
|
)
|
|
|
|
)
|
2018-09-08 15:27:34 +00:00
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
# Prepare Photos section
|
2018-09-08 15:27:34 +00:00
|
|
|
if opts.with_photos:
|
2020-04-18 19:50:04 +00:00
|
|
|
photos_path = os.path.join(media_path, "Photos")
|
|
|
|
has_photos = setup_images(photos_path)
|
|
|
|
|
|
|
|
sections.append(
|
|
|
|
dict(
|
|
|
|
name="Photos",
|
|
|
|
type="photo",
|
2020-04-29 12:51:55 +00:00
|
|
|
location="/data/Photos" if opts.no_docker is False else photos_path,
|
2020-04-18 19:50:04 +00:00
|
|
|
agent="com.plexapp.agents.none",
|
|
|
|
scanner="Plex Photo Scanner",
|
|
|
|
expected_media_count=has_photos,
|
|
|
|
)
|
|
|
|
)
|
2018-09-14 18:03:23 +00:00
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
# Create the Plex library in our instance
|
2018-09-08 15:27:34 +00:00
|
|
|
if sections:
|
2020-04-18 19:50:04 +00:00
|
|
|
print("Creating the Plex libraries on %s" % server.friendlyName)
|
2018-09-08 15:27:34 +00:00
|
|
|
for section in sections:
|
2019-10-16 02:44:10 +00:00
|
|
|
create_section(server, section, opts)
|
2018-09-14 18:03:23 +00:00
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
# Share this instance with the specified username
|
2018-09-15 08:42:42 +00:00
|
|
|
if account:
|
2020-04-18 19:50:04 +00:00
|
|
|
shared_username = os.environ.get("SHARED_USERNAME", "PKKid")
|
2018-09-15 08:42:42 +00:00
|
|
|
try:
|
|
|
|
user = account.user(shared_username)
|
|
|
|
account.updateFriend(user, server)
|
2020-04-18 19:50:04 +00:00
|
|
|
print("The server was shared with user %s" % shared_username)
|
2018-09-15 08:42:42 +00:00
|
|
|
except NotFound:
|
|
|
|
pass
|
2018-09-08 15:27:34 +00:00
|
|
|
|
2019-10-16 02:23:25 +00:00
|
|
|
# Finished: Display our Plex details
|
2020-04-18 19:50:04 +00:00
|
|
|
print("Base URL is %s" % server.url("", False))
|
2018-09-15 08:42:42 +00:00
|
|
|
if account and opts.show_token:
|
2020-04-18 19:50:04 +00:00
|
|
|
print("Auth token is %s" % account.authenticationToken)
|
|
|
|
print("Server %s is ready to use!" % opts.server_name)
|