-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.js
executable file
·59 lines (43 loc) · 1.44 KB
/
youtube.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
"use strict";
/**
* @description Search youtube content
* `node youtube.js`
*/
// require modules
const puppeteer = require("puppeteer"),
readline = require("readline-sync");
// declare
let userInput = false;
async function initPupp(url) {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
return { page, browser, url };
}
async function gotoPage(pageInfo) {
await pageInfo.page.goto(pageInfo.url, { waitUntil: "networkidle2" });
await pageInfo.page.setViewport({ width: 1248, height: 1024 });
return pageInfo;
}
async function searchForm(pageInfo) {
const inputForm = await pageInfo.page.$("#search");
await inputForm.type(userInput);
await inputForm.press("Enter");
await pageInfo.page.waitFor(1000);
return pageInfo;
}
async function pageControl(pageInfo) {
await pageInfo.page.screenshot({ path: "youtube_search_list.png" });
const videos = await pageInfo.page.$$("ytd-thumbnail.ytd-video-renderer");
await videos[2].click();
await pageInfo.page.waitFor(2000);
await pageInfo.page.screenshot({ path: "youtube_search_detail.png" });
console.log("saved");
await pageInfo.browser.close();
}
userInput = readline.question("검색어 입력 (종료는 Ctrl+c) > ");
if (userInput.trim().length > 0) {
initPupp("https://youtube.com")
.then(pageInfo => gotoPage(pageInfo))
.then(pageInfo => searchForm(pageInfo))
.then(pageInfo => pageControl(pageInfo));
}