You are not logged in.

#1 2023-06-08 18:49:11

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Script to extract JSON info from pyLoad

Hello all,

I used to have the following script working (stopped working about 2/3 weeks ago, but didn't realised) - it connect to pyLoad and extract a json response whether or not pyLoad was actually downloading  or not.

#!/bin/bash

set -e
set -x

SessionID=$(curl -s "http://192.168.1.90:8000/api/login" -g -H "Host: 192.168.1.90:8000" -H "Content-Type: application/x-www-form-urlencoded" --data "username=user&password=password" | jq -r)

SessionStatus=$(curl -s  "http://192.168.1.90:8000/api/statusServer" -g -H "Host: 192.168.1.90" -H "Content-Type: application/x-www-form-urlencoded" --data "session=$SessionID" | jq -r '.active')

if [ $SessionStatus -eq 1 ]
then
  exit 0
else
  exit 1
fi

That's the JSON available on 'http://192.168.1.90:8000/api/statusServer':

{"pause": false, "active": 0, "queue": 4, "total": 5, "speed": 0, "download": true, "reconnect": false, "captcha": false}

And that's the answer I get, with the "null" return instead of the "0" or number of active download.

$ sh autosuspend_script/pyload_autosuspend.sh 
++ curl -s http://192.168.1.90:8000/api/login -g -H 'Host: 192.168.1.90:8000' -H 'Content-Type: application/x-www-form-urlencoded' --data 'username=user&password=password'
++ jq -r
+ SessionID='{
  "_permanent": true,
  "authenticated": true,
  "id": 1,
  "name": "pyload",
  "role": 0,
  "perms": 0,
  "template": "default",
  "_flashes": [
    [
      "message",
      "Logged in successfully"
    ]
  ]
}'
++ curl -s http://192.168.1.90:8000/api/statusServer -g -H 'Host: 192.168.1.90' -H 'Content-Type: application/x-www-form-urlencoded' --data 'session={
  "_permanent": true,
  "authenticated": true,
  "id": 1,
  "name": "pyload",
  "role": 0,
  "perms": 0,
  "template": "default",
  "_flashes": [
    [
      "message",
      "Logged in successfully"
    ]
  ]
}'
++ jq -r .active
+ SessionStatus=null
+ '[' null -eq 1 ']'
autosuspend_script/pyload_autosuspend.sh: line 10: [: null: integer expression expected
+ exit 1

Thank you for taking the time to read my topic, and for your assistance.

Offline

#2 2023-06-08 19:15:44

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,232
Website

Re: Script to extract JSON info from pyLoad

Remove the pipe to 'jq -r ".active"' and post the actual output from that curl command in the context of that script.

Last edited by Trilby (2023-06-08 19:16:01)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2023-06-08 19:26:58

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Script to extract JSON info from pyLoad

Thank you for your help and assistance, please see below the output of the script without the 'jq'

$ sh autosuspend_script/pyload_autosuspend.s
h
++ curl -s http://192.168.1.90:8000/api/login -g -H 'Host: 192.
168.1.90:8000' -H 'Content-Type: application/x-www-form-urlenco
ded' --data 'username=user&password=password'
+ SessionID='{"_permanent": true, "authenticated": true, "id":
1, "name": "pyload", "role": 0, "perms": 0, "template": "defaul
t", "_flashes": [["message", "Logged in successfully"]]}'
++ curl -s http://192.168.1.90:8000/api/statusServer -g -H 'Hos
t: 192.168.1.90' -H 'Content-Type: application/x-www-form-urlen
coded' --data 'session={"_permanent": true, "authenticated": tr
ue, "id": 1, "name": "pyload", "role": 0, "perms": 0, "template
": "default", "_flashes": [["message", "Logged in successfully"
]]}'
+ SessionStatus='{"error": "Unauthorized"}'
+ '[' '{"error":' '"Unauthorized"}' -eq 1 ']'
autosuspend_script/pyload_autosuspend.sh: line 10: [: too many
arguments
+ exit 1

Offline

#4 2023-06-08 19:39:50

seth
Member
Registered: 2012-09-03
Posts: 57,140

Re: Script to extract JSON info from pyLoad

It's also actually in the original post.
Now take a close look at it.
Does it look like "{"pause": false, "active": 0, "queue": 4, "total": 5, "speed": 0, "download": true, "reconnect": false, "captcha": false}"?

Offline

#5 2023-06-08 19:42:01

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Script to extract JSON info from pyLoad

No it doesn't (uses to work mind)

It is the answer to the first URL api/login

Offline

#6 2023-06-08 19:47:22

seth
Member
Registered: 2012-09-03
Posts: 57,140

Re: Script to extract JSON info from pyLoad

According to your OP it's the reply to both requests.

Offline

#7 2023-06-08 19:49:32

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Script to extract JSON info from pyLoad

Sorry for any confusion, the first curl command is to log on and get the credentials to the second curl command to extract the right JSON response.

Offline

#8 2023-06-08 19:53:04

seth
Member
Registered: 2012-09-03
Posts: 57,140

Re: Script to extract JSON info from pyLoad

+ SessionStatus='{"error": "Unauthorized"}'

Offline

#9 2023-06-08 19:55:39

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Script to extract JSON info from pyLoad

Yes, I have seen that - and it seems that the credentials are no longer past on to the second curl command.

Credentials are correct though and I can't figure out why it is isn't working any longer.

Offline

#10 2023-06-08 19:57:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,232
Website

Re: Script to extract JSON info from pyLoad

seth wrote:

It's also actually in the original post.

Well, no, it wasn't - that was my point.  What was in the OP was the expected json reply which would pruduce a "0" if piped to that jq command.  As it didn't produce a zero, it was clear the actual reply to the curl command was not what was expected.  This was confirmed in post 3 which makes the problem quite clear.

For the authorization error, is there a reason you are passing a different "Host" header for the two curl commands?  The second one drops the port specification.

Last edited by Trilby (2023-06-08 19:59:26)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#11 2023-06-08 19:59:38

seth
Member
Registered: 2012-09-03
Posts: 57,140

Re: Script to extract JSON info from pyLoad

If you enter this into a terminal

curl -s http://192.168.1.90:8000/api/statusServer -g -H 'Host: 192.168.1.90' -H 'Content-Type: application/x-www-form-urlencoded' --data 'session={
  "_permanent": true,
  "authenticated": true,
  "id": 1,
  "name": "pyload",
  "role": 0,
  "perms": 0,
  "template": "default",
  "_flashes": [
    [
      "message",
      "Logged in successfully"
    ]
  ]
}'

what is the response.

Offline

#12 2023-06-08 20:40:47

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Script to extract JSON info from pyLoad

See below the output

{"error": "Unauthorized"}

Offline

#13 2023-06-08 20:46:59

seth
Member
Registered: 2012-09-03
Posts: 57,140

Re: Script to extract JSON info from pyLoad

Trilby wrote:

For the authorization error, is there a reason you are passing a different "Host" header for the two curl commands?  The second one drops the port specification.

Offline

#14 2023-06-08 20:52:12

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Script to extract JSON info from pyLoad

Sorry no reason, but I have now added the port onto the second curl command and I am still getting the error:

~]$ sh autosuspend_script/pyload_autosuspend.s
h
++ curl -s http://192.168.1.90:8000/api/login -g -H 'Host: 192.
168.1.90:8000' -H 'Content-Type: application/x-www-form-urlenco
ded' --data 'username=*****&password=****'
+ SessionID='{"_permanent": true, "authenticated": true, "id":
1, "name": "pyload", "role": 0, "perms": 0, "template": "defaul
t", "_flashes": [["message", "Logged in successfully"]]}'
++ curl -s http://192.168.1.90:8000/api/statusServer -g -H 'Hos
t: 192.168.1.90:8000' -H 'Content-Type: application/x-www-form-
urlencoded' --data 'session={"_permanent": true, "authenticated
": true, "id": 1, "name": "pyload", "role": 0, "perms": 0, "tem
plate": "default", "_flashes": [["message", "Logged in successf
ully"]]}'
+ SessionStatus='{"error": "Unauthorized"}'
+ '[' '{"error":' '"Unauthorized"}' -eq 1 ']'
autosuspend_script/pyload_autosuspend.sh: line 10: [: too many
arguments
+ exit 1 

Offline

#15 2023-06-08 20:55:59

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,090
Website

Re: Script to extract JSON info from pyLoad

"Content-Type: application/x-www-form-urlencoded" and then send "application/json".
If I was the webserver, I'd  rap the sender over the knuckles.
How is this even working?


Inofficial first vice preseident of the Rust Evangelism Strike Force

Offline

#16 2023-06-08 21:04:22

seth
Member
Registered: 2012-09-03
Posts: 57,140

Re: Script to extract JSON info from pyLoad

--data-urlencode, but the SessionID doesn't look particularily "ID" to me either.

Offline

#17 2023-06-08 21:29:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,232
Website

Re: Script to extract JSON info from pyLoad

seth wrote:

--data-urlencode, but the SessionID doesn't look particularily "ID" to me either.

This was also my thought from the start.  That output confirms a login, but there's nothing in it at all that should be resent in another query.  I speculated that if this ever worked it might be due to the server maintaining a log of the authorized client IP.  But now I'm starting to think it perhaps previously worked only by accident, and now it is "failing" correctly.

sweetthdevil, do you have any documentation on which this means of loging in / authenticating is recommended?  From a very brief web search pyLoad appears to provide a web interface on port 8000, and if a web user logged in, they'd most likely be provided a login cookie of some sort.  Curl can manage browser cookies if needed - but you don't have any code to do so in that script.

More often, if there was a (not-strictly-web) API, you'd get back a token from a login command that you would then send in subsequent requests.

Last edited by Trilby (2023-06-08 21:31:13)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB