-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtwit.js
72 lines (67 loc) · 2.57 KB
/
twit.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
const Twitt = require('twitter');
const twitter = new Twitt({ bearer_token: 'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA' });
const twittOpts = {
tweet_mode: 'extended'
};
const tweet = {
download: async (url) => {
const isTwitterUrl = /https?:\/\/twitter.com\/[0-9-a-zA-Z_]{1,20}\/status\/([0-9]*)/;
let twittId = isTwitterUrl.exec(url)[1]
return twitter.get(`statuses/show/${twittId}`, twittOpts).then(res => {
if (res.extended_entities) {
let fixedMedia;
if (res.extended_entities && res.extended_entities.media[0].type == 'video') {
fixedMedia = {
url: res.extended_entities.media[0].video_info.variants.filter(file => file.content_type == 'video/mp4').slice(-1)[0].url,
type: res.extended_entities.media[0].type
}
} else {
fixedMedia = {
url: res.extended_entities.media[0].media_url_https,
type: res.extended_entities.media[0].type
}
}
let media = []
for (let i of res.extended_entities.media) {
if (i.type == 'video' || i.type == 'animated_gif') {
media.push({
mediaUrl: i.video_info.variants.filter(file => file.content_type == 'video/mp4').slice(-1)[0].url,
mediaType: i.type
})
} else {
media.push({
mediaUrl: i.media_url_https,
mediaType: i.type
})
}
}
return {
fixedMedia,
full_text: res.full_text,
created_at: res.created_at,
username: res.user.screen_name,
name: res.user.name,
media
}
}
}).catch(e => console.log(e))
},
user: async (username) => {
return await twitter.get('users/lookup', {
screen_name: username
})
.then(res => {
return {
status: true,
...res[0]
}
})
.catch(e => {
return {
status: false,
...e[0]
}
})
}
}
module.exports = tweet