[BULK] Re: [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 = 106401
interpreted = N
texte = This is a multi-part message in MIME format. --------------020304090904070704030202 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit way cool. jym On 3/19/11 1:18 PM, David Bastedo wrote: > 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 2000 census data. > > > > http://www.infochimps.com/datasets/digital-element-ip-intelligence-geolocation > > 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-geolocation/#more-95 > > > [!]--depending on your server you might be able to use the [ipaddress] > tag--[/!] > [ListMIMEHeaders] > > [showif "[name]"="X-Real-IP"] > [text]ip_address=[value][/text] > [/showif] > > [showif "[name]"="User-Agent"] > [text]thebrowserType=[value][/text] > [/showif] > > [/ListMIMEHeaders] > > > > > [function name=ParseIP] > [!]--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=1[/text] > [text]showVariables=1[/text] > > > [!]--------Technical Variables-------[/!] > [text]updateIDNumber=0[/text] > [text]theBrowserStatus=Y[/text] > [!]--Date and time stamp encoding ready for use --[/!] > [text]date_num=[math]{[date]}[/math][/text] > [text]time_num=[math]{[time]}[/math][/text] > [!]--path variable--[/!] > [text]pagePath2=[/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=browserBotWords&fields=botWord] > googlebot > Baiduspider > nagios > crawler > bingbot > spider > spyder > siteBot > Slurp > dotbot > [/table] > > > > > > > > [!]--------Function Variables-------[/!] > [text]theDOMAIN=api.infochimps.com [/text] > [text]thePATH=/web/an/de/[/text] > [text]theMethod=geo[/text] > [text]theResultTYPE=json[/text] > [!]----Get your own api key and insert below--[/!] > [text]theAPIkey=XXXXXXXXXXXXX[/text] > [!]--the ip address - how ever you get it - it cannot have extra "0"s > as in .092 and must be .92--[/!] > [text]theCALL=[ip_address][/text] > [!]--an id I create for the data --[/!] > [text]theAugmentID=[math][lookup > db=[pagePath2]databases/XXXXXX.db&lookInField=numberID&value=augmentID&returnField=number]+1[/math][/text] > [!]--check to see if the session info has been recorded already--[/!] > [text]updateSession=[lookup > db=[pagePath2]databases/XXXXXXXX.db&lookInField=sessionID&value=[MySessionID]&returnField=sessionID¬found=0][/text] > > [!]--chek the browser info to see if it is a bot and if it is - do > nothing -- [/!] > > [search table=browserBotWords&gebotWorddata=[blank]] > > > [showif [showVariables]=1] > results: [numfound]
> [/showif] > > [text]mystring=[thebrowserType][/text] > [founditems] > > [showif [showVariables]=1] > botWord: [botWord]
> [/showif] > > > [text]found_at=[FindString source=[mystring]&find=[botWord]][/text] > > > > [showif [found_at]>0] > > [showif [showVariables]=1] >
---------------------------

> 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=N[/text] > [/showif] > > [/founditems] > [/search] > > [!]--make sure we know if the session has been recorded--[/!] > > [If "[updateSession]" = "0"] > [then][text]updateSession=0[/text][/then] > [else][text]updateSession=1[/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]=] > [text]MySessionID=[date_num]_[time_num]_[cart][/text] > [/showif] > > [showif [sessionID]=[raw][sessionID][/raw]] > [text]MySessionID=[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]=1] >

