AppleScript名:ip-apiで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 getGeoLocationByIPAIP("45.59.69.202") of me –> {lon:-75.5281, zip:"19801", query:"45.59.69.202", as:"AS3800 Ionity Corporation", countryCode:"US", isp:"Ionity Corporation", lat:39.7157, city:"Wilmington", region:"DE", timezone:"America/New_York", org:"Ionity Corporation", country:"United States", regionName:"Delaware", status:"success"} –http://ip-api.com/docs/api:json on getGeoLocationByIPAIP(myIP) set aURL to "http://ip-api.com/json/" & 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 getGeoLocationByIPAIP –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 list of string or string set resHeaders to (dRes’s allHeaderFields()) as list of string or string else set resCode to 0 set resHeaders to {} end if return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders} end callRestGETAPIAndParseResults |
More from my site
(Visited 46 times, 1 visits today)