also available in rss format

pop3 extraction and API fun

04/04/09 @ 10:50:51

you may (or most likely may not) have noticed i've had a few problems with twitpic and twitxr of late. not that either of the services aren't admirable and thoroughly decent but for my purposes neither quite hit the mark. twitpic's xml feed changed since they switched to new servers (basically destroying my uberFeed call no matter how much fiddling i did to fix) and twitxr seems to have an issue with the shortening of urls when posted to your linked twitter account (if you post in the region of 120chars + url) - this was all much to the annoyance of some of my followers who were quick to pick me up on the fact that my boring snapshots had failed - nice one Dean / Tim / Rob!

if you want a job doing, do it yourself - the old adage runs true, at least this way i'd have full control over the output and not be reliant on a third party who might change their setup without warning and subsequently break my code.

the snapping of a scene and sending it to twitter with my mobile phone has so far quenched my prattling needs so that method needed to be followed with the new system. first off i needed an account i could access with coldfusion and being an avid google fanboy i set one up. after a bit of poking around it would seem CF7 doesn't directly support google's pop3 implementation over SSL but after some in depth research (we all know what that means) i found a nice stub of code which when placed before the cfpop tag utilises the underlying java to enable it:

<!--- set some java properties to enable ssl use with gmail --->
<cfset javaSystem = createObject("java", "java.lang.System") />
<cfset jProps = javaSystem.getProperties() />
<cfset jProps.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory") />
<cfset jProps.setproperty("mail.pop3.port",995) />
<cfset jProps.setProperty("mail.pop3.socketFactory.port", 995) />


the cfpop tag did its thing and looping over the data produced some fine header information about what was sitting in the box. it's at this point we really should consider security, you don't afterall want spammers emailing you obscene material only to find your autoEmail process has published it under your name for all the world to see. the best way to deal with it i concluded would be to only process emails that came from a reputable source ie me :) so a quick check before processing that the current email record's 'from' was from me did the trick.

using a second cfpop tag to get at the current message via it's uniqueId (uid) provided some more data to work with including checking for an extension of 'jpg' on the extracted attachment (not foolproof of course but enough for a single user). at this point we can be fairly confident the message isn't bogus and the real processing can begin:

<cfset thisTimeStamp = #dateFormat(Now(),'DDMMYY')# & #timeFormat(Now(),'HHMMSS')#>
<cfset thisFile = "posted_" & thisTimeStamp & ".jpg">
<cffile action="rename" source="#expandPath('./')#images\#email.attachments#" destination="#thisFile#">
<cfset thisPath = expandPath('./') & "images\" & thisFile>
<cfset thisUrl = siteUrl & "?post=" & thisTimeStamp>
<cfset thisSubject = email.subject>
<cfset thisSubjectLen = len(thisSubject)>


this little lot just renames the attachment to something unique (dateTime) and gives us the url which we'll ultimately be sending visitors to (that page is just one that's waiting for a post= url parameter to display the pic). please note #siteUrl# is a global variable and #email.xx# variables were provided by the cfpop query. now, why do you think we've set #thisSubjectLen#? because, frankly it matters if we want to update twitter with our new status since it has a 140chars limit - you'll see it later on.

now we move into the final exciting stages and API (application programming interface) goodness.

this was very simple but pleasing chunk of code for setting up the final status ready for sending to twitter:

<cfhttp method="Get" url="http://is.gd/api.php?longurl=#thisUrl#" result="shortUrl">
<cfset shortUrl = trim(shortUrl.fileContent)>
<cfset shortUrlLen = len(shortUrl)>
<cfif shortUrlLen thisSubjectLen GT 137>
<cfset status = left(thisSubject,thisSubjectLen-shortUrlLen-4) & " - " & shortUrl>
<cfelse>
<cfset status = thisSubject & " - " & shortUrl>
</cfif>


firstly a 'cfhttp get' to http://is.gd (url shortening service's API - i use the term loosely in this instance) and pass it our created url, then grab the newly shortened url from the fileContent returned and tack it onto the end of the email's subject. you may notice a basic calculation in there for making sure the subject and the hypen sandwiched by spaces + the url doesn't go over the 140 limit - something twitxr would be wise implement!

finally the status package is sent to twitter via it's own API and BAM! the job is complete!

<cfhttp url="http://twitter.com/statuses/update.xml" method="post" username="#twitterUser#" password="#twitterPass#"
<cfhttpparam name="status" value="#status#" type="formfield" />
</cfhttp>


i'd recommend setting up a test twitter account before you go live with this bit since you're likely to irritate any followers with test status updates.

don't forget to add your cf template to the cfschedule for processing every so often, if you don't the script won't ever check that bulging pop3 box of yours.

summing up

in short we've done all the following:

- checked a pop3 account for messages
- looped over each message check it's from a trusted source
- extracted a jpg attachment and created a url to it
- shortened the url
- appended the shortened url to the email's subject
- sent the status update to twitter



on 05/09/10 @ 10:23:11 posted from twitter

is dukenukem forever really gonna happen in 2011? linkage bring it on!


on 03/09/10 @ 13:34:26 posted from twitter

are you in design hell? linkage


on 02/09/10 @ 19:15:28 posted from twitter

google's new artificial intelligence API linkage (aka skynet!)


Android GPS navigation: X-wing edition

on 17/08/10 @ 21:17:04 posted from google news

oh yes! stay on target.


Yet Another Google Bookmarks Extension Is a Highly Customizable Bookmarks Manager [Downloads]

on 03/08/10 @ 21:29:57 posted from google news

been using this for a while, can say it's rather excellent.


How to Deal with Distractions in a Web Worker's World

on 23/07/10 @ 16:54:02 posted from google news

believe it or not, whilst reading this - i got distracted :-/


Dell Streak Tablet Phone

on 23/07/10 @ 16:41:25 posted from google news

i'd missed this... how many more variations on the size of panel do we need?


on 29/06/10 @ 17:01:39 posted from newWord

volition - to deliberately choose or decide


James Bond Aston Martin on the block for $4m (or more)

on 02/06/10 @ 17:31:38 posted from google news

the classic DB5 up for auction, yeah - i'll be bidding...


on 25/05/10 @ 10:38:34 posted from newWord

hyperbole - intentional exaggeration


on 19/05/10 @ 13:47:32 posted from newWord

prowess - exceptional or superior ability or strength


on 12/05/10 @ 12:22:43 posted from newWord

plaudit - enthusiastic approval


on 28/04/10 @ 14:12:45 posted from newWord

bigot - a prejudiced person who is intolerant of any opinions differing from their own


Easter 2010

on 04/04/10 @ 12:00:00 posted from picasa

an Easter bank holiday weekend away in Devon

5 photos after the jump!


Gran Canaria 2010

on 07/03/10 @ 19:33:43 posted from picasa

a week's holiday in the Canaries, March 2010

33 photos after the jump!


Random Snaps

on 11/02/10 @ 13:00:00 posted from picasa

a bunch of random photos taken when out and about

7 photos after the jump!


Icy Studland 09-01-10

on 09/01/10 @ 17:51:42 posted from picasa

a chilly walk out across the Studland beach in Dorset

18 photos after the jump!


Oxfordshire 2009

on 23/12/09 @ 16:21:46 posted from picasa

good ol' family xmas in Sparsholt, Oxfordshire 2009

17 photos after the jump!


time-lapse of me in office

on 05/10/09 @ 11:51:32 posted from youtube

time lapse test of me sat at my desk for a couple of hours (not exciting!). taken using sheado.net 'time-lapse' application on Android phone

42 second clip after the jump!


Eurofighter Typhoon at Bournemouth Airshow 2009

on 25/08/09 @ 06:44:33 posted from youtube

the stunning Eurofighter Typhoon seen at Bournemouth Air Festival 2009

170 second clip after the jump!


Heading north on m40, SLOWLY!

on 24/07/09 @ 17:39:54 posted from youtube

This video was uploaded from an Android phone.

20 second clip after the jump!


first video with my htc magic

on 17/07/09 @ 13:35:48 posted from youtube

This video was uploaded from an Android phone.

14 second clip after the jump!


Durdle Door Dorset

on 15/03/09 @ 21:23:31 posted from youtube

the lovely Durdle seen from the cliff above on a sunny Sunday in March

65 second clip after the jump!



latest snaps

stuck in the car until the rain stops #noCoat
now that is one wacky carrot
they still make them! Brannigans beef n mustard crisps, i've not seen them for years!
it's a veritable 'quornucopia' in the freezer these days
arghhh! there's a scary stair post living in our house!
it's a dog's life
That is anything but a dirty gate
i mean seriously, what year is this? 1985? i need a barber, NOW.
Its got a propellor!
Advantec really don't hold back when it comes to the stationary budget
Is there any finer sight on a June Friday afternoon?
Enjoying a beer by the river :)
well that's it, end of an era. i just walked out of the echo for the last time #emotional
awwww the guys made me a frontpage :)
Ze duck, she is ready :) #foodpic
My 'friend' @r0bfleming can't even wait till I've gone! Two days left!!
time to bring out the big guns in the overheating server room. soooo noisy though :-( #fixIt
beer and postcards in Brighton, bohemian Britain at its best :-)
Luke and Vicki move into their new home
its just like summer today in Bournemouth but without the crowds
o2 arena is definitely the coolest place for gigs in my neighborhood
James are quite honestly brilliant, sorry about the poor pic! they're amazing live still!
up and out for a stroll, looks like it's gonna be a lovely day!
Pip was not impressed by Over The Rainbow, i have to agree
anything that can read my left-handed scrawl is truly impressive
ahhhh Devon, it was pouring 20 mins ago!
created a custom shadow stuck-on screenshot effect in photoshop
MAN THIS STUFF IS FRICKIN AMAZING. if you've got any mold, this is the answer, its INCREDIBLE! REALLY!
hahah @r0bfleming just made a great little adjustment to one of my holiday snaps!
hello spring! please stick around...
rounded off the corner of my desk :-)
just improvised a netbook splatter-wall
closely followed by the CLASSIC mkii Ford Escort rally car. awesome.
spent most of the day in Beaulie Motor Museum... again :-) this was my favourite - the AC Cobra
the darkness it cometh... *runs back to the office*
beaming wildly like a small boy who's just woke up Xmas morning and found his new bike
counting a glass jar's worth of coinage, this is the best bit :-) cashback!
now able to watch our feathered friends from the kitchen window, nice one Victoria!
awoke to find a guard and watchdog on duty