-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (72 loc) · 2.9 KB
/
index.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
// Author: Hae-Ji Park (github.com/positive235)
// Date: Jul 2021
// Summary: Nature image web scraper. Returns nature image URLs.
// Deployed by Google Cloud.
const puppeteer = require('puppeteer');
const express = require('express');
const app = express();
const urls = [
'https://unsplash.com/s/photos/natural',
'https://unsplash.com/s/photos/nature',
'https://unsplash.com/s/photos/landscape',
'https://unsplash.com/s/photos/beach',
'https://unsplash.com/s/photos/sky',
'https://unsplash.com/s/photos/mountains',
'https://unsplash.com/s/photos/desert',
'https://unsplash.com/s/photos/water',
'https://unsplash.com/s/photos/ocean',
'https://unsplash.com/s/photos/forest',
'https://unsplash.com/s/photos/flowers',
'https://unsplash.com/s/photos/grass',
'https://unsplash.com/s/photos/flower-garden',
'https://unsplash.com/s/photos/vegetation'
]
// Home page.
app.get('/', (req, res) => {
res.header("Access-Control-Allow-Origin", "*")
res.send("Welcome to Nature Image Web Scraper(Author: Hae-Ji Park). \n\n\n" +
"Go to '/a-nature-image' to get a nature image randomly. \n\n" +
"Go to '/a-set-of-nature-images' to get a set of nature images randomly (About 8 images).")
});
// A nature image.
// Returns a nature image URL.
app.get('/a-nature-image', (req, res) => {
(async () =>{
let a_set_images = [];
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(urls[Math.floor(Math.random() * urls.length)], {
waitUntil: 'networkidle2',
timeout: 60000000
});
const resultsSelector = '.oCCRx';
const imageUrl = await page.evaluate(() => document.querySelector('.oCCRx').getAttribute('src'));
res.header("Access-Control-Allow-Origin", "*")
res.send(JSON.stringify({'imageUrl': imageUrl}));
await browser.close();
})();
});
// Eight to ten nature images.
// Returns nature image URLs in an array.
app.get('/a-set-of-nature-images', (req, res) => {
(async () =>{
let a_set_images = [];
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(urls[Math.floor(Math.random() * urls.length)], {
waitUntil: 'networkidle2',
timeout: 60000000
});
const resultsSelector = '.oCCRx';
const results = await page.$$eval(resultsSelector, el => el.map(el => el.getAttribute('src')));
results.forEach(result => a_set_images.push(result));
res.header("Access-Control-Allow-Origin", "*")
res.send(JSON.parse(JSON.stringify(a_set_images)));
await browser.close();
})();
});
// 8080 Port for Google Cloud.
var server_port = process.env.PORT || 8080;
app.listen(server_port, function() {
console.log('Listening on port %d', server_port);
});