Using jQuery and JavaScript to backup Twitter tweets
jQuery is a great JavaScript library and I asked myself how I can use it to backup my Twitter tweets. Of course, there are a lot of Twitter backup services available in the web, among them:
Especially for paranoid people, all these services (except of TweetBackup) have one disadvantage: they ask for your Twitter password. I wondered whether I can implement a backup service without (!) a password and – as a personal challange – with JavaScript, especially jQuery. The answer is: yes and no. I can write such a client, but it requires some lines of PHP code. What did I do?
The basic problem of direct Twitter access from a given website is the browser security feature that forbids cross-site AJAX calls. So, my first step was to write a proxy on my server to access Twitter pages directly with JavaScript. Of course, you can have a great Proxy configuration on Apache instead, but this was too easy. Here is the proxy script – working for any given URL, not only for Twitter (after adapting the CURLOPT_URL):
$ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, 'http://twitter.com/'.$_REQUEST['username'] .'?page='.$_REQUEST['page']); curl_setopt ($ch, CURLOPT_USERAGENT, 'twitterpark.com'); curl_setopt ($ch, CURLOPT_HEADER, 1); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_TIMEOUT, 120); $result = curl_exec ($ch); $result = substr($result, $hsize); curl_close($ch); echo $result;
As you can see, I request Twitter page by page as you would do it in your browser. When the result is coming in, the browser receives a string that can be traversed by jQuery. If you have a look at the HTML code of a Twitter page, you can easily identify, what CSS is used to format tweets. This CSS is our marker and can be used as a jQuery selector (assuming res is containing the Twitter page received by the proxy):
$("span.entry-content",res).each(function(i)
{
lastget++;
inter=$(this).html(); // inter now contains your tweet
});
I add date/time and a link to the tweet (e.g. http://twitter.com/selfreferential/status/1189630438).
You can download, modify and redistribute the gettw.php (PHP proxy script) and the concerning backuptwitter.htm (JavaScript part) freely. You can see the script in action on this page.
Please note, that I am using jQuery 1.2.6 (not the current version!), although it should work with the current version, too. The script is fully functional. After processing the backup, you can copy the CSV formatted backup file from your browser window. If you have any time, you may improve it:
- Implemented it more efficient
- Print out results as soon as they are retrieved and not only at the end
- automatically add the backup to the clipboard after finishing
- …
If you have any questions or suggestions or an improved version, please don’t hesitate to leave a comment.
You can find my Twitter news service here: http://www.twitterpark.com or your follow the tweets about Twitter service at http://twitter.com/selfreferential. The Twitter feed for this blog you can find at http://twitter.com/lesereins

am 26. Februar 2009 um 07:30 Uhr.
[...] LeserEins » Blog Archive » Using jQuery and JavaScript to backup Twitter tweets [...]