|
|
@ -1,24 +1,40 @@
|
|
|
|
const Twitter = require('twitter')
|
|
|
|
const Twitter = require('twit')
|
|
|
|
const morph = require('morphmorph')
|
|
|
|
const morph = require('morphmorph')
|
|
|
|
|
|
|
|
|
|
|
|
const RATE_LIMIT_CODE = 420
|
|
|
|
const RATE_LIMIT_CODE = 420
|
|
|
|
|
|
|
|
const MAX_ALT_TEXT_LENGTH = 420
|
|
|
|
|
|
|
|
|
|
|
|
const client = new Twitter({
|
|
|
|
const client = new Twitter({
|
|
|
|
consumer_key: process.env.TWITTER_CONSUMER_KEY,
|
|
|
|
consumer_key: process.env.TWITTER_CONSUMER_KEY,
|
|
|
|
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
|
|
|
|
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
|
|
|
|
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
|
|
|
|
access_token: process.env.TWITTER_ACCESS_TOKEN_KEY,
|
|
|
|
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
|
|
|
|
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const extractMediaId = morph.get('data.media_id_string')
|
|
|
|
|
|
|
|
const extractImageUrl = morph.get('data.entities.media.0.display_url')
|
|
|
|
|
|
|
|
const extractErrorCode = morph.get('0.code')
|
|
|
|
|
|
|
|
|
|
|
|
const uploadImage = data => client.post('media/upload', { media_data: data })
|
|
|
|
const uploadImage = data => client.post('media/upload', { media_data: data })
|
|
|
|
const uploadTweet = (media = {}) =>
|
|
|
|
const uploadMetadata = (altText, twitterRes = {}) => {
|
|
|
|
client.post('statuses/update', {
|
|
|
|
if (!altText)
|
|
|
|
status: `Carbon Copy #${media.media_id_string.slice(0, 8)}`,
|
|
|
|
return twitterRes
|
|
|
|
media_ids: media.media_id_string
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const extractImageUrl = morph.get('entities.media.0.display_url')
|
|
|
|
const formattedAltText = altText.length > MAX_ALT_TEXT_LENGTH
|
|
|
|
const extractErrorCode = morph.get('0.code')
|
|
|
|
? `${altText.slice(0, MAX_ALT_TEXT_LENGTH - 3)}...`
|
|
|
|
|
|
|
|
: altText
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return client
|
|
|
|
|
|
|
|
.post('media/metadata/create', {
|
|
|
|
|
|
|
|
media_id: extractMediaId(twitterRes),
|
|
|
|
|
|
|
|
alt_text: { text: formattedAltText }
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.then(() => twitterRes)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadTweet = (twitterRes = {}) => client.post('statuses/update', {
|
|
|
|
|
|
|
|
status: `Carbon Copy #${extractMediaId(twitterRes).slice(0, 8)}`,
|
|
|
|
|
|
|
|
media_ids: extractMediaId(twitterRes)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const respondSuccess = (res, url) => res.json({ url })
|
|
|
|
const respondSuccess = (res, url) => res.json({ url })
|
|
|
|
const respondFail = (res, err) => {
|
|
|
|
const respondFail = (res, err) => {
|
|
|
@ -35,11 +51,12 @@ const respondFail = (res, err) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = (req, res) => {
|
|
|
|
module.exports = (req, res) => {
|
|
|
|
if (!req.body.data) {
|
|
|
|
if (!req.body.imageData) {
|
|
|
|
return res.status(400).send()
|
|
|
|
return res.status(400).send()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return uploadImage(req.body.data)
|
|
|
|
return uploadImage(req.body.imageData)
|
|
|
|
|
|
|
|
.then(uploadMetadata.bind(null, req.body.altText))
|
|
|
|
.then(uploadTweet)
|
|
|
|
.then(uploadTweet)
|
|
|
|
.then(extractImageUrl)
|
|
|
|
.then(extractImageUrl)
|
|
|
|
.then(respondSuccess.bind(null, res))
|
|
|
|
.then(respondSuccess.bind(null, res))
|
|
|
|