-
Notifications
You must be signed in to change notification settings - Fork 0
/
installKaggle.mjs
105 lines (91 loc) · 2.92 KB
/
installKaggle.mjs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* This will use Official Kaggle CLI to retrieve data,
* Python must be installed to interact with Kaggle.
*/
import { exec } from 'child_process';
import { promisify } from 'util';
import fs from "fs";
import path from "path";
import unzipper from "unzipper";
import 'dotenv/config';
const DATASET_OWNER = process.env.DATASET_OWNER;
const DATASET_NAME = process.env.DATASET_NAME;
const DATASET_PATH = `${DATASET_OWNER}/${DATASET_NAME}`
const args = process.argv;
const dir = path.join(args[1], process.platform === "win32" ? "..\\" : "../");
const execPromise = promisify(exec);
async function executeCommand(command) {
try {
const { stdout, stderr } = await execPromise(command);
return stdout || stderr;
} catch (error) {
throw error;
}
}
async function checkPython() {
try {
await executeCommand('python --version');
console.log('Python is installed.');
return true;
} catch (error) {
console.error('Python is not installed.');
return false;
}
}
async function checkKagglePackage() {
try {
await executeCommand('pip show kaggle');
console.log('Kaggle package is already installed.');
return true;
} catch (error) {
console.log('Kaggle package is not installed.');
return false;
}
}
async function installKagglePackage() {
try {
const result = await executeCommand('pip install kaggle');
console.log('Kaggle package installed successfully:', result);
} catch (error) {
console.error('Failed to install Kaggle package:', error);
}
}
async function downloadDatasetFiles(dataset) {
try {
const result = await executeCommand(`kaggle datasets download -d ${dataset}`);
console.log(`Files for dataset "${dataset}" downloaded successfully:`, result);
} catch (error) {
console.error(`Failed to download files for dataset "${dataset}":`, error);
}
}
async function unzipDatasetFiles() {
console.log(`Unzipping files for dataset "${DATASET_PATH}...`);
const zipDest = path.resolve(dir, `${DATASET_NAME}.zip`);
return new Promise((resolve, reject) => {
fs.createReadStream(zipDest)
.pipe(unzipper.Extract({ path: path.resolve(dir, "artifacts") }))
.on("finish", function (err) {
if (err) {
reject(err);
return;
}
fs.unlinkSync(zipDest);
console.log("Unzip completed.");
resolve();
});
})
}
async function main(dataset) {
const isPythonInstalled = await checkPython();
if (!isPythonInstalled) {
console.error('Please install Python before running this script.');
return;
}
const isKaggleInstalled = await checkKagglePackage();
if (!isKaggleInstalled) {
await installKagglePackage();
}
await downloadDatasetFiles(dataset);
await unzipDatasetFiles();
}
main(DATASET_PATH);