Automating picture upload to Twitter

I've been doing a spot of micro blogging on Twitter for the last week or two and I'm especially intrigued by the picture services that are available as posting a picture is a lot more interesting then a bit of text. Besides, I hear a picture is worth a thousand words and that fact is especially pertinent when it comes to Twitter's 140 characters per post limit.

Twitter doesn't currently support picture uploads however there are a couple of other services that do and it's Twitpic and Twitxr that I've been messing around with. You can use Twitxr as your micro blogging portal in its own right or, more likely, you can configure these services to integrate with your Twitter page so that when you upload a picture to them the accompanying text and a link to your picture appear on your corner of Twitter.

Twitpic is the official site for sharing your snaps on Twitter however I prefer Twitxr. Both work in a similar way and in my case I can take a picture on my mobile phone and email it to either service along with some text to accompany it. I find Twitxr is quicker to show my uploaded image and it is shown in a more logical format with the description preceding the link to the picture rather than Twitpic's method of showing the link before the description. It makes more sense to me to talk about something and then link to it rather than Twitpics method of doing it the other way around although I understand that this is to ensure the link to the picture is not cut off by too many characters in the body text.

A comparison of the Tweets from Twitxr (top) and Twitpic. Notice Twitxr shows the text before the link
while the Twitpic layout is the reversal.

Twitxr is also quieter and at the time of writing an uploaded picture will stay on the public timeline for several minutes whereas Twitpic is so busy you'll miss it if you blink. Twitxr also has a local timeline which is interesting as you can see what other people in your general geographic location are doing. At the moment, the only other person in my geographic location is a bloke in Birmingham who hasn't uploaded anything for about a month!

While browsing the public timeline in Twitxr last weekend I noticed someone in Italy was uploading images from a webcam which got me wondering if I could automate sending a picture from my evil server webcam on a regular basis, say once a day at about noon. I figured it shouldn't be too difficult from my Linux server so long as I had a Mail User Agent (MUA) to create an email with the webcam image as an attachment, a Mail Transfer Agent (MTA) to send the email to an SMTP server from where it can be beamed to one of these picture services and a cron job to kick it off at the same time each day.

For this task, I'm going to use mutt as the MUA and esmtp as the MTA. Both are in my repository and can be installed via Synaptic or with:

sudo apt-get install mutt esmtp

Mutt is a command line email program that I admit I'm not too familiar with although from what I can gather it will allow you to stitch together an email without being responsible for its transport. That is to say, there is no setting in the mutt config file where an SMTP server can be specified in order for the email to be sent and mutt doesn't have SMTP built-in. This is why I've installed esmtp. Mutt will create the mail and hand it over to esmtp for it to be thrown at an SMTP server, which in my case is hosted for me by an external provider.

Thus, for me the configuration of esmtp is simple. I can edit the global esmtp config file by entering:
sudo nano /etc/esmtprc

and within my config file all I need is:    #port 587: SMTP submission
username=<my smtp username>
password=<my smtp password>

Similarly my mutt configuration file is also pretty simple. The default file is located in my profile so I can edit it with:
nano ~/.muttrc

and all I need in this file is:
set realname="<your mail display name>"  #See note 1
set from="<your email address>"          #See note 2
set use_envelope_from=yes                #See note 3
set sendmail="/usr/bin/esmtp"           
#Specifies MTA program

Note 1: This is your display name and may not be rquired
Note 2: Some services may require your picture to be submitted from the mail address you have registered with them
Note 3: If not set, your mail will be sent from @localhost

For debugging purposes, the following line is also useful as it gives you a log file:
set sendmail="/usr/bin/esmtp -v -X ~/mutt.log"

I'm going to test my configuration by entering the following command (all on one line) which should submit a picture, text and location to Twitxr:
mutt -s "Leamington Spa, Warwickshire UK" -a /home/saveryd/Desktop/testpic.jpg This email address is being protected from spambots. You need JavaScript enabled to view it. < /home/saveryd/Desktop/body.txt

