Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/link-preview.js
1129 views
1
"use strict";
2
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
if (k2 === undefined) k2 = k;
4
var desc = Object.getOwnPropertyDescriptor(m, k);
5
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
desc = { enumerable: true, get: function() { return m[k]; } };
7
}
8
Object.defineProperty(o, k2, desc);
9
}) : (function(o, m, k, k2) {
10
if (k2 === undefined) k2 = k;
11
o[k2] = m[k];
12
}));
13
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
Object.defineProperty(o, "default", { enumerable: true, value: v });
15
}) : function(o, v) {
16
o["default"] = v;
17
});
18
var __importStar = (this && this.__importStar) || function (mod) {
19
if (mod && mod.__esModule) return mod;
20
var result = {};
21
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
__setModuleDefault(result, mod);
23
return result;
24
};
25
Object.defineProperty(exports, "__esModule", { value: true });
26
exports.getUrlInfo = void 0;
27
const messages_media_1 = require("./messages-media");
28
const THUMBNAIL_WIDTH_PX = 192;
29
/** Fetches an image and generates a thumbnail for it */
30
const getCompressedJpegThumbnail = async (url, { thumbnailWidth, timeoutMs }) => {
31
const stream = await (0, messages_media_1.getHttpStream)(url, { timeout: timeoutMs });
32
const result = await (0, messages_media_1.extractImageThumb)(stream, thumbnailWidth);
33
return result;
34
};
35
/**
36
* Given a piece of text, checks for any URL present, generates link preview for the same and returns it
37
* Return undefined if the fetch failed or no URL was found
38
* @param text first matched URL in text
39
* @returns the URL info required to generate link preview
40
*/
41
const getUrlInfo = async (text, opts = { thumbnailWidth: THUMBNAIL_WIDTH_PX, timeoutMs: 3000 }) => {
42
try {
43
const { getLinkPreview } = await Promise.resolve().then(() => __importStar(require('link-preview-js')));
44
let previewLink = text;
45
if (!text.startsWith('https://') && !text.startsWith('http://')) {
46
previewLink = 'https://' + previewLink;
47
}
48
const info = await getLinkPreview(previewLink, { timeout: opts.timeoutMs });
49
if (info && 'title' in info) {
50
const [image] = info.images;
51
let jpegThumbnail = undefined;
52
try {
53
jpegThumbnail = image
54
? await getCompressedJpegThumbnail(image, opts)
55
: undefined;
56
}
57
catch (error) {
58
}
59
return {
60
'canonical-url': info.url,
61
'matched-text': text,
62
title: info.title,
63
description: info.description,
64
jpegThumbnail
65
};
66
}
67
}
68
catch (error) {
69
if (!error.message.includes('receive a valid')) {
70
throw error;
71
}
72
}
73
};
74
exports.getUrlInfo = getUrlInfo;
75
76