Using MaxMind's GeoIP.js

Q&A's, tips, howto's
Locked
hilti
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

Using MaxMind's GeoIP.js

Post by hilti »

This one might become useful in your web projects. It calls an external javascript file on MaxMinds geolocation servers and returns your browsers location.

I'm using it in a Dragonfly project to detect the users location and serving current weather condition.

Code: Select all

(set 'jsdata (get-url "http://j.maxmind.com/app/geoip.js"))
(set 'location (find-all [text]'(.*)'[/text] jsdata))
(print location)
And something cool - it's free if you put a link on your website :-)

http://dev.maxmind.com/geoip/javascript
Free use of the service is allowed with attribution in the following form:

This website uses <a href="http://www.maxmind.com/en/javascript">GeoIP Javascript from MaxMind</a>
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Using MaxMind's GeoIP.js

Post by Lutz »

Here are some modifications of the find-all expression for further data transformation.

The original find-all returns this (can also use {,} in this case):

Code: Select all

> (set 'location (find-all {'(.*)'} jsdata))
("'US'" "'United States'" "'Altadena'" "'CA'" "'California'" "'34.2005'" "'-118.1362'" 
 "'91001'" "'626'" "'803'")
> 
Get rid of single quotes by returning only the $1 sub-expression:

Code: Select all

> (set 'location (find-all {'(.*)'} jsdata $1))
("US" "United States" "Altadena" "CA" "California" "34.2005" "-118.1362" "91001" 
 "626" "803")
Transform data types using float on the sub-expression, the second $1 is the return value for the float function when conversion fails:

Code: Select all

> (set 'location (find-all {'(.*)'} jsdata (float $1 $1)))
("US" "United States" "Altadena" "CA" "California" 34.2005 -118.1362 91001 626 803)
> 

hilti
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

Re: Using MaxMind's GeoIP.js

Post by hilti »

Thanks so much Lutz! Especially for this explanation:
Transform data types using float on the sub-expression, the second $1 is the return value for the float function when conversion fails:
I didn't notice that find-all is so powerful.
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

Locked