Skip to content

Commit

Permalink
Shortened Plex push output and changes from cjnaz
Browse files Browse the repository at this point in the history
  • Loading branch information
XDGFX committed Jan 7, 2020
1 parent e1397c7 commit d30dbe8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
59 changes: 37 additions & 22 deletions PPP.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# --- PPP (Plex Playlist Pusher) v3.0.2 ---
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# --- PPP (Plex Playlist Pusher) v3.0.3 ---
# Synchronises playlists between local files (.m3u) and Plex playlists.
# If there are differences between local and Plex playlists, both will be
# merged and duplicates deleted; meaning tracks can be added on one and
Expand All @@ -14,6 +17,9 @@
# 23/03/19 v2.1.2 Fixed v2.1 and v2.1.1 releases, no longer using tempfile
# 30/03/19 v2.1.3 Added timestamp and improved character support
# 20/12/19 v3.0.0 MAJOR REWRITE: Added setup procedure and UNIX / Windows compatibility
# 30/12/19 v3.0.1 Added ability to ignore SSL certificates
# 02/01/20 v3.0.2 Fixed prepend conversion when PPP and playlist machine not same type
# 07/01/20 v3.0.3 Touches and tweaks by cjnaz

# Uses GNU General Public License

Expand Down Expand Up @@ -122,12 +128,10 @@ def plexPlaylist(server_url, plex_token, key, check_ssl):


def setupVariables():
import getpass

# Remove variables.json if it already exists
if os.path.isfile('variables.json'):
try:
shutil.rmtree('variables.json')
os.remove('variables.json')
except Exception as e:
print(
"ERROR: I couldn't remove existing variables.json. Try deleting manually?")
Expand All @@ -153,30 +157,30 @@ def setupVariables():
# SERVER URL
print("First things first... what is your Plex server URL, as seen by " +
"PPP? It must include port, in the form '192.198.1.10:32400'")
server_url = input("Please enter your server URL: ")
server_url = input("Please enter your server URL: ").strip()

if not re.match('(?:http|https)://', server_url):
server_url = "http://" + server_url

br()

# Regex to check URL with port
if re.compile("^(?:http|https)://[\\d.]+:\\d+$").match(server_url) is None:
if re.compile(r"^(?:http|https)://[\d.\w]+:[\d]+$").match(server_url) is None:
input("WARNING: Entered URL '" + server_url + "' does not appear to follow the correct format!\n" +
"If you believe this is a mistake, press enter to continue... ")
"If you believe the entered URL is correct, press enter to continue (else ^C and start over)... ")

br()

# PLEX TOKEN
print("Next we need your Plex Token. You can find this by following these instructions: https://bit.ly/2p7RtOu")
plex_token = getpass.getpass("Please enter your Plex Token: ")
plex_token = input("Please enter your Plex Token: ").strip()

br()

# Regex to check token
if re.compile("^[A-Za-z1-9]+$").match(plex_token) is None:
input("WARNING: Entered token '" + plex_token + "' does not appear to follow the correct format!\n" +
"If you believe this is a mistake, press enter to continue... ")
"If you believe the entered token is correct, press enter to continue (else ^C and start over)... ")

br()

Expand All @@ -197,13 +201,19 @@ def setupVariables():

br()

print("Fetching sample playlist to determine prepend...")
_, playlist = plexPlaylist(server_url, plex_token, keys[0], check_ssl)
if len(keys) > 0:
print("Fetching sample playlist to determine prepend...")
_, playlist = plexPlaylist(server_url, plex_token, keys[0], check_ssl)

plex_unix = playlist[0].startswith("/")
plex_unix = playlist[0].startswith("/")

print("It looks like your Plex machine uses %s paths" %
("UNIX" if plex_unix else "Windows"))
print("It looks like your Plex machine uses %s paths" %
("UNIX" if plex_unix else "Windows"))

else:
print("At least one playlist must exist in Plex in order to determine the Plex machine type.")
print("Create a dummy playlist in Plex containing at least two titles from different artists, then rerun this script.")
raise SystemExit

# Convert from Windows to UNIX paths
if plex_unix != ppp_unix:
Expand Down Expand Up @@ -339,6 +349,9 @@ def getArguments():
parser.add_argument('-retention', metavar='n', type=int, nargs=1, default=10,
help='Number of previous local playlist backups to keep (Default 10)')

parser.add_argument('-nocleanup', action='store_true',
help='Disable removal of .tmp directory (for debug)')

return parser.parse_args()


Expand Down Expand Up @@ -436,7 +449,7 @@ def stripPrepend(path, prepend, invert):
# --- MAIN ---

# Setup version and get any arguments passed to PPP
vers = "v3.0.2"
vers = "v3.0.3"
args = getArguments()

print("""
Expand Down Expand Up @@ -658,7 +671,8 @@ def stripPrepend(path, prepend, invert):
response = requests.post(url, data="", headers=headers, params=querystring, verify=check_ssl)

# Should return nothing but if there's an issue there may be an error shown
print(response.text)
if not response.text == '':
print(response.text)

br()

Expand All @@ -684,9 +698,10 @@ def stripPrepend(path, prepend, invert):

br()

try:
shutil.rmtree('.tmp')
print('Complete!\n')
except shutil.Error as e:
print("Program complete, but I had trouble cleaning .tmp directory. Check it's not open somewhere else \n ERROR: %s" % e)
raise SystemExit
if not args.nocleanup:
try:
shutil.rmtree('.tmp')
print('Complete!\n')
except shutil.Error as e:
print("Program complete, but I had trouble cleaning .tmp directory. Check it's not open somewhere else \n ERROR: %s" % e)
raise SystemExit
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ If you want to delete a playlist or song from a playlist, it must be removed fro
4. Run PPP with Python 3

```
PPP.py [-h] [-setup] [-nobackups] [-retention n]
usage: PPP.py [-h] [-setup] [-nobackups] [-retention n] [-nocleanup]
optional arguments:
-h, --help show this help message and exit
-setup Force-run the setup procedure
-nobackups Disable backup of local playlists completely!
-retention n Number of previous local playlist backups to keep (Default 10)
-nocleanup Disable removal of .tmp directory (for debugging only)
```
---

Expand Down

0 comments on commit d30dbe8

Please sign in to comment.