AppleScript名:ipinfo.ioでIPアドレスから場所を検索 |
— Created 2017-06-05 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set myIP to getGeoLocationByIPinfo("45.59.69.202") of me –> {region:"Delaware", city:"Wilmington", country:"US", org:"AS3800 Talent House, Inc.", hostname:"No Hostname", postal:"19801", ip:"45.59.69.202", loc:"39.7157,-75.5281"} –http://ipinfo.io/developers on getGeoLocationByIPinfo(myIP) set aURL to "http://ipinfo.io/" & myIP set aRes to callRestGETAPIAndParseResults(aURL) of me set aRESTres to (json of aRes) set aRESTcode to responseCode of aRes if aRESTcode is not equal to 200 then return false return aRESTres as record end getGeoLocationByIPinfo –GET methodのREST APIを呼ぶ on callRestGETAPIAndParseResults(aURL) set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL) aRequest’s setHTTPMethod:"GET" aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) aRequest’s setHTTPShouldHandleCookies:false aRequest’s setTimeoutInterval:60 aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept" set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value) set resList to aRes as list set bRes to contents of (first item of resList) set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding) set jsonString to current application’s NSString’s stringWithString:resStr set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) –Get Response Code & Header set dRes to contents of second item of resList if dRes is not equal to missing value then set resCode to (dRes’s statusCode()) as number set resHeaders to (dRes’s allHeaderFields()) as record else set resCode to 0 set resHeaders to {} end if return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders} end callRestGETAPIAndParseResults |
More from my site
(Visited 34 times, 1 visits today)