Download THE DATA CENTER

Transcript
update is status/update.json. Read the API, and you’ll notice that
it supports four different output formats, all of which are a pain
to parse within a script, unfortunately. One of those is json, and it
re-occurs here as the update-receiving URL address.
If you’ve already worked with Web sites from the command line, you know there are lots of illegal characters that
cannot be included in URLs and, by extension, on command
lines of utilities that interact with the Web, such as curl. As a
result, one of the tasks of our send.twitter.update script will be
to make all of the necessary substitutions before sending the
new status message to the Twitter server.
On a lightweight service like Twitter, I think it’s probably
crazy to go through too many hoops to ensure security, so I
actually will be including the account name and password in
the script. Given some of the suggested applications we’ll
explore later, it makes sense to create a new Twitter account
just for the command-line updates, in which case, a shared
password isn’t that big a problem anyway.
Here’s a first stab at a simple stu (sent twitter update) script:
#!/bin/sh
user="DaveTaylor"
pass="--mypw-- "
curl="/usr/bin/curl"
$curl --basic --user "$user:$pass" --data-ascii \
"status=`echo $@ | tr ' ' '+'`" \
"http://twitter.com/statuses/update.json"
exit 0
In use, simply type in the script name and desired
status update:
$ stu Writing makes me sleepy
{"user":{"name":"Dave Taylor","description":"Blogger, entrepreneur, public
¯speaker, dad!","screen_name":"DaveTaylor","profile_image_url":
¯"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\
¯/35534842\/dticon_normal.gif","location":"Boulder,
¯Colorado","url":"http:\/\/www.AskDaveTaylor.com\/","id":9973392,
¯"protected":false},"created_at":"Sat Jan 12 21:31:37 +0000
¯2008","truncated":false,"text":"Writing makes me
¯sleepy","source":"web","id":592217322}
$
Eek. That’s a scary output, isn’t it? So, before wrapping up
this column, I strongly suggest that immediately after the
invocation of curl, you append >& /dev/null, so you can
discard the output. If you want to be fancy, check $? to
see whether it’s nonzero, but let’s talk about that level of
improvement in the next column.I
Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently
author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours,
among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up
tech support at AskDaveTaylor.com. Follow him on Twitter if you’d like: twitter.com/DaveTaylor.