Christian Heilmann

You are currently browsing the Christian Heilmann blog archives for June, 2009.

Archive for June, 2009

Safer JSON-P? An interesting feature of the Bing API

Monday, June 15th, 2009

I just looked through the API of Microsoft’s new Bing search (which is really a re-branding of the live search – also, switch to “low bandwidth view” to be able to use the docs much more smoothly) and I found an interesting step in protecting code from throwing errors.

When you provide a JSON output for developers it does make sense to also allow for a callback parameter. That way your code can be used in script nodes without having to use any backend at all. If you for example provide an API to return the names of the Beatles you could have a data endpoint like getBeatles and a parameter for output format:

http://example.com/API/getBeatles?output=json

The return data then will be:

{
“members”:
[

‘Paul’:{ ... more info … },
‘Ringo’:{ ... more info … },
‘John’:{ ... more info … },
‘George’:{ ... more info … }
]

}

This I cannot use in JavaScript without hacks. I’d need to eval (or to be safe JSON parse) the results and with conventional Ajax I cannot reach data outside my domain. To make JSON work as easy as possible you can provide a callback parameter.

http://example.com/API/getBeatles?output=json&callback=eleanorRigby

This should wrap the code in a function call which means the output is already evaluated and the user has to define a callback method to read that information. The output would be:

eleanorRigby({
“members”:
[

‘Paul’:{ ... more info … },
‘Ringo’:{ ... more info … },
‘John’:{ ... more info … },
‘George’:{ ... more info … }
]

});

If I define a function like eleanorRigby(o){} o will be the object returned from the data and I can use it immediately:


Now the issue there is if eleanorRigby is not defined it throws an error.

The Bing API is the first instance where I have seen that they worked around that as the output is this:


