じゃらんの宿表示 Web API経由でリクエストした宿泊情報を返すAppleScriptです。
利用のためには、じゃらんにアカウント登録を行い、アクセスキーを取得。取得したキーをretAccessKey()ハンドラ中に記入することが必要です。
AppleScript名:じゃらんAPIで宿情報を検索する |
— Created 2016-11-20 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" –http://www.jalan.net/jw/jwp0100/jww0102.do property dictStack : missing value — stack to hold array of dictionaries property textInProgress : "" — string to collect text as it is found property anError : missing value — if we get an error, store it here set reqURLStr to "http://jws.jalan.net/APIAdvance/HotelSearch/V1/" set aKey to retAccessKey() of me –横浜みなとみらい地区で2016年11月21日宿泊の1室、大人2名で宿泊可能な施設 set aRec to {|key|:aKey, s_area:"140202", stay_date:"20180421", room_count:"1", adult_num:"2", sc_num:"0"} –日付を「過去」に設定すると結果が返ってこない点に注意 set aURL to retURLwithParams(reqURLStr, aRec) of me set aRes to callRestGETAPIAndParseResults(aURL) of me set aRESCode to responseCode of aRes if aRESCode is not equal to 200 then return false set aRESHeader to responseHeader of aRes set aXMLres to (xml of aRes) set aNameRes to (aXMLres’s valueForKeyPath:"Results.Hotel.HotelName.contents") as list –> {"ホテルルートイン横浜馬車道", "ホテル エディット 横濱", "ホテル アイマーレ 横浜伊勢佐木町", "ホテルパセラの森 横浜関内", "横浜 マンダリンホテル", "ホテルモントレ横浜", "ヨコハマホステルヴィレッジ林会館", "スターホテル横浜", "アパホテル<横浜関内>", "リゾートカプセル桜木町(BBHホテルグループ)"} –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 aXmlRec to my makeRecordWithXML:resStr –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 {xml:aXmlRec, 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 retAccessKey() return "xxxXXXXXXXXXxX" –じゃらんAPIキー end retAccessKey on urlencodeStr(aStr) set aString to current application’s NSString’s stringWithString:aStr set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text return aString end urlencodeStr ——–XMLParse Lib on makeRecordWithXML:xmlString set my dictStack to current application’s NSMutableArray’s array() — empty mutable array set anEmpty to current application’s NSMutableDictionary’s |dictionary|() (my dictStack)’s addObject:anEmpty — add empty mutable dictionary set my textInProgress to current application’s NSMutableString’s |string|() — empty mutable string set anNSString to current application’s NSString’s stringWithString:xmlString set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set theNSXMLParser to current application’s NSXMLParser’s alloc()’s initWithData:theData theNSXMLParser’s setDelegate:me set theResult to theNSXMLParser’s parse() if theResult then — went OK, get first item on stack return ((my dictStack)’s firstObject()) –as record else error (my anError’s localizedDescription() as text) end if end makeRecordWithXML: — this is an XML parser delegate method. Called when new element found on parser:anNSXMLParser didStartElement:elementName namespaceURI:aString qualifiedName:qName attributes:aRecord set parentDict to my dictStack’s lastObject() set childDict to current application’s NSMutableDictionary’s |dictionary|() if aRecord’s |count|() > 0 then childDict’s setValue:aRecord forKey:"attributes" end if set existingValue to parentDict’s objectForKey:elementName if existingValue is not missing value then if (existingValue’s isKindOfClass:(current application’s NSMutableArray)) as boolean then set theArray to existingValue else set theArray to current application’s NSMutableArray’s arrayWithObject:existingValue parentDict’s setObject:theArray forKey:elementName end if theArray’s addObject:childDict else parentDict’s setObject:childDict forKey:elementName end if (my dictStack)’s addObject:childDict end parser:didStartElement:namespaceURI:qualifiedName:attributes: — this is an XML parser delegate method. Called at the end of an element on parser:anNSXMLParser didEndElement:elementName namespaceURI:aString qualifiedName:qName if my textInProgress’s |length|() > 0 then set dictInProgress to my dictStack’s lastObject() dictInProgress’s setObject:textInProgress forKey:"contents" set my textInProgress to current application’s NSMutableString’s |string|() end if my dictStack’s removeLastObject() end parser:didEndElement:namespaceURI:qualifiedName: — this is an XML parser delegate method. Called when string is found. May be called repeatedly on parser:anNSXMLParser foundCharacters:aString if (aString’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceAndNewlineCharacterSet()))’s |length|() > 0 then (my textInProgress)’s appendString:aString end if end parser:foundCharacters: — this is an XML parser delegate method. Called when there’s an error on parser:anNSXMLParser parseErrorOccurred:anNSError set my anError to anNSError end parser:parseErrorOccurred: |
More from my site
(Visited 61 times, 1 visits today)