-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
306 lines (281 loc) · 13.9 KB
/
index.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
var express = require('express');
var app = express();
require('dotenv').config()
const request = require('request');
const { createCanvas } = require('canvas')
const puppeteer = require('puppeteer');
let browser;
(async () => {
browser = await puppeteer.launch({ args: ['--no-sandbox'] })
})();
//error canvas
const canvas1 = createCanvas(500,500)
var ctx1 = canvas1.getContext('2d')
ctx1.font = '15px Verdana'
ctx1.fillText('Oops! Not Found', 50, 50)
app.get('/live', function(req, res){
res.send('Hello World')
});
app.get('/', function (req, res) {
let count = 1
let twitterHandle = ''
let followers = 0
let following = 0
let baseURL = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='
let endURL = 'exclude_replies=true&include_rts=false'
let max_id = Infinity
let tweet = []
try{
if (req.query.twitterHandle){
twitterHandle = req.query.twitterHandle
if(req.query.count){
count = req.query.count
}
let url = baseURL+twitterHandle+'&count='+count.toString()+'&'+endURL
request(url, {
'auth': {
'bearer': process.env.TWITTER_OAUTH_TOKEN
}
}, function (err, resp, body) {
if (resp.statusCode === 200) {
let tweets = JSON.parse(resp.body)
if (tweets.length !== 0) {
// set params from first tweet and get last tweet ID
twitterHandle = tweets[0].user.screen_name
followers = tweets[0].user.followers_count
following = tweets[0].user.friends_count
max_id = tweets[0].id_str
request(url + '&max_id=' + max_id, {
'auth': {
'bearer': process.env.TWITTER_OAUTH_TOKEN
}
}, function(err, resp1, body){
if(resp1.statusCode === 200){
let tweets1 = JSON.parse(resp1.body)
if (tweets1.length !== 0) {
tweets1.forEach(element => {
let z = element.text.split('\n')
tweet.push(z)
});
let canvas_shape = 0
tweet.forEach(el => {
canvas_shape = canvas_shape + el.length
})
// Add linebreak after each tweet and heading to the total length of canvas
canvas_shape = (canvas_shape+tweet.length+1) * 100
const canvas = createCanvas(1300, canvas_shape)
var ctx = canvas.getContext('2d')
ctx.font = '15px Verdana'
var gradient1 = ctx.createLinearGradient(0, 0, canvas.width, 0)
gradient1.addColorStop(0, "blue")
gradient1.addColorStop(0.5, "magenta");
ctx.fillStyle = gradient1;
let heading = 'Twitter Handle:' + twitterHandle + ' Followers:' + followers.toString() + ' Following:' + following.toString()
ctx.fillText(heading, 0, 50)
ctx.strokeStyle = 'rgba(0,0,0,0.5)'
ctx.beginPath()
ctx.lineTo(0, 55)
ctx.lineTo(ctx.measureText(heading).width,55)
ctx.stroke()
ctx.fillText("",0, 70)
// Create gradient
var gradient = ctx.createLinearGradient(0, 57, canvas.width, 57);
gradient.addColorStop(0, "magenta");
gradient.addColorStop(0.5, "blue");
gradient.addColorStop(1.0, "red");
// Fill with gradient
ctx.fillStyle = gradient;
let line_number = 2
for(var i =0;i<tweet.length; i++){
for(var j=0;j<tweet[i].length;j++){
ctx.fillText(tweet[i][j], 0, 50 + ( line_number * 20))
line_number = line_number + 1
}
// Draw line under each tweet
var text = ctx.measureText(tweet[i][j-1])
ctx.beginPath()
ctx.lineTo(0, 55 + ( (line_number-1) * 20))
ctx.lineTo(text.width, 55 + ((line_number-1)* 20))
ctx.stroke()
ctx.fillText("", 0, 50 + (line_number*20))
// Add line break line number
line_number = line_number+1
}
var img = canvas.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
res.end(canvas.toBuffer())
} else{
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
} else {
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
})
} else {
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
} else {
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
})
} else{
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
}
catch(err) {
console.log(err)
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
});
app.get('/tweet', async function (req, res) {
let count = 1
let twitterHandle = ''
let baseURL = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='
let endURL = 'exclude_replies=true&include_rts=false'
let max_id = Infinity
let oembedBase = 'https://publish.twitter.com/oembed?url=https://twitter.com/'
let theme = 'dark'
let maxwidth = '550'
let lang = 'en'
let height = 2000
let id = ''
try {
req.query.id ? (id = req.query.id) : id = ''
req.query.theme ? (theme = req.query.theme) : theme = 'dark'
req.query.maxwidth ? (maxwidth = req.query.maxwidth) : maxwidth = '550'
req.query.lang ? (lang = req.query.lang) : lang = 'en'
req.query.height ? (height = req.query.height) : height = 2000
if (req.query.twitterHandle) {
twitterHandle = req.query.twitterHandle
if (req.query.count) {
count = req.query.count
}
let url = baseURL + twitterHandle + '&count=' + count.toString() + '&' + endURL
request(url, {
'auth': {
'bearer': process.env.TWITTER_OAUTH_TOKEN
}
}, function (err, resp, body) {
if (resp.statusCode === 200) {
let tweets = JSON.parse(resp.body)
if (tweets.length !== 0) {
// set params from first tweet, and get last tweet ID
twitterHandle = tweets[0].user.screen_name
id ? max_id = id : max_id = tweets[0].id_str
let oembedUrl = oembedBase + twitterHandle + '/status/' + max_id + '&theme=' + theme + '&align=center&lang=' + lang + '&maxwidth=' + maxwidth
request(oembedUrl,
async function (err, resp2, body) {
const data = JSON.parse(resp2.body)
let page = ''
try{
if(browser){
page = await browser.newPage()
} else {
browser = await puppeteer.launch({ args: ['--no-sandbox'] })
page = await browser.newPage()
}
await page.setViewport({
width: parseInt(maxwidth),
height: parseInt(height)
})
await page.setContent(data.html, { waitUntil: 'networkidle0' })
let ele = await page.$('.twitter-tweet');
const boundingBox = await ele.boundingBox();
var img = await ele.screenshot({ type: 'png',
clip: {
x: boundingBox.x,
y: boundingBox.y,
width: Math.min(page.viewport().width, boundingBox.width),
height: Math.min(boundingBox.height, page.viewport().height),
}
})
await page.close()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
res.end(img)
} catch(err){
console.log(err)
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
})
} else {
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
} else {
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
})
} else {
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
} catch (err){
console.log(err)
var img = canvas1.toBuffer()
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(canvas1.toBuffer())
}
});
const port = process.env.PORT || 8081
var server = app.listen(port, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})