if(typeof eleanorRigby 'function') eleanorRigby(
{

"SearchResponse":
{

Version",
"Query":
{

SearchTerms hard day's night"
},
"Translation":
{

"Results":
[

{TranslatedTerm harten Tag-Nacht "}
]

}
}

} /* pageview_candidate */);

I have no clue what the /* pageview_candidate */ is about and frown upon omitting the {} of the if statement, but I must say I do like this.

The issue is now that errors are silent, which might make debugging a pain. Maybe a better option would be to have an error case where the API writes out an error to the console when the callback is not defined:

if(typeof callback = ‘function’) {
callback(... data … );
} else {
if (typeof console!==’undefined’ &&
typeof console.log !== ‘undefined’){
console.log(‘Error: Callback method not defined’);
}

}

All in all an interesting approach though!

TTMMHTM: Piracy figures, GIF or Jiff,Pixar Short, Blind Prom,Page Speed, Bing API and Tetris

Friday, June 12th, 2009

Things that made me happy this morning:

Good to know:

Everybody say “awwww”

Code stuff

Erm, what?

Postcode from latitude and longitude or even IP – fun with Geo APIs and YQL

Tuesday, June 9th, 2009

One of the more complex things about GeoFill was to get postcode information from an IP. However with a collection of APIs and a collated YQL statement even this was possible.

The first thing I needed to get was the IP of the user. This is done with the GeoIP API based on the GeoLite API from MaxMind. This is available as an open table in YQL and can be used thus:

select * from ip.location where ip=”“

Try the lookup in the console or check the lookup result

Response”: {
“Ip”: “216.39.58.17”,
“Status”: “OK”,
“CountryCode”: “US”,
“CountryName”: “United States”,
“RegionCode”: “06”,
“RegionName”: “California”,
“City”: “Sunnyvale”,
“ZipPostalCode”: “94089”,
“Latitude”: “37.4249”,
“Longitude”: “-122.007”,
“Gmtoffset”: “-8.0”,
“Dstoffset”: “-7.0”
}

This gives us a lot of information. What’s really important here is latitude and longitude, as this can be used in the flickr.places API to get a where on earth ID which is a much more defined identifier:

select * from flickr.places where (lat,lon) in (
select Latitude,Longitude from ip.location where ip=””
)

Try the flickr places call in the console or check the flickr result

“places”: {
“accuracy”: “16”,
“latitude”: “37.4249”,
“longitude”: “-122.007”,
“total”: “1”,
“place”: {
“latitude”: “37.371”,
“longitude”: “-122.038”,
“name”: “Sunnyvale, California, United States”,
“place_id”: “P_ls_fybBJwdHP8t”,
“place_type”: “locality”,
“place_type_id”: “7”,
“place_url”: “/United+States/California/Sunnyvale”,
“timezone”: “America/Los_Angeles”,
“woeid”: “2502265”
}

}


Here the interesting part is the woeid which we can use to dig deeper into geo.places:

select * from geo.places where woeid in (
select place.woeid from flickr.places where (lat,lon) in (
select Latitude,Longitude from ip.location where ip=””
)

)

Try the geo places call in the console or check the geo places result

The result is all the information you’d ever want.

“place”: {
“lang”: “en-US”,
“xmlns”: “http://where.yahooapis.com/v1/schema.rng”,
“yahoo”: “http://www.yahooapis.com/v1/base.rng”,
“uri”: “http://where.yahooapis.com/v1/place/28751237”,
“woeid”: “28751237”,
“placeTypeName”: {
“code”: “22”,
“content”: “Suburb”
},
“name”: “Fairgrounds”,
“country”: {
“code”: “US”,
“type”: “Country”,
“content”: “United States”
},
“admin1”: {
“code”: “US-CA”,
“type”: “State”,
“content”: “California”
},
“admin2”: {
“code”: “”,
“type”: “County”,
“content”: “Santa Clara”
},
“admin3”: null,
“locality1”: {
“type”: “Town”,
“content”: “San Jose”
},
“locality2”: {
“type”: “Suburb”,
“content”: “Fairgrounds”
},
“postal”: {
“type”: “Zip Code”,
“content”: “95112”
},
“centroid”: {
“latitude”: “37.326611”,
“longitude”: “-121.878441”
},
“boundingBox”: {
“southWest”: {
“latitude”: “37.275379”,
“longitude”: “-121.89254”
},
“northEast”: {
“latitude”: “37.330879”,
“longitude”: “-121.808723”
}

}
}

Adding address information automatically to forms with GeoFill

Sunday, June 7th, 2009

One of the things I mentioned in my latest talk and the follow-up Q&A at Gumtree was that I am very interested in Geolocation and automatically detecting the user’s location to save them having to enter all the same info over and over again. Sure, location services like Fire Eagle and autofill options in browsers already do these kind of things, but they are power user toys for now.

Using Rasmus Lerdorf’s GeoIP, YQL and Yahoo Geo I’ve put together a small JavaScript wrapper that does exactly this for you:

GeoFill - automatically filling form data with geo information by  you.

GeoFill (also available on GitHub) allows you to use two different methods to automatically fill forms with geo information:

  • geofill.find(properties) does an IP lookup of the current user and tries to get the geographical data from that one.
  • geofill.lookup(properties,postcode) tries to get the geographical data from the postcode provided in your form.

Both methods take an object which lists the form field IDs to be filled as the parameter. The data returned is city, country, postcode, latitude and longitude.

Both methods also allow you to define a callback function to handle errors or re-use the data in other ways than filling form fields.

For example to get the information connected with a post code you could simply do the following:

geofill.lookup(
{

callback:function(o){
console.log(o);
}

},’wc2h8ad’);

The returned object is:

{
city:”London”,
country:”United Kingdom”,
latitude:”51.51384”,
longitude:”-0.12857”,
postcode:”WC2H 8AD”
}

Useful? In any case it made me play more with the Geo API. Don’t forget that on the 7th of July I’ll be giving a free Tech Talk on Yahoo Placemaker where this will be one of the demos (probably, knowing myself I’ll have hacked other stuff by then). You can sign up for the tech talk on upcoming.

TTMMHTM: Wii, Star Trek office, Han Solo PI, Microsoft fix!

Saturday, June 6th, 2009

Things that made me happy this morning

Inspiring and fun

Tech stuff

My stuff:

Videos: