[WebDNA] API Code - IP to Country code and zip Code to us census (2000)

This WebDNA talk-list message is from

2011


It keeps the original formatting.
numero = 106392
interpreted = N
texte = --bcaec53f8ebf9f5c88049edb9909 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi, I'll be releasing a bunch more of these. This is the code - rough as it may be - to hook into infochimp api structure. This particular api converts an IP address to more useful information such as country code, zip/postal code and city + long/lat It's better for city I would say, than an actual specific zip/postal area - it says I am across town from where I actually am, but it is very useful. Combined with other api's - I'll release the code to census data next. Another api from info chimp. that converts lat/long or zip code to some 200= 0 census data. http://www.infochimps.com/datasets/digital-element-ip-intelligence-geolocat= ion I'm going to start posting code especially about api integration. Here is the post about the code below: http://www.bastedo.com/2011/03/api-digital-element-ip-intelligence-geolocat= ion/#more-95 [!]--depending on your server you might be able to use the [ipaddress] tag--[/!] [ListMIMEHeaders] [showif "[name]"=3D"X-Real-IP"] [text]ip_address=3D[value][/text] [/showif] [showif "[name]"=3D"User-Agent"] [text]thebrowserType=3D[value][/text] [/showif] [/ListMIMEHeaders] [function name=3DParseIP] [!]--I pass in a sessionID and record the info for the session once per session--[/!] [!]--------Error Check and Working Variables-------[/!] [!]----set these to 0 to turn off----[/!] [text]errorCatch=3D1[/text] [text]showVariables=3D1[/text] [!]--------Technical Variables-------[/!] [text]updateIDNumber=3D0[/text] [text]theBrowserStatus=3DY[/text] [!]--Date and time stamp encoding ready for use --[/!] [text]date_num=3D[math]{[date]}[/math][/text] [text]time_num=3D[math]{[time]}[/math][/text] [!]--path variable--[/!] [text]pagePath2=3D[/text] [!]----I found a lot of bots - especially google - and this is an easy way to get rid of a lot of them - but should be built into a complete table -----[/!] [table name=3DbrowserBotWords&fields=3DbotWord] googlebot Baiduspider nagios crawler bingbot spider spyder siteBot Slurp dotbot [/table] [!]--------Function Variables-------[/!] [text]theDOMAIN=3Dapi.infochimps.com[/text] [text]thePATH=3D/web/an/de/[/text] [text]theMethod=3Dgeo[/text] [text]theResultTYPE=3Djson[/text] [!]----Get your own api key and insert below--[/!] [text]theAPIkey=3DXXXXXXXXXXXXX[/text] [!]--the ip address - how ever you get it - it cannot have extra "0"s as in .092 and must be .92--[/!] [text]theCALL=3D[ip_address][/text] [!]--an id I create for the data --[/!] [text]theAugmentID=3D[math][lookup db=3D[pagePath2]databases/XXXXXX.db&lookInField=3DnumberID&value=3DaugmentI= D&returnField=3Dnumber]+1[/math][/text] [!]--check to see if the session info has been recorded already--[/!] [text]updateSession=3D[lookup db=3D[pagePath2]databases/XXXXXXXX.db&lookInField=3DsessionID&value=3D[MySe= ssionID]&returnField=3DsessionID¬found=3D0][/text] [!]--chek the browser info to see if it is a bot and if it is - do nothing -- [/!] [search table=3DbrowserBotWords&gebotWorddata=3D[blank]] [showif [showVariables]=3D1] results: [numfound]
[/showif] [text]mystring=3D[thebrowserType][/text] [founditems] [showif [showVariables]=3D1] botWord: [botWord]
[/showif] [text]found_at=3D[FindString source=3D[mystring]&find=3D[botWord]][/text] [showif [found_at]>0] [showif [showVariables]=3D1]
---------------------------

Found '[botWord]' in source browserType - we suspect it's a BOT!

---------------------------
[/showif] [!]--set variable to N so that this will not be checked--[/!] [text]theBrowserStatus=3DN[/text] [/showif] [/founditems] [/search] [!]--make sure we know if the session has been recorded--[/!] [If "[updateSession]" =3D "0"] [then][text]updateSession=3D0[/text][/then] [else][text]updateSession=3D1[/text][/else] [/if] [!]--check to see that we have a session id and if we don't then create one --[/!] [!]--Check for session ID--[/!] [showif [sessionID]=3D] [text]MySessionID=3D[date_num]_[time_num]_[cart][/text] [/showif] [showif [sessionID]=3D[raw][sessionID][/raw]] [text]MySessionID=3D[date_num]_[time_num]_[cart][/text] [/showif] [!]--this lists all the ids we have Userid in my example is brought in from a cookie if they are currently registered--[/!] [showif [showVariables]=3D1]

------------------------------------------------------------------<= br> UserID: [UserID]
MysessionID: [MysessionID]
updateSession: [updateSession]
theAugmentID: [theAugmentID]
browserType: [thebrowserType]
browserStatus: [theBrowserStatus]
------------------------------------------------------------------

[/showif] [!]---run the api if the broswer is not a bot and the session has not been previously recorded--[/!] [if "([theBrowserStatus]"=3D"Y") & ("[updateSession]"=3D"0")] [then] [text]MyTestVariable=3D[!] [/!][TCPConnect host=3D[theDOMAIN]][TCPSend skipheader=3DT]GET [thePATH][theMethod].[theResultTYPE]?ip=3D[theCALL]&apikey=3D[theAPIkey] HTTP/1.0[UnURL]%0D%0A[/Unurl]Host: [theDOMAIN][UnURL]%0D%0A%0D%0A[/UnURL][/TCPSend] [/TCPConnect][/text] [!]--it's easy to parse the jason using istwords as we know what the expected results are from the api documentation--[/!] [table name=3DJSON_Convert&fields=3Dfrom,to] { } " [/table] [!]--strip the json and list it out--[/!] [ListWords Words=3D[convertchars table=3DJSON_Convert][MyTestVariable][/convertchars]&Delimiters=3D,] [showif [errorCatch]=3D1] [index]
[/showif] [!]--the structure of the json is that of value:name [ListWords Words=3D[word]&Delimiters=3D:] [showif [index]=3D1] [showif [errorCatch]=3D1] theField: [word]
[/showif] [text]theField=3D[word][/text] [/showif] [showif [index]=3D2] [showif [errorCatch]=3D1] theValue: [word]
[/showif] [text]theValue=3D[word][/text] [/showif] [!]--knowing the names of the fields - just show the field you want to act on--[/!] [!]------------ZIP----------[/!] [showif [theField]=3Dzip] [text]theZip=3D[theValue][/text] [/showif] [!]-----------------------------------[/!] [!]------------Country----------[/!] [showif [theField]=3Dtwo_letter_country] [text]theCountryCode=3D[theValue][/text] [/showif] [!]-----------------------------------[/!] [/ListWords] [showif [errorCatch]=3D1] [index]: [theAugmentID]: [theField]: [theValue]
[/showif] [!]--if this is a new session then record it in the database--[/!] [showif [updateSession]=3D0] [showif [errorCatch]=3D1] News Session! Replacing& Appending
[/showif] [!]------- header file ----[/!] [!]------id UserID sessionID region city zip area_code in_dst zip_country country country_code postal_code city_code gmt_offset region_code continent_code conn_speed two_letter_country metro_code longitude internal_code lat zip_code_ignored dateNum timeNum browserType theIP------[/!] [replace db=3D[pagePath2]databases/XXXXXX.db&eqiddatarq=3D[theAugmentID]&eqsessionID= datarq=3D[sessionID]&append=3DT]id=3D[theAugmentID]&[theField]=3D[theValue]= &UserID=3D[UserID]&sessionID=3D[sessionID]&dateNum=3D[date_num]&timeNum=3D[= time_num]&browserType=3D[thebrowserType]&theIP=3D[theCall][/replace] [/showif] [!]--show content based on two letter country code -- [/!] [showif [theField]=3Dtwo_letter_country] [showif [theValue]=3Dca]aaaa[/showif] [hideif [theValue]=3Dus]bbb[/hideif] [/showif] [/listwords] [!]---if the countrycode is in the us the run the census api - this can be aded to the above two_letter_countrycode--[/!] [showif [theCountryCode]=3Dus] [showif [errorCatch]=3D1] theZip: [theZip]
[/showif] [!]--------Function Variables-------[/!] [text]theDOMAIN=3Dapi.infochimps.com[/text] [text]thePATH2=3D/web/an/census/[/text] [text]theMethod=3Dcombined[/text] [text]theResultTYPE=3Djson[/text] [text]theAPIkey=3DXXXXXXXXXX[/text] [text]theCALL2=3D[theZip][/text] [text]MyTestVariable2=3D[!] [/!][TCPConnect host=3D[theDOMAIN]][TCPSend skipheader=3DT]GET [thePATH2][theMethod].[theResultTYPE]?zip=3D[theCALL2]&apikey=3D[theAPIkey] HTTP/1.0[UnURL]%0D%0A[/Unurl]Host: [theDOMAIN][UnURL]%0D%0A%0D%0A[/UnURL][/TCPSend] [/TCPConnect][/text] [ListWords Words=3D[convertchars table=3DJSON_Convert][MyTestVariable2][/convertchars]&Delimiters=3D,] [showif [showVariables]=3D1] theField: [word]
[/showif] [listwords words=3D[word]&Delimiters=3D:] [showif [index]=3D1] [showif [showVariables]=3D1] theField: [word]
[/showif] [text]theFieldName=3D[word][/text] [/showif] [showif [index]=3D2] [showif [showVariables]=3D1] theValue: [word]
[/showif] [!]--if I know the databse info set is fixed then I run addfields once and set up the header of the databse file then I take it out - so you'd have to do that once for this api to record the field names to your db--[/!] [replace db=3D[pagePath2]databases/XXXXXXX.db&eqiddatarq=3D[theAugmentID]&eqsessionI= Ddatarq=3D[sessionID]&append=3DT][theFieldName]=3D[word][/replace] [/showif] [/listwords] [/listwords] [/showif] [showif [errorCatch]=3D1] Replacing Next Number to [theAugmentID]
[/showif] [replace db=3D[pagePath2]databases/XXXXXX.db&eqnumberIDdatarq=3DaugmentID]NUMBER=3D[= theAugmentID][/replace] [/then] [else][/else] [/if] [/function] --bcaec53f8ebf9f5c88049edb9909 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi, I'll=A0 be releasing a bunch more of these.

This is the code= - rough as it may be - to hook into infochimp api structure.
This parti= cular api converts an IP address to more useful information such as country= code, zip/postal code and city + long/lat
It's better for city I would say, than an actual specific zip/postal ar= ea - it says I am across town from=A0 where I actually am, but it is very u= seful.

Combined with other api's - I'll release the code to = census data next. Another api from info chimp. that converts lat/long or zi= p code to some 2000 census data.



http://www.infochimps.com/datasets/digital-elem= ent-ip-intelligence-geolocation

I'm going to start posting c= ode especially about api integration.

Here is the post about the code below:
http= ://www.bastedo.com/2011/03/api-digital-element-ip-intelligence-geolocation/= #more-95


[!]--depending on your server you might be able to use the [ipaddre= ss] tag--[/!]
[ListMIMEHeaders]

[showif "[name]"=3D&quo= t;X-Real-IP"]
=A0=A0=A0 [text]ip_address=3D[value][/text]
[/show= if]

[showif "[name]"=3D"User-Agent"]
=A0=A0=A0 [text= ]thebrowserType=3D[value][/text]
[/showif]

[/ListMIMEHeaders]



[function name=3DParseIP]
[!]--I pass in a sessionID an= d record the info for the session once per session--[/!]

[!]--------Error Check and Working Variables-------[/!]
[!]----set t= hese to 0 to turn off----[/!]
[text]errorCatch=3D1[/text]
[text]showV= ariables=3D1[/text]


[!]--------Technical Variables-------[/!] [text]updateIDNumber=3D0[/text]
[text]theBrowserStatus=3DY[/text]
[!]= --Date and time stamp encoding ready for use --[/!]
[text]date_num=3D[ma= th]{[date]}[/math][/text]
[text]time_num=3D[math]{[time]}[/math][/text]<= br>[!]--path variable--[/!]
[text]pagePath2=3D[/text]


[!]----I found a lot of bots - especia= lly google - and this is an easy way to get rid of a lot of them - but shou= ld be built into a complete table -----[/!]
[table name=3DbrowserBotWord= s&fields=3DbotWord]
googlebot
Baiduspider
nagios
crawler
bingbot
spider
spyde= r
siteBot
Slurp
dotbot
[/table]







= [!]--------Function Variables-------[/!]
[text]theDOMAIN=3Dapi.infochimps.com[/text]
[text]thePATH=3D/web/an/de/[/text]
[text]theMethod=3Dgeo[/text]
[text= ]theResultTYPE=3Djson[/text]
[!]----Get your own api key and insert belo= w--[/!]
[text]theAPIkey=3DXXXXXXXXXXXXX[/text]
[!]--the ip address - = how ever you get it - it cannot have extra "0"s as in .092 and mu= st be .92--[/!]
[text]theCALL=3D[ip_address][/text]
[!]--an id I create for the data --[= /!]
[text]theAugmentID=3D[math][lookup db=3D[pagePath2]databases/XXXXXX.= db&lookInField=3DnumberID&value=3DaugmentID&returnField=3Dnumbe= r]+1[/math][/text]
[!]--check to see if the session info has been recorded already--[/!]
[t= ext]updateSession=3D[lookup db=3D[pagePath2]databases/XXXXXXXX.db&lookI= nField=3DsessionID&value=3D[MySessionID]&returnField=3DsessionID&am= p;notfound=3D0][/text]

[!]--chek the browser info to see if it is a bot and if it is - do noth= ing -- [/!]

[search table=3DbrowserBotWords&gebotWorddata=3D[bla= nk]]


[showif [showVariables]=3D1]
=A0=A0=A0 results: [numfoun= d]<br>
[/showif]

[text]mystring=3D[thebrowserType][/text]
[founditems]
[showif [showVariables]=3D1]
=A0=A0=A0 botWord: [botWord]<br>= ;
[/showif]


[text]found_at=3D[FindString source=3D[mystring]&= amp;find=3D[botWord]][/text]



[showif [found_at]>0]

[showif [showVariables]=3D1]<br>---------------------------<br><br>
Found '[b= otWord]' in source browserType - we suspect it's a BOT!<br> <br>---------------------------<br>
[/showif]
[!]--set va= riable to N so that this will not be checked--[/!]
[text]theBrowserStatu= s=3DN[/text]
[/showif]

[/founditems]
[/search]

[!]--mak= e sure we know if the session has been recorded--[/!]

[If "[updateSession]" =3D "0"]
[then][text]updat= eSession=3D0[/text][/then]
[else][text]updateSession=3D1[/text][/else][/if]

[!]--check to see that we have a session id and if we don= 9;t then create one --[/!]
[!]--Check for session ID--[/!]
[showif [sessionID]=3D]
=A0=A0=A0 [te= xt]MySessionID=3D[date_num]_[time_num]_[cart][/text]
[/showif]

[s= howif [sessionID]=3D[raw][sessionID][/raw]]
=A0=A0=A0 [text]MySessionID= =3D[date_num]_[time_num]_[cart][/text]
[/showif]

[!]--this lists all the ids we have Userid in my example i= s brought in from a cookie if they are currently registered--[/!]

[s= howif [showVariables]=3D1]
<br><br>-------------------------= -----------------------------------------<br>
UserID: [UserID]<br>
MysessionID:=A0 [MysessionID]<br>
up= dateSession: [updateSession]<br>
theAugmentID: [theAugmentID]<b= r>
browserType: [thebrowserType]<br>
browserStatus: [theBrow= serStatus]<br>


------------------------------------------------------------------&= lt;br><br>
[/showif]




[!]---run the api if th= e broswer is not a bot and the session has=A0 not been previously recorded-= -[/!]


[if "([theBrowserStatus]"=3D"Y") & ("[= updateSession]"=3D"0")]
[then]


[text]MyTestVar= iable=3D[!]
[/!][TCPConnect host=3D[theDOMAIN]][TCPSend skipheader=3DT]G= ET [thePATH][theMethod].[theResultTYPE]?ip=3D[theCALL]&apikey=3D[theAPI= key] HTTP/1.0[UnURL]%0D%0A[/Unurl]Host: [theDOMAIN][UnURL]%0D%0A%0D%0A[/UnU= RL][/TCPSend]
[/TCPConnect][/text]

[!]--it's easy to parse the jason using ist= words as we know what the expected results are from the api documentation--= [/!]

[table name=3DJSON_Convert&fields=3Dfrom,to]
{=A0=A0=A0 =
}=A0=A0=A0
"=A0=A0=A0
[/table]

[!]--strip the json and list it out--[/= !]
[ListWords Words=3D[convertchars table=3DJSON_Convert][MyTestVariable= ][/convertchars]&Delimiters=3D,]

[showif [errorCatch]=3D1]
= =A0=A0=A0 <b>[index]</b><br />
[/showif]

[!]--the structure of the json is that of value:name
[ListWords Words=3D[word]&Delimiters=3D:]
[showif [index]=3D1]
= [showif [errorCatch]=3D1]
=A0=A0=A0 theField: <b>[word]</b>&= lt;br />
[/showif]

[text]theField=3D[word][/text]

[/showif]

[sh= owif [index]=3D2]
=A0=A0=A0 [showif [errorCatch]=3D1]
=A0=A0=A0 =A0= =A0=A0 theValue: <b>[word]</b><br />
=A0=A0=A0 [/showi= f]
=A0=A0=A0
[text]theValue=3D[word][/text]
[/showif]

[!]--knowing the names of the fields - just show the field= you want to act on--[/!]

