Jump to content

Bash Script for Active Sessions / Currently Logged in Users


AcidRainX

Recommended Posts

AcidRainX

Hi!

I'm new in Emby, and I'm absoluetly beginner in bash...
But, maybe someone can help me, o better, some has a example for bash (curl?) for this:

I am trying to make a bash script for getting active users (currently logged in / last activity within last 20 minutes) from Emby?
It's for a script for Unraid, for helping not going to standby if a user is logged / had an activity wihtin the last 20 (or variable) minutes.
Would be nice, if this is working / authenticating in Emby an api key...

@chef

 

best regards

Link to comment
Share on other sites

You probably want to access two different end point.

 

"/sessions" will give you all the current session data, such as users, clients, now playing, etc.

The other ebdpoint you want to access is the "/users" endpoint 

This will give you user data. 

 

When you access sessions you will get limited user data for that session.

But you can extended the data, by getting the "userId" from sessions, and then requesting "/users/{userId}".

That should return last active date etc.

 You'll need an API key if you are not building a client object.

You can create an API key in the dashboard, and use "&api_key=" (or something similar to that).

Edited by chef
  • Like 1
Link to comment
Share on other sites

I should also mention that just requesting "/users" will return all the user data.

You could then iterate the json and find the user by name.

Link to comment
Share on other sites

  • 9 months later...
requa3r0

@AcidRainX

I am trying to do the exact same thing.

But I have no idea how the API works.

Did you solve it, and Can you post a snippet of code that pull the information if a user is logged in on the server.

Thanks.

 

Edited by requa3r0
Link to comment
Share on other sites

requa3r0

Hi @Luke Thanks for trying to point me in the right direction - but its not really helpful. I have been down that road an given up completely.

 

The API makes no sense to me

NB: If I go to the API from dashboard i am redirected to  https://swagger.emby.media/  with this error

Failed to load API definition.

Errors

Fetch error

NetworkError when attempting to fetch resource. http://192.168.1.100:8096/emby/openapi?serverUrl=http%3A%2F%2F192.168.1.100%3A8096

Fetch error

Possible mixed-content issue? The page was loaded over https:// but a http:// URL was specified. Check that you are not attempting to load mixed content.

If i remove the S in https, I get the APIO webpage with a bunch of categories

ActivityLogService

ArtistsService

BifService

ETC

But I have no idea where to find the necessary active user information.

I looked under UserService

And I can curl a list of users, but cannot differentiate if one is active or not.

Can you perhaps give me the correct link, that I can use to curl to identify if someone/any user is logged in, for my power saver script on my server ;O)

the API is beyond me to understand.

 

All I need to know is if any user is logged in or not. Then Ill prevent the server standby until the next hour.

 

Thanks for all you help.

Link to comment
Share on other sites

If I may,

You'll need an API key to do this.

End you url request with "&api_key=", then put your key there.

 

1. Request /sessions

If there are any open sessions, then you can assume that you have logged in user.

To get users specifically that are logged in. Iterate the session object that is returned, and look for the "UserId" key in the JSON. The value will be their ID.

 

To get further information about the currently logged in user make a new request to the server for each UserId you find.

"Users/{userId}"

 

This will return the full user object.

 

 

Edited by chef
Link to comment
Share on other sites

  • 3 weeks later...
requa3r0

Thanks for your reply. @chef

I do not fully agree with your assumption, or I misunderstand how to interpret the API

 

1. Request /sessions

If there are any open sessions, then you can assume that you have logged in user.

 

When I load the Sessions API with a token. and all users are logged out, I still have 2 active sessions

0: Seems to be the server session

1: is my Mobil phone where emby for android is logged in but not active

2: is my LG TV app also logged in but not active

If I log in on chrome I get a 3 hit on the API request , but its similar to the 2 above.

How do I distinguish between an inactive app user that is not logged out, and  a user is actually logged in and actively browsing the emby server.

This can not be done with the Sessions API. I see no difference on a inactive logged in app user, and an active one.

The use-case is to prevent the server from sleeping, if you start browsing at 13:55 and then at 1400 the cron script check for server activity and network activity (with a 10mb threshold) to suspend if inactive.

The server will suspend and kill the active session when the user is still just browsing..if a stream has been started, the network threshold is met and it will stay alive.

I can not lower the treshold, then the server will not suspend, because there is always some traffic etc on a server that is not in use.

 

Would be great if the APT could be used for this use-case.

Can you make a specific API request  just to return a boolean if any user is actually active.

 

Link to comment
Share on other sites

Cool. The session object should have userId. You might be able to query lastActiveDate as well.

 

Edited by chef
Link to comment
Share on other sites

  • 5 months later...
embylad892746

I just wrote a simple python script to check for any active emby users within the last 10 minutes. You just need to update the top 2 variables (api token and emby url).

I call this script from my backup script, so that i can avoid shutting down all my docker containers while people are using emby. I don't bother with the Sessions endpoint as it's not needed. Simply checking the LastActivityDate works very effectively.

 

import json
import os
import requests
import dateutil.parser as dp
from datetime import datetime, timedelta, timezone

'''
Checks for any active emby users within last x minutes.
Prints True if active users found, otherwise False.
'''


api_token = "<api token>"
emby_url  = "<emby url>"
endpoint  = "Users"
minutes   = 10

url       = os.path.join(emby_url, "emby", endpoint, "?api_key=" + api_token)
headers   = {'Accept': 'application/json'}
response  = requests.get(url, headers=headers)
json_data = json.loads(response.text)

now = datetime.now(timezone.utc)

active_status = False
for user in json_data:
  last_active = dp.isoparse(user["LastActivityDate"])
  if now - timedelta(minutes=minutes) <= last_active <= now:
    active_status = True
    break

print(active_status)

 

Edited by embylad892746
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...