Parsing a syntax response and using the response to create another request

I am trying to create a shell script that will retrieve the image URL for a random XKCD comic so that I can display it with Übersicht. Since you can only request the XKCD API for the latest comic or specific comic, I need:

  • Send a GET request to http://xkcd.com/info.0.json , select the value of the num element.
  • Send another request http://xkcd.com/XXX/info.0.json , where XXX is the num value.

My current command looks like this and successfully returns a comic number:

 curl -s 'http://xkcd.com/1510/info.0.json' | grep -Eo '"num": \d+' | grep -Eo '\d+' 
  • I was not able to figure out how to use capture groups with grep , so I need to grep json twice. A general tip is to use -P , which is not supported on Mac OS X 10.10.
  • I do not know how to read the output of grep (as XXX ) into the second curl -s 'http://xkcd.com/XXX/info.0.json' command curl -s 'http://xkcd.com/XXX/info.0.json' .
+6
source share
1 answer
  • On OS X, you can use the Perl system:

      curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*/\1/' 
  • You can save the output in a variable with the replacement of the command:

     num=$(curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*/\1/') curl -sS "http://xkcd.com/${num}/info.0.json" 

    or more briefly, two in one, although not very readable:

     curl -sS "http://xkcd.com/$(curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*/\1/')/info.0.json" 

By the way, I highly recommend jq as a JSON command line processor. To extract num with jq is as simple as

 curl -sS http://xkcd.com/info.0.json | jq '.num' 

and although you didn’t ask for it, here is a simple single line with jq that retrieves the URI of the last image:

 curl -sS "http://xkcd.com/$(curl -sS http://xkcd.com/info.0.json | jq '.num')/info.0.json" | jq -r '.img' 

Output Example:

 http://imgs.xkcd.com/comics/spice_girl.png 
+7
source

Source: https://habr.com/ru/post/985099/


All Articles