-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (62 loc) · 2.28 KB
/
Copy pathindex.js
File metadata and controls
87 lines (62 loc) · 2.28 KB
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
87
import puppeteer from 'puppeteer'
import fs from 'fs'
import { spawn } from 'child_process'
function visualizeData(str, fileName, videoLength) {
fs.writeFile(fileName, str, (err) => {
if (err) {
console.error(err);
return;
}
const pythonProcess = spawn('python', ['script.py', videoLength]);
pythonProcess.stdout.on('data', (data) => {
console.log(`Python script output: ${data}`);
});
pythonProcess.stderr.on('data', (data) => {
console.error(`Python script error: ${data}`);
});
pythonProcess.on('close', (code) => {
console.log(`Python script exited with code ${code}`);
});
});
}
async function extractHTMLFromYouTubeVideo(videoURL) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(videoURL);
setTimeout(async function () {
// Wait for the video player to load.
await page.waitForSelector('.ytp-heat-map-svg');
await page.waitForSelector('.ytp-progress-bar');
const videoLength = await page.evaluate(() => {
const progressBar = document.querySelector('.ytp-progress-bar');
return progressBar.getAttribute('aria-valuemax');
});
const heatMapContainer = await page.$('.ytp-heat-map-svg');
if (heatMapContainer) {
const heatMapHTML = await page.evaluate(el => el.innerHTML, heatMapContainer);
// console.log(heatMapHTML);
function extractDAttributeValue(data) {
let dAttributeValue;
let dAttributeStartIndex = data.indexOf("class=");
// console.log(dAttributeStartIndex)
if (dAttributeStartIndex !== -1) {
let dAttributeEndIndex = data.indexOf("fill", dAttributeStartIndex);
if (dAttributeEndIndex === -1) {
dAttributeEndIndex = data.length;
}
dAttributeValue = data.substring(dAttributeStartIndex + 41, dAttributeEndIndex - 2);
}
return dAttributeValue;
}
let data = extractDAttributeValue(heatMapHTML)
visualizeData(data, "test.txt", videoLength);
} else {
console.log('No heat map container found on this page.');
}
await browser.close();
}, 3000);
}
(async () => {
const videoURL = 'https://www.youtube.com/watch?v=H_hgw-V-wBI';
await extractHTMLFromYouTubeVideo(videoURL);
})();