Using MaxMind's GeoIP.js

Started by hilti, February 12, 2013, 06:51:59 AM

Previous topic - Next topic

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.



(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">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%22%3EGeoIP">http://www.maxmind.com/en/javascript">GeoIP Javascript from MaxMind</a>
--()o Dragonfly web framework for newLISP

http://dragonfly.apptruck.de\">http://dragonfly.apptruck.de

Lutz

#1
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):



> (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:



> (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:



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

hilti

#2
Thanks so much Lutz! Especially for this explanation:


QuoteTransform 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\">http://dragonfly.apptruck.de