DPZC-5/api.py

39 lines
946 B
Python

from fastapi import FastAPI, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from tika import parser
import uvicorn
import re
def parse(pdf):
content = parser.from_buffer(pdf)
content = content['content'].split('\n')
content = [c for c in content if c != '']
total = float(re.findall(r'\d+[.]\d+', list(filter(lambda x: 'Razem' in x, content))[0])[-1])
content = content[content.index('Sprzedawca:') : content.index('Nabywca:')]
seller = content[1]
nip = content[-1].replace('NIP: ', '')
return {
'vat_id' : nip,
'address' : seller,
'total' : total
}
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post('/invoice')
async def root(file: UploadFile):
return parse(file.file.read())
uvicorn.run(app, host="0.0.0.0", port=8000)