Christian Heilmann

Author Archive

TTMMHTM: Apptember, fonts to embed, time API, mobile frameworks and charity hacks

Wednesday, September 2nd, 2009

Things that made me happy this morning:

  • There is a new cafe in my street: Maria e Munti - new cafe around my streetMaria e Munti - new cafe around my street
  • Font Squirrel has a repository of free fonts to embed via @font-face
  • Apptember is a one-month hackathon, no rules, no business model, no prices – just build some good stuff
  • Man builds house from Lego – now this is dedication.
  • Some very impressive photorealistic Star Wars Illustrations
  • Blockchalk has an API now and I am impressed with the ease of approach to keep it totally open
  • Both jQTouch and Pretty Mobile are frameworks to make mobile web development easier
  • Baseline is a typographical CSS framework
  • The Time API is a very clever semantic API that understands natural language to turn it into timestamps
  • Browser for the better is a Microsoft campaign that provides food for homeless people when you upgrade to IE8. Seems a bit stretched, but hey upgrade is good!
  • Charity Hack is a hackday by Paypal an Justgiving on 19-20th of September in London
  • Top Trumps 3D is an interesting concept of Top Trumps with 2D barcodes that turn into augmented reality games.

Converting a data table on the web to an autocomplete translator with YQL and YUI

Monday, August 31st, 2009

During the Summer of Widgets hack event last weekend, Tomas Caspers, Nina Wieland and Jens Grochdreis had the idea of creating a translation tool to translate from the local Cologne accent to German and back.

For this, they found a pretty impressive data source on the web, namely this web site by Reinhard Kaaden. The task was now to turn this into a fancy interface to make it easy for people to enter a “Kölsch” term and get the German equivalent and vice versa. For this, I proposed YQL und YUI and here is a step-by-step explanation of how you can do it.

You can see the final outcome here: Deutsch-Kölsch übersetzer
or by clicking the screenshot:

Deutsch-Koelsch Uebersetzer by  you.

Step 1: Retrieve and convert the data

A very easy way to get data from the web is using YQL. In order to get the whole HTML of the source page all we had to do is select * from html where url='http://www.magicvillage.de/~reinhard_kaaden/d-k.html'. That gave us the whole data though and we only wanted to get the content of the tables.

Using Firebug and looking up some XPATH we came up with the following statement that would give us the language pairs as German-Koelsch inside paragraphs: //table[1]/tr/td/p[not(a)]. The not(a) statement is needed to filter out the A-Z navigation table cells. We chose JSON as the output format in YQL and dktrans as the callback function name.

All in all this gave us a URL that would load the data we wanted and send it to the function dktrans once it has been pulled:



All that had to go in there to create the Autocomplete controls was more or less 100% copied from the simple Autocomplete example on the YUI site.
First thing is to get some handlers to the input fields I want to populate with the translation data:

var di = YAHOO.util.Dom.get('deutschinput');
var ci = YAHOO.util.Dom.get('koelschinput');

Then you need to instantiate the data source for the autocomplete and give it the language array. As a responseSchema you can define a field called term:

dktransdata.cologneDS = new YAHOO.util.LocalDataSource(
dktransdata.koelsch
);
dktransdata.cologneDS.responseSchema = {fields:['term']};

Next you need to instantiate the AutoComplete widget. This one gets three parameters: the input element, the output container and the data source. You can set useShadow to get a small dropshadow on the container:

dktransdata.cologneAC = new YAHOO.widget.AutoComplete(
'koelschinput','koelschoutput',dktransdata.cologneDS
);
dktransdata.cologneAC.useShadow = true;

This turns the input of the Cologne language into an Autocomplete, but it doesn’t yet populate the other field. For this we need to subscribe to the itemSelectEvent of the AutoComplete widget. The event handler of that event gets a few parameters, the text content of the chosen element is the first element of the third element in the second parameter (this is explained in detail on the YUI site). All you need to do is set the value of the other field to the corresponding element of the translation maps we defined:

dktransdata.cologneAC.itemSelectEvent.subscribe(cologneHandler);
function cologneHandler(s,a){
di.value = dktransdata.dk[a[2][0]];
}

All that is left is to do the same for the German to Cologne field:


dktransdata.germanDS = new YAHOO.util.LocalDataSource(
dktransdata.deutsch
);
dktransdata.germanDS.responseSchema = {fields:['term']};
dktransdata.germanAC = new YAHOO.widget.AutoComplete(
'deutschinput','deutschoutput',dktransdata.germanDS
);
dktransdata.germanAC.useShadow = true;
dktransdata.germanAC.itemSelectEvent.subscribe(germanHandler);
function germanHandler(s,a){
ci.value = dktransdata.kd[a[2][0]];
}

Step 5:Putting it all together

You can see the full source of the translation tool on GitHub and can download it there, too.
Of course we are not really finished here as this only works in JavaScript environments. As the translator was meant to be a widget though, this was not an issue. That the autocomplete does not seem to work on mobiles is one, though :).

Making this work without JavaScript would be pretty easy, too. As the data is returned in JSON we can also use this in PHP and write a simple form script If wanted, I can do that later.

Tutorial: scraping and turning a web site into a widget with YQL

Tuesday, August 25th, 2009

