-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFailedBatch_tranTotalNull_path1.py
More file actions
76 lines (62 loc) · 3.46 KB
/
FailedBatch_tranTotalNull_path1.py
File metadata and controls
76 lines (62 loc) · 3.46 KB
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
import http.client
import csv
import os
import re
conn = http.client.HTTPSConnection("platform.adobe.io")
payload = ""
headers = {
"Accept": "application/json",
"Authorization": "Bearer XXXXXXXXXXXXXXXX",
"x-api-key": "XXXXXXXXXXXXXXXX",
"x-gw-ims-org-id": "XXXXXXXXXXXXXXXX@AdobeOrg",
"x-sandbox-name": "prod",
}
current_directory = os.getcwd()
# open the csv file with batch and path information
inp_csv_file_path = os.path.join(current_directory, "transactionTotalNull_batch_path1.csv")
# Create the full path for the CSV file
#out_csv_file_path = os.path.join(current_directory, "failedBatchesData_tranTotalNull_path1.csv")
out_csv_file_path = os.path.join(current_directory, "failedBatchesData_tranTotalNull_path1_onlydata.json")
# Open the inp csv file for reading
with open(inp_csv_file_path,mode ='r') as file:
# creating dictreader object
inpcsvfile = csv.DictReader(file)
#inpcsvfile = csv.reader(file)
# Open a CSV file for writing
with open(out_csv_file_path, "w", newline="") as csvfile:
#outcsvfile = csv.writer(csvfile)
# Write header to the CSV file
# outcsvfile.writerow(["FAILED_BATCH_ID", "PATH", "ECID", "xdmEntity"])
for lines in inpcsvfile:
url = (
f"/data/foundation/export/batches/{lines['FAILED_BATCH_ID']}/failed?path={lines['PATH']}"
)
conn.request("GET", url, payload, headers)
res = conn.getresponse()
data = res.read().decode("utf-8")
start_index = 0
while start_index != -1:
# Find the start and end indices of "xdmEntity" in the response
start_index = data.find('"xdmEntity":', start_index)
if start_index != -1:
xdmstart_index = data.find('"xdmEntity":', start_index)+12
end_index = data.find('},"_errors":', start_index)
# Extract the "xdmEntity" part
xdm_entity_part = data[xdmstart_index:end_index]
# Extract the "ECID" part
ecidstart_index = 0
ecidstart_index = xdm_entity_part.find('"ECID":[{"id":"', ecidstart_index)+15
ecidend_index = xdm_entity_part.find('","authenticatedState"', ecidstart_index)
ecid_part = xdm_entity_part[ecidstart_index:ecidend_index]
# Check for ECID null part
xdm_entity_part_withecid = xdm_entity_part.replace('"ECID":null', '"ECID":"'+ecid_part+'"')
# Check for transactionTotal null part
xdm_entity_part_withecid_withTT = xdm_entity_part_withecid.replace('"transactionTotal":null', '"transactionTotal":0')
# Check for transactionTotal not-null part
xdm_entity_part_withecid_withTT0 = re.sub('"transactionTotal":"([0-9.]+)"', '"transactionTotal":0', xdm_entity_part_withecid_withTT)
# Write batch_id, path, and xdmEntity to the CSV file
#outcsvfile.writerow([lines['FAILED_BATCH_ID'], lines['PATH'], ecid_part, xdm_entity_part_withecid_withTT0])
#outcsvfile.writerow(xdm_entity_part_withecid_withTT0)
csvfile.write(xdm_entity_part_withecid_withTT0+','+'\n')
start_index = end_index
print(f"CSV file '{out_csv_file_path}' created successfully.")