-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeepnote_check_notebooks.js
92 lines (75 loc) · 2.79 KB
/
deepnote_check_notebooks.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
87
88
89
90
91
92
/* Requires Node.js and Puppeteer
Linux installation:
cd /tmp
curl -sL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install -y nodejs
npm init -y # Initialize project
npm install puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
macOS installation:
brew install node@20 # Specific version
npm init -y # Initialize project
npm install puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
Then run with:
node deepnote_check_notebooks.js "https://deepnote.com/workspace/{workspace}/project/{project_id}/"
*/
'use strict';
const { executablePath } = require('puppeteer');
const puppeteer = require('puppeteer-extra');
const fs = require('fs');
(async function main() {
try {
const url = process.argv[2];
if (!url) {
throw new Error('Please provide a Deepnote workspace URL as a command line argument.\nUsage: node deepnote_check_notebooks.js "https://deepnote.com/workspace/{workspace}/project/{project_id}/"');
}
const browser = await puppeteer.launch({executablePath: executablePath()});
// or, for debugging:
//const browser = await puppeteer.launch({headless:false, devtools:true});
// Close the default page that the browser always opens:
const pages = await browser.pages();
await pages[0].close();
const page = await browser.newPage();
try {
await page.goto(url);
} catch (error) {
throw new Error(`Failed to load URL: ${url}. Error: ${error.message}`);
}
try {
await page.waitForSelector('[data-cy^=notebooks_upload-droparea] [data-cy^=file-sidebar-item]');
} catch (error) {
throw new Error('Could not find notebook elements on the page. Please check if the URL is correct and you have access to the workspace.');
}
const notebooks = await page.evaluate(() => {
const books = document.querySelectorAll('[data-cy^=notebooks_upload-droparea] [data-cy^=file-sidebar-item]');
const notebooks = [];
for (let i = 0; i < books.length; i++) {
notebooks.push(books[i].innerText);
}
return notebooks;
});
notebooks.forEach(notebook => {
if (notebook) {
console.log(notebook);
}
});
console.log("---"); // delimiter
const filenames = await page.evaluate(() => {
const files = document.querySelectorAll('[data-cy^=file-explorer_upload-droparea] [data-cy^=file-sidebar-item]');
const filenames = [];
for (let i = 0; i < files.length; i++) {
filenames.push(files[i].getAttribute('title'));
}
return filenames;
});
filenames.forEach(filename => {
if (filename) {
console.log(filename);
}
});
await browser.close();
} catch (err) {
console.error(err.message);
process.exit(1); // Exit with error code
}
})();