Back in , Twitter announced the ability to geo-tag your tweets, which was only released as an API (Application Programming Interface) at the time (meaning that only Twitter clients (like Tweetie) could access the geo-location data, and the information wasn’t visible on the Twitter website). But recently, Twitter has made significant enhancements to the API and the location info, if it’s available, is now displayed on their website too.
I noticed a couple weeks ago that the Twitter API, (which returns data in RSS, XML, JSON and/or ATOM formats), now includes a wealth of location information, and I was excited to start using some of that information on my websites. If you check out DanielMenjivar.com, or the homepage of my blog, you can see it in action. it’s very neat.
For my geo-tagged tweets, (yes, you can turn it on or off on a per-tweet basis), I’m now displaying the name of the community where that tweet was submitted, which links to a google map that shows the actual location of that tweet… (My location data has been available via twitter.com/DanielMenjivar for some time now, so I’m not worried about displaying my location info on my websites. Whether or not I should geo-tag my tweets is a separate issue – and I can always permanently remove all my geo-data from Twitter at the click of a button if I want/need to, so I’m not worried about it.)
Apart from that, I also made some other changes. Any links included within my tweets actually work as links now, @usernames now link to Twitter profiles, #hashtags link to Twitter Search, and I made changes to how the dates are displayed (ex. 5 minutes ago, 35 seconds ago, etc.).
At the same time, when I was reading up on the Twitter API, I realized that there is a limit of 150 requests per hour, which for unauthenticated requests like the ones I’m making, are based on your IP address. (In this case, that would be the IP address of my server.) Considering the amount of traffic I get just to DanielMenjivar.com, 150 requests per hour is not really a lot. (Granted, I only display Twitter updates on my home page, but still…) So instead of fetching the data from Twitter with every page request (like I was before), I created a new script to run once every minute on my server, (as a CRON job). The script fetches the data from Twitter (only if there are changes), removes the excess API data that I’m not using, and then saves everything to a very small JSON file. This way, my server only makes 60 requests per hour to Twitter, and then all my sites just need to access this significantly smaller, cached JSON file.
Apart from staying under the limit of 150 requests per hour, the advantage is that I’m not having to access Twitter’s servers (which are very slow, and go down often) with every page request. Now I’m just locally accessing a very small text file and all the parsing is done in the background by the CRON job. My page load times are not at all affected by the speed of Twitter’s servers anymore. (I still have to format the dates with each request though, so that they display the correct information, since I want to display them as "35 seconds ago", for example, and not a specific time… But the time it takes to format this is very insignificant.)
There are only two disadvantages:
Anyways, if you’re interested seeing some of my code, or are interested in knowing more about this, let me know in the comments below. If not, that’s cool too – just check out the Twitter updates on my sites, and follow me on Twitter!
I just got an idea after posting this…
I’ve decided that rather than formatting the dates with each page load, I’m going to do it in the CRON script instead. It makes the code easier to maintain if I can just have it all in one place.
I remember I initially did it this way, (since I want to display the dates the same way across all my sites), but the issue was that if I just recently posted to Twitter within the minute, and you visited my sites, the "35 seconds ago" didn’t update when you refreshed the page – it was static according to when the CRON job last checked for new tweets.
So what I’m going to do to get past that, is display "less than a minute ago" for these tweets. it’s not as specific as showing how many seconds ago, but having minutes as the lowest denominator is fine, no? Especially since I’m only checking for new tweets once a minute anyways, I think it makes more sense that way.