|
16 | 16 | from plexapi.compat import which, makedirs |
17 | 17 | from plexapi.exceptions import BadRequest, NotFound |
18 | 18 | from plexapi.myplex import MyPlexAccount |
| 19 | +from plexapi.server import PlexServer |
19 | 20 | from plexapi.utils import download, SEARCHTYPES |
20 | 21 |
|
21 | 22 | DOCKER_CMD = [ |
22 | 23 | 'docker', 'run', '-d', |
23 | | - '--name', 'plex-test-%(image_tag)s', |
| 24 | + '--name', 'plex-test-%(container_name_extra)s%(image_tag)s', |
24 | 25 | '--restart', 'on-failure', |
25 | 26 | '-p', '32400:32400/tcp', |
26 | 27 | '-p', '3005:3005/tcp', |
|
43 | 44 | ] |
44 | 45 |
|
45 | 46 |
|
46 | | -def get_claim_token(myplex): |
47 | | - """ |
48 | | - Returns a str, a new "claim-token", which you can use to register your new Plex Server instance to your account |
49 | | - See: https://hub.docker.com/r/plexinc/pms-docker/, https://www.plex.tv/claim/ |
50 | | -
|
51 | | - Arguments: |
52 | | - myplex (:class:`~plexapi.myplex.MyPlexAccount`) |
53 | | - """ |
54 | | - retry = 0 |
55 | | - status_code = None |
56 | | - while retry < 3 and status_code not in (200, 201, 204): |
57 | | - if retry > 0: |
58 | | - sleep(2) |
59 | | - response = myplex._session.get('https://plex.tv/api/claim/token.json', headers=myplex._headers(), |
60 | | - timeout=plexapi.TIMEOUT) |
61 | | - status_code = response.status_code |
62 | | - retry += 1 |
63 | | - |
64 | | - if status_code not in (200, 201, 204): |
65 | | - errtext = response.text.replace('\n', ' ') |
66 | | - raise BadRequest('(%s) unable to get status code %s; %s' % (response.status_code, response.url, errtext)) |
67 | | - return response.json()['token'] |
68 | | - |
69 | | - |
70 | 47 | def get_ips(): |
71 | 48 | import socket |
72 | 49 | return list(set([i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None) |
@@ -146,9 +123,15 @@ def alert_callback(data): |
146 | 123 | default_ip = available_ips[0] |
147 | 124 |
|
148 | 125 | parser = argparse.ArgumentParser(description=__doc__) |
149 | | - parser.add_argument('--username', help='Your Plex username') |
150 | | - parser.add_argument('--password', help='Your Plex password') |
151 | | - parser.add_argument('--token', help='Plex.tv authentication token', default=plexapi.CONFIG.get('auth.server_token')) |
| 126 | + |
| 127 | + mg = parser.add_mutually_exclusive_group() |
| 128 | + |
| 129 | + g = mg.add_argument_group() |
| 130 | + g.add_argument('--username', help='Your Plex username') |
| 131 | + g.add_argument('--password', help='Your Plex password') |
| 132 | + mg.add_argument('--token', help='Plex.tv authentication token', default=plexapi.CONFIG.get('auth.server_token')) |
| 133 | + mg.add_argument('--unclaimed', help='Do not claim the server', default=False, action='store_true') |
| 134 | + |
152 | 135 | parser.add_argument('--timezone', help='Timezone to set inside plex', default='UTC') |
153 | 136 | parser.add_argument('--destination', help='Local path where to store all the media', |
154 | 137 | default=os.path.join(os.getcwd(), 'plex')) |
@@ -176,33 +159,40 @@ def alert_callback(data): |
176 | 159 | print('Got an error when executing docker pull!') |
177 | 160 | exit(1) |
178 | 161 |
|
179 | | - if opts.token: |
180 | | - account = MyPlexAccount(token=opts.token) |
181 | | - else: |
182 | | - account = plexapi.utils.getMyPlexAccount(opts) |
| 162 | + account = None |
| 163 | + if not opts.unclaimed: |
| 164 | + if opts.token: |
| 165 | + account = MyPlexAccount(token=opts.token) |
| 166 | + else: |
| 167 | + account = plexapi.utils.getMyPlexAccount(opts) |
183 | 168 | path = os.path.realpath(os.path.expanduser(opts.destination)) |
184 | 169 | makedirs(os.path.join(path, 'media'), exist_ok=True) |
185 | 170 | arg_bindings = { |
186 | 171 | 'destination': path, |
187 | 172 | 'hostname': opts.server_name, |
188 | | - 'claim_token': get_claim_token(account), |
| 173 | + 'claim_token': account.claimToken() if account else '', |
189 | 174 | 'timezone': opts.timezone, |
190 | 175 | 'advertise_ip': opts.advertise_ip, |
191 | 176 | 'image_tag': opts.docker_tag, |
| 177 | + 'container_name_extra': '' if account else 'unclaimed-' |
192 | 178 | } |
| 179 | + |
193 | 180 | docker_cmd = [c % arg_bindings for c in DOCKER_CMD] |
194 | 181 | exit_code = call(docker_cmd) |
195 | 182 |
|
196 | 183 | if exit_code != 0: |
197 | 184 | exit(exit_code) |
198 | 185 |
|
199 | | - print('Let`s wait while the instance register in your plex account...') |
| 186 | + print('Let`s wait while the instance boots...') |
200 | 187 | start_time = time() |
201 | 188 | server = None |
202 | 189 | while not server and (time() - start_time < opts.bootstrap_timeout): |
203 | 190 | try: |
204 | | - device = account.device(opts.server_name) |
205 | | - server = device.connect() |
| 191 | + if account: |
| 192 | + device = account.device(opts.server_name) |
| 193 | + server = device.connect() |
| 194 | + else: |
| 195 | + server = PlexServer('http://%s:32400' % opts.advertise_ip) |
206 | 196 | if opts.accept_eula: |
207 | 197 | server.settings.get('acceptedEULA').set(True) |
208 | 198 | server.settings.save() |
@@ -351,18 +341,17 @@ def get_movie_path(name, year): |
351 | 341 | for section in sections: |
352 | 342 | create_section(server, section) |
353 | 343 |
|
354 | | - import logging |
355 | | - logging.basicConfig(level=logging.INFO) |
356 | | - shared_username = os.environ.get('SHARED_USERNAME', 'PKKid') |
357 | | - try: |
358 | | - user = account.user(shared_username) |
359 | | - account.updateFriend(user, server) |
360 | | - print('The server was shared with user "%s"' % shared_username) |
361 | | - except NotFound: |
362 | | - pass |
| 344 | + if account: |
| 345 | + shared_username = os.environ.get('SHARED_USERNAME', 'PKKid') |
| 346 | + try: |
| 347 | + user = account.user(shared_username) |
| 348 | + account.updateFriend(user, server) |
| 349 | + print('The server was shared with user "%s"' % shared_username) |
| 350 | + except NotFound: |
| 351 | + pass |
363 | 352 |
|
364 | 353 | print('Base URL is %s' % server.url('', False)) |
365 | | - if opts.show_token: |
| 354 | + if account and opts.show_token: |
366 | 355 | print('Auth token is %s' % account.authenticationToken) |
367 | 356 |
|
368 | 357 | print('Server %s is ready to use!' % opts.server_name) |
0 commit comments