[!]------------ZIP----------[/!]
[showi= f [theField]=3Dzip]

[text]theZip=3D[theValue][/text]

[/showif= ]
[!]-----------------------------------[/!]

[!]------------Country---= -------[/!]
[showif [theField]=3Dtwo_letter_country]
=A0=A0=A0=A0=A0= =A0 [text]theCountryCode=3D[theValue][/text]
[/showif]
[!]-----------= ------------------------[/!]
[/ListWords]



[showif [errorCatch]=3D1]
=A0=A0=A0 [index]= : [theAugmentID]: [theField]: [theValue]<br>
[/showif]


=
[!]--if this is a new session then record it in the database--[/!]
[= showif=A0 [updateSession]=3D0]

[showif [errorCatch]=3D1]
=A0=A0=A0 News Session! Replacing& App= ending<br>
[/showif]

[!]------- header file ----[/!]
[!]= ------id=A0=A0=A0 UserID=A0=A0=A0 sessionID=A0=A0=A0 region=A0=A0=A0 city= =A0=A0=A0 zip=A0=A0=A0 area_code=A0=A0=A0 in_dst=A0=A0=A0 zip_country=A0=A0= =A0 country=A0=A0=A0 country_code=A0=A0=A0 postal_code=A0=A0=A0 city_code= =A0=A0=A0 gmt_offset=A0=A0=A0 region_code=A0=A0=A0 continent_code=A0=A0=A0 = conn_speed=A0=A0=A0 two_letter_country=A0=A0=A0 metro_code=A0=A0=A0 longitu= de=A0=A0=A0 internal_code=A0=A0=A0 lat=A0=A0=A0 zip_code_ignored=A0=A0=A0 d= ateNum=A0=A0=A0 timeNum=A0=A0=A0 browserType=A0=A0=A0 theIP------[/!]

[replace db=3D[pagePath2]databases/XXXXXX.db&eqiddatarq=3D[theAugme= ntID]&eqsessionIDdatarq=3D[sessionID]&append=3DT]id=3D[theAugmentID= ]&[theField]=3D[theValue]&UserID=3D[UserID]&sessionID=3D[sessio= nID]&dateNum=3D[date_num]&timeNum=3D[time_num]&browserType=3D[t= hebrowserType]&theIP=3D[theCall][/replace]



[/showif]

[!]--show content based on two letter country = code -- [/!]
[showif [theField]=3Dtwo_letter_country]

[showif [th= eValue]=3Dca]aaaa[/showif]
[hideif [theValue]=3Dus]bbb[/hideif]

<= br> [/showif]



[/listwords]


[!]---if the countrycode i= s in the us the run the census api - this can be aded to the above two_lett= er_countrycode--[/!]
[showif [theCountryCode]=3Dus]

[showif [erro= rCatch]=3D1]
=A0=A0=A0=A0 theZip: [theZip]<br />
[/showif]
[!]--------Functi= on Variables-------[/!]
[text]theDOMAIN=3Dapi.infochimps.com[/text]
[text]thePATH2=3D/web/an/census/[/= text]
[text]theMethod=3Dcombined[/text]
[text]theResultTYPE=3Djson[/text]
[text]theAPIkey=3DXXXXXXXXXX[/text][text]theCALL2=3D[theZip][/text]





[text]MyTestVariab= le2=3D[!]
[/!][TCPConnect host=3D[theDOMAIN]][TCPSend skipheader=3DT]GET= [thePATH2][theMethod].[theResultTYPE]?zip=3D[theCALL2]&apikey=3D[theAP= Ikey] HTTP/1.0[UnURL]%0D%0A[/Unurl]Host: [theDOMAIN][UnURL]%0D%0A%0D%0A[/Un= URL][/TCPSend]
[/TCPConnect][/text]

[ListWords Words=3D[convertchars table=3DJSON_C= onvert][MyTestVariable2][/convertchars]&Delimiters=3D,]
[showif [sho= wVariables]=3D1]
=A0=A0=A0 theField: <b>[word]</b><br /&g= t;
=A0[/showif]
=A0=A0=A0
=A0[listwords words=3D[word]&Delimiters=3D:]
[showif [= index]=3D1]

[showif [showVariables]=3D1]
=A0=A0=A0 theField:=A0 = [word]<br />
[/showif]
=A0
[text]theFieldName=3D[word][/tex= t]

[/showif]

[showif [index]=3D2]

[showif [showVariables]=3D1]
=A0=A0=A0 theVa= lue:=A0 [word]<br />
[/showif]

[!]--if I know the databse = info set is fixed then I run addfields once and set up the header of the da= tabse file then I take it out - so you'd have to do that once for this = api to record the field names to your db--[/!]


[replace db=3D[pagePath2]databases/XXXXXXX.db&eqiddatarq=3D[the= AugmentID]&eqsessionIDdatarq=3D[sessionID]&append=3DT][theFieldName= ]=3D[word][/replace]
[/showif]
=A0=A0=A0
[/listwords]

=A0= =A0=A0

[/listwords]


[/showif]


[showif [errorCatch]=3D1]
=A0=A0=A0 Replaci= ng Next Number to [theAugmentID]<br>
[/showif]
=A0=A0=A0 [repla= ce db=3D[pagePath2]databases/XXXXXX.db&eqnumberIDdatarq=3DaugmentID]NUM= BER=3D[theAugmentID][/replace]

[/then]
[else][/else]
[/if]


[/function]
--bcaec53f8ebf9f5c88049edb9909-- Associated Messages, from the most recent to the oldest:

    
  1. [BULK] Re: [WebDNA] API Code - IP to Country code and zip Code to us census (2000) (Jym Duane 2011)
  2. [WebDNA] API Code - IP to Country code and zip Code to us census (2000) (David Bastedo 2011)