------------------------------------------------------------------
> 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]"="Y") & ("[updateSession]"="0")] > [then] > > > [text]MyTestVariable=[!] > [/!][TCPConnect host=[theDOMAIN]][TCPSend skipheader=T]GET > [thePATH][theMethod].[theResultTYPE]?ip=[theCALL]&apikey=[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=JSON_Convert&fields=from,to] > { > } > " > [/table] > > [!]--strip the json and list it out--[/!] > [ListWords Words=[convertchars > table=JSON_Convert][MyTestVariable][/convertchars]&Delimiters=,] > > [showif [errorCatch]=1] > [index]
> [/showif] > > [!]--the structure of the json is that of value:name > > [ListWords Words=[word]&Delimiters=:] > [showif [index]=1] > [showif [errorCatch]=1] > theField: [word]
> [/showif] > > [text]theField=[word][/text] > > [/showif] > > [showif [index]=2] > [showif [errorCatch]=1] > theValue: [word]
> [/showif] > > [text]theValue=[word][/text] > [/showif] > > [!]--knowing the names of the fields - just show the field you want to > act on--[/!] > > [!]------------ZIP----------[/!] > [showif [theField]=zip] > > [text]theZip=[theValue][/text] > > [/showif] > [!]-----------------------------------[/!] > > [!]------------Country----------[/!] > [showif [theField]=two_letter_country] > [text]theCountryCode=[theValue][/text] > [/showif] > [!]-----------------------------------[/!] > [/ListWords] > > > > [showif [errorCatch]=1] > [index]: [theAugmentID]: [theField]: [theValue]
> [/showif] > > > > [!]--if this is a new session then record it in the database--[/!] > [showif [updateSession]=0] > > [showif [errorCatch]=1] > 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=[pagePath2]databases/XXXXXX.db&eqiddatarq=[theAugmentID]&eqsessionIDdatarq=[sessionID]&append=T]id=[theAugmentID]&[theField]=[theValue]&UserID=[UserID]&sessionID=[sessionID]&dateNum=[date_num]&timeNum=[time_num]&browserType=[thebrowserType]&theIP=[theCall][/replace] > > > > [/showif] > > [!]--show content based on two letter country code -- [/!] > [showif [theField]=two_letter_country] > > [showif [theValue]=ca]aaaa[/showif] > [hideif [theValue]=us]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]=us] > > [showif [errorCatch]=1] > theZip: [theZip]
> [/showif] > [!]--------Function Variables-------[/!] > [text]theDOMAIN=api.infochimps.com [/text] > [text]thePATH2=/web/an/census/[/text] > [text]theMethod=combined[/text] > [text]theResultTYPE=json[/text] > [text]theAPIkey=XXXXXXXXXX[/text] > [text]theCALL2=[theZip][/text] > > > > > > [text]MyTestVariable2=[!] > [/!][TCPConnect host=[theDOMAIN]][TCPSend skipheader=T]GET > [thePATH2][theMethod].[theResultTYPE]?zip=[theCALL2]&apikey=[theAPIkey] HTTP/1.0[UnURL]%0D%0A[/Unurl]Host: > [theDOMAIN][UnURL]%0D%0A%0D%0A[/UnURL][/TCPSend] > [/TCPConnect][/text] > > [ListWords Words=[convertchars > table=JSON_Convert][MyTestVariable2][/convertchars]&Delimiters=,] > [showif [showVariables]=1] > theField: [word]
> [/showif] > > [listwords words=[word]&Delimiters=:] > [showif [index]=1] > > [showif [showVariables]=1] > theField: [word]
> [/showif] > > [text]theFieldName=[word][/text] > > [/showif] > > [showif [index]=2] > > [showif [showVariables]=1] > 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=[pagePath2]databases/XXXXXXX.db&eqiddatarq=[theAugmentID]&eqsessionIDdatarq=[sessionID]&append=T][theFieldName]=[word][/replace] > [/showif] > > [/listwords] > > > > [/listwords] > > > [/showif] > > > [showif [errorCatch]=1] > Replacing Next Number to [theAugmentID]
> [/showif] > [replace > db=[pagePath2]databases/XXXXXX.db&eqnumberIDdatarq=augmentID]NUMBER=[theAugmentID][/replace] > > [/then] > [else][/else] > [/if] > > > [/function] > --------------------------------------------------------- This message > is sent to you because you are subscribed to the mailing list . To > unsubscribe, E-mail to: archives: > http://mail.webdna.us/list/talk@webdna.us Bug Reporting: > support@webdna.us -- Jym Duane - CTO - Purpose Media Creating Your Success Story Marketing : Television - Internet - Print Phone: (877) 443-1323 Email: jym@purposemedia.com Web: www.purposemedia.com Oregon - www.GuideToOregon.com PO Box 1725,Jacksonville, OR 97530 California - www.OrangeCounty.net PO Box 2025,Capistrano Beach, CA 92624 --------------020304090904070704030202 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit way cool.
jym

