I’m on the command line quite a bit at home (Mac OS X) and at work (*nix). I’ll often strew together a bunch of commands with pipes and redirects, and I usually think “That was useful. I should write that down.” Here is the first installment, of probably zero more, where I’ll actually fulfill that promise to myself.
The problem was that I needed to view the raw GData query responses while working on TubePress. The method I used could probably be used for any Atom or RSS service just as well.
wget was working to some extent, but it was a pain in the ass to get it to just display to stdout instead of writing to a file. It’s probably possible but I got impatient with the man file.
curl seemed a lot more promising in that it spits out the HTTP response to stdout, but when you try to pipe the output to anything, you get a nasty status bar in the output. For instance, if you run
curl google.com | less
you’ll get something like
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 219 100 219 0 0 364 0 --:--:-- --:--:-- --:--:-- 0
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
You can hush curl’s progress meter thingy with the -s switch:
Drumroll please, here is the final command:
# curl -s "http://gdata.youtube.com/feeds/api/videos?q=hough" | xmllint --format - > out.xml
You can see that I sent the HTTP response (which is just a slew of XML) to xmllint for formatting, then spit the final result out to an XML file. One gotcha is that for URLs with many parameters, you’re best off enclosing them in double quotes when passing them as arguments to curl.
Anyone else have an easy way of viewing raw XML responses?