AppleScript名:Twitterで指定アカウントのタイムラインを取得 |
— Created 2016-11-22 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set myConsumerKey to "xXXXXXXXxxXXxXxxxXXxXxXXX" set myConsumerSecret to "xxxxXXXxXxXXXxXXXxxxXXXXXxxXxxXXXxXXXxxXXxxxXXxxXx" –認証を行って認証済みのBarer Tokenを取得する set authedToken to getAuthedTokenFromTwitter(myConsumerKey, myConsumerSecret) of me if authedToken = missing value then return –実際のAPI呼び出し set reqURLStr to "https://api.twitter.com/1.1/statuses/user_timeline.json" –URLの前後に空白などが入らないように!! set bRec to {|count|:"10", screen_name:"realDonaldTrump"} set bURL to retURLwithParams(reqURLStr, bRec) of me set aRes to callRestGETAPIAWithAuth(bURL, authedToken) of me set aRESCode to responseCode of aRes –Response Code if aRESCode is not equal to 200 then return false set aRESHeader to responseHeader of aRes –Response Header set aRESTres to (json of aRes) as list of string or string –set timeLine to (aRESTres’s valueForKeyPath:"text") as list –> {"Many people would like to see @Nigel_Farage represent Great Britain as their Ambassador to the United States. He would do a great job!", "Prior to the election it was well known that I have interests in properties all over the world.Only the crooked media makes this a big deal!", ".@transition2017 update and policy plans for the first 100 days. https://t.co/HTgPXfPWeJ", "I have always had a good relationship with Chuck Schumer. He is far smarter than Harry R and has the ability to get things done. Good news!", "General James \"Mad Dog\" Mattis, who is being considered for Secretary of Defense, was very impressive yesterday. A true General’s General!", "I watched parts of @nbcsnl Saturday Night Live last night. It is a totally one-sided, biased show – nothing funny at all. Equal time for us?", "Numerous patriots will be coming to Bedminster today as I continue to fill out the various positions necessary to MAKE AMERICA GREAT AGAIN!", "The cast and producers of Hamilton, which I hear is highly overrated, should immediately apologize to Mike Pence for their terrible behavior", "The Theater must always be a safe and special place.The cast of Hamilton was very rude last night to a very good man, Mike Pence. Apologize!", "Our wonderful future V.P. Mike Pence was harassed last night at the theater by the cast of Hamilton, cameras blazing.This should not happen!"} –Authenticate APIを呼び出して認証を行う on getAuthedTokenFromTwitter(aConsumerKey, aConsumerSecret) set aURL to "https://api.twitter.com/oauth2/token" set barerToken to aConsumerKey & ":" & aConsumerSecret set aStr to current application’s NSString’s stringWithString:barerToken set aData to aStr’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set bStr to (aData’s base64EncodedStringWithOptions:0) as string set bStr to current application’s NSString’s stringWithString:("Basic " & bStr) set aRec to {grant_type:"client_credentials"} set a2URL to retURLwithParams(aURL, aRec) of me set a2Res to callRestPOSTAPIWithAuth(a2URL, bStr) of me if (responseCode of a2Res) is not equal to 200 then return set aJSON to (json of a2Res) set authedToken to "Bearer " & (aJSON’s valueForKey:"access_token") return authedToken end getAuthedTokenFromTwitter 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 –POST methodのREST APIを呼ぶ(認証つき) on callRestPOSTAPIWithAuth(aURL, anAPIkey) set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL) aRequest’s setHTTPMethod:"POST" aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) aRequest’s setHTTPShouldHandleCookies:false aRequest’s setTimeoutInterval:60 if anAPIkey is not equal to "" then aRequest’s setValue:anAPIkey forHTTPHeaderField:"Authorization" end if aRequest’s setValue:"gzip" forHTTPHeaderField:"Accept-Encoding" aRequest’s setValue:"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:"Accept" –CALL REST API set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value) –Parse Results 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 callRestPOSTAPIWithAuth –GET methodのREST APIを呼ぶ(認証つき) on callRestGETAPIAWithAuth(aURL, anAPIkey) 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 if anAPIkey is not equal to "" then aRequest’s setValue:anAPIkey forHTTPHeaderField:"Authorization" end if aRequest’s setValue:"gzip" forHTTPHeaderField:"Accept-Encoding" aRequest’s setValue:"application/x-www-form-urlencoded;charset=UTF-8" 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 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 callRestGETAPIAWithAuth |
More from my site
(Visited 30 times, 1 visits today)