DPZC_Ola/Zadanie_5.2_5.3/api.py
2023-01-28 22:13:05 +01:00

40 lines
1.0 KiB
Python

from fastapi import FastAPI, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from tika import parser
import uvicorn
import re
#Celem jest wyekstrahowanie nazwy sprzedawcy, jego numeru NIP oraz kwoty łącznej brutto faktury
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)