forked from tanshuai/alphabiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publicTwitter.js
67 lines (59 loc) · 1.97 KB
/
publicTwitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { TwitterApi } = require('twitter-api-v2')
const fs = require('fs')
require('dotenv').config()
let describe = fs.readFileSync('./github-describe/github-describe.txt', 'utf-8')
describe = describe.replace(/\r\n/gm, '\n')
// describe length cannot exceed 280
const getDescribe = (describe) => {
let desc = describe.replace(/(,\s|)skip e2e/gm, '').replace(/^\s*\n/gm, '')
desc = desc.substring(0, 280)
desc = desc.replace(/\r?\n?[^\r\n]*$/, "").replace(/\.(?=\w+)/gm, ' ')
return desc
}
const publicTwitter = async (describe) => {
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 res = await twitterClient.v2.userTimeline('424858141')
// const lastDesc = res._realData.data[0].text
// const tweetList = res._realData.data
// for (const tweetObj of tweetList) {
// const lastDesc = tweetObj.text
// // console.log(tweetObj)
// // console.log('------------------------')
// if (describe === lastDesc + '\n' || describe === lastDesc.replace(/\r/gm, '\n')) {
// console.log('duplicate content!')
// return false
// }
// }
await twitterClient.v2.tweet(describe).catch(err => {
if (!err.data.detail.includes('Tweet with duplicate content')) throw err
})
console.log('Tweet successfully!')
return true
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
(async () => {
const descArr = []
let remainDesc = describe
while (1) {
if (remainDesc.length < 280) {
descArr.push(remainDesc)
break
}
const desc = getDescribe(remainDesc)
descArr.push(desc)
remainDesc = remainDesc.slice(desc.length + 1, remainDesc.length)
}
console.log(descArr)
for (const d of descArr) {
const result = await publicTwitter(d)
if (!result) return
await sleep(5000)
}
})()