saas-chmurki/api.py

39 lines
976 B
Python
Raw Normal View History

2023-01-27 21:36:03 +01:00
from fastapi import FastAPI, UploadFile
from tika import parser
import uvicorn, re
from fastapi.middleware.cors import CORSMiddleware
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]
vat_id = content[-1].replace('NIP: ', '')
return {
'vat_id' : vat_id,
'seller' : 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())
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)