いつもお世話になります。TwitterAPIからツイートデータを取得してスプレッドシートに保存するGASコードを他サイト等のサンプルと手がかりに作成しましたが、下記の点が実行できないことから、コードの記述方法等ご教示頂ければ幸いです(詳細は下記コードの★該当部分★をご確認下さい)
・ツイート本文中のメディアファイルのリンクURLについて、ツイート中に複数のリンクがある場合
に、最初のURLしか保存できない
・ツイート内のメディアファイル以外のURLについても保存したい
1var SCREEN_NAME_FIRST = "hogehoge" 2var CONSUMER_KEY = " "; 3var CONSUMER_SECRET = " "; 4var PROJECT_KEY = " "; 5var API_USER_TIMELINE = "https://api.twitter.com/1.1/statuses/user_timeline.json"; 6var API_USER_LOOKUP = "https://api.twitter.com/1.1/users/lookup.json"; 7var API_URL_TIMELINE = "https://api.twitter.com/1.1/statuses/home_timeline.json" 8var SHEET_NAME = "Tweetsave" 9var LIMIT_COUNT = 200; 10var CREATED_AT = 1; 11var USERNAME = 2; 12var USERSNAME = 3; 13var TWEETID = 4; 14var TEXT = 5; 15var RETWEET_COUNT = 6; 16var FAVORITE_COUNT = 7; 17var ENTITIES_URL = 8; 18var ENTITIES_MEDIA = 9; 19var URL = 10; 20 21function getTwitterService() { 22 var service = OAuth1.createService("twitter"); 23 service.setAccessTokenUrl("https://api.twitter.com/oauth/access_token") 24 service.setRequestTokenUrl("https://api.twitter.com/oauth/request_token") 25 service.setAuthorizationUrl("https://api.twitter.com/oauth/authorize") 26 service.setConsumerKey(CONSUMER_KEY); 27 service.setConsumerSecret(CONSUMER_SECRET); 28 service.setProjectKey(PROJECT_KEY); 29 service.setCallbackFunction("authCallback"); 30 service.setPropertyStore(PropertiesService.getScriptProperties()); 31 return service; 32} 33 34function authCallback(request) { 35 var service = getTwitterService(); 36 var isAuthorized = service.handleCallback(request); 37 if(isAuthorized) { 38 return HtmlService.createHtmlOutput("Success! You can close this page."); 39 } else { 40 return HtmlService.createHtmlOutput("Denied. You can close this page"); 41 } 42} 43 44function getuserTweetsfirst() { 45 Logger.log("[START] getuserTweetsfirst()"); 46 47 var service = getTwitterService(); 48 if(service.hasAccess()) { 49 try { 50 var as = SpreadsheetApp.getActiveSpreadsheet(); 51 var tws = as.getSheetByName(SCREEN_NAME_FIRST); // tweetシート 52 var iRow = 1; // 書き込む行数の開始位置 53 var lookup = usersLookup(SCREEN_NAME_FIRST); // /users/lookup.json 54 var statusesCount = Math.floor(lookup.statuses_count); // 全ツイート数 55 var division = Math.floor(statusesCount / LIMIT_COUNT); // 全ツイートを200で割る除算 56 var remain = Math.floor(statusesCount % LIMIT_COUNT); // 全ツイートを200で割る剰余算 57 var apiRequestCount = remain == 0 ? division : division + 1; // APIのリクエスト数 58 var max_id = ""; 59 var since_id = "" 60 var apiUrl = API_USER_TIMELINE + "?screen_name=" + SCREEN_NAME_FIRST + "&count=" + LIMIT_COUNT + "&since_id=" + since_id; 61 62 Logger.log("statusesCount = " + statusesCount); 63 Logger.log("apiRequestCount = " + apiRequestCount); 64 65 for(var i = 0; i < apiRequestCount; i++) { 66 var url = max_id != "" ? apiUrl + "&max_id=" + max_id : apiUrl; 67 var response = service.fetch(url); 68 var tweets = JSON.parse(response.getContentText()); 69 if (tweets.length < 1) { 70 break; 71 } 72 max_id = decStrNum(tweets[tweets.length -1].id_str); 73 74 Logger.log("tweets = " + tweets.length); 75 Logger.log("max_id = " + max_id); 76 77 for(var ii = 0; ii < tweets.length; ii++) { 78 79 var d = new Date(tweets[ii].created_at); 80 tws.getRange(iRow, CREATED_AT ).setValue(Utilities.formatDate(d, "JST", "yyyy-MM-dd HH:mm:ss")); 81 tws.getRange(iRow, USERNAME ).setValue(tweets[ii].user.name); 82 tws.getRange(iRow, USERSNAME ).setValue(tweets[ii].user.screen_name); 83 tws.getRange(iRow, TWEETID ).setValue(tweets[ii].id_str); 84 tws.getRange(iRow, TEXT ).setValue(tweets[ii].text); 85 tws.getRange(iRow, RETWEET_COUNT ).setValue(tweets[ii].retweet_count); 86 tws.getRange(iRow, FAVORITE_COUNT).setValue(tweets[ii].favorite_count); 87 var media = new Array(); 88 //★該当部分★ 89 media = tweets[ii].entities.media; 90 if(media instanceof Array) { 91 Logger.log("media.length = " + media.length); 92 var media_url = []; 93 for(var iii = 0; iii < media.length; iii++) { 94 media_url.push(media[iii].media_url_https); 95 tws.getRange(iRow, ENTITIES_MEDIA).setValue(media_url.join('\n')); 96 } 97 } 98 tws.getRange(iRow, URL).setValue("https://twitter.com/" + SCREEN_NAME + "/status/" + tweets[ii].id_str); 99 iRow++; 100 //★該当部分★ 101 } 102 } 103 104 } catch(e) { 105 Logger.log("[ERROR] getAllTweetsfirst() : " + e.message); 106 MailApp.sendEmail("daiisi.0001@gmail.com", "[ERROR] getAllTweetsfirst()", e.message); 107 } 108 } else { 109 var authorizationUrl = service.authorize(); 110 Logger.log("Please visit the following URL and then re-run the script: " + authorizationUrl); 111 } 112 113 Logger.log("[END] userTweetsfirst()"); 114} 115 116function usersLookup(screen_name) { 117 var service = getTwitterService(); 118 if(service.hasAccess()) { 119 var url = API_USER_LOOKUP + "?screen_name=" + screen_name; 120 var response = service.fetch(url); 121 var tweets = JSON.parse(response.getContentText()); 122 return tweets[0] 123 } else { 124 var authorizationUrl = service.authorize(); 125 Logger.log("Please visit the following URL and then re-run the script: " + authorizationUrl); 126 } 127} 128 129/* 130 * Decreasing 64-bit Tweet ID 131 */ 132function decStrNum(n) { 133 n = n.toString(); 134 var result = n; 135 var i = n.length - 1; 136 while(i > -1) { 137 if(n[i] === "0") { 138 result = result.substring(0, i) + "9" + result.substring(i + 1); 139 i --; 140 } else { 141 result = result.substring(0, i) + (parseInt(n[i], 10) -1).toString() + result.substring(i + 1); 142 return result; 143 } 144 } 145 return result; 146} 147 148function onOpen() { 149 var ss = SpreadsheetApp.getActiveSpreadsheet(); 150 var menuEntries = [ 151 {name: "Tweetをダウンロード", functionName: "getAllTweets"} 152 ]; 153 ss.addMenu("スクリプト",menuEntries); 154 155 156
回答1件
あなたの回答
tips
プレビュー