ApitoreのREST API「テックサイト ごちゃまぜフィード」を呼び出して、さまざまなテック系サイトのNews Feedを一括で取得するAppleScriptです。
# Apitoreは2019年5月31日をもってサービスを終了
「テックサイト ごちゃまぜフィード」は、30分に1回の頻度でRSSの更新を確認にいきます。REST APIで最新情報が取得できるオンラインクローラーです。
本AppleScript利用のためには、Apitoreにサインアップしてアカウントを作成し、新規プロジェクトを作成(Test AppleScriptとか)。そこに、本APIを利用できるように登録し、「アクセストークン」を取得する必要があります。
アクセストークンを取得せずに掲載状態のまま本AppleScriptを実行してもエラーになります。
取得したアクセストークンを本AppleScript中のretAccessToken()ハンドラ内に記載し、実行してください。
AppleScript名:テックサイト ごちゃまぜフィード |
— Created 2016-10-27 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set allFeeds to {} repeat with i from 1 to 100 set {aRes, maxNum} to getAFeeds(i) of me if aRes = false then exit repeat set maxNum to maxNum as number set allFeeds to allFeeds & aRes if (maxNum div 10) ≤ i then exit repeat end if end repeat return allFeeds –> (* {{author:"Ittousai", sourceTitle:"Engadget Japanese RSS Feed", title:" 速報:サムスンGalaxy S8発表イベント UNPACKED 2017 (ライブ更新ページ) ", link:"http://japanese.engadget.com/2017/03/29/galaxy-s8-unpacked-2017/", pubDate:1.49079606E+12, description:"<p> <img src=\"http://o.aolcdn.com/hss/storage/midas/a009a6b57896291f7e66fe091c6bdf94/205070326/unbox.png\" /> </p>", sourceLink:"http://japanese.engadget.com/rss.xml"}, *) on getAFeeds(aNum) set reqURLStr to "https://api.apitore.com/api/35/feeds/tech" set accessToken to retAccessToken() of me —Access Token set aRec to {access_token:accessToken, page:(aNum as string)} 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 record set sentiRes to entries of aRESTres set allNum to num of aRESTres log allNum return {sentiRes, allNum} end getAFeeds –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 |