MacOS – How to download images from an xml feed using terminal

applescriptbashcommand linemacosterminal

How can I download png images from an xml feed that looks the following way?

<ListBucketResult xmlns="http://url1.com">
  <Name>imagesToUse</Name>
<Prefix/>
<Marker/>
<IsTruncated>false</IsTruncated>
<Contents>
  <ImageStuff>imagetodownload.png</ImageStuff>
  <Size>17293</Size>
</Contents>

I was taking a look at this question on here about the same topic, except that I don't need to rename them, just download them.

Being new to shell scripts, I'm not quite sure how to do this (although I can definitely do this in 2 minutes in objective-c).

I have tried creating the following script and running bash myscript.sh

#!/bin/bash
URL='https://theurlwiththexml.com'
i=0
for u in $(curl -s "$URL" |
           grep png | 
           sed -E 's/.*<ImageStuff>(.+)<\/ImageStuff>/\1/'); do
    curl -s "$u" -o $i.png
    (( i++ ))
done

But nothing happens. It just prints another line without errors.

Best Answer

After curl http://theurlwiththexml.com:

sed -n 's,.*<ImageStuff>\(.*\.png\)<.*,\1,p'|xargs -I, curl http://url1.com/, -o ,

sed -n disables printing, \( and \) are a portable way to specify groups, and p prints the pattern space. xargs -I specifies a replacement string and curl -o specifies an output file.

Or using xmlstarlet, parallel, and wget:

xml sel -t -v //ImageStuff|parallel wget http://url1.com/{}

sel is an abbreviation for select, -t -v uses the value template, and //ImageStuff is an XPath expression that matches all ImageStuff nodes.