-
-
Save Artiume/bba6f754b081db9a4be01ff0d4b528ad to your computer and use it in GitHub Desktop.
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
import requests # https://www.pythonforbeginners.com/requests/using-requests-in-python | |
from pprint import pprint | |
from urllib.parse import urlencode | |
# Update with your information | |
server = 'https://jellyfin.example.com' | |
username = 'testuser' | |
password = 'badpassword' | |
class API(): | |
def __init__(self, server, user, password): | |
''' | |
Initialize required variables for the API | |
and call the login function | |
''' | |
self.headers = {} | |
self.user_id = '' | |
self.server = server | |
self.user = user | |
self.password = password | |
self._login() | |
def _login(self): | |
''' | |
Performs necessary steps to login to a Jellyfin server | |
''' | |
# Endpoint for authentication | |
path = '/Users/AuthenticateByName' | |
# Authentication payload | |
auth_dict = { | |
'username': self.user, | |
'Pw': self.password | |
} | |
# Build required headers for the server | |
authorization = 'MediaBrowser , ' | |
authorization += 'Client="API Script", ' | |
authorization += 'Device="API Script", ' | |
authorization += 'DeviceId="API_Script", ' | |
authorization += 'Version="0.0.0"' | |
self.headers['x-emby-authorization'] = authorization | |
# Perform API call to login | |
auth_details = self.post(path, auth_dict) | |
# Check return values and append auth token to headers | |
if auth_details: | |
self.headers['x-mediabrowser-token'] = auth_details.get('AccessToken') | |
self.user_id = auth_details['User'].get('Id') | |
else: | |
print(f'Unable to login to {self.server}') | |
return | |
def get(self, path, params={}): | |
''' | |
Performs an http get request | |
Returns json data stored in a python dictionary | |
''' | |
r = requests.get( | |
f'{self.server}{path}', | |
data=params, | |
headers=self.headers) | |
# Check return status, return json or empty dictionary | |
if r.status_code == 200: | |
return r.json() | |
else: | |
print('Error making API call') | |
return {} | |
def post(self, path, params={}): | |
''' | |
Performs an http post request | |
Returns json data stored in a python dictionary | |
''' | |
r = requests.post( | |
f'{self.server}{path}', | |
data=params, | |
headers=self.headers) | |
# Check return status, return json or empty dictionary | |
if r.status_code == 200: | |
return r.json() | |
else: | |
print('Error making API call') | |
return {} | |
# Initialize API object | |
jellyfin = API(server, username, password) | |
# Get the user_id out of the API (needed for some API calls) | |
user_id = jellyfin.user_id | |
########### | |
# Example 1 | |
########### | |
# Pull list of libraries the user has access to | |
libraries = jellyfin.get(f'/Users/{user_id}/Views') | |
# Pretty print the list of libraries | |
pprint(libraries) | |
########### | |
# Example 2 | |
########### | |
# Build a query about what items to look for | |
# In this case, movies that have been watched | |
query = { | |
'Recursive': 'true', | |
'IncludeItemTypes': 'movie', | |
'Filters': 'IsPlayed', | |
} | |
# Format the query in urlencoded format | |
encoded_query = urlencode(query) | |
# Perform the API call with the query | |
unwatched_movies = jellyfin.get(f'/Users/{user_id}/Items?{encoded_query}') | |
# Pretty print the the entire api result | |
pprint(unwatched_movies) | |
# Print only movie names | |
for movie in unwatched_movies['Items']: | |
print(movie.get('Name')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment