-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate_xml.py
109 lines (98 loc) · 3.15 KB
/
validate_xml.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from acdh_tei_pyutils.tei import TeiReader
from datetime import datetime
import os
import requests
import glob
BASEROW_TOKEN = os.environ.get('BASEROW_TOKEN')
XML_TABLE_ID = "1477"
ROW_API = "https://baserow.acdh-dev.oeaw.ac.at/api/database/rows/table/"
DIR = "werke"
def verify_xml(dir):
works = glob.glob('./werke/*/*.xml')
print(f"{len(works)} Werk(e) gefunden.")
array = []
date = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
for x in works:
filename = x
try:
doc = TeiReader(x)
data = "no errors found"
array.append({
"verification_errors": data,
"filename": filename,
"verified": True,
"date": date
})
print(data)
print("updating baserow table xml")
except Exception as e:
data = e
array.append({
"verification_errors": data,
"filename": filename,
"verified": False,
"date": date
})
print(data)
print("updating baserow table xml")
# print(array)
return array
# with open(f"logs/log__{datetime.today().strftime('%Y-%m-%d_%H:%M:%S')}.txt", "a") as f:
# data = f"no errors found in werke/{work}/{w} \n"
# f.write(data)
def get_base_row():
url = f"{ROW_API}{XML_TABLE_ID}/?user_field_names=true"
next_page = True
while next_page:
print(url)
response = None
result = None
x = None
response = requests.get(
url,
headers={
"Authorization": f"Token {BASEROW_TOKEN}"
}
)
result = response.json()
next_page = result['next']
url = result['next']
for x in result['results']:
yield(x)
def update_base_row():
rows = verify_xml(DIR)
for r in rows:
if r["filename"] in [x["filename"] for x in get_base_row()]:
# print(r["filename"])
# print([x["filename"] for x in getBaseRow()])
id = [x["id"] for x in get_base_row()]
for i in id:
url = f"{ROW_API}{XML_TABLE_ID}/{i}/?user_field_names=true"
print(url)
try:
requests.patch(
url,
headers={
"Authorization": f"Token {BASEROW_TOKEN}",
"Content-Type": "application/json"
},
json=r
)
# print(p.json())
# print(p)
except Exception as err:
print(err)
else:
try:
requests.post(
f"{ROW_API}{XML_TABLE_ID}/?user_field_names=true",
headers={
"Authorization": f"Token {BASEROW_TOKEN}",
"Content-Type": "application/json"
},
json=r
)
# print(p.json())
except Exception as err:
print(err)
update_base_row()