Long toots

This commit is contained in:
ekardnam 2019-07-23 20:54:03 +02:00
parent 2128b99294
commit a314ce0496

76
main.js
View file

@ -5,6 +5,8 @@ let Twit = require('twit')
// load the configuration
const config = require('./config.js')
const TWITTER_MAX_CHARS = 280
let twitter = new Twit({
consumer_key: config.twitter_consumer_key,
consumer_secret: config.twitter_consumer_secret,
@ -20,19 +22,77 @@ let mastodon = new Mastodon({
api_url: `https://${config.instance}/api/v1/`, // optional, defaults to https://mastodon.social/api/v1/
})
function twitterPost(post) {
twitter.post('statuses/update', { status: post }, (err, data, response) => {
console.log(data)
})
let lastPostDate = new Date().getTime()
// post to twitter
// if post is longer than TWITTER_MAX_CHARS divides in multiple tweets
function twitterPost(post, originalSource) {
function recursivePost(post, inReplyTo = null, tweetCount = 0, fullPost = '') {
if (inReplyTo) {
if (post.length < TWITTER_MAX_CHARS) {
twitter.post('statuses/update', { status: post, in_reply_to_status_id: inReplyTo }, (err, data, response) => {
if (tweetCount > 0) console.log(`[INFO] Partial tweet #${tweetCount}`)
console.log(`[INFO] Tweeted ${fullPost}`)
if (err) console.error(err)
// exit recursion
})
} else {
twitter.post('statuses/update', { status: post.substring(0, TWITTER_MAX_CHARS-3) + '...', in_reply_to_status_id: inReplyTo }, (err, data, response) => {
console.log(`[INFO] Partial tweet #${tweetCount}`)
if (err) console.error(err)
recursivePost(post.substring(TWITTER_MAX_CHARS-3), data.id_str, tweetCount + 1, fullPost)
})
}
} else {
if (post.length < TWITTER_MAX_CHARS) {
twitter.post('statuses/update', { status: post }, (err, data, response) => {
console.log(`[INFO] Tweeted ${post}`)
if (err) console.error(err)
// exit recursion
})
} else {
twitter.post('statuses/update', { status: post.substring(0, TWITTER_MAX_CHARS-3) + '...' }, (err, data, response) => {
console.log(`[INFO] Partial tweet #${tweetCount}`)
if (err) console.error(err)
recursivePost(post.substring(TWITTER_MAX_CHARS-3), data.id_str, tweetCount + 1, post)
})
}
}
}
recursivePost(post)
}
// makes a post formatted for twitter
// mastodon returns posts in HTML
function twitterFormat(post) {
post = post.replace('<br/>', '\n') // new lines
post = post.replace('<p>', '')
post = post.replace('<p/>', '')
return post
}
// poll on mastodon status updates
setInterval(() => {
mastodon.get('timelines/home', {}).then(resp =>
resp.data.filter(it => it.account.username === config.mastodon_user).forEach((post) => {
twitterPost(post.content)
mastodon.get('timelines/home', {}).then(resp => {
let lastPostDateRecord = lastPostDate
resp.data
.filter(it => it.account.username === config.mastodon_user)
.filter(it => Date.parse(it.created_at) > lastPostDate)
.forEach((status) => {
let date = Date.parse(status.created_at)
if (date > lastPostDateRecord) lastPostDateRecord = date
twitterPost(twitterFormat(status.content), status.uri)
})
lastPostDate = lastPostDateRecord
})
)
}, 10000)