dpzc5.2-5.3/api.py

40 lines
947 B
Python
Raw Normal View History

2023-02-15 22:30:37 +01:00
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)