-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
82 lines (69 loc) · 2.11 KB
/
index.ts
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
import express from "express";
import request from "request";
import cheerio from "cheerio";
const app = express();
const url = "https://pinterest.com/";
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.get("/", (req, res) => {
res.json({
mesg: " Pintrest Scrapper By Sai kishore ",
routes: {
Info: req.protocol + '://' + req.get('host')+"/api/:username/:boardName/info",
Pins: req.protocol + '://' + req.get('host')+"/api/:username/:boardName/pins",
},
});
});
app.get("/api/:boardName", (req, res) => {
res.json({ BoardName: req.params.boardName });
});
/*
* Get Info of pins count and title
*/
/**
* @param username - Pinterest Username
* @param boardName - Pinterest Username you wanna scrape
*/
app.get("/api/:username/:boardName/info", (req, res) => {
let title = "";
request.get(url+req.params.username+'/'+req.params.boardName, (error, response, html) => {
const $ = cheerio.load(html);
title = $("h1").text();
const count: number = parseInt(
$("header")
.find('div > div > div[data-test-id="board-count-info"]')
.text()
.replace(/\D/g, "")
);
res.json({
title: title,
totalPins: count,
});
});
});
/**
* @param username - Pinterest Username
* @param boardName - Pinterest Username you wanna scrape
*/
app.get("/api/:username/:boardName/pins", (req, res) => {
let results: { src: string | undefined; alt: string | undefined }[] = [];
request.get(url+req.params.username+'/'+req.params.boardName, async (error, response, html) => {
const $ = cheerio.load(html);
$("img").each(function (i, image) {
var re = /236x/gi;
let src = $(image).attr("src")?.toString().replace(re, "564x");
let alt = $(image).attr("alt");
results[i] = { src, alt };
});
res.json({ images: results });
});
});
const port = process.env.PORT || 3000;
console.log("PORT " + port);
app.listen(port, () => console.log(`App Listening on PORT ${port}`));