2023-01-30 10:46:34 +01:00
|
|
|
from fastapi import FastAPI, UploadFile
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2023-01-30 15:49:19 +01:00
|
|
|
from tika import parser
|
|
|
|
import uvicorn
|
|
|
|
import re
|
2023-01-30 10:46:34 +01:00
|
|
|
|
2023-01-30 15:49:19 +01:00
|
|
|
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
|
|
|
|
}
|
2023-01-30 10:46:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2023-01-30 15:49:19 +01:00
|
|
|
return parse(file.file.read())
|
|
|
|
|
2023-01-30 10:46:34 +01:00
|
|
|
|
2023-01-30 15:49:19 +01:00
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|