--bcaec53f8ebf9f5c88049edb9909 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi, I'll be releasing a bunch more of these. This is the code - rough as it may be - to hook into infochimp api structure. This particular api converts an IP address to more useful information such as country code, zip/postal code and city + long/lat It's better for city I would say, than an actual specific zip/postal area - it says I am across town from where I actually am, but it is very useful. Combined with other api's - I'll release the code to census data next. Another api from info chimp. that converts lat/long or zip code to some 200= 0 census data. http://www.infochimps.com/datasets/digital-element-ip-intelligence-geolocat= ion I'm going to start posting code especially about api integration. Here is the post about the code below: http://www.bastedo.com/2011/03/api-digital-element-ip-intelligence-geolocat= ion/#more-95 [!]--depending on your server you might be able to use the [ipaddress] tag--[/!] [listmimeheaders] [showif "[name]"=3D"X-Real-IP"] [text]ip_address=3D[value][/text] [/showif] [showif "[name]"=3D"User-Agent"] [text]thebrowserType=3D[value][/text] [/showif] [/ListMIMEHeaders] [function name=3DParseIP] [!]--I pass in a sessionID and record the info for the session once per session--[/!] [!]--------Error Check and Working Variables-------[/!] [!]----set these to 0 to turn off----[/!] [text]errorCatch=3D1[/text] [text]showVariables=3D1[/text] [!]--------Technical Variables-------[/!] [text]updateIDNumber=3D0[/text] [text]theBrowserStatus=3DY[/text] [!]--Date and time stamp encoding ready for use --[/!] [text]date_num=3D[math]{[date]}[/math][/text] [text]time_num=3D[math]{[time]}[/math][/text] [!]--path variable--[/!] [text]pagePath2=3D[/text] [!]----I found a lot of bots - especially google - and this is an easy way to get rid of a lot of them - but should be built into a complete table -----[/!] [table name=3DbrowserBotWords&fields=3DbotWord] googlebot Baiduspider nagios crawler bingbot spider spyder siteBot Slurp dotbot [/table] [!]--------Function Variables-------[/!] [text]theDOMAIN=3Dapi.infochimps.com[/text] [text]thePATH=3D/web/an/de/[/text] [text]theMethod=3Dgeo[/text] [text]theResultTYPE=3Djson[/text] [!]----Get your own api key and insert below--[/!] [text]theAPIkey=3DXXXXXXXXXXXXX[/text] [!]--the ip address - how ever you get it - it cannot have extra "0"s as in .092 and must be .92--[/!] [text]theCALL=3D[ip_address][/text] [!]--an id I create for the data --[/!] [text]theAugmentID=3D[math][lookup db=3D[pagePath2]databases/XXXXXX.db&lookInField=3DnumberID&value=3DaugmentI= D&returnField=3Dnumber]+1[/math][/text] [!]--check to see if the session info has been recorded already--[/!] [text]updateSession=3D[lookup db=3D[pagePath2]databases/XXXXXXXX.db&lookInField=3DsessionID&value=3D[MySe= ssionID]&returnField=3DsessionID¬found=3D0][/text] [!]--chek the browser info to see if it is a bot and if it is - do nothing -- [/!] [search table=3DbrowserBotWords&gebotWorddata=3D[blank]] [showif [showVariables]=3D1] results: [numfound]
[/showif] [text]mystring=3D[thebrowserType][/text] [founditems] [showif [showVariables]=3D1] botWord: [botWord]
[/showif] [text]found_at=3D[FindString source=3D[mystring]&find=3D[botWord]][/text] [showif [found_at]>0] [showif [showVariables]=3D1]
---------------------------

Found '[botWord]' in source browserType - we suspect it's a BOT!

---------------------------
[/showif] [!]--set variable to N so that this will not be checked--[/!] [text]theBrowserStatus=3DN[/text] [/showif] [/founditems] [/search] [!]--make sure we know if the session has been recorded--[/!] [If "[updateSession]" =3D "0"] [then][text]updateSession=3D0[/text][/then] [else][text]updateSession=3D1[/text][/else] [/if] [!]--check to see that we have a session id and if we don't then create one --[/!] [!]--Check for session ID--[/!] [showif [sessionID]=3D] [text]MySessionID=3D[date_num]_[time_num]_[cart][/text] [/showif] [showif [sessionID]=3D[raw][sessionID][/raw]] [text]MySessionID=3D[date_num]_[time_num]_[cart][/text] [/showif] [!]--this lists all the ids we have Userid in my example is brought in from a cookie if they are currently registered--[/!] [showif [showVariables]=3D1]

------------------------------------------------------------------<= br> UserID: [UserID]
MysessionID: [MysessionID]
updateSession: [updateSession]
theAugmentID: [theAugmentID]
browserType: [thebrowserType]
browserStatus: [theBrowserStatus]
------------------------------------------------------------------

[/showif] [!]---run the api if the broswer is not a bot and the session has not been previously recorded--[/!] [if "([theBrowserStatus]"=3D"Y") & ("[updateSession]"=3D"0")] [then] [text]MyTestVariable=3D[!] [/!][TCPConnect host=3D[theDOMAIN]][TCPSend skipheader=3DT]GET [thePATH][theMethod].[theResultTYPE]?ip=3D[theCALL]&apikey=3D[theAPIkey] HTTP/1.0[unurl]%0D%0A[/Unurl]Host: [theDOMAIN][unurl]%0D%0A%0D%0A[/UnURL][/TCPSend] [/TCPConnect][/text] [!]--it's easy to parse the jason using istwords as we know what the expected results are from the api documentation--[/!] [table name=3DJSON_Convert&fields=3Dfrom,to] { } " [/table] [!]--strip the json and list it out--[/!] [ListWords Words=3D[convertchars table=3DJSON_Convert][MyTestVariable][/convertchars]&Delimiters=3D,] [showif [errorCatch]=3D1] [index]
[/showif] [!]--the structure of the json is that of value:name [ListWords Words=3D[word]&Delimiters=3D:] [showif [index]=3D1] [showif [errorCatch]=3D1] theField: [word]
[/showif] [text]theField=3D[word][/text] [/showif] [showif [index]=3D2] [showif [errorCatch]=3D1] theValue: [word]
[/showif] [text]theValue=3D[word][/text] [/showif] [!]--knowing the names of the fields - just show the field you want to act on--[/!] [!]------------ZIP----------[/!] [showif [theField]=3Dzip] [text]theZip=3D[theValue][/text] [/showif] [!]-----------------------------------[/!] [!]------------Country----------[/!] [showif [theField]=3Dtwo_letter_country] [text]theCountryCode=3D[theValue][/text] [/showif] [!]-----------------------------------[/!] [/ListWords] [showif [errorCatch]=3D1] [index]: [theAugmentID]: [theField]: [theValue]
[/showif] [!]--if this is a new session then record it in the database--[/!] [showif [updateSession]=3D0] [showif [errorCatch]=3D1] News Session! Replacing& Appending
[/showif] [!]------- header file ----[/!] [!]------id UserID sessionID region city zip area_code in_dst zip_country country country_code postal_code city_code gmt_offset region_code continent_code conn_speed two_letter_country metro_code longitude internal_code lat zip_code_ignored dateNum timeNum browserType theIP------[/!] [replace db=3D[pagePath2]databases/XXXXXX.db&eqiddatarq=3D[theAugmentID]&eqsessionID= datarq=3D[sessionID]&append=3DT]id=3D[theAugmentID]&[theField]=3D[theValue]= &UserID=3D[UserID]&sessionID=3D[sessionID]&dateNum=3D[date_num]&timeNum=3D[= time_num]&browserType=3D[thebrowserType]&theIP=3D[theCall][/replace] [/showif] [!]--show content based on two letter country code -- [/!] [showif [theField]=3Dtwo_letter_country] [showif [theValue]=3Dca]aaaa[/showif] [hideif [theValue]=3Dus]bbb[/hideif] [/showif] [/listwords] [!]---if the countrycode is in the us the run the census api - this can be aded to the above two_letter_countrycode--[/!] [showif [theCountryCode]=3Dus] [showif [errorCatch]=3D1] theZip: [theZip]
[/showif] [!]--------Function Variables-------[/!] [text]theDOMAIN=3Dapi.infochimps.com[/text] [text]thePATH2=3D/web/an/census/[/text] [text]theMethod=3Dcombined[/text] [text]theResultTYPE=3Djson[/text] [text]theAPIkey=3DXXXXXXXXXX[/text] [text]theCALL2=3D[theZip][/text] [text]MyTestVariable2=3D[!] [/!][TCPConnect host=3D[theDOMAIN]][TCPSend skipheader=3DT]GET [thePATH2][theMethod].[theResultTYPE]?zip=3D[theCALL2]&apikey=3D[theAPIkey] HTTP/1.0[unurl]%0D%0A[/Unurl]Host: [theDOMAIN][unurl]%0D%0A%0D%0A[/UnURL][/TCPSend] [/TCPConnect][/text] [ListWords Words=3D[convertchars table=3DJSON_Convert][MyTestVariable2][/convertchars]&Delimiters=3D,] [showif [showVariables]=3D1] theField: [word]
[/showif] [listwords words=3D[word]&Delimiters=3D:] [showif [index]=3D1] [showif [showVariables]=3D1] theField: [word]
[/showif] [text]theFieldName=3D[word][/text] [/showif] [showif [index]=3D2] [showif [showVariables]=3D1] theValue: [word]
[/showif] [!]--if I know the databse info set is fixed then I run addfields once and set up the header of the databse file then I take it out - so you'd have to do that once for this api to record the field names to your db--[/!] [replace db=3D[pagePath2]databases/XXXXXXX.db&eqiddatarq=3D[theAugmentID]&eqsessionI= Ddatarq=3D[sessionID]&append=3DT][theFieldName]=3D[word][/replace] [/showif] [/listwords] [/listwords] [/showif] [showif [errorCatch]=3D1] Replacing Next Number to [theAugmentID]
[/showif] [replace db=3D[pagePath2]databases/XXXXXX.db&eqnumberIDdatarq=3DaugmentID]NUMBER=3D[= theAugmentID][/replace] [/then] [else][/else] [/if] [/function] --bcaec53f8ebf9f5c88049edb9909 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi, I'll=A0 be releasing a bunch more of these.

This is the code= - rough as it may be - to hook into infochimp api structure.
This parti= cular api converts an IP address to more useful information such as country= code, zip/postal code and city + long/lat
It's better for city I would say, than an actual specific zip/postal ar= ea - it says I am across town from=A0 where I actually am, but it is very u= seful.

Combined with other api's - I'll release the code to = census data next. Another api from info chimp. that converts lat/long or zi= p code to some 2000 census data.



http://www.infochimps.com/datasets/digital-elem= ent-ip-intelligence-geolocation

I'm going to start posting c= ode especially about api integration.

Here is the post about the code below:
http= ://www.bastedo.com/2011/03/api-digital-element-ip-intelligence-geolocation/= #more-95


[!]--depending on your server you might be able to use the [ipaddre= ss] tag--[/!]
[listmimeheaders]

[showif "[name]"=3D&quo= t;X-Real-IP"]
=A0=A0=A0 [text]ip_address=3D[value][/text]
[/show= if]

[showif "[name]"=3D"User-Agent"]
=A0=A0=A0 [text= ]thebrowserType=3D[value][/text]
[/showif]

[/ListMIMEHeaders]



[function name=3DParseIP]
[!]--I pass in a sessionID an= d record the info for the session once per session--[/!]

[!]--------Error Check and Working Variables-------[/!]
[!]----set t= hese to 0 to turn off----[/!]
[text]errorCatch=3D1[/text]
[text]showV= ariables=3D1[/text]


[!]--------Technical Variables-------[/!] [text]updateIDNumber=3D0[/text]
[text]theBrowserStatus=3DY[/text]
[!]= --Date and time stamp encoding ready for use --[/!]
[text]date_num=3D[ma= th]{[date]}[/math][/text]
[text]time_num=3D[math]{[time]}[/math][/text]<= br>[!]--path variable--[/!]
[text]pagePath2=3D[/text]


[!]----I found a lot of bots - especia= lly google - and this is an easy way to get rid of a lot of them - but shou= ld be built into a complete table -----[/!]
[table name=3DbrowserBotWord= s&fields=3DbotWord]
googlebot
Baiduspider
nagios
crawler
bingbot
spider
spyde= r
siteBot
Slurp
dotbot
[/table]







= [!]--------Function Variables-------[/!]
[text]theDOMAIN=3Dapi.infochimps.com[/text]
[text]thePATH=3D/web/an/de/[/text]
[text]theMethod=3Dgeo[/text]
[text= ]theResultTYPE=3Djson[/text]
[!]----Get your own api key and insert belo= w--[/!]
[text]theAPIkey=3DXXXXXXXXXXXXX[/text]
[!]--the ip address - = how ever you get it - it cannot have extra "0"s as in .092 and mu= st be .92--[/!]
[text]theCALL=3D[ip_address][/text]
[!]--an id I create for the data --[= /!]
[text]theAugmentID=3D[math][lookup db=3D[pagePath2]databases/XXXXXX.= db&lookInField=3DnumberID&value=3DaugmentID&returnField=3Dnumbe= r]+1[/math][/text]
[!]--check to see if the session info has been recorded already--[/!]
[t= ext]updateSession=3D[lookup db=3D[pagePath2]databases/XXXXXXXX.db&lookI= nField=3DsessionID&value=3D[MySessionID]&returnField=3DsessionID&am= p;notfound=3D0][/text]

[!]--chek the browser info to see if it is a bot and if it is - do noth= ing -- [/!]

[search table=3DbrowserBotWords&gebotWorddata=3D[bla= nk]]


[showif [showVariables]=3D1]
=A0=A0=A0 results: [numfoun= d]<br>
[/showif]

[text]mystring=3D[thebrowserType][/text]
[founditems]
[showif [showVariables]=3D1]
=A0=A0=A0 botWord: [botWord]<br>= ;
[/showif]


[text]found_at=3D[FindString source=3D[mystring]&= amp;find=3D[botWord]][/text]



[showif [found_at]>0]

[showif [showVariables]=3D1]<br>---------------------------<br><br>
Found '[b= otWord]' in source browserType - we suspect it's a BOT!<br> <br>---------------------------<br>
[/showif]
[!]--set va= riable to N so that this will not be checked--[/!]
[text]theBrowserStatu= s=3DN[/text]
[/showif]

[/founditems]
[/search]

[!]--mak= e sure we know if the session has been recorded--[/!]

[If "[updateSession]" =3D "0"]
[then][text]updat= eSession=3D0[/text][/then]
[else][text]updateSession=3D1[/text][/else][/if]

[!]--check to see that we have a session id and if we don= 9;t then create one --[/!]
[!]--Check for session ID--[/!]
[showif [sessionID]=3D]
=A0=A0=A0 [te= xt]MySessionID=3D[date_num]_[time_num]_[cart][/text]
[/showif]

[s= howif [sessionID]=3D[raw][sessionID][/raw]]
=A0=A0=A0 [text]MySessionID= =3D[date_num]_[time_num]_[cart][/text]
[/showif]

[!]--this lists all the ids we have Userid in my example i= s brought in from a cookie if they are currently registered--[/!]

[s= howif [showVariables]=3D1]
<br><br>-------------------------= -----------------------------------------<br>
UserID: [UserID]<br>
MysessionID:=A0 [MysessionID]<br>
up= dateSession: [updateSession]<br>
theAugmentID: [theAugmentID]<b= r>
browserType: [thebrowserType]<br>
browserStatus: [theBrow= serStatus]<br>


------------------------------------------------------------------&= lt;br><br>
[/showif]




[!]---run the api if th= e broswer is not a bot and the session has=A0 not been previously recorded-= -[/!]


[if "([theBrowserStatus]"=3D"Y") & ("[= updateSession]"=3D"0")]
[then]


[text]MyTestVar= iable=3D[!]
[/!][TCPConnect host=3D[theDOMAIN]][TCPSend skipheader=3DT]G= ET [thePATH][theMethod].[theResultTYPE]?ip=3D[theCALL]&apikey=3D[theAPI= key] HTTP/1.0[unurl]%0D%0A[/Unurl]Host: [theDOMAIN][unurl]%0D%0A%0D%0A[/UnU= RL][/TCPSend]
[/TCPConnect][/text]

[!]--it's easy to parse the jason using ist= words as we know what the expected results are from the api documentation--= [/!]

[table name=3DJSON_Convert&fields=3Dfrom,to]
{=A0=A0=A0 =
}=A0=A0=A0
"=A0=A0=A0
[/table]

[!]--strip the json and list it out--[/= !]
[ListWords Words=3D[convertchars table=3DJSON_Convert][MyTestVariable= ][/convertchars]&Delimiters=3D,]

[showif [errorCatch]=3D1]
= =A0=A0=A0 <b>[index]</b><br />
[/showif]

[!]--the structure of the json is that of value:name
[ListWords Words=3D[word]&Delimiters=3D:]
[showif [index]=3D1]
= [showif [errorCatch]=3D1]
=A0=A0=A0 theField: <b>[word]</b>&= lt;br />
[/showif]

[text]theField=3D[word][/text]

[/showif]

[sh= owif [index]=3D2]
=A0=A0=A0 [showif [errorCatch]=3D1]
=A0=A0=A0 =A0= =A0=A0 theValue: <b>[word]</b><br />
=A0=A0=A0 [/showi= f]
=A0=A0=A0
[text]theValue=3D[word][/text]
[/showif]

[!]--knowing the names of the fields - just show the field= you want to act on--[/!]

[!]------------ZIP----------[/!]
[showi= f [theField]=3Dzip]

[text]theZip=3D[theValue][/text]

[/showif= ]
[!]-----------------------------------[/!]

[!]------------Country---= -------[/!]
[showif [theField]=3Dtwo_letter_country]
=A0=A0=A0=A0=A0= =A0 [text]theCountryCode=3D[theValue][/text]
[/showif]
[!]-----------= ------------------------[/!]
[/ListWords]



