-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
101 lines (71 loc) · 2.35 KB
/
main.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
93
94
95
96
97
98
99
100
// Modules to control application life and create native browser window
const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const path = require('path')
// To Run Python Script
let spawn = require('child_process').spawn
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
title: 'Scraper',
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
mainWindow.webContents.openDevTools()
}
ipcMain.on('url', (event, url) => {
getDirectory(url, event);
})
const getDirectory = (url, event) => {
const mainWindow = BrowserWindow.getAllWindows()[0];
dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory']
}).then(result => {
if (result.filePaths.length > 0) {
let dir = result.filePaths[0];
letScrap(url, dir, (msg) => {
console.log(msg)
dialog.showMessageBoxSync(mainWindow, {
type: 'info',
buttons: ['Cancel'],
title: "File Scraped Successfully",
message: 'Scraped file location:' + msg
})
event.reply('message', msg)
})
}
}).catch(err => {
console.log(err)
})
}
app.whenReady().then(createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
// Python Communication
const letScrap = (url, dir, cb) => {
var py = spawn('python', ["scrap.py", url, dir]);
let dataString = '';
/*We have to stringify the data first otherwise our python process wont recognize it*/
// py.stdin.write(url);
/*Here we are saying that every time our node application receives data from the python process output stream(on 'data'), we want to convert that received data into a string and append it to the overall dataString.*/
py.stdout.on('data', function (data) {
dataString += data.toString();
});
/*Once the stream is done (on 'end') we want to simply log the received data to the console.*/
py.stdout.on('end', function () {
dataString = dataString.trim();
cb(dataString);
});
py.stdin.end();
}