-
Notifications
You must be signed in to change notification settings - Fork 5
/
tweet.js
208 lines (179 loc) · 7.35 KB
/
tweet.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const twit = require('twit');
const axios = require('axios');
const _ = require('lodash');
const Queue = require('queue-fifo');
const singleSaleQ = new Queue()
const events = require('events');
require('dotenv').config();
const { getUsername, getStats, getTokenData, getFlipped} = require('./utils.js');
const twitterConfig = {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN_KEY,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
};
const twitterClient = new twit(twitterConfig);
async function threadTweetWithImage(mainTweetText, imageUrl, buyer, seller, tokenID, isSubtweet) {
// Processing image
const processedImage = await getBase64(imageUrl);
let subTweetText;
if(isSubtweet === 'true'){
// Fetching buyer and seller stats from API by https://2.5.dev/
const statsBuyer = await getStats(buyer)
const statsSeller = await getStats(seller)
// Fetching opensea username
const buyerName = await getUsername(buyer);
const sellerName = await getUsername(seller);
// Fetching data from nftgo
const nftMetadata = await getFlipped();
let buyerLabel = ``;
let sellerLabel = ``;
try{
statsBuyer.labels.forEach(element => {
if(element.name === 'whale') {
buyerLabel = buyerLabel + `🐳`
if(element.name === 'smart-money') buyerLabel = buyerLabel + '🧠'
else if(element.name === 'paperhands') buyerLabel = buyerLabel + '🔃'
}else{
if(element.name === 'smart-money') buyerLabel = buyerLabel + '🧠'
if(element.name === 'paperhands') buyerLabel = buyerLabel + '🔃'
}
});
}catch(error){
console.log("Error in processing buyer stats")
}
try{
statsSeller.labels.forEach(element => {
if(element.name === 'whale') {
sellerLabel = sellerLabel + `🐳`
if(element.name === 'smart-money') sellerLabel = sellerLabel + '🧠'
else if(element.name === 'paperhands') sellerLabel = sellerLabel + '🔃'
}else{
if(element.name === 'smart-money') sellerLabel = sellerLabel + '🧠'
if(element.name === 'paperhands') sellerLabel = sellerLabel + '🔃'
}
});
}catch(error){
console.log("Error in processing seller stats")
}
// forming subtweet text
subTweetText = `Buyer: ${buyerName.username == null? buyer.substring(0,8):buyerName.username} ${buyerLabel}
\bSeller: ${sellerName.username == null? seller.substring(0,8):sellerName.username} ${sellerLabel}
\b${nftMetadata.flippedCount!=='unknown'?`Flipped: ${nftMetadata.flippedCount + 1} times`:""}`
}
// uploading image to twitter
twitterClient.post('media/upload', { media_data: processedImage }, (error, media, response) => {
if (!error) {
const tweet = {
status: mainTweetText,
media_ids: [media.media_id_string]
};
// tweeting main tweet text with image
twitterClient.post('statuses/update', tweet, (error, tweet, response) => {
if (!error) {
console.log(`Successfully tweeted main tweet: ${mainTweetText}`);
if(isSubtweet == 'true'){
const subTweet = {
status: subTweetText,
in_reply_to_status_id: tweet.id_str,
auto_populate_reply_metadata: true
};
// tweeting subtweet text
twitterClient.post('statuses/update', subTweet, (error, subTweet, response) => {
if (!error) {
console.log(`Successfully tweeted subtweet: ${subTweetText}`);
} else {
console.log('Error in tweeting subtweet');
}
});
}
} else {
console.log('Error in tweeting main tweet');
}
});
} else {
console.error("Error in uploading individual sale image");
}
});
}
async function multiSaleTweetWithImage(tweetText, mediaID) {
const tweet = {
status: tweetText,
media_ids: mediaID
};
twitterClient.post('statuses/update', tweet, (error, tweet, response) => {
if (!error) {
console.log(`Successfully tweeted sweep tweet: ${tweetText}`);
}else {
console.error(error);
}
})
}
async function getMediaID(tokens) {
let interval = 0;
let flag = false;
let mediaID = [];
let length = tokens.length>4? 4:tokens.length;
for (let i = 0; i < length; i++) {
singleSaleQ.enqueue(tokens[i]);
}
console.log("Processing Sweep images");
const singleSaleEmitter = new events.EventEmitter()
singleSaleEmitter.on('processNextSale', async () => {
if (!singleSaleQ.isEmpty()){
let data = singleSaleQ.dequeue();
console.log("Fetching next image link");
await getTokenData(data).then(async (tokenData) => {
let imageUrl = _.get(tokenData, 'image_url','false' )
if(imageUrl !== 'false'){
let processedImage = await getBase64(imageUrl);
twitterClient.post('media/upload', { media_data: processedImage }, (error, media, response) => {
if (!error) {
mediaID.push(media.media_id_string);
console.log("Image uploaded successfully");
} else {
console.log("Error while uploading image");
}
});
}
}).catch(error =>{
console.log("Error in fetching image link");
})
}else{
console.log("Processed all transactions in Sweep");
flag=true;
}
});
const until = () =>
new Promise((resolve) => {
interval = setInterval( () => {
if (flag == true) {
clearInterval(interval);
resolve(true);
}
singleSaleEmitter.emit('processNextSale');
}, process.env.DELAY/4)
}
);
await until();
return mediaID;
}
function getBase64(url) {
return axios.get(url, { responseType: 'arraybuffer'}).then(response => {
if(response.status == 200){
console.log("Successfully fetched and processed image")
return Buffer.from(response.data, 'binary').toString('base64')
}
else{
throw "not 200"
}
}).catch(error =>{
console.log("Error in processing image");
})
}
module.exports = {
threadTweetWithImage: threadTweetWithImage,
multiSaleTweetWithImage:multiSaleTweetWithImage,
getBase64:getBase64,
getMediaID:getMediaID
};