mirror of
https://github.com/hrfee/jellyfin-accounts.git
synced 2024-12-22 17:10:11 +00:00
Speed up interface through adding caching
jf_api now caches the list of users, only fetching new data if the cache is more than 30 minutes old. Also remove the pointless tempJF with an instance named auth_jf since jf_api can now handle multiple authentications.
This commit is contained in:
parent
17aca69583
commit
8497cd4927
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import requests
|
import requests
|
||||||
|
import time
|
||||||
|
|
||||||
class Error(Exception):
|
class Error(Exception):
|
||||||
pass
|
pass
|
||||||
@ -20,6 +20,9 @@ class Jellyfin:
|
|||||||
self.version = version
|
self.version = version
|
||||||
self.device = device
|
self.device = device
|
||||||
self.deviceId = deviceId
|
self.deviceId = deviceId
|
||||||
|
self.timeout = 30 * 60
|
||||||
|
self.userCacheAge = time.time() - self.timeout - 1
|
||||||
|
self.userCachePublicAge = self.userCacheAge
|
||||||
self.useragent = f"{self.client}/{self.version}"
|
self.useragent = f"{self.client}/{self.version}"
|
||||||
self.auth = "MediaBrowser "
|
self.auth = "MediaBrowser "
|
||||||
self.auth += f"Client={self.client}, "
|
self.auth += f"Client={self.client}, "
|
||||||
@ -37,20 +40,32 @@ class Jellyfin:
|
|||||||
}
|
}
|
||||||
def getUsers(self, username="all", id="all", public=True):
|
def getUsers(self, username="all", id="all", public=True):
|
||||||
if public is True:
|
if public is True:
|
||||||
response = requests.get(self.server+"/emby/Users/Public").json()
|
if (time.time() - self.userCachePublicAge) >= self.timeout:
|
||||||
elif public is False and hasattr(self, 'username') and hasattr(self, 'password'):
|
response = requests.get(self.server+"/emby/Users/Public").json()
|
||||||
response = requests.get(self.server+"/emby/Users",
|
self.userCachePublic = response
|
||||||
headers=self.header,
|
self.userCachePublicAge = time.time()
|
||||||
params={'Username': self.username,
|
|
||||||
'Pw': self.password})
|
|
||||||
if response.status_code == 200:
|
|
||||||
response = response.json()
|
|
||||||
else:
|
else:
|
||||||
try:
|
response = self.userCachePublic
|
||||||
self.authenticate(self.username, self.password)
|
elif (public is False and
|
||||||
return self.getUsers(username, id, public)
|
hasattr(self, 'username') and
|
||||||
except self.AuthenticationError:
|
hasattr(self, 'password')):
|
||||||
raise self.AuthenticationRequiredError
|
if (time.time() - self.userCacheAge) >= self.timeout:
|
||||||
|
response = requests.get(self.server+"/emby/Users",
|
||||||
|
headers=self.header,
|
||||||
|
params={'Username': self.username,
|
||||||
|
'Pw': self.password})
|
||||||
|
if response.status_code == 200:
|
||||||
|
response = response.json()
|
||||||
|
self.userCache = response
|
||||||
|
self.userCacheAge = time.time()
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
self.authenticate(self.username, self.password)
|
||||||
|
return self.getUsers(username, id, public)
|
||||||
|
except self.AuthenticationError:
|
||||||
|
raise self.AuthenticationRequiredError
|
||||||
|
else:
|
||||||
|
response = self.userCache
|
||||||
else:
|
else:
|
||||||
raise self.AuthenticationRequiredError
|
raise self.AuthenticationRequiredError
|
||||||
if username == "all" and id == "all":
|
if username == "all" and id == "all":
|
||||||
|
@ -11,13 +11,11 @@ from __main__ import auth_log as log
|
|||||||
from jellyfin_accounts.jf_api import Jellyfin
|
from jellyfin_accounts.jf_api import Jellyfin
|
||||||
from jellyfin_accounts.web_api import jf
|
from jellyfin_accounts.web_api import jf
|
||||||
|
|
||||||
|
auth_jf = Jellyfin(config['jellyfin']['server'],
|
||||||
def tempJF():
|
config['jellyfin']['client'],
|
||||||
return Jellyfin(config['jellyfin']['server'],
|
config['jellyfin']['version'],
|
||||||
config['jellyfin']['client'],
|
config['jellyfin']['device'],
|
||||||
config['jellyfin']['version'],
|
config['jellyfin']['device_id'] + '_authClient')
|
||||||
config['jellyfin']['device'] + '_temp',
|
|
||||||
config['jellyfin']['device_id'] + '_temp')
|
|
||||||
|
|
||||||
class Account():
|
class Account():
|
||||||
def __init__(self, username=None, password=None):
|
def __init__(self, username=None, password=None):
|
||||||
@ -35,9 +33,8 @@ class Account():
|
|||||||
if not self.jf:
|
if not self.jf:
|
||||||
return pwd_context.verify(password, self.password_hash)
|
return pwd_context.verify(password, self.password_hash)
|
||||||
else:
|
else:
|
||||||
temp_jf = tempJF()
|
|
||||||
try:
|
try:
|
||||||
return temp_jf.authenticate(self.username, password)
|
return auth_jf.authenticate(self.username, password)
|
||||||
except Jellyfin.AuthenticationError:
|
except Jellyfin.AuthenticationError:
|
||||||
return False
|
return False
|
||||||
def generate_token(self, expiration=1200):
|
def generate_token(self, expiration=1200):
|
||||||
@ -107,7 +104,6 @@ def verify_password(username, password):
|
|||||||
else:
|
else:
|
||||||
user = accounts['adminAccount']
|
user = accounts['adminAccount']
|
||||||
verified = Account().verify_token(username, accounts)
|
verified = Account().verify_token(username, accounts)
|
||||||
|
|
||||||
if not verified:
|
if not verified:
|
||||||
if username == user.username and user.verify_password(password):
|
if username == user.username and user.verify_password(password):
|
||||||
g.user = user
|
g.user = user
|
||||||
|
Loading…
Reference in New Issue
Block a user