-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (81 loc) · 2.91 KB
/
Copy pathindex.js
File metadata and controls
89 lines (81 loc) · 2.91 KB
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
const cheerio = require("cheerio");
const request = require("request");
const getByProp = ($, property) =>
$(`meta[property='${property}']`)
.first()
.attr("content") || null;
function collectMeta($, url, deepVideo) {
const ogUrl = getByProp($, "og:url");
const ogVideoUrl = getByProp($, "og:video:secure_url") || getByProp($, "og:video:url");
const res = {
url,
image: getByProp($, "og:image"),
imageWidth: getByProp($, "og:image:width"),
imageHeight: getByProp($, "og:image:height"),
imageType: getByProp($, "og:image:type"),
title: getByProp($, "og:title"),
description: getByProp($, "og:description"),
siteName: getByProp($, "og:site_name"),
ogVideoUrl: (ogVideoUrl || "").indexOf("youtube.com") >= 0 ? null : ogVideoUrl,
ogUrl,
youtube:
(ogVideoUrl || "").indexOf("youtube.com") >= 0
? ogVideoUrl
: !ogUrl ? null : ogUrl.indexOf("youtube.com") >= 0 ? `https://youtube.com/embed/${getValue(ogUrl, "v")}` : null
};
if (deepVideo && !res.ogVideoUrl && !res.youtube)
return getDeepVideo(
(getByProp($, "og:description") || "").match(/(http(s)?:\/\/([a-z0-9]+\.)+[a-z0-9]+(\/[a-z0-9]+)?)/gi)
).then(ogVideoUrl => {
videoUrl = ogVideoUrl.filter(url => !!url)[0] || "";
return Object.assign(res, {
[videoUrl.indexOf("youtube.com") >= 0 ? "youtube" : "ogVideoUrl"]: videoUrl || null
});
});
return Promise.resolve(res);
}
const getDeepVideo = urls => {
if (!urls) return Promise.resolve([null]);
return Promise.all(
urls.map(url =>
linkPreview
.makeRequest(url, 10000)
.then(
({ response, body }) =>
!response || response.statusCode !== 200
? null
: getByProp(cheerio.load(body), "og:video:secure_url") || getByProp(cheerio.load(body), "og:video:url")
)
)
);
};
const getValue = (url, name) => {
const i = url.indexOf(`${name}=`) + name.length + 1;
const j = url.indexOf("&", i);
const end = j < 0 ? url.length : j;
return i < 0 ? "" : url.slice(i, end);
};
const getError = url => ({
url,
image: null,
imageWidth: null,
imageHeight: null,
imageType: null,
title: undefined,
description: null,
siteName: null
});
function linkPreview(url, deepVideo = false, timeout = 100000) {
if (!url || url === "") return Promise.reject({ message: "You must add a valid url" });
if (!url.match(/^http(s)?:\/\/[a-z]+\.[a-z]+(.)+/i)) return Promise.resolve(getError(url));
return linkPreview.makeRequest(url, timeout).then(({ response, body }) => {
if (!response) return getError(url);
if (response.statusCode === 200) return collectMeta(cheerio.load(body), url, deepVideo);
return getError(url);
});
}
linkPreview.makeRequest = (url, timeout) =>
new Promise((resolve, reject) => {
request(url, { timeout: timeout }, (error, response, body) => resolve({ body, response }));
});
module.exports = linkPreview;