This commit is contained in:
ekardnam 2019-07-26 18:55:25 +02:00
parent 9869f60213
commit 0649ade820
2 changed files with 14 additions and 13 deletions

2
app.js
View file

@ -16,7 +16,7 @@ let main = () => {
} }
if (storage.data.token && storage.data.token_secret) { if (storage.data.token && storage.data.token_secret) {
console.log("[INFO] Starting bot") console.log('[INFO] Starting bot')
bot.start() bot.start()
} else { } else {
oauth.start() oauth.start()

27
bot.js
View file

@ -57,6 +57,7 @@ let bot = {
// Video 15MB // Video 15MB
// medias is an array of filenames // medias is an array of filenames
twitterPostWithMedias: function (post, medias) { twitterPostWithMedias: function (post, medias) {
let that = this
let pieces = this.splitForImage(post) let pieces = this.splitForImage(post)
let uploads = [] let uploads = []
@ -65,7 +66,7 @@ let bot = {
let b64content = readBase64(media) let b64content = readBase64(media)
uploads.push( uploads.push(
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
this.twitter.post('media/upload', { media_data: b64content }, (err, data, response) => { that.twitter.post('media/upload', { media_data: b64content }, (err, data, response) => {
if (err) reject(err) if (err) reject(err)
else resolve(data) else resolve(data)
}) })
@ -75,19 +76,18 @@ let bot = {
return Promise.all(uploads).then(datas => { return Promise.all(uploads).then(datas => {
let mediaIds = datas.map(data => data.media_id_string) let mediaIds = datas.map(data => data.media_id_string)
let altText = 'test' return new Promise((resolve, reject) => {
mediaIds.forEach(mediaId => { that.twitter.post('statuses/update', { status: pieces[0], media_ids: mediaIds }, (err, data, response) => {
this.twitter.post('media/metadata/create', { media_id: mediaId, alt_text: { text: altText } }, (err, data, response) => {
if (!err) {
this.twitter.post('statuses/update', { status: pieces[0], media_ids: mediaIds }, (err, data, response) => {
if (!err) { if (!err) {
resolve(data)
} else reject(err)
})
})
}).then((data) => {
if (pieces.length > 1) return that.twitterPost(pieces[1], data.id_str)
else return Promise.resolve()
}).then(() => {
console.log(`[INFO] Tweeted ${post} with ${medias}`) console.log(`[INFO] Tweeted ${post} with ${medias}`)
if (pieces.length > 1) this.twitterPost(pieces[1], data.id_str)
} else throw err
})
} else throw err
})
})
}) })
}, },
@ -111,7 +111,7 @@ let bot = {
promise = promise.then(data => this.promisePost(pieces[i], data.id_str)) promise = promise.then(data => this.promisePost(pieces[i], data.id_str))
} }
promise.then(() => console.log(`[INFO] Tweeted ${post}`)) return promise.then(() => console.log(`[INFO] Tweeted ${post}`))
.catch(err => console.error(err)) .catch(err => console.error(err))
}, },
@ -138,6 +138,7 @@ let bot = {
.replace(/<br\/>/g, '\n') .replace(/<br\/>/g, '\n')
.replace(/<\/p>/g, '\n') .replace(/<\/p>/g, '\n')
.replace(/&apos;/g, '\'') .replace(/&apos;/g, '\'')
.replace(/&quot;/g, '"')
.replace(/<[^>]*>?/gm, '') .replace(/<[^>]*>?/gm, '')
}, },