-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
76 lines (70 loc) · 2.13 KB
/
app.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
const { default: axios } = require("axios");
const probe = require("probe-image-size");
var bodyParser = require("body-parser");
const express = require("express"),
cors = require("cors");
require("dotenv").config();
const { getPreviewFromContent } = require("link-preview-js");
const youtube_parser = (url) => {
const regExp =
/.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|live\/)([^#\&\?]*).*/;
const match = url.match(regExp);
return match && match[1].length == 11 ? match[1] : false;
};
const makeUrl = ({ url }) => {
if (!url || url == "") {
throw new Error("not valid link");
}
let newUrl = url;
if (!/^https?:\/\//i.test(url)) {
newUrl = "http://" + url;
}
return newUrl;
};
const app = express();
app.use(cors());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.get("/", async (_, res) => {
res.status(200).json({ message: "Hello there!" });
});
app.post("/", async (req, res) => {
try {
const url = makeUrl(req.body);
const response = await axios.get(url);
getPreviewFromContent({ ...response, url }).then(async (data) => {
console.log("data ---", data);
let newData = data;
const imagesource = data?.images[0] || null;
if (imagesource) {
try {
let result = await probe(imagesource);
const ratio = Number((result.width / result.height).toFixed(2));
newData = {
...newData,
ratio,
};
} catch (error) {
newData = {
...newData,
ratio: 1,
};
}
}
// check here if it is youtube video
if (newData.mediaType == "video.other") {
const videoId = youtube_parser(url);
newData = { ...newData, videoId };
}
console.log("newData ---", newData);
return res.status(200).json(newData);
});
} catch (error) {
return res.status(401).json({ message: error?.message });
}
});
const server = app.listen(process.env.PORT || 4000, function () {
console.log("app runing on port " + server.address().port);
});