-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.js
130 lines (116 loc) · 3.45 KB
/
api.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* This makes the link between back-end and front-end via Twit-lite and Twitter API.
*
* Authors : Corentin Forler, Pierre Sibut-Bourde, 2021.
*/
const Twitter = require('twitter-lite');
if (process.env.NODE_ENV !== 'production') require('dotenv').config()
/**
* @returns {Twitter.default.prototype}
*/
function newClient() {
return new Twitter({
consumer_key: process.env.API_KEY,
consumer_secret: process.env.API_KEY_SECRET,
access_token_key: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
});
}
/**
* @returns {Twitter.default.prototype}
*/
function newUploadClient() {
return new Twitter({
subdomain: 'upload',
version: '1.1',
consumer_key: process.env.API_KEY,
consumer_secret: process.env.API_KEY_SECRET,
access_token_key: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
});
}
/**
* @typedef {{
* text: string
* images?: Buffer[]
* }} Status
*/
/**
* Tweets a text status with one picture
* @param {string} text
* @param {Buffer[]} imageDataArray
* @param {object} additionalParams
* @param {Twitter.default.prototype} client
* @param {Twitter.default.prototype} uploadClient
*/
async function tweetTextAndImages(text, imageDataArray, additionalParams, client, uploadClient) {
const media_ids = [];
// For each image stored in the status as a Buffer
for (const imageData of imageDataArray) {
// Upload it to Twitter's backend
console.warn(`did not upload image Buffer<${imageData.toString('hex').slice(0,8)}>…: not implemented`);
// const media_id = await uploadImage(imageData, uploadClient);
// media_ids.push(media_id);
}
return client.post('statuses/update', {
status: text,
auto_populate_reply_metadata: true,
media_ids: media_ids.length === 0 ? '' : media_ids.join(','),
...additionalParams
});
}
/**
*
* @param {Status[]} thread
* @param {Twitter.default.prototype} client
* @param {Twitter.default.prototype} uploadClient
*/
async function tweetThread(thread, client, uploadClient) {
let lastTweetID = '';
for (const status of thread) {
console.log(`Tweeting ${JSON.stringify(status)}`, lastTweetID.length === 0 ? '(standalone)' : `(in reply to ${lastTweetID})`);
const hasImages = Array.isArray(status.images) && status.images.length > 0;
if (hasImages) {
// Tweet with images
const tweet = await tweetTextAndImages(
status.text,
status.images,
{ in_reply_to_status_id: lastTweetID },
client,
uploadClient
);
lastTweetID = tweet.id_str;
} else {
// Text-only tweet
const tweet = await client.post('statuses/update', {
status: status.text,
auto_populate_reply_metadata: true,
in_reply_to_status_id: lastTweetID,
});
lastTweetID = tweet.id_str;
}
}
}
/**
* @param {Twitter.default.prototype} uploadClient
* @param {Buffer} imageData
*/
async function uploadImage(uploadClient, imageData) {
// post2(uploadClient, imageData.toString('base64'));
// return await _post(uploadClient, 'media/upload', {
// media_category: 'tweet_image',
// media_data: imageData.toString('base64')
// });
}
function API() {
const client = newClient();
const uploadClient = newUploadClient();
const api = {
/** @param {Status[]} thread */
async tweetThread(thread) {
return tweetThread(thread, client, uploadClient);
}
};
return api;
}
module.exports = { API };