ヴァル研究所が提供している、公共交通機関の乗り換え検索アプリケーション「駅すぱあと」のREST APIを呼び出して、バス路線の検索を行うAppleScriptです。
(「47都道府県鉄道路線図」より引用)
掲載の本Scriptをためしてみる場合には、かならずご自分で「駅すぱあとWebサービス スタンダードプラン」90日間無料試用コードを取得してScript末尾にあるハンドラに記入してから実行してください。そのまま実行するとエラーになります。
駅コードで発停留所、着停留所をIDで指定して経路検索を行います。本サンプルでは、関東バスの「中村橋」から、「阿佐ヶ谷駅」までの経路を検索しています。時間については平均待ち時間を適用しているとのこと。
AppleScript名:(GET)駅すぱあとAPIで平均待ち時間バスのみ探索 |
— Created 2018-03-16 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.5" –macOS 10.11 or later use scripting additions use framework "Foundation" –http://docs.ekispert.com/v1/api/search/course/bus.html set aURL to "https://api.ekispert.jp/v1/json/search/course/bus" set apiKey to retAccessToken() of me set aRec to {|key|:apiKey, |from|:"45848", |to|:"45507"} –関東バス 「中村橋駅」発、「阿佐ヶ谷」着 set reqURLStr to retURLwithParams(aURL, aRec) of me set aRes to callRestGETAPIAndParseResults(reqURLStr) of me set aRESCode to (responseCode of aRes) as integer if aRESCode is not equal to 200 then return false set aRESHeader to responseHeader of aRes set aRESTres to json of aRes as list of string or string –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 setValue:"gzip" forHTTPHeaderField:"Content-Encoding" 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 on retURLwithParams(aBaseURL, aRec) set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec set aKeyList to (aDic’s allKeys()) as list set aValList to (aDic’s allValues()) as list set aLen to length of aKeyList set qList to {} repeat with i from 1 to aLen set aName to contents of item i of aKeyList set aVal to contents of item i of aValList set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal) end repeat set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL aComp’s setQueryItems:qList set aURL to (aComp’s |URL|()’s absoluteString()) as text return aURL end retURLwithParams on retAccessToken() return "xxxx_XXxxXXXxxXX" –API Key end retAccessToken |
More from my site
(Visited 325 times, 1 visits today)