2023-01-28 20:18:57 +01:00
|
|
|
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: ', '')
|
2023-01-29 21:03:14 +01:00
|
|
|
|
2023-01-28 20:18:57 +01:00
|
|
|
return {
|
|
|
|
'vat_id' : nip,
|
2023-01-29 00:17:41 +01:00
|
|
|
'salesman' : sprzedawca_nazwa,
|
2023-01-28 20:18:57 +01:00
|
|
|
'total' : razem
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
2023-01-28 21:04:23 +01:00
|
|
|
|
2023-01-28 20:18:57 +01:00
|
|
|
origins = ["*"]
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=origins,
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
|
|
|
|
2023-01-28 21:04:23 +01:00
|
|
|
|
2023-01-28 20:18:57 +01:00
|
|
|
@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)
|