-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.js
86 lines (80 loc) · 2.57 KB
/
scraper.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
const express = require('express');
const app = express();
const cors = require('cors');
app.use(express.json());
app.use(cors());
const axios = require('axios');
// const cheerio = require('cheerio');
const InstagramClient = require('./utils/insta-client');
const client = new InstagramClient();
// // accepts the URL of an instagram page
// const getVideo = async (url) => {
// // calls axios to go to the page and stores the result in the html variable
// const html = await axios.get(url);
// // calls cheerio to process the html received
// // const $ = cheerio.load(html.data);
// // searches the html for the videoString
// const videoString = $("meta[property='og:video']").attr('content');
// console.log(videoString);
// // returns the videoString
// return videoString;
// };
// // the callback is an async function
// app.post('/api/download', async (request, response) => {
// console.log('request coming in...', request.body);
// try {
// // call the getVideo function, wait for videoString and store it
// // in the videoLink variable
// const videoLink = await getVideo(request.body.url);
// // if we get a videoLink, send the videoLink back to the user
// if (videoLink !== undefined) {
// response.json({ downloadLink: videoLink });
// } else {
// // if the videoLink is invalid, send a JSON response back to the user
// response.json({ error: 'The link you have entered is invalid. ' });
// }
// } catch (err) {
// // handle any issues with invalid links
// response.json({
// error: 'There is a problem with the link you have provided.',
// });
// }
// });
app.get('/:instagramName/followers', async (request, response) => {
const instagramName = request.params.instagramName;
try {
const followers = await client.getFollowers(instagramName);
response.json({
success: true,
followers: followers,
});
} catch (e) {
response.json({
success: false,
error: e.toString(),
});
return;
}
});
app.post('/download/video', async (req, res) => {
const url = req.body.url;
try {
const downloadLink = await client.getVideoDownloadLink(url);
if (downloadLink != undefined) {
res.json({
downloadLink: downloadLink,
});
} else {
res.json({ error: 'Link is not valid.' });
}
} catch (e) {
res.json({
error: 'There is a problem with the link you have provided.',
});
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, async () => {
await client.start();
console.log(`listening on ${PORT}`);
});