Skip to content

Commit

Permalink
Twitter channel is simpler now. See #37
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptype committed May 27, 2023
1 parent e8d2f0d commit 9f5adfd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 73 deletions.
58 changes: 9 additions & 49 deletions add
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const fs = require('fs')
const inquirer = require('inquirer')
const axios = require('axios')
const cheerio = require('cheerio')
const { TwitterApi } = require('twitter-api-v2')
const links = require('./links')
const { execute } = require('./scripts/helpers')

Expand All @@ -18,36 +17,7 @@ const isTweet = url => {
return /twitter.com\/.+\/status\/\d+$/.test(url)
}

const getTweetStatusID = tweetUrl => {
return tweetUrl.match(/twitter.com\/.+\/status\/(\d+)$/)[1]
}

const getTweet = tweetUrl => new Promise((resolve, reject) => {
const twitterClient = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_KEY,
appSecret: process.env.TWITTER_CONSUMER_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN_KEY,
accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET
})

const tweetStatusID = getTweetStatusID(tweetUrl)

twitterClient.v2.singleTweet(tweetStatusID, {
'tweet.fields': ['entities']
}).then(tweet => {
let linkUrl = tweetUrl
if (tweet.entities.urls && tweet.entities.urls.length) {
linkUrl = tweet.entities.urls[0].expanded_url
}
resolve({
tweetUrl,
id: tweetStatusID,
linkUrl
})
}).catch(reject)
})

let tweet = null
let linkIsTweet

console.log('Add a new link')

Expand All @@ -61,16 +31,8 @@ const questions = [
return 'Url is required'
}

let url = value

if (isTweet(value)) {
try {
tweet = await getTweet(value)
url = tweet.linkUrl
} catch (e) {
console.error(e)
}
}
const url = value
linkIsTweet = isTweet(url)

const alreadyAdded = links.find(link =>
link.url === url || (
Expand All @@ -91,7 +53,7 @@ const questions = [
message: 'Title',
async default({ url }) {
try {
const $ = await loadPage(tweet ? tweet.linkUrl : url)
const $ = await loadPage(url)
return $('title').text()
} catch {
console.log("Couldn't scrape title, enter manually")
Expand All @@ -108,7 +70,7 @@ const questions = [
name: 'RTQuote',
message: 'Retweet quote',
filter: String,
when: () => tweet
when: () => linkIsTweet
},
{
type: 'input',
Expand All @@ -130,15 +92,13 @@ inquirer.prompt(questions).then(answers => {
.map(tag => tag.trim())
.filter(tag => tag.length)

const twitterOptions = tweet ? {
tweet: {
...tweet,
quote: answers.RTQuote
}
const twitterOptions = linkIsTweet ? {
isTweet: true,
retweetQuote: answers.RTQuote
} : {}

const link = {
url: tweet ? tweet.linkUrl : answers.url,
url: answers.url,
title: answers.title,
tags,
...twitterOptions
Expand Down
29 changes: 5 additions & 24 deletions scripts/channels/twitter.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
const { TwitterApi } = require('twitter-api-v2')
const Channel = require('../lib/Channel')

const userId = '1018316345849630721'

const client = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_KEY,
appSecret: process.env.TWITTER_CONSUMER_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN_KEY,
accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET
})

const statusTemplate = link => (
`${link.title}: ${link.url}`
)
const statusTemplate = link => {
const title = link.isTweet ? link.retweetQuote : link.title
return [title, link.url].filter(Boolean).join(': ')
}

const tweet = item => {
return client.v2.tweet(statusTemplate(item))
}

const retweet = item => {
return client.v2.retweet(userId, item.tweet.id)
}

const retweetWithQuote = item => {
return client.v2.tweet(item.tweet.quote, {
attachment_url: item.tweet.tweetUrl
})
}

module.exports = new Channel({
name: 'twitter',
method(item) {
if (item.tweet) {
if (item.tweet.quote) {
return retweetWithQuote(item)
}
return retweet(item)
}
return tweet(item)
}
method: tweet
})

0 comments on commit 9f5adfd

Please sign in to comment.