On 3/19/11 1:18 PM, David Bastedo wrote: 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 2000 census data.



http://www.infochimps.com/datasets/digital-element-ip-intelligence-geolocation

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-geolocation/#more-95


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

[showif "[name]"="X-Real-IP"]
    [text]ip_address=[value][/text]
[/showif]

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

[/ListMIMEHeaders]




[function name=ParseIP]
[!]--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=1[/text]
[text]showVariables=1[/text]


[!]--------Technical Variables-------[/!]
[text]updateIDNumber=0[/text]
[text]theBrowserStatus=Y[/text]
[!]--Date and time stamp encoding ready for use --[/!]
[text]date_num=[math]{[date]}[/math][/text]
[text]time_num=[math]{[time]}[/math][/text]
[!]--path variable--[/!]
[text]pagePath2=[/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=browserBotWords&fields=botWord]
googlebot
Baiduspider
nagios
crawler
bingbot
spider
spyder
siteBot
Slurp
dotbot
[/table]







[!]--------Function Variables-------[/!]
[text]theDOMAIN=api.infochimps.com[/text]
[text]thePATH=/web/an/de/[/text]
[text]theMethod=geo[/text]
[text]theResultTYPE=json[/text]
[!]----Get your own api key and insert below--[/!]
[text]theAPIkey=XXXXXXXXXXXXX[/text]
[!]--the ip address - how ever you get it - it cannot have extra "0"s as in .092 and must be .92--[/!]
[text]theCALL=[ip_address][/text]
[!]--an id I create for the data --[/!]
[text]theAugmentID=[math][lookup db=[pagePath2]databases/XXXXXX.db&lookInField=numberID&value=augmentID&returnField=number]+1[/math][/text]
[!]--check to see if the session info has been recorded already--[/!]
[text]updateSession=[lookup db=[pagePath2]databases/XXXXXXXX.db&lookInField=sessionID&value=[MySessionID]&returnField=sessionID&notfound=0][/text]

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

[search table=browserBotWords&gebotWorddata=[blank]]


[showif [showVariables]=1]
    results: [numfound]<br>
[/showif]

[text]mystring=[thebrowserType][/text]
[founditems]

[showif [showVariables]=1]
    botWord: [botWord]<br>
[/showif]


[text]found_at=[FindString source=[mystring]&find=[botWord]][/text]



[showif [found_at]>0]

[showif [showVariables]=1]
<br>---------------------------<br><br>
Found '[botWord]' in source browserType - we suspect it's a BOT!<br>
<br>---------------------------<br>
[/showif]
[!]--set variable to N so that this will not be checked--[/!]
[text]theBrowserStatus=N[/text]
[/showif]

[/founditems]
[/search]

[!]--make sure we know if the session has been recorded--[/!]

[If "[updateSession]" = "0"]
[then][text]updateSession=0[/text][/then]
[else][text]updateSession=1[/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]=]
    [text]MySessionID=[date_num]_[time_num]_[cart][/text]
[/showif]

[showif [sessionID]=[raw][sessionID][/raw]]
    [text]MySessionID=[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]=1]
<br><br>------------------------------------------------------------------<br>
UserID: [UserID]<br>
MysessionID:  [MysessionID]<br>
updateSession: [updateSession]<br>
theAugmentID: [theAugmentID]<br>
browserType: [thebrowserType]<br>
browserStatus: [theBrowserStatus]<br>


------------------------------------------------------------------<br><br>
[/showif]




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


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


[text]MyTestVariable=[!]
[/!][TCPConnect host=[theDOMAIN]][TCPSend skipheader=T]GET [thePATH][theMethod].[theResultTYPE]?ip=[theCALL]&apikey=[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=JSON_Convert&fields=from,to]
{   
}   
"   
[/table]

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

[showif [errorCatch]=1]
    <b>[index]</b><br />
[/showif]

[!]--the structure of the json is that of value:name

[ListWords Words=[word]&Delimiters=:]
[showif [index]=1]
[showif [errorCatch]=1]
    theField: <b>[word]</b><br />
[/showif]

[text]theField=[word][/text]

[/showif]

[showif [index]=2]
    [showif [errorCatch]=1]
        theValue: <b>[word]</b><br />
    [/showif]
   
