ApitoreのREST API「RSS2JSON」を呼び出して、さまざまな種類のRSS(RSS0.9、RSS1.0、RSS2.0、Atomなど)をJSONに変換し、AppleScriptのrecord(record in list)オブジェクトに変換するAppleScriptです。
# Apitoreは2019年5月31日をもってサービスを終了
Language-Detectionは、テキストの言語判定を行います。検索機能に言語の絞り込みを付けたい場合や、テキストに対して言語別の処理をしたい場合に利用します。
本AppleScript利用のためには、Apitoreにサインアップしてアカウントを作成し、新規プロジェクトを作成(Test AppleScriptとか)。そこに、本APIを利用できるように登録し、「アクセストークン」を取得する必要があります。
アクセストークンを取得せずに掲載状態のまま本AppleScriptを実行してもエラーになります。
取得したアクセストークンを本AppleScript中のretAccessToken()ハンドラ内に記載し、実行してください。
AppleScript名:RSS2JSON |
— Created 2016-10-27 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –use keychainLib : script "keychainLib" set rssURL to "http://piyocast.com/as/feed/" set reqURLStr to "https://api.apitore.com/api/28/rome/rss2json" –set accessToken to getPasswordForAccount("apitore API Key", "maro@piyocast.com") of keychainLib set accessToken to retAccessToken() of me —Access Token set aRec to {access_token:accessToken, rss:rssURL} set aURL to retURLwithParams(reqURLStr, aRec) of me set aRes to callRestGETAPIAndParseResults(aURL) of me set aRESCode to (responseCode of aRes) as integer if aRESCode is not equal to 200 then return {false, false} set aRESTres to (json of aRes) as list of string or string –as anything –> {|description|:"Useful & Practical AppleScript archive", author:missing value, link:"http://piyocast.com/as", |log|:"", pubDate:1.520068773E+12, title:"AppleScript Hole", entries:{{author:"Takaaki Naganoya", title:"(GET)国土地理院APIで現在位置の標高を取得する", pubDate:1.519998541E+12, |description|:"<p>国土地理院のREST API「標高API」を呼んで、現在位置の標高を取得するAppleScriptです。 現在位置の取得のためにMac本体のWiFiがオンになっている必要があります。 AppleScript名:(GET) … <a href=\"http://piyocast.com/as/archives/2545\" class=\"more-link\"><span class=\"screen-reader-text\">\"(GET)国土地理院APIで現在位置の標高を取得する\"の</span>続きを読む</a></p><p>投稿 <a rel=\"nofollow\" href=\"http://piyocast.com/as/archives/2545\">(GET)国土地理院APIで現在位置の標高を取得する</a> は <a rel=\"nofollow\" href=\"http://piyocast.com/as\">AppleScript Hole</a> に最初に表示されました。</p>", link:"http://piyocast.com/as/archives/2545"}, …. –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" –aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding" –aRequest’s setValue:"Test AppleScript (gzip)" forHTTPHeaderField:"User-Agent" 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 "xxXXXxxX-XxXx-XXXX-xXXX-XXxXXxxXxxXx" –API Tore Access Token end retAccessToken |
More from my site
(Visited 36 times, 1 visits today)