During the mentoring sessions at last weekend’s Young Rewired State one of the most asked questions was how you can easily re-use content on the web. The answer I gave was by using YQL and I promised a short introduction to the topic so here it is. What we are going to do here and now is to turn a web sites into a widget with YQL and a few lines of JavaScript:

Turning a web page into a widget with yql by  you.

Say you have a web site with a list of content and you want to turn it into widget to include in other web sites. For example this list of funny TV facts (which is really a Usenet classic). The first thing you need to do with this is to find out its structure, either by looking at the source code of the page or by using Firebug:

Note: The original joke site is dead, so I fixed the widget to use another one. The concept still works though.

Finding out the HTML structure by using firebug by  you.

If you right-click on the item in Firebug you can get the XPATH to the element you want to reach – we’ll need this later. In this case the xpath is /html/body/ul/li[92] which gets us that single element. If we want all TV facts, then we need to shorten this to //ul/li.

Copying the XPATH in firebug by  you.

The next step is to go to the YQL console and enter the following statement.

select * from html where url='http://www.dcs.gla.ac.uk/~joy/fun/jokes/TV.html' and xpath='//ul/li'

This follows the syntax select * from html where url='{url}' and xpath='{xpath}'. This will result in YQL pulling the page content and giving it back to us as XML:

Yahoo! Query Language - YDN by  you.

Notice that YQL has inserted P elements in the results. This is because YQL runs the XML through HTML Tidy to remove invalid HTML. This means that we need to alter our XPATH to be //ul/li/p to get to the texts.

The next step is to define the output format as JSON, define a callback function with the name funfacts, hit the test button, wait for the results and copy and paste the REST query.

Steps to get the HTML in the right format by  you.

That’s all you need to do. You will now have the HTML as a JavaScript-readable object and all you need to do is to define a function called funfacts that gets the data from YQL and add another SCRIPT node with the REST URL you copied from YQL as the src attribute:


The function will get the data from YQL as you were able to see in the console. Therefore getting to the TV facts is as easy as accessing o.query.results.p.

The rest of the functionality is plain and simple DOM Scripting. Check the comments for explanations:

Funny TV facts

Some funny TV facts

Add a bit of styling and you’ll end up with quite a cool little widget powered by the data on the jokes site. Check the source of the demo to see all the CSS needed.

That is all there is to it – get scraping!

TTMMHTM: 8 bit lego animation, blind phreaker, code collaboration, uk postcodes and SVG for IE

Monday, August 24th, 2009

Things that made me happy this morning:

Wow, so that is how memes happen – #songsincode

Friday, August 21st, 2009

Update: I did not start the songsincode meme, I just inspired and was a catalyst of it. The guys in Frogtrade started the craziness and the full story is available here

I am quite amazed just how much the #songsincode thing is mushrooming right now on twitter. Some months ago I had posted this one:

{
'name':'Lola',
'occupation':'showgirl',
'fashion':['music','passion'],
'location':[-22.970834, -43.191665]
}

To me this retweet by Adam Hepton kicked the thing off but other people have disputed that. It would be interesting to see if mine was really the first and if Adam was the first to use the hashtag (full story is now available here).

In any case, this is more geeky fun than I had ever imagined and it is interesting to see just how many people are utterly confused by it. It is also sad to see how spammers work Twitter hard.

If you don’t get it: songsincode tries to display either a title of a song or part of its lyrics (as some songs are more known by the refrain than their title – for example there is no such thing as “all the lonely people” by The Beatles) in code. This could be PHP, JavaScript or any other language. For this, sad geeksclever people use code constructs like if statements and loops to describe conditions and repetition. If you don’t get it, don’t feel left behind, it is hard core geek.

Here are some more #songsincode from me:

Bob Marley: I shot the sheriff:

var i = {shot:{sheriff:true,deputy:false}}

The Ramones: Sheena is a punk rocker

while(Sheena.type=='punk rocker'){Riff.repeat();}

Reel2Reel: I like to move it

x=0;while(x<100){it.style.left=x+'px';x++};i.likeTo=true;

Procol Harum: A whiter shade of Pale

for(i in dance){if(i=='light fandango'){continue;}};cartwheels.turn({floor);me.feeling='seasick';crowd.call('more');

Queen: Bohemian Rhapsody

if(man.silhouetto.size=='small'){scaramouche.do(fandango);if(thunderbolt&&lightning){me.frightened=true}}

Sisters of Mercy: If you don’t see me

if(!u.see('me')||u.loc!=me.loc){me.exist=false;you.makeBelieve=1;}if(eye.see==0){heart.break=0};if(you.leave){me.disappear()}

Blood Hound Gang: The bad touch

do{if(!(you+me) >= mammals){ doit('like they do on the discovery channel') }while(theTouch == 'bad')}

Leonard Cohen: Everybody Knows

{'everybodyKnows':true,'facts':{'boat':'sinking','captain':'lied'},'co':'hen'}

Simon and Garfunkel: Sound of Silence

oldfriend='#000';talk++;while(vision<100){vision++;};sleep.seed();brain.vision=vision;r.mains='still;volume=0; #songsincode

And of course the rick roll:

if(we!=strangersToLove){u.knowRules=1;i.knowRules=1;me.think{committment:'full'};otherGuy:{type:'any',delivery:'false'};}

Keep them coming!