Created
November 4, 2019 23:17
-
-
Save nagleaidan/dcc132c16d15565d88bf2d9200351c6e to your computer and use it in GitHub Desktop.
Run plex through ngrok
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/python3 | |
from plexapi.myplex import MyPlexAccount | |
import sys | |
import json | |
import requests | |
import time | |
# need time for ngrok to start up before requesting data | |
print("Waiting 15 seconds for ngrok to start up") | |
time.sleep(15) | |
def get_ngrok_port(): | |
url = "http://localhost:4040/api/tunnels" | |
res = requests.get(url) | |
res_unicode = res.content.decode("utf-8") | |
res_json = json.loads(res_unicode) | |
for i in res_json["tunnels"]: | |
if i['proto'] == 'tcp': | |
return i['public_url'].split(':')[-1] | |
def build_url_list(): | |
NGROK_PORT = get_ngrok_port() | |
NGROK_BASES = ["https://0.tcp.ngrok.io:", "https://18.216.53.253:", | |
"https://52.15.62.13:", "https://52.15.72.79:"] | |
NGROK_URLS = map(lambda base: base + NGROK_PORT, NGROK_BASES) | |
CUSTOM_URL = "" | |
for url in NGROK_URLS: | |
CUSTOM_URL += url + ", " | |
CUSTOM_URL = CUSTOM_URL[:-2] | |
return CUSTOM_URL | |
# Load user defined config" | |
PLEX_USER = "[email protected]" | |
PLEX_PWORD = "Hunter2" | |
PLEX_SERVER = "myServer" | |
# stores plex user login info into a variable | |
account = MyPlexAccount(PLEX_USER, PLEX_PWORD) | |
plex = account.resource(PLEX_SERVER).connect() # returns a PlexServer instance | |
# displays current plex custom url settings. Not needed but nice to see | |
print("Old settings: " + plex.settings.customConnections) | |
# sets plex's "Custom server access URLs" with one from Ngrok | |
customUrl = plex.settings.get('customConnections') | |
customUrl.set(build_url_list()) | |
plex.settings.save() | |
# displays new custom plex url from Ngrok. Not needed but nice to see | |
account = MyPlexAccount(PLEX_USER, PLEX_PWORD) | |
plex = account.resource(PLEX_SERVER).connect() # returns a PlexServer instance | |
print("New settings: " + plex.settings.customConnections) |
@Rihcus looks good! I haven't had a need to use this script in a year so I haven't worked on it at all. Thanks for taking my initial script and improving it!
Anyone who comes to this link from my old reddit post, use the forked version linked above.
I made a fork of this script here:
https://gist.github.com/anjumrafidofficial/fe3b986ff15246a47851f9d0da0e202f
The main change is this scripts will follow @Rihcus changes & also will use token instead of password.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made a fork of this script here:
https://gist.github.com/Rihcus/4a217e0092e1fa34299e395222759165
Main change is plex will not accept 0.tcp.ngrok.io urls as custom domains need to start with the plex.yourdomain.com (or .org, etc). The forked script will take the domain from the base url ping and obtain the base url ip and submite on url with port instead of four making it a bit more seamless.