Last active
August 31, 2018 15:37
-
-
Save m77so/1ac47c78d1c77b9b68b3eaf861549d74 to your computer and use it in GitHub Desktop.
mastdon toot -> twitter tweet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var twitter = TwitterWebService.getInstance( | |
PropertiesService.getScriptProperties().getProperty('twitterCS'), | |
PropertiesService.getScriptProperties().getProperty('twitterCK') | |
) | |
function authorize() { | |
twitter.authorize() | |
} | |
twitter.getService = function() { | |
return OAuth1.createService('Twitter2') | |
.setAccessTokenUrl('https://api.twitter.com/oauth/access_token') | |
.setRequestTokenUrl('https://api.twitter.com/oauth/request_token') | |
.setAuthorizationUrl('https://api.twitter.com/oauth/authorize') | |
.setConsumerKey(twitter.consumer_key) | |
.setConsumerSecret(twitter.consumer_secret) | |
.setCallbackFunction('authCallback') | |
.setPropertyStore(PropertiesService.getUserProperties()) | |
} | |
function authCallback(request) { | |
return twitter.authCallback(request) | |
} | |
function postUpdateStatus(text) { | |
var service = twitter.getService() | |
var response = service.fetch('https://api.twitter.com/1.1/statuses/update.json', { | |
method: 'post', | |
payload: { status: text } | |
}) | |
Logger.log(JSON.parse(response)) | |
} | |
//mastodonTimeline | |
function getToots(token, domain) { | |
var domain = PropertiesService.getScriptProperties().getProperty('mastodonDomain') | |
var token = PropertiesService.getScriptProperties().getProperty('mastodonToken') | |
var url = 'https://' + domain + '/api/v1/accounts/'+PropertiesService.getUserProperties().getProperty('mastodonID')+'/statuses' | |
var options = { | |
method: 'get', | |
headers: { Authorization: 'Bearer ' + token } | |
} | |
var res = UrlFetchApp.fetch(url, options) | |
var jsonData = JSON.parse(res.getContentText()) | |
var filtered = jsonData | |
.filter(function(e) { | |
return e.reblog === null && e.visibility !== 'direct' && e.visibility !== 'private' && e.content.indexOf('@') === -1 | |
}) | |
.map(function(e) { | |
return { | |
text: e.content.replace(/<br \/>/g, '\n').replace(/<("[^"]*"|'[^']*'|[^'">])*>/g, '').replace(/@/g,'@.'), | |
url: e.url, | |
id: e.id | |
} | |
}) | |
return filtered | |
} | |
function getCredentials() { | |
var domain = PropertiesService.getScriptProperties().getProperty('mastodonDomain') | |
var token = PropertiesService.getScriptProperties().getProperty('mastodonToken') | |
var url = 'https://' + domain + '/api/v1/accounts/verify_credentials' | |
var options = { | |
method: 'get', | |
headers: { Authorization: 'Bearer ' + token } | |
} | |
var res = UrlFetchApp.fetch(url, options) | |
var jsonData = JSON.parse(res.getContentText()) | |
PropertiesService.getUserProperties().setProperty('mastodonID', jsonData.id) | |
return jsonData.id | |
} | |
function syncMstdn2Twitter() { | |
var domain = PropertiesService.getScriptProperties().getProperty('mastodonDomain') | |
var token = PropertiesService.getScriptProperties().getProperty('mastodonToken') | |
var toots = getToots(token, domain) | |
var start = PropertiesService.getUserProperties().getProperty('lastid') | |
if(start==null)start='0' | |
var pad0 = function(num){return ('00000000000000000000'+num).slice(-20)} | |
var tweet = toots.filter(function(toot) { | |
return pad0(toot.id) > pad0(start) | |
}) | |
PropertiesService.getUserProperties().setProperty('lastid', toots[0].id) | |
tweet.reverse().forEach(function(t) { | |
postUpdateStatus( t.url + '\n' + t.text.substr(0, 110)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment