-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_learning.py
44 lines (38 loc) · 1.41 KB
/
pre_learning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import json
import os
import sqlite3
import requests
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# open connection
conn = sqlite3.connect(os.path.join(BASE_DIR, 'examples', 'examples.db'))
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute("SELECT * FROM received_invoices")
# get all received invoices
rows = cur.fetchall()
for row in rows:
with open(os.path.join(BASE_DIR, 'examples', 'files', row['file_path']), 'rb') as invoice_file:
files = {
"file": (row['file_path'].split('/')[-1], invoice_file.read(),),
}
request_data = {
"document_type_name": 'pre-learning-example',
"customer": 'me',
"learning_fields": json.dumps(
[
{'name': 'supplier', 'value': row['supplier']},
{'name': 'invoice_number', 'value': row['invoice_number']},
{'name': 'issue_date', 'value': row['issue_date']}, # convert to YYYY-MM-DD string if your database has datetime type
{'name': 'total_amount', 'value': '%.2f' % row['total_amount']},
]
)
}
if os.getenv('API_KEY') is None:
raise Exception('YOU MUST SET API KEY!')
response = requests.post(
f'https://developers.typless.com/api/document-types/learn/',
files=files,
data=request_data,
headers={'Authorization': f'Token {os.getenv("API_KEY")}'}
)
print(response.text)