[text]theValue=[word][/text]
[/showif]

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

[!]------------ZIP----------[/!]
[showif [theField]=zip]

[text]theZip=[theValue][/text]

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

[!]------------Country----------[/!]
[showif [theField]=two_letter_country]
       [text]theCountryCode=[theValue][/text]
[/showif]
[!]-----------------------------------[/!]
[/ListWords]



[showif [errorCatch]=1]
    [index]: [theAugmentID]: [theField]: [theValue]<br>
[/showif]



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

[showif [errorCatch]=1]
    News Session! Replacing& Appending<br>
[/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=[pagePath2]databases/XXXXXX.db&eqiddatarq=[theAugmentID]&eqsessionIDdatarq=[sessionID]&append=T]id=[theAugmentID]&[theField]=[theValue]&UserID=[UserID]&sessionID=[sessionID]&dateNum=[date_num]&timeNum=[time_num]&browserType=[thebrowserType]&theIP=[theCall][/replace]



[/showif]

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

[showif [theValue]=ca]aaaa[/showif]
[hideif [theValue]=us]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]=us]

[showif [errorCatch]=1]
     theZip: [theZip]<br />
[/showif]
[!]--------Function Variables-------[/!]
[text]theDOMAIN=api.infochimps.com[/text]
[text]thePATH2=/web/an/census/[/text]
[text]theMethod=combined[/text]
[text]theResultTYPE=json[/text]
[text]theAPIkey=XXXXXXXXXX[/text]
[text]theCALL2=[theZip][/text]





[text]MyTestVariable2=[!]
[/!][TCPConnect host=[theDOMAIN]][TCPSend skipheader=T]GET [thePATH2][theMethod].[theResultTYPE]?zip=[theCALL2]&apikey=[theAPIkey] HTTP/1.0[UnURL]%0D%0A[/Unurl]Host: [theDOMAIN][UnURL]%0D%0A%0D%0A[/UnURL][/TCPSend]
[/TCPConnect][/text]

[ListWords Words=[convertchars table=JSON_Convert][MyTestVariable2][/convertchars]&Delimiters=,]
[showif [showVariables]=1]
    theField: <b>[word]</b><br />
 [/showif]
   
 [listwords words=[word]&Delimiters=:]
[showif [index]=1]

[showif [showVariables]=1]
    theField:  [word]<br />
[/showif]
 
[text]theFieldName=[word][/text]

[/showif]

[showif [index]=2]

[showif [showVariables]=1]
    theValue:  [word]<br />
[/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=[pagePath2]databases/XXXXXXX.db&eqiddatarq=[theAugmentID]&eqsessionIDdatarq=[sessionID]&append=T][theFieldName]=[word][/replace]
[/showif]
   
[/listwords]

   

[/listwords]


[/showif]


[showif [errorCatch]=1]
    Replacing Next Number to [theAugmentID]<br>
[/showif]
    [replace db=[pagePath2]databases/XXXXXX.db&eqnumberIDdatarq=augmentID]NUMBER=[theAugmentID][/replace]

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


[/function]
--------------------------------------------------------- This message is sent to you because you are subscribed to the mailing list . To unsubscribe, E-mail to: archives: http://mail.webdna.us/list/talk@webdna.us Bug Reporting: support@webdna.us
--


Jym Duane - CTO - Purpose Media

Creating Your Success Story

Marketing : Television - Internet - Print


Phone: (877) 443-1323   

Email: jym@purposemedia.com   

Web: www.purposemedia.com


Oregon - www.GuideToOregon.com

PO Box 1725,  Jacksonville, OR 97530


California - www.OrangeCounty.net

PO Box 2025,  Capistrano Beach, CA 92624

--------------020304090904070704030202-- Associated Messages, from the most recent to the oldest:

    
This is a multi-part message in MIME format. --------------020304090904070704030202 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit way cool. jym On 3/19/11 1:18 PM, David Bastedo wrote: > 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 2000 census data. > > > > http://www.infochimps.com/datasets/digital-element-ip-intelligence-geolocation > > 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-geolocation/#more-95 > > > [!]--depending on your server you might be able to use the [ipaddress] > tag--[/!] > [listmimeheaders] > > [showif "[name]"="X-Real-IP"] > [text]ip_address=[value][/text] > [/showif] > > [showif "[name]"="User-Agent"] > [text]thebrowserType=[value][/text] > [/showif] > > [/ListMIMEHeaders] > > > > > [function name=ParseIP] > [!]--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=1[/text] > [text]showVariables=1[/text] > > > [!]--------Technical Variables-------[/!] > [text]updateIDNumber=0[/text] > [text]theBrowserStatus=Y[/text] > [!]--Date and time stamp encoding ready for use --[/!] > [text]date_num=[math]{[date]}[/math][/text] > [text]time_num=[math]{[time]}[/math][/text] > [!]--path variable--[/!] > [text]pagePath2=[/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=browserBotWords&fields=botWord] > googlebot > Baiduspider > nagios > crawler > bingbot > spider > spyder > siteBot > Slurp > dotbot > [/table] > > > > > > > > [!]--------Function Variables-------[/!] > [text]theDOMAIN=api.infochimps.com [/text] > [text]thePATH=/web/an/de/[/text] > [text]theMethod=geo[/text] > [text]theResultTYPE=json[/text] > [!]----Get your own api key and insert below--[/!] > [text]theAPIkey=XXXXXXXXXXXXX[/text] > [!]--the ip address - how ever you get it - it cannot have extra "0"s > as in .092 and must be .92--[/!] > [text]theCALL=[ip_address][/text] > [!]--an id I create for the data --[/!] > [text]theAugmentID=[math][lookup > db=[pagePath2]databases/XXXXXX.db&lookInField=numberID&value=augmentID&returnField=number]+1[/math][/text] > [!]--check to see if the session info has been recorded already--[/!] > [text]updateSession=[lookup > db=[pagePath2]databases/XXXXXXXX.db&lookInField=sessionID&value=[MySessionID]&returnField=sessionID¬found=0][/text] > > [!]--chek the browser info to see if it is a bot and if it is - do > nothing -- [/!] > > [search table=browserBotWords&gebotWorddata=[blank]] > > > [showif [showVariables]=1] > results: [numfound]
> [/showif] > > [text]mystring=[thebrowserType][/text] > [founditems] > > [showif [showVariables]=1] > botWord: [botWord]
> [/showif] > > > [text]found_at=[FindString source=[mystring]&find=[botWord]][/text] > > > > [showif [found_at]>0] > > [showif [showVariables]=1] >
---------------------------

> 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=N[/text] > [/showif] > > [/founditems] > [/search] > > [!]--make sure we know if the session has been recorded--[/!] > > [If "[updateSession]" = "0"] > [then][text]updateSession=0[/text][/then] > [else][text]updateSession=1[/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]=] > [text]MySessionID=[date_num]_[time_num]_[cart][/text] > [/showif] > > [showif [sessionID]=[raw][sessionID][/raw]] > [text]MySessionID=[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]=1] >

------------------------------------------------------------------
> 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]"="Y") & ("[updateSession]"="0")] > [then] > > > [text]MyTestVariable=[!] > [/!][TCPConnect host=[theDOMAIN]][TCPSend skipheader=T]GET > [thePATH][theMethod].[theResultTYPE]?ip=[theCALL]&apikey=[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=JSON_Convert&fields=from,to] > { > } > " > [/table] > > [!]--strip the json and list it out--[/!] > [ListWords Words=[convertchars > table=JSON_Convert][MyTestVariable][/convertchars]&Delimiters=,] > > [showif [errorCatch]=1] > [index]
> [/showif] > > [!]--the structure of the json is that of value:name > > [ListWords Words=[word]&Delimiters=:] > [showif [index]=1] > [showif [errorCatch]=1] > theField: [word]
> [/showif] > > [text]theField=[word][/text] > > [/showif] > > [showif [index]=2] > [showif [errorCatch]=1] > theValue: [word]
> [/showif] > > [text]theValue=[word][/text] > [/showif] > > [!]--knowing the names of the fields - just show the field you want to > act on--[/!] > > [!]------------ZIP----------[/!] > [showif [theField]=zip] > > [text]theZip=[theValue][/text] > > [/showif] > [!]-----------------------------------[/!] > > [!]------------Country----------[/!] > [showif [theField]=two_letter_country] > [text]theCountryCode=[theValue][/text] > [/showif] > [!]-----------------------------------[/!] > [/ListWords] > > > > [showif [errorCatch]=1] > [index]: [theAugmentID]: [theField]: [theValue]
> [/showif] > > > > [!]--if this is a new session then record it in the database--[/!] > [showif [updateSession]=0] > > [showif [errorCatch]=1] > 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=[pagePath2]databases/XXXXXX.db&eqiddatarq=[theAugmentID]&eqsessionIDdatarq=[sessionID]&append=T]id=[theAugmentID]&[theField]=[theValue]&UserID=[UserID]&sessionID=[sessionID]&dateNum=[date_num]&timeNum=[time_num]&browserType=[thebrowserType]&theIP=[theCall][/replace] > > > > [/showif] > > [!]--show content based on two letter country code -- [/!] > [showif [theField]=two_letter_country] > > [showif [theValue]=ca]aaaa[/showif] > [hideif [theValue]=us]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]=us] > > [showif [errorCatch]=1] > theZip: [theZip]
> [/showif] > [!]--------Function Variables-------[/!] > [text]theDOMAIN=api.infochimps.com [/text] > [text]thePATH2=/web/an/census/[/text] > [text]theMethod=combined[/text] > [text]theResultTYPE=json[/text] > [text]theAPIkey=XXXXXXXXXX[/text] > [text]theCALL2=[theZip][/text] > > > > > > [text]MyTestVariable2=[!] > [/!][TCPConnect host=[theDOMAIN]][TCPSend skipheader=T]GET > [thePATH2][theMethod].[theResultTYPE]?zip=[theCALL2]&apikey=[theAPIkey] HTTP/1.0[unurl]%0D%0A[/Unurl]Host: > [theDOMAIN][unurl]%0D%0A%0D%0A[/UnURL][/TCPSend] > [/TCPConnect][/text] > > [ListWords Words=[convertchars > table=JSON_Convert][MyTestVariable2][/convertchars]&Delimiters=,] > [showif [showVariables]=1] > theField: [word]
> [/showif] > > [listwords words=[word]&Delimiters=:] > [showif [index]=1] > > [showif [showVariables]=1] > theField: [word]
> [/showif] > > [text]theFieldName=[word][/text] > > [/showif] > > [showif [index]=2] > > [showif [showVariables]=1] > 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=[pagePath2]databases/XXXXXXX.db&eqiddatarq=[theAugmentID]&eqsessionIDdatarq=[sessionID]&append=T][theFieldName]=[word][/replace] > [/showif] > > [/listwords] > > > > [/listwords] > > > [/showif] > > > [showif [errorCatch]=1] > Replacing Next Number to [theAugmentID]
> [/showif] > [replace > db=[pagePath2]databases/XXXXXX.db&eqnumberIDdatarq=augmentID]NUMBER=[theAugmentID][/replace] > > [/then] > [else][/else] > [/if] > > > [/function] > --------------------------------------------------------- This message > is sent to you because you are subscribed to the mailing list . To > unsubscribe, E-mail to: archives: > http://mail.webdna.us/list/talk@webdna.us Bug Reporting: > support@webdna.us -- Jym Duane - CTO - Purpose Media Creating Your Success Story Marketing : Television - Internet - Print Phone: (877) 443-1323 Email: jym@purposemedia.com Web: www.purposemedia.com Oregon - www.GuideToOregon.com PO Box 1725,Jacksonville, OR 97530 California - www.OrangeCounty.net PO Box 2025,Capistrano Beach, CA 92624 --------------020304090904070704030202 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit way cool.
jym

On 3/19/11 1:18 PM, David Bastedo wrote: 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 2000 census data.



http://www.infochimps.com/datasets/digital-element-ip-intelligence-geolocation

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-geolocation/#more-95


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

[showif "[name]"="X-Real-IP"]
    [text]ip_address=[value][/text]
[/showif]

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

[/ListMIMEHeaders]




[function name=ParseIP]
[!]--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=1[/text]
[text]showVariables=1[/text]


[!]--------Technical Variables-------[/!]
[text]updateIDNumber=0[/text]
[text]theBrowserStatus=Y[/text]
[!]--Date and time stamp encoding ready for use --[/!]
[text]date_num=[math]{[date]}[/math][/text]
[text]time_num=[math]{[time]}[/math][/text]
[!]--path variable--[/!]
[text]pagePath2=[/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=browserBotWords&fields=botWord]
googlebot
Baiduspider
nagios
crawler
bingbot
spider
spyder
siteBot
Slurp
dotbot
[/table]







[!]--------Function Variables-------[/!]
[text]theDOMAIN=api.infochimps.com[/text]
[text]thePATH=/web/an/de/[/text]
[text]theMethod=geo[/text]
[text]theResultTYPE=json[/text]
[!]----Get your own api key and insert below--[/!]
[text]theAPIkey=XXXXXXXXXXXXX[/text]
[!]--the ip address - how ever you get it - it cannot have extra "0"s as in .092 and must be .92--[/!]
[text]theCALL=[ip_address][/text]
[!]--an id I create for the data --[/!]
[text]theAugmentID=[math][lookup db=[pagePath2]databases/XXXXXX.db&lookInField=numberID&value=augmentID&returnField=number]+1[/math][/text]
[!]--check to see if the session info has been recorded already--[/!]
[text]updateSession=[lookup db=[pagePath2]databases/XXXXXXXX.db&lookInField=sessionID&value=[MySessionID]&returnField=sessionID&notfound=0][/text]

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

[search table=browserBotWords&gebotWorddata=[blank]]


[showif [showVariables]=1]
    results: [numfound]<br>
[/showif]

[text]mystring=[thebrowserType][/text]
[founditems]

[showif [showVariables]=1]
    botWord: [botWord]<br>
[/showif]


[text]found_at=[FindString source=[mystring]&find=[botWord]][/text]



[showif [found_at]>0]

[showif [showVariables]=1]
<br>---------------------------<br><br>
Found '[botWord]' in source browserType - we suspect it's a BOT!<br>
<br>---------------------------<br>
[/showif]
[!]--set variable to N so that this will not be checked--[/!]
[text]theBrowserStatus=N[/text]
[/showif]

[/founditems]
[/search]

[!]--make sure we know if the session has been recorded--[/!]

[If "[updateSession]" = "0"]
[then][text]updateSession=0[/text][/then]
[else][text]updateSession=1[/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]=]
    [text]MySessionID=[date_num]_[time_num]_[cart][/text]
[/showif]

[showif [sessionID]=[raw][sessionID][/raw]]
    [text]MySessionID=[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]=1]
<br><br>------------------------------------------------------------------<br>
UserID: [UserID]<br>
MysessionID:  [MysessionID]<br>
updateSession: [updateSession]<br>
theAugmentID: [theAugmentID]<br>
browserType: [thebrowserType]<br>
browserStatus: [theBrowserStatus]<br>


------------------------------------------------------------------<br><br>
[/showif]




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


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


[text]MyTestVariable=[!]
[/!][TCPConnect host=[theDOMAIN]][TCPSend skipheader=T]GET [thePATH][theMethod].[theResultTYPE]?ip=[theCALL]&apikey=[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=JSON_Convert&fields=from,to]
{   
}   
"   
[/table]

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

[showif [errorCatch]=1]
    <b>[index]</b><br />
[/showif]

[!]--the structure of the json is that of value:name

[ListWords Words=[word]&Delimiters=:]
[showif [index]=1]
[showif [errorCatch]=1]
    theField: <b>[word]</b><br />
[/showif]

[text]theField=[word][/text]

[/showif]

[showif [index]=2]
    [showif [errorCatch]=1]
        theValue: <b>[word]</b><br />
    [/showif]
   
[text]theValue=[word][/text]
[/showif]

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

[!]------------ZIP----------[/!]
[showif [theField]=zip]

[text]theZip=[theValue][/text]

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

[!]------------Country----------[/!]
[showif [theField]=two_letter_country]
       [text]theCountryCode=[theValue][/text]
[/showif]
[!]-----------------------------------[/!]
[/ListWords]



[showif [errorCatch]=1]
    [index]: [theAugmentID]: [theField]: [theValue]<br>
[/showif]



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

[showif [errorCatch]=1]
    News Session! Replacing& Appending<br>
[/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=[pagePath2]databases/XXXXXX.db&eqiddatarq=[theAugmentID]&eqsessionIDdatarq=[sessionID]&append=T]id=[theAugmentID]&[theField]=[theValue]&UserID=[UserID]&sessionID=[sessionID]&dateNum=[date_num]&timeNum=[time_num]&browserType=[thebrowserType]&theIP=[theCall][/replace]



[/showif]

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

[showif [theValue]=ca]aaaa[/showif]
[hideif [theValue]=us]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]=us]

[showif [errorCatch]=1]
     theZip: [theZip]<br />
[/showif]
[!]--------Function Variables-------[/!]
[text]theDOMAIN=api.infochimps.com[/text]
[text]thePATH2=/web/an/census/[/text]
[text]theMethod=combined[/text]
[text]theResultTYPE=json[/text]
[text]theAPIkey=XXXXXXXXXX[/text]
[text]theCALL2=[theZip][/text]





[text]MyTestVariable2=[!]
[/!][TCPConnect host=[theDOMAIN]][TCPSend skipheader=T]GET [thePATH2][theMethod].[theResultTYPE]?zip=[theCALL2]&apikey=[theAPIkey] HTTP/1.0[unurl]%0D%0A[/Unurl]Host: [theDOMAIN][unurl]%0D%0A%0D%0A[/UnURL][/TCPSend]
[/TCPConnect][/text]

[ListWords Words=[convertchars table=JSON_Convert][MyTestVariable2][/convertchars]&Delimiters=,]
[showif [showVariables]=1]
    theField: <b>[word]</b><br />
 [/showif]
   
 [listwords words=[word]&Delimiters=:]
[showif [index]=1]

[showif [showVariables]=1]
    theField:  [word]<br />
[/showif]
 
[text]theFieldName=[word][/text]

[/showif]

[showif [index]=2]

[showif [showVariables]=1]
    theValue:  [word]<br />
[/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=[pagePath2]databases/XXXXXXX.db&eqiddatarq=[theAugmentID]&eqsessionIDdatarq=[sessionID]&append=T][theFieldName]=[word][/replace]
[/showif]
   
[/listwords]

   

[/listwords]


[/showif]


[showif [errorCatch]=1]
    Replacing Next Number to [theAugmentID]<br>
[/showif]
    [replace db=[pagePath2]databases/XXXXXX.db&eqnumberIDdatarq=augmentID]NUMBER=[theAugmentID][/replace]

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


[/function]
--------------------------------------------------------- This message is sent to you because you are subscribed to the mailing list . To unsubscribe, E-mail to: archives: http://mail.webdna.us/list/talk@webdna.us Bug Reporting: support@webdna.us
--


Jym Duane - CTO - Purpose Media

Creating Your Success Story

Marketing : Television - Internet - Print


Phone: (877) 443-1323   

Email: jym@purposemedia.com   

Web: www.purposemedia.com


Oregon - www.GuideToOregon.com

PO Box 1725,  Jacksonville, OR 97530


California - www.OrangeCounty.net

PO Box 2025,  Capistrano Beach, CA 92624

--------------020304090904070704030202-- Jym Duane

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:

[browsername] not working? (1997) Nav. 4 probs with cart - Serious problem (1997) Refreshing Javascript (2001) Bug or syntax error on my part? (1997) Anyone get webdna to run on apache 2.x on osx? (2006) Nested tags count question (1997) [BULK] [WebDNA] [BULK] 7.x and 6.x on same server (2012) Add/remove problem (1999) Greeting Card System (2000) Document Contains No Data! (1997) Shipping calculations (1997) possible, WebCat2.0 and checkboxes-restated (1997) Extended [ConvertChars] (1997) LineItem variable set (2007) Re:I've had it! (1999) search same field twice??? (1999) [WebDNA] WebDNA 6.1 Info (2008) BBEdit and WebCatalog 2.0? (1997) Smart caching problems with 2.1b3? (1997) apostrophe in search item (1997)