Christian Heilmann

Posts Tagged ‘twitter’

Using Twitter as a data provider to automatically fill forms

Thursday, January 8th, 2009

I hate having to enter all my details when I fill forms – especially when I know for a fact that I’ve entered them before. Looking at Smashing Magazine’s Twitter Avatar WordPress Plugin I got to play with the show API of twitter, which gives you user information.

For example to get my information in Twitter you can just call http://twitter.com/users/show/codepo8.xml and get all I entered. This is also available in JSON and with a callback parameter. You can also do a lookup by email, by calling http://twitter.com/users/show/show.xml?email=you@yourserver.com. Using both of these together, I thought it’d be a good idea to use this API to automatically fill forms.

I’ve created a quick proof of concept: automatically filling forms with twitter data . If you want to use the script yourself (twitterfill.js), simply add it to the page with your form and give it the right parameters in the init method. For the demo page this is:




The parameters are label, which is the text of the button, loading, which is the loading message, mailfield, which is the ID of the email field (the button will be inserted after it), and name,location and url which are the IDs of the form fields you want to fill.

TTMMHTM: Was it a good day, food info, scripting enabled video and twitter

Tuesday, January 6th, 2009

Things that made me happy this morning:

Detecting and displaying the information of a logged-in twitter user

Monday, January 5th, 2009

Wouldn’t it be cool (and somehow creepy) to greet your visitors by their twitter name, and maybe ask them to tweet a post? It can be really easily done.

Check it out yourself: Hello Twitter Demo
Update: this is not working any longer. Twitter have discontinued this functionality because of the phishing opportunities it posed.

This page should show you your avatar, name, location and latest update when you are logged into twitter. If nothing show up you either are not logged in or already exceeded your API limit for the hour (if you have twhirl running, like me, that can happen fast)

This is actually very easy to do as a logged-in twitter user can be detected with a simple API call in a script node:


http://twitter.com/statuses/user_timeline.json?count=1&callback=yourcallback

All you need to do is provide a callback function that gets the data provided by the API and get the right information out. The demo does this by assembling a string:





Trying to think of a cool use for this that is not spooky :)

Show the world your Twitter type (using PHP and Google Charts)

Sunday, November 23rd, 2008

I just had a bit of fun with Twitter and the Google charts API. You can now add an image to your blog, web site or wherever and show a picture of what kind of a twitter user you are. All you need to do is embed an image and give it the right source:

For example my user name is codepo8, which would be:

And the resulting image is:

For John Hicks for example it is:

And the resulting image is:

How it is done and how to “change stuff”

You can download the source code and have a play with this (I hope this will not spike my traffic :) so it might go offline if that is the case). There’s really not much magic to this:

First I get the user name and filter out nasties:


$user = $_GET[‘user’];
$isjs = “/^[a-z|A-Z|_|-|$|0-9|.]+$/”;
if(preg_match($isjs,$user)){

Then I set the content type to show the image and use cURL to get the information from the user’s twitter page.

header(‘Content-type:image/png’);
$info = array();
$cont = get(‘http://twitter.com/’.$user);

I get the information using regular expressions and put them in an associative array:

preg_match_all(‘/([^>]+)/msi’,$cont,$follow);
$info[‘follower’] = convert($follow[1][0]);
preg_match_all(‘/([^>]+)/msi’,$cont,$follower);
$info[‘followed’] = convert($follower[1][0]);
preg_match_all(‘/([^>]+)/msi’,$cont,$updates);
$info[‘updater’] = convert($updates[1][0]);

The convert function removes the comma punctuation added by twitter and makes sure the values are integers.

I then need to determine which of the three values is the highest and define a scaling factor as the Google API only allows values up to 100. I then check what the type of the user is by getting the right array key and change the values for displaying.


$max = max($info);
$convert = 100 / $max ;
foreach($info as $k=>$f){
if($f = $max){
$type = $k;
}

$disp[$k] = $f * $convert;
}


I check the type and assemble the display string accordingly:

if($type = ‘updater’){
$t = ’ is an ‘;
}

if($type = 'follower'){
$t = ' is a ';
}

if($type = ‘followed’){
$t = ’ is being ‘;
}

$title = $user . $t . $type;


I assemble the labels array and the values array and add all up to the correct Google charts API url. I use cURL to get the image and echo it out.

$out = array();
foreach($info as $k=>$i){
$out[] = $k.’+(‘.$i.’)’;
}

$labels = join($out,’|’);
$values = join($disp,’,’);
$img = get(‘http://chart.apis.google.com/chart?cht=p3&chco=336699&’.
‘chtt=’.urlencode($title).’&chd=t:’.$values.
‘&chs=350×100&chl=’.$labels);
echo $img;
}


The rest are the cURL and convert helper functions.

function get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
return $feed;
}

function convert($x){
$x = str_replace(‘,’,’‘,$x);
$x = (int)$x;
return $x;
}

You like?

Faster version (one cURL, instead of two)

Instead of setting the PNG header and echoing out the image you can also just set a location header at the end and redirect the URL request to Google’s servers. I guess they have more bandwidth. :)

GreaseMonkey script to show if a twitter user follows you or not

Wednesday, October 22nd, 2008

As requested by Josh here’s a greasemonkey script that shows if a certain user follows you on twitter by adding a (follows you) to their name on their profile page.

It works – kinda. As there is no way to get a list of all the people a certain twitter user follows on one page (unless I missed something in the API - help please?) the script first checks if you are in the 30 first followers on the page (the thumbs) and if you are not it loads the “friends” page of a certain user and checks if you are in that page. This covers 60 cases, but not all. Oh well…

Source:


// UserScript
// @name Follower
// @namespace twittertools
// @description Shows in an obvious way if a certain twitter user is following you
// @include http://twitter.com/
// /UserScript

(function(){
if(document.getElementById(‘profile_link’)){
var you = document.getElementById(‘profile_link’).href.replace(/.*//,’‘);
var friends = document.getElementById(‘friends’);
var header = document.getElementsByTagName(‘h2’)[0];
var friends = document.getElementById(‘friends’);
if(friends.innerHTML.indexOf(‘/’+you)!==-1){
var side = document.getElementById(‘friends’);
var header = document.getElementsByTagName(‘h2’)[0];
header.innerHTML += ’ (follows you)’;
} else {
var url = window.location.href + ‘/friends’;
var isme = ‘http://twitter.com/’ + you;
GM_xmlhttpRequest({
method: ‘GET’,
url:url,
onload: function(responseDetails) {
if(responseDetails.responseText.indexOf(isme)!==-1){
header.innerHTML += ’ (follows you)’;
}

}
});
}

}
})();