2020-02-25 15:13:09 +01:00
|
|
|
const fs = require('fs')
|
|
|
|
const Twitter = require('twitter')
|
|
|
|
|
|
|
|
const {
|
|
|
|
TWITTER_CONSUMER_KEY,
|
|
|
|
TWITTER_CONSUMER_SECRET,
|
|
|
|
TWITTER_ACCESS_TOKEN_KEY,
|
|
|
|
TWITTER_ACCESS_TOKEN_SECRET,
|
2020-02-28 08:57:30 +01:00
|
|
|
NUXT_ENV_TWITTER_HASHTAG,
|
2020-02-29 10:17:53 +01:00
|
|
|
TWITTER_INTERVAL_ATTESTATION,
|
|
|
|
NODE_ENV
|
2020-02-25 15:13:09 +01:00
|
|
|
} = process.env
|
|
|
|
|
|
|
|
const client = new Twitter({
|
|
|
|
consumer_key: TWITTER_CONSUMER_KEY,
|
|
|
|
consumer_secret: TWITTER_CONSUMER_SECRET,
|
|
|
|
access_token_key: TWITTER_ACCESS_TOKEN_KEY,
|
|
|
|
access_token_secret: TWITTER_ACCESS_TOKEN_SECRET
|
|
|
|
})
|
|
|
|
|
|
|
|
const { Contribution } = require('./models')
|
|
|
|
|
2020-02-29 10:17:53 +01:00
|
|
|
function attestationWatcher() {
|
2020-02-25 15:13:09 +01:00
|
|
|
// get the last saved tweet
|
|
|
|
let initTweet
|
|
|
|
try {
|
|
|
|
initTweet = require('/tmp/lastTweet.json').lastTweet
|
|
|
|
} catch (e) {
|
2020-02-29 10:17:53 +01:00
|
|
|
initTweet = 0
|
2020-02-25 15:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
since_id: initTweet,
|
2020-02-28 08:57:30 +01:00
|
|
|
q: `#${NUXT_ENV_TWITTER_HASHTAG} -filter:retweets`,
|
2020-02-25 15:13:09 +01:00
|
|
|
result_type: 'recent',
|
|
|
|
count: 100
|
|
|
|
}
|
|
|
|
|
|
|
|
// search tweets with params
|
2020-02-29 10:17:53 +01:00
|
|
|
client.get('search/tweets', params, async function(error, tweets, response) {
|
2020-02-25 15:13:09 +01:00
|
|
|
if (!error) {
|
2020-02-29 10:17:53 +01:00
|
|
|
for (const tweet of tweets.statuses) {
|
|
|
|
if (NODE_ENV === 'development') {
|
|
|
|
console.log(
|
|
|
|
'\x1B[36m%s\x1B[0m',
|
|
|
|
`${tweet.text} https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the contribution id in a tweet
|
|
|
|
let matchTweetContributionId = null
|
|
|
|
let tweetContributionId = null
|
|
|
|
|
|
|
|
if ((matchTweetContributionId = tweet.text.match(/#([0-9]+)/))) {
|
|
|
|
tweetContributionId = Number(matchTweetContributionId[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// if found the contribution id then search a contribution
|
|
|
|
if (tweetContributionId) {
|
|
|
|
// try update the database record by id
|
|
|
|
try {
|
|
|
|
const result = await Contribution.update(
|
|
|
|
{ attestation: tweet.id_str },
|
|
|
|
{
|
|
|
|
where: {
|
|
|
|
id: tweetContributionId,
|
|
|
|
handle: tweet.user.screen_name,
|
|
|
|
attestation: null
|
|
|
|
}
|
|
|
|
}
|
2020-02-25 15:13:09 +01:00
|
|
|
)
|
2020-02-29 10:17:53 +01:00
|
|
|
if (result[0]) {
|
|
|
|
console.log(
|
|
|
|
`Succesful attestation #${tweetContributionId} https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
2020-02-25 15:13:09 +01:00
|
|
|
}
|
2020-02-29 10:17:53 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-25 15:13:09 +01:00
|
|
|
|
|
|
|
// save the last tweet received
|
|
|
|
fs.writeFileSync(
|
|
|
|
'/tmp/lastTweet.json',
|
|
|
|
JSON.stringify({ lastTweet: tweets.search_metadata.max_id_str })
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
console.error('attestationWatcher error', error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
attestationWatcher()
|
|
|
|
}, TWITTER_INTERVAL_ATTESTATION)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = attestationWatcher
|