45 lines
1.0 KiB
Python
45 lines
1.0 KiB
Python
from fastapi import FastAPI, UploadFile
|
|
from tika import parser
|
|
import uvicorn, re
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
def daj_dane(plik):
|
|
faktura = parser.from_buffer(plik)
|
|
dane = faktura['content']
|
|
dane = dane.split('\n')
|
|
dane = [i for i in dane if i != '']
|
|
razem = list(filter(lambda x: 'Razem' in x, dane))
|
|
razem = float(re.findall(r'\d+[.]\d+', razem[0])[-1])
|
|
|
|
sprzedawca = dane[dane.index('Sprzedawca:') : dane.index('Nabywca:')]
|
|
sprzedawca_nazwa = sprzedawca[1]
|
|
nip = sprzedawca[-1].replace('NIP: ', '')
|
|
|
|
return {
|
|
'vat_id' : nip,
|
|
'salesman' : sprzedawca_nazwa,
|
|
'total' : razem
|
|
}
|
|
|
|
|
|
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 daj_dane(file.file.read())
|
|
|
|
if __name__ == '__main__':
|
|
uvicorn.run(app, host="0.0.0.0", port=8000) |