To break this command down:
-s specifies a subject line (use quotes if there are spaces). Twitxr likes your Google Map location as the subject (Leamington Spa, Warwickshire UK in my case).
-a specifies the attachment. For my test purposes I have a jpeg called testpic on my Desktop.
This email address is being protected from spambots. You need JavaScript enabled to view it. represents my unique address specified by my twitxr account.
< /home/saveryd/Desktop/body.txt is a text file on my Desktop which contains the plain text to be used in the body of my message (i.e. my micro blog text of up to 140 characters to accompany the picture).

If using Twitpic, the command is slightly simpler as Twitpic uses the subject line for the blog text (i.e. there is no body text file or location identifier to worry about):
mutt -s "Blog text to accompany picture goes here" -a /home/saveryd/Desktop/testpic.jpg This email address is being protected from spambots. You need JavaScript enabled to view it.

Either way, if the test is successful, the specified mail address should receive the email with the correct subject, attachment and, if defined, some body text.

With the MUA and MTA working, all I had to do now was bung the command in a bash script and set it to run automatically.

For the purposes of this example I shall create a blank text file on the Desktop called 'twittersnap':
nano ~/Desktop/twittersnap

and populate as follows:

#Automatically upload image to Twitxr or Twitpic

#Uncomment and amend these lines for Twitxr
#SUBJECT="Leamington Spa, Warwickshire UK"
#BODYMSG="My corner of Leamington Spa, UK at noon - auto upload from webcam at"
#PROGRAM="mutt -s \"$SUBJECT\" -a /path-to-picture.jpg This email address is being protected from spambots. You need JavaScript enabled to view it."
#eval "echo $BODYMSG | $PROGRAM &"

#Uncomment and amend these lines for Twitpic
#SUBJECT="My corner of Leamington Spa, UK at noon - auto upload from webcam at"
#PROGRAM="mutt -s \"$SUBJECT\" -a /path-to-picture.jpg This email address is being protected from spambots. You need JavaScript enabled to view it."
#eval $PROGRAM &

You'll notice the Twitxr configuration is slightly more involved simply because Twitxr requires a subject and a body for the location and blog text respectively while Twitpic only requires a subject for use as the blog text.

The subject has to be placed in the variable $SUBJECT separate from the program variable as the subject includes spaces and therefore needs to be encompassed by quotes. If we were to leave it looking like the string below....

#PROGRAM="mutt -s "This is the subject” -a /path-to-picture.jpg This email address is being protected from spambots. You need JavaScript enabled to view it."
... then the subject quotes get confused with the quotes for the $PROGRAM variable, the result being incorrect subject text and the possibility of mail misdirection.

Once the script is in place, save changes, exit Nano (or whatever other text editor is in use) and then make the script executable with:
chmod +x ~/Desktop/twittersnap

The script can be tested by entering:

The final part of the process was to automate the running of the script. I'm setting my cron job for 11:59AM daily. First, enter the crontab editor:
crontab -e

Then set the cron job in the order of:
min    hour  date  month  day-of-week       command
0-59   0-23  1-31  1-12   0-6 (Sunday-0)    Command to execute

Asterisks are used where no specific value is given.

For this task (11:59AM daily), my crontab would be set as:
59  11  *  *   *     /<path-to-script>/./twittersnap

Despite my preference for Twitxr, I'd prefer to upload my noon camera picture to Twitpic simply because I don't want to clutter my Twitxr timeline with the automated daily snaps from my webcam. I also think it doesn't hurt to have some content on Twitpic seeing as there are more people looking around there. I'll keep the one-off picture content on Twitxr however (although it can all be viewed in one place via my page on Twitter). That said, at the time of writing Twitpic seems to have had some outages and issues over the last couple of weeks so we'll see how it goes.

Wow, it's November - a full 7 months since this article was written. I'm still using Twitter but thought I should add some extra notes on what I wrote back in April now that I'm more familiar with this new toy.

Firstly, I haven't used Twitxr since July. This is because I started Twittering with Gravity on my Nokia and Twitxr isn't supported on there.

Secondly, I disabled the automatic webcam upload to Twitter after a couple of months as I got bored of it appearing in my timeline. So much for all that flippin' effort then!

Thirdly, Twitpic sucks. Too much down time, too many pictures not appearing, sometimes no picture appearing, sometimes the wrong picture appearing. I know it's a free service but then so is Twitgoo and that just works.