-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (104 loc) · 3.2 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
'use strict';
var got = require('got');
var cheerio = require('cheerio');
var Promise = require('pinkie-promise');
var isURL = require('is-url');
function matchRegEx(backURL) {
var regPar = /(?:\(['"]?)(.*?)(?:['"]?\))/;
return regPar.exec(backURL)[1];
}
function convertImageToVideo(imageURL) {
return imageURL.replace('tweet_video_thumb', 'tweet_video').replace('.jpg', '.mp4');
}
function profileImage(username) {
if (typeof username !== 'string') {
return Promise.reject(new Error('username required'));
}
var url = 'https://twitter.com/' + username;
return got(url).then(function (res) {
var $ = cheerio.load(res.body);
return $('.ProfileAvatar-image').attr('src') || null;
}).catch(function (err) {
if (err.statusCode === 404) {
err.message = 'not a twitter user';
}
throw err;
});
}
function coverImage(username) {
if (typeof username !== 'string') {
return Promise.reject(new Error('username required'));
}
var url = 'https://twitter.com/' + username;
return got(url).then(function (res) {
var $ = cheerio.load(res.body);
return $('.ProfileCanopy-headerBg img').attr('src') || null;
}).catch(function (err) {
if (err.statusCode === 404) {
err.message = 'not a twitter user';
}
throw err;
});
}
function singleImage(mediaURL) {
if (typeof mediaURL !== 'string' && isURL(mediaURL) === false) {
return Promise.reject(new Error('url required'));
}
return got(mediaURL).then(function (res) {
var $ = cheerio.load(res.body);
return $('.AdaptiveMedia-singlePhoto .AdaptiveMedia-photoContainer img').attr('src') || null;
}).catch(function (err) {
if (err.statusCode === 404) {
err.message = 'Link do not contain any image file';
}
});
}
function gifPreview(getURL) {
if (typeof getURL !== 'string' && isURL(getURL) === false) {
return Promise.reject(new Error('url required'));
}
return got(getURL).then(function (res) {
var $ = cheerio.load(res.body);
return matchRegEx(($('.PlayableMedia-player').attr('style'))) || null;
}).catch(function (err) {
if (err.statusCode === 404) {
err.message = 'Link do not contain any gif preivew';
}
});
}
function gifLink(getURL) {
if (typeof getURL !== 'string' && isURL(getURL) === false) {
return Promise.reject(new Error('url required'));
}
return got(getURL).then(function (res) {
var $ = cheerio.load(res.body);
var getMediaLink = matchRegEx(($('.PlayableMedia-player').attr('style'))) || null;
return convertImageToVideo(getMediaLink);
}).catch(function (err) {
if (err.statusCode === 404) {
err.message = 'Link do not contain any gif media';
}
throw err;
});
}
function videoPreview(tweetURL) {
if (typeof tweetURL !== 'string' && isURL(tweetURL) === false) {
return Promise.reject(new Error('url required'));
}
return got(tweetURL).then(function (res) {
var $ = cheerio.load(res.body);
var getMediaLink = matchRegEx(($('.PlayableMedia-player').attr('style'))) || null;
return getMediaLink;
}).catch(function (err) {
if (err.statusCode === 404) {
err.message = 'Link do not contain any video preview';
}
throw err;
});
}
exports.profile = profileImage;
exports.cover = coverImage;
exports.single = singleImage;
exports.preview = gifPreview;
exports.gif = gifLink;
exports.vidPrev = videoPreview;