From b7b52901e69f46b6a7e62d8867b94facec55ffdb Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Tue, 26 Apr 2022 14:04:56 -0500 Subject: [PATCH] pdf parsing and selection from project file system --- app.js | 58 ++++++++++++++++++++++++++--------------------------- read_pdf.py | 2 +- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/app.js b/app.js index 3ab35b3..4470ca8 100644 --- a/app.js +++ b/app.js @@ -3,51 +3,49 @@ const http = require('http'); const express = require('express'); const spawn = require('child_process').spawn; +const secondSpawn = require('child_process').spawn; const app = express(); 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) => { res.send("Python stuff?") }) -app.get('/:name', (req, res) => { +app.get('/pdfs/:name', (req, res) => { const { name } = req.params; - const python = spawn('python3', ['test.py', name]); + const PATH = `recipe_text/${name}` - python.stdout.on('data', (data) => { - res.send(data.toString()); + try { + 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()); }) }) -// 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, () => { console.log(`Listening on port ${PORT}`); }); \ No newline at end of file diff --git a/read_pdf.py b/read_pdf.py index 5ce5611..1b1e3c9 100644 --- a/read_pdf.py +++ b/read_pdf.py @@ -9,6 +9,6 @@ def read_PDF(path): with open(f'recipe_text/{text[:20]}.txt', 'w') as f: f.write(text) -read_PDF('recipes/TheBestOfBaseballAwards.pdf') +read_PDF(sys.argv[1]) sys.stdout.flush() \ No newline at end of file