Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active December 7, 2023 11:06
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save JonnyWong16/f8139216e2748cb367558070c1448636 to your computer and use it in GitHub Desktop.
Save JonnyWong16/f8139216e2748cb367558070c1448636 to your computer and use it in GitHub Desktop.
Automatically share and unshare libraries for Plex users
# Run this script using "share" or "unshare" as arguments:
# To share the Plex libraries:
# python share_unshare_libraries.py share
# To unshare the Plex libraries:
# python share_unshare_libraries.py unshare
import requests
import sys
from xml.dom import minidom
## EDIT THESE SETTINGS ###
PLEX_TOKEN = "ENTER_YOUR_PLEX_TOKEN_HERE"
SERVER_ID = "ENTER_YOUR_SERVER_ID_HERE" # Example: https://i.imgur.com/EjaMTUk.png
# Get the User IDs and Library IDs from
# https://plex.tv/api/servers/SERVER_ID/shared_servers
# Example: https://i.imgur.com/yt26Uni.png
# Enter the User IDs and Library IDs in this format below:
# {UserID1: [LibraryID1, LibraryID2],
# UserID2: [LibraryID1, LibraryID2]}
USER_LIBRARIES = {1234567: [1234567, 1234567]}
## DO NOT EDIT BELOW ##
def share():
headers = {"X-Plex-Token": PLEX_TOKEN,
"Accept": "application/json"}
url = "https://plex.tv/api/servers/" + SERVER_ID + "/shared_servers"
for user_id, library_ids in USER_LIBRARIES.iteritems():
payload = {"server_id": SERVER_ID,
"shared_server": {"library_section_ids": library_ids,
"invited_id": user_id}
}
r = requests.post(url, headers=headers, json=payload)
if r.status_code == 401:
print "Invalid Plex token"
return
elif r.status_code == 400:
print r.content
return
elif r.status_code == 200:
print "Shared libraries with user %s" % str(user_id)
return
return
def unshare():
headers = {"X-Plex-Token": PLEX_TOKEN,
"Accept": "application/json"}
url = "https://plex.tv/api/servers/" + SERVER_ID + "/shared_servers"
r = requests.get(url, headers=headers)
if r.status_code == 401:
print "Invalid Plex token"
return
elif r.status_code == 400:
print r.content
return
elif r.status_code == 200:
response_xml = minidom.parseString(r.content)
MediaContainer = response_xml.getElementsByTagName("MediaContainer")[0]
SharedServer = MediaContainer.getElementsByTagName("SharedServer")
shared_servers = {int(s.getAttribute("userID")): int(s.getAttribute("id"))
for s in SharedServer}
for user_id, library_ids in USER_LIBRARIES.iteritems():
server_id = shared_servers.get(user_id)
if server_id:
url = "https://plex.tv/api/servers/" + SERVER_ID + "/shared_servers/" + str(server_id)
r = requests.delete(url, headers=headers)
if r.status_code == 200:
print "Unshared libraries with user %s" % str(user_id)
else:
print "No libraries shared with user %s" % str(user_id)
return
if __name__ == "__main__":
if len(sys.argv) != 2:
print 'You must provide "share" or "unshare" as an argument'
elif sys.argv[1] == "share":
share()
elif sys.argv[1] == "unshare":
unshare()
@Magikarplvl4
Copy link

I got a error when i try this very amazing script :(

PlexPy Notifiers :: Script error: 
    File "D:\plexpy\scripts\stream_limiter.py", line 107 
        elif r.status_code == 200: 
              ^ 
SyntaxError: invalid syntax

@Triskelen
Copy link

i get a error :
PS C:\Users\Administrator> C:\Python27\python.exe C:\Plexpy\share_unshare_libraries.py unshare
line 7 cannot import requests

@ninthwalker
Copy link

Just posting that I also get errors. Specifically just trying with the POST code only to update a shared Library, but i get the returned response code:

Tried with some general python POSTS but kept getting returned with:

<Response code="400" status="You're already sharing this server with plexFriend. Please edit your existing share."/>

Was using the following POST:

headers = {"X-Plex-Token": 'Ycx2134yTyaJHGFxKtgezn9',
           "Accept": "application/json"}
url = "https://plex.tv/api/servers/GUte86560eJHGa7fagde5j77dd4bjgrLGHT05/shared_servers"

payload = {"server_id": "GUte86560eJHGa7fagde5j77dd4bjgrLGHT05",
           "shared_server": {"library_section_ids": "[35723967]",
                                 "invited_id": "2120385"}
          }

r = requests.post(url, headers=headers, json=payload)

So I must have been doing the request wrong.
(Tokens in code all fake)

@tweakfour17
Copy link

Hello,
I've got this to work..sort of. When I do the unshare it unshares for all 4 users, however when I do the share function it will only share for 1 of the users, and if I attempt to re-run the share it stops and says I'm already sharing. Any idea why this could be?

@timespaced
Copy link

This script works for unsharing all users, but like tweakfour17 said, it will only enable sharing for 1 user then stops. I'm too stupid to troubleshoot this - hopefully someone can figure it out.

@someredpanda
Copy link

@haltdev and @tweakfour17.

If you're still curious about why the script stops after sharing with one user; there's a return statement on line 50 that interrupts the for loop. Just remove it and it'll handle multiple users fine.

@yonderbread
Copy link

Howdy, is there an endpoint for retrieving the ID of a user by their email?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment