AppleScript名:Twitterでフォロワーリストの情報を取得して作成日とscreen_nameを抽出して最終Tweet日を取得 |
— 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 set nextCursor to "0" set allFolList to current application’s NSMutableArray’s new() –実際のAPI呼び出し set reqURLStr to "https://api.twitter.com/1.1/followers/list.json" repeat 100 times if nextCursor = "0" then set bRec to {|count|:"200", screen_name:"Piyomaru"} else set bRec to {|count|:"200", cursor:nextCursor, screen_name:"Piyomaru"} end if 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) set followerList to (aRESTres’s valueForKeyPath:"users") as list allFolList’s addObjectsFromArray:followerList set nextCursor to aRESTres’s valueForKeyPath:"next_cursor_str" if (nextCursor as string) = "0" then exit repeat end repeat –return allFolList set creList to (allFolList’s valueForKeyPath:"created_at") as list of string or string set idList to (allFolList’s valueForKeyPath:"id_str") as list of string or string set scNameList to (allFolList’s valueForKeyPath:"screen_name") as list of string or string –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 37 times, 1 visits today)