pdf parsing and selection from project file system

This commit is contained in:
2022-04-26 14:04:56 -05:00
parent e7058e2d2d
commit b7b52901e6
2 changed files with 29 additions and 31 deletions

58
app.js
View File

@@ -3,51 +3,49 @@ const http = require('http');
const express = require('express'); const express = require('express');
const spawn = require('child_process').spawn; const spawn = require('child_process').spawn;
const secondSpawn = require('child_process').spawn;
const app = express(); const app = express();
const PORT = 3000; const PORT = 3000;
const pyPromise = async (data) => {
const python = spawn('python', ['test.py']);
python.stdout.on("data", (data) => {
resolve(data.toString());
});
python.stderr.on("data", (data) => {
reject(data.toString());
});
}
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.send("Python stuff?") res.send("Python stuff?")
}) })
app.get('/:name', (req, res) => { app.get('/pdfs/:name', (req, res) => {
const { name } = req.params; const { name } = req.params;
const python = spawn('python3', ['test.py', name]); const PATH = `recipe_text/${name}`
python.stdout.on('data', (data) => { try {
res.send(data.toString()); fs.access(PATH, fs.constants.F_OK, (err) => {
if (err) {
throw err;
} else {
fs.readFile(PATH, 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
const dataSlice = data.slice(0, 150);
res.send("Data found. Preview: " + dataSlice);
});
}
});
} catch(e) {
console.log(e);
}
})
app.post('/pdf-gen', (req, res) => {
const href = req.query.href;
const pdfRead = secondSpawn('python3', ['read_pdf.py', href]);
pdfRead.stdout.on('data', (data) => {
res.status(200).send("PDF created!");
}) })
python.stderr.on("data", (data) => { pdfRead.stderr.on("data", (data) => {
res.send(data.toString()); res.send(data.toString());
}) })
}) })
// app.get('/', (req, res) => {
// let dataToSend;
// const python = spawn('python', ['test.py']);
// python.stdout.on('data', (data) => {
// dataToSend = data.toString();
// console.log('caught');
// });
// python.on('close', (code) => {
// console.log(`All close with code ${code}`);
// res.send(dataToSend);
// })
// })
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`); console.log(`Listening on port ${PORT}`);
}); });

View File

@@ -9,6 +9,6 @@ def read_PDF(path):
with open(f'recipe_text/{text[:20]}.txt', 'w') as f: with open(f'recipe_text/{text[:20]}.txt', 'w') as f:
f.write(text) f.write(text)
read_PDF('recipes/TheBestOfBaseballAwards.pdf')
read_PDF(sys.argv[1])
sys.stdout.flush() sys.stdout.flush()