Skip to content

Instantly share code, notes, and snippets.

@nagleaidan
Created November 4, 2019 23:17
Show Gist options
  • Save nagleaidan/dcc132c16d15565d88bf2d9200351c6e to your computer and use it in GitHub Desktop.
Save nagleaidan/dcc132c16d15565d88bf2d9200351c6e to your computer and use it in GitHub Desktop.
Run plex through ngrok
#!/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 = "john.doe@hotmail.com"
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)
@nagleaidan
Copy link
Author

@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.

@origamiofficial
Copy link

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