[showif [errorCatch]=3D1]
=A0=A0=A0 [index]= : [theAugmentID]: [theField]: [theValue]<br>
[/showif]


=
[!]--if this is a new session then record it in the database--[/!]
[= showif=A0 [updateSession]=3D0]

[showif [errorCatch]=3D1]
=A0=A0=A0 News Session! Replacing& App= ending<br>
[/showif]

[!]------- header file ----[/!]
[!]= ------id=A0=A0=A0 UserID=A0=A0=A0 sessionID=A0=A0=A0 region=A0=A0=A0 city= =A0=A0=A0 zip=A0=A0=A0 area_code=A0=A0=A0 in_dst=A0=A0=A0 zip_country=A0=A0= =A0 country=A0=A0=A0 country_code=A0=A0=A0 postal_code=A0=A0=A0 city_code= =A0=A0=A0 gmt_offset=A0=A0=A0 region_code=A0=A0=A0 continent_code=A0=A0=A0 = conn_speed=A0=A0=A0 two_letter_country=A0=A0=A0 metro_code=A0=A0=A0 longitu= de=A0=A0=A0 internal_code=A0=A0=A0 lat=A0=A0=A0 zip_code_ignored=A0=A0=A0 d= ateNum=A0=A0=A0 timeNum=A0=A0=A0 browserType=A0=A0=A0 theIP------[/!]

[replace db=3D[pagePath2]databases/XXXXXX.db&eqiddatarq=3D[theAugme= ntID]&eqsessionIDdatarq=3D[sessionID]&append=3DT]id=3D[theAugmentID= ]&[theField]=3D[theValue]&UserID=3D[UserID]&sessionID=3D[sessio= nID]&dateNum=3D[date_num]&timeNum=3D[time_num]&browserType=3D[t= hebrowserType]&theIP=3D[theCall][/replace]



[/showif]

[!]--show content based on two letter country = code -- [/!]
[showif [theField]=3Dtwo_letter_country]

[showif [th= eValue]=3Dca]aaaa[/showif]
[hideif [theValue]=3Dus]bbb[/hideif]

<= br> [/showif]



[/listwords]


[!]---if the countrycode i= s in the us the run the census api - this can be aded to the above two_lett= er_countrycode--[/!]
[showif [theCountryCode]=3Dus]

[showif [erro= rCatch]=3D1]
=A0=A0=A0=A0 theZip: [theZip]<br />
[/showif]
[!]--------Functi= on Variables-------[/!]
[text]theDOMAIN=3Dapi.infochimps.com[/text]
[text]thePATH2=3D/web/an/census/[/= text]
[text]theMethod=3Dcombined[/text]
[text]theResultTYPE=3Djson[/text]
[text]theAPIkey=3DXXXXXXXXXX[/text][text]theCALL2=3D[theZip][/text]





[text]MyTestVariab= le2=3D[!]
[/!][TCPConnect host=3D[theDOMAIN]][TCPSend skipheader=3DT]GET= [thePATH2][theMethod].[theResultTYPE]?zip=3D[theCALL2]&apikey=3D[theAP= Ikey] HTTP/1.0[unurl]%0D%0A[/Unurl]Host: [theDOMAIN][unurl]%0D%0A%0D%0A[/Un= URL][/TCPSend]
[/TCPConnect][/text]

[ListWords Words=3D[convertchars table=3DJSON_C= onvert][MyTestVariable2][/convertchars]&Delimiters=3D,]
[showif [sho= wVariables]=3D1]
=A0=A0=A0 theField: <b>[word]</b><br /&g= t;
=A0[/showif]
=A0=A0=A0
=A0[listwords words=3D[word]&Delimiters=3D:]
[showif [= index]=3D1]

[showif [showVariables]=3D1]
=A0=A0=A0 theField:=A0 = [word]<br />
[/showif]
=A0
[text]theFieldName=3D[word][/tex= t]

[/showif]

[showif [index]=3D2]

[showif [showVariables]=3D1]
=A0=A0=A0 theVa= lue:=A0 [word]<br />
[/showif]

[!]--if I know the databse = info set is fixed then I run addfields once and set up the header of the da= tabse file then I take it out - so you'd have to do that once for this = api to record the field names to your db--[/!]


[replace db=3D[pagePath2]databases/XXXXXXX.db&eqiddatarq=3D[the= AugmentID]&eqsessionIDdatarq=3D[sessionID]&append=3DT][theFieldName= ]=3D[word][/replace]
[/showif]
=A0=A0=A0
[/listwords]

=A0= =A0=A0

[/listwords]


[/showif]


[showif [errorCatch]=3D1]
=A0=A0=A0 Replaci= ng Next Number to [theAugmentID]<br>
[/showif]
=A0=A0=A0 [repla= ce db=3D[pagePath2]databases/XXXXXX.db&eqnumberIDdatarq=3DaugmentID]NUM= BER=3D[theAugmentID][/replace]

[/then]
[else][/else]
[/if]


[/function]
--bcaec53f8ebf9f5c88049edb9909-- David Bastedo

DOWNLOAD WEBDNA NOW!

Top Articles:

Talk List

The WebDNA community talk-list is the best place to get some help: several hundred extremely proficient programmers with an excellent knowledge of WebDNA and an excellent spirit will deliver all the tips and tricks you can imagine...

Related Readings:

upgrading (1997) [WebDNA] TCPConnect/cURL alternative for windows? (2010) $Replace with [founditems] (1997) Web Catalog 2 demo (1997) New command suggestion (was Modifying databases manually) (1997) Applescript, WebDNA, and PDF (1997) search form problem.. (1997) PIXO (1997) Wireless WebDNA available today! (2000) [CART] (1997) still having search problem, please help :) (2004) version history page for webcat on the SM site? (2000) WebCatalog [FoundItems] Problem - AGAIN - (1997) So .. How fast (2003) Number date (2006) AOL and referrers on target=_blank (2000) Permissions still causing problems... (2002) calculating time (2002) Attention all list readers (1997) RE: OK, here goes... (1997)