Jump to content

Trigger a scheduled task (scan media library)


TheShanMan

Recommended Posts

TheShanMan

Is there a rest api that will allow me to tell the server to start scanning for new content (or better yet, to specify the specific location of the new content)? I would love to be able to add this into the script I use whenever I get something new rather than relying on the server to eventually discover it on its own.

Link to comment
Share on other sites

TheShanMan

Thanks! I have that working but it seems like logging the user out via /sessions/logout post does nothing. I get a 204 response with message "Connection #0 to host <myhost> left intact" and I can keep using the auth token. What am I doing wrong?

curl -v -d "" -H "X-MediaBrowser-Token: 74d68efd75ce4846b3865505290baec3" http://<myhost>:8096/sessions/logout

Also, any chance of adding some optional parameters to /library/refresh to make it do a more targeted rescan since my script knows exactly what was added and where? Not a big deal, but since rescans take about 45 seconds it would make it much more instantaneous.

Link to comment
Share on other sites

TheShanMan

Still wondering about the above, plus one more question... why does /library/refresh take 10 or more seconds to return? It does it every time. Is it attempting to complete the refresh before returning, but timing out and returning since the refresh takes ~ 45 seconds on my server?

Link to comment
Share on other sites

TheShanMan

Anyone? I'd really like to do the logout correctly. And I am finding it pretty annoying that the /library/refresh rest call takes so long to return.

Link to comment
Share on other sites

I'm pretty sure that "logging out" is purely a client concept.  To remove a valid auth token, I believe you would need to use a different API call (whatever one is used by the web dashboard to delete them).

Link to comment
Share on other sites

TheShanMan

Thanks! So the auth token is not a session token and therefore there is no reason to invalidate it (i.e. there really is no concept of a "session", at least in the context of what I'm doing)? What is the point of the /sessions/logout call then?

 

Is the 10+ second /library/refresh call perhaps a bug? I guess if I don't get feedback on this issue I will file it as a bug and see what comes of it that way.

Link to comment
Share on other sites

TheShanMan

Yes, I assume it's doing something, but what? And does it need to do it before the rest call returns? It is triggering a library rescan, so why would triggering it take 10+ seconds? If I watch the web interface I can see that the rescan starts immediately even though the rest call continues to block continuation of my script. The only thing I can think of is that it is intending to complete the rescan, but my library takes much longer to rescan than the rest call lasts. So is it timing out waiting for the rescan to complete? Even if I did want to know that it was done processing my new media, it's not actually telling me that due presumably to timing out. But in my case I don't care to know that it's done processing, so I would like an option to not wait for completion if the option doesn't already exist.

Link to comment
Share on other sites

i just took a really quick look and I don't see it awaiting anything in the chain and it is task-oriented.  The one thing it does do is cancel a running one before queuing.  But I don't know that that would cause a delay either.

 

I did reproduce the same behavior but I'm not sure what it is waiting on.  The one place I've used this call (EMC) I fire it on an async thread so it doesn't have any impact on the UI.

Link to comment
Share on other sites

TheShanMan

Thanks for verifying. It's not clear to me how to report this as a bug. As best as I can tell the server support forum is where I'd do that, for lack of a better option I can find. Is that right?

Link to comment
Share on other sites

  • 2 years later...
kRyszard

yes /library/refresh

 

thanks. is it possible to monitor the progress of the rescan (via rest api) or at least get to know if it is still working or done?

Link to comment
Share on other sites

PenkethBoy

you can check the scheduled task end point

 

"/emby/ScheduledTasks"

 

i was testing earlier with refresh guide - but its essentially the same

 

sinipet

 

    {
        "Name":  "Refresh Guide",
        "State":  "Running",
        "CurrentProgressPercentage":  15,
 
so state and currentprogress% tell you how its going
Link to comment
Share on other sites

  • 1 month later...

created a litte bash wrapper for myself to get an API key (-g), list the tasks(-l), get info on one task(-i=ID with ID being the task ID), run one task (-r=ID) and run a task but also wait to finish (-w=ID)

 

Running then a library scan and waiting for it to finish via:

 

./emby_task.sh -w=6330ee8fb4a957f33981f89aa78b030f

 

Output then looks like:

 

28/09/2018-14:02:55: running task 6330ee8fb4a957f33981f89aa78b030f / Scan media library
28/09/2018-14:02:55: started task 6330ee8fb4a957f33981f89aa78b030f / Scan media library
28/09/2018-14:02:55:  task state=Running, completion=0%, wait to finish
28/09/2018-14:03:00:  task state=Running, completion=13%, wait to finish
...
28/09/2018-14:04:41:  task state=Running, completion=96%, wait to finish
28/09/2018-14:04:51:  task state=Idle, completion=100%, wait to finish
28/09/2018-14:04:56: task 6330ee8fb4a957f33981f89aa78b030f completed, duration=114 seconds

 

 

 Besides from bash and curl, requires jq to be installed to query the json responses


#!/bin/bash
 
# credentials
client="xxx"
device="xxx"
deviceid="xxx"
userid="xxx"
username="xxx"
password="xxx"
 
# task polling time
poll=5
 
# local timezone
TZ="CET"
 
function get_api_key {
api_key="$(curl -s -H "Authorization: MediaBrowser Client=$client, Device=$device, DeviceId=$deviceid, Version=1.0.0.0, UserId=$userid" -d "username=$username" -d "password=$password" 'http://127.0.0.1:8096/users/authenticatebyname?format=json'| python -m json.tool | grep 'AccessToken' | sed 's/\"//g; s/AccessToken://g; s/\,//g; s/ //g')"
if [[ $api_key == "" ]]; then
echo `date +"%d/%m/%Y-%H:%M:%S"`": could not get API key, check credentials"
exit
fi
}
 
function print_help {
echo "available command line options:"
echo " -g / --getget: get and display API key"
exit
}
 
function list_tasks {
list=$(eval $command)
echo "$list"
}
 
function list_task {
# get name
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .Name'"
name=$(eval $command)
 
# get state
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .State'"
state=$(eval $command)
 
# get start time
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .LastExecutionResult.StartTimeUtc'"
start=$(eval $command)
 
start_cet=$(TZ=$TZ date --date "TZ=\"UTC\" $start" "+%d/%m/%Y %H:%M:%S")
start_epoch=$(TZ=$TZ date --date "TZ=\"UTC\" $start" "+%s")
 
# get end time
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .LastExecutionResult.EndTimeUtc'"
end=$(eval $command)
 
end_cet=$(TZ=$TZ date --date "TZ=\"UTC\" $end" "+%d/%m/%Y %H:%M:%S")
end_epoch=$(TZ=$TZ date --date "TZ=\"UTC\" $end" "+%s")
 
# get end time
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .LastExecutionResult.Status'"
status=$(eval $command)
 
duration=$end_epoch-$start_epoch
duration=$(expr $end_epoch - $start_epoch)
 
echo `date +"%d/%m/%Y-%H:%M:%S"`": name=$name, state=$state, last execution start=$start_cet, last execution end=$end_cet, last execution status=$status, duration=$duration seconds"
}
 
function run_task {
# get state
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .State'"
state=$(eval $command)
 
# get name
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .Name'"
name=$(eval $command)
 
if [[ $state == "Running" ]]; then
echo `date +"%d/%m/%Y-%H:%M:%S"`": task $ID / $name already running"
else
echo `date +"%d/%m/%Y-%H:%M:%S"`": running task $ID / $name"
# run task
 
result=$(eval $command)
 
if [[ $result = "" ]]; then
echo `date +"%d/%m/%Y-%H:%M:%S"`": started task $ID / $name"
else
echo `date +"%d/%m/%Y-%H:%M:%S"`": running task $ID / $name failed, error=$result"
exit
fi
fi
}
 
function wait_task {
 
# get state
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .State'"
state=$(eval $command)
 
if [[ $state != "Running" ]]; then
echo `date +"%d/%m/%Y-%H:%M:%S"`": task did not start"
else
while [ $state != "Idle" ]; do
 
# get percentage
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .CurrentProgressPercentage'"
perc=$(eval $command)
if [[ $perc != "" && $perc != "null" ]]; then
percentage=$(printf "%.0f\n" "$perc")
else
percentage=100
fi
 
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .State'"
state=$(eval $command)
 
echo `date +"%d/%m/%Y-%H:%M:%S"`":  task state=$state, completion=$percentage%, wait to finish"
sleep $poll
done
 
# get start time
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .LastExecutionResult.StartTimeUtc'"
start=$(eval $command)
 
start_cet=$(TZ=$TZ date --date "TZ=\"UTC\" $start" "+%d/%m/%Y %H:%M:%S")
start_epoch=$(TZ=$TZ date --date "TZ=\"UTC\" $start" "+%s")
 
# get end time
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .LastExecutionResult.EndTimeUtc'"
end=$(eval $command)
 
end_cet=$(TZ=$TZ date --date "TZ=\"UTC\" $end" "+%d/%m/%Y %H:%M:%S")
end_epoch=$(TZ=$TZ date --date "TZ=\"UTC\" $end" "+%s")
 
# get end time
command="curl -s \"http://localhost:8096/mediabrowser/ScheduledTasks?api_key=$api_key\"| jq -r '.[] | select(.Id | contains(\""
command+=$ID
command+="\")) | .LastExecutionResult.Status'"
status=$(eval $command)
 
duration=$end_epoch-$start_epoch
duration=$(expr $end_epoch - $start_epoch)
 
echo `date +"%d/%m/%Y-%H:%M:%S"`": task $ID completed, duration=$duration seconds"
 
fi
 
}
 
if [[ $# -eq 0 ]]; then
echo "no command line parameters given"
print_help
fi
 
for i in "$@"
do
case $i in
    -g|--getkey)
    shift
    get_api_key
  echo "$api_key"
    ;;
    -l|--list)
    shift
    get_api_key
    list_tasks
    ;;
    -i=*|--info=*)
    ID="${i#*=}"
    shift
    get_api_key
    list_task
    ;;
    -r=*|--run=*)
    ID="${i#*=}"
    shift
    get_api_key
    run_task
    ;;
    -w=*|--runwait=*)
    ID="${i#*=}"
    shift
    get_api_key
    run_task
    wait_task
    ;;
    *)
      echo "unknown command line option"
    print_help
    ;;
esac
done
Link to comment
Share on other sites

  • 2 years later...
muzicman0

I know this is an old thread, but I was using it as a guide to create a bash script to get the API Key for a user, but I can't get it to work.  I keep getting:

12/07/2021-21:26:42: could not get API key, check credentials

But as far as I can tell, everything is correct.  Any help would be appreciated.  I pulled my UserID off of the user page for my admin user (username and password also match).  

If someone is available to help, what can I provide to help troubleshoot?

Link to comment
Share on other sites

PenkethBoy

@muzicman0

one thing to try

"password=$password" needs to change to "pw=$password" this was a change made awhile ago since the above was posted

i have not been through the rest of the code as dont use bash but that jumped out at me as needing to be changed

see how you go

  • Like 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...