newLISP Fan Club

Forum => newLISP in the real world => Topic started by: hilti on February 12, 2013, 06:51:59 AM

Title: Using MaxMind's GeoIP.js
Post by: hilti on February 12, 2013, 06:51:59 AM
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

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>
Title: Re: Using MaxMind's GeoIP.js
Post by: Lutz on February 12, 2013, 09:21:58 AM
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)
>
Title: Re: Using MaxMind's GeoIP.js
Post by: hilti on February 12, 2013, 09:42:21 PM
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.