Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active June 8, 2017 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyWong16/e6ac91c8e5208a811e872dcd5ddf406b to your computer and use it in GitHub Desktop.
Save JonnyWong16/e6ac91c8e5208a811e872dcd5ddf406b to your computer and use it in GitHub Desktop.
Create a Plex movies library using the API
import platform
import requests
from uuid import getnode
'''
Available options to create a movie library:
agent: com.plexapp.agents.imdb # Freebase
com.plexapp.agents.themoviedb # The Movie Database
com.plexapp.agents.none # Personal Media
scanner: Plex Movie Scanner
Plex Video Files Scanner
'''
options = {'name': 'Movies Library', # Then name of the library
'type': 'movie',
'agent': 'com.plexapp.agents.imdb',
'scanner': 'Plex Movie Scanner',
'language': 'en',
'importFromiTunes': '',
'location': 'C:\Users\Name\Videos\Movies' # Absolute path to movies folder
}
# Plex server settings
host = '127.0.0.1'
port = 32400
token = '<token>'
ssl = '' # 's' or ''
def fetch(path, t='GET', params=None):
url = 'http%s://%s:%s/' % (ssl, host, port)
headers = {'X-Plex-Token': token,
'Accept': 'application/json',
'X-Plex-Provides': 'controller',
'X-Plex-Platform': platform.uname()[0],
'X-Plex-Platform-Version': platform.uname()[2],
'X-Plex-Product': 'Custom Script',
'X-Plex-Version': '0.0.1',
'X-Plex-Device': platform.platform(),
'X-Plex-Client-Identifier': str(hex(getnode()))
}
try:
if t == 'GET':
r = requests.get(url + path, headers=headers, params=params, verify=False)
elif t == 'POST':
r = requests.post(url + path, headers=headers, params=params, verify=False)
elif t == 'DELETE':
r = requests.delete(url + path, headers=headers, params=params, verify=False)
if r and len(r.content): # incase it dont return anything
return r.json()
else:
return r.content
except Exception as e:
return e
if __name__ == '__main__':
print fetch('library/sections', 'POST', options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment