Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ツイッター定期消去サービス
var CONSUMER_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var CONSUMER_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var PROJECT_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
function AccessTwitter(endPoint,payload,method) {
var service = getTwitterService();
if (service.hasAccess()) {
var url = 'https://api.twitter.com/1.1/'+endPoint+'.json';
var options = {
method: method,
escaping: false,
};
var payload_str = Object.keys(payload).map(function(key) {
return encodeToRfc3986(key) + '=' + encodeToRfc3986(payload[key]);
}).join('&');
if ("get" === method)
url += '?' + payload_str;
else if ("post" == method)
options.payload = payload_str;
else
Logger.log("methodはpostかgetにしてね");
if (payload.media||payload.image)
options.contentType = "multipart/form-data;charset=UTF-8";
var response = service.fetch(url,options);
return JSON.parse(response.getContentText());
} else {
var authorizationUrl = service.authorize();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
return {error:"authorization"}
}
}
function getTwitterService() {
var service = OAuth1.createService('twitter');
service.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
service.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
service.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
service.setConsumerKey(CONSUMER_KEY);
service.setConsumerSecret(CONSUMER_SECRET);
service.setProjectKey(PROJECT_KEY);
service.setCallbackFunction('authCallback');
service.setPropertyStore(PropertiesService.getScriptProperties());
return service;
}
function authCallback(request) {
var service = getTwitterService();
var isAuthorized = service.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Success! You can close this page.');
} else {
return HtmlService.createHtmlOutput('Denied. You can close this page');
}
}
function encodeToRfc3986(str) {
return encodeURIComponent(str).replace(/[!'()]/g, function(char) {
return escape(char);
}).replace(/\*/g, "%2A");
}
function Tweet(Message){
var payload = {
status: Message
};
AccessTwitter("statuses/update",payload,"post");
}
function test(){
Tweet("test");
}
function deleateTweet(){
var count = 0;
var deleate_count = 0;
var payload = {
count:"200"
};
try{
while(true){
var tweets = AccessTwitter("statuses/user_timeline",payload,"get");
for(var i = 0; i < tweets.length;i++){
if(count>50 && !(tweets[i].text.match(/\#なかひこくんメモ/)) && !(tweets[i].text.match(/\#本日の文化的ツイート/)) ){
AccessTwitter("statuses/destroy/"+tweets[i].id_str,{},"post");
deleate_count++;
}
if(deleate_count%5===0) Utilities.sleep(1000);
count++;
}
Logger.log(tweets.length+","+count);
if(count>50 || tweets.length===0)
break;
else
payload.max_id = tweets[tweets.length-1].id-1;
}
Tweet(deleate_count+"個のツイートを削除しました。(TweetDeleater : "+(new Date())+")");
}catch(e){
Logger.log(e);
Tweet("なんかよくわからないエラーが起きました:"+e+"(TweetDeleater)");
}
}
function reset(){
var service = getTwitterService().reset();
}
function test2(){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment