-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_targ_rep_targeting_vector_rrid.py
130 lines (108 loc) · 5.77 KB
/
insert_targ_rep_targeting_vector_rrid.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import json
import requests
import os
import sys
class Updater:
def __init__(self):
# Set service URL based on environment variable
env = os.getenv('GENTAR_ENV', 'LOCAL')
self.service = self.get_service_url(env)
self.api = "api/"
self.token = None
self.obtain_token()
self.targ_rep_targeting_vector_rr_ids = set()
def get_service_url(self, env):
# Define service URLs based on the environment
if env == 'PRODUCTION':
return "https://www.gentar.org/tracker-api/"
elif env == 'SANDBOX':
return "https://www.gentar.org/production-tracker-sandbox-api/"
else:
return "http://127.0.0.1:8080/"
def read_targ_rep_targeting_vector(self, file):
# Read targ_rep_targeting_vectordata from a text file and store in a set
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), file)
if os.path.isfile(filepath):
with open(filepath, 'rt') as targ_rep_targeting_vector_file:
for line in targ_rep_targeting_vector_file:
# Split the line into words and take the first word as the targ_rep_targeting_vector
self.targ_rep_targeting_vector_rr_ids.add(line)
def process_targ_rep_targeting_vector_rr_ids(self):
# Process targ_rep_targeting_vector_rr_ids and update outcomes
for targ_rep_targeting_vector_rr_id in self.targ_rep_targeting_vector_rr_ids:
try:
# Split the targ_rep_targeting_vector_rr_ids
targ_rep_targeting_vector_request = targ_rep_targeting_vector_rr_id.split("\\t")
print("Processing {}".format(targ_rep_targeting_vector_request))
if len(targ_rep_targeting_vector_request) < 5:
sys.exit(
"column size must be 5 ")
targeting_vector_name = targ_rep_targeting_vector_request[0]
distributionNetworkName = targ_rep_targeting_vector_request[1]
startDate = targ_rep_targeting_vector_request[2]
endDate = targ_rep_targeting_vector_request[3]
distributionIdentifier = targ_rep_targeting_vector_request[4]
if distributionIdentifier.startswith("RRID:"):
# Fetch the outcome data
targeting_vector_update_url = self.service + self.api + "targ_rep/targeting_vectors/distribution_product"
# Update the distributionIdentifier with the provided rr_id
targeting_vector_dto = {
"targetingVectorName": targeting_vector_name,
"distributionNetworkName": distributionNetworkName,
"startDate": startDate,
"endDate": endDate,
"distributionIdentifier": distributionIdentifier
}
# Revise the outcome data on the GenTaR service
self.revise_service(targeting_vector_update_url, targeting_vector_dto)
print("Successfully Updated: {}".format(targ_rep_targeting_vector_request))
else:
print("RRID is not in the correct format: {}".format(targ_rep_targeting_vector_request[4]))
except ValueError:
print("Format your file please. Broken line is {}".format(targ_rep_targeting_vector_request))
except Exception as error:
if hasattr(error, 'response') and error.response.text:
try:
response_data = error.response.text.encode('utf-8')
response_json = json.loads(response_data)
message = response_json['apierror']['message']
print("Message:", message)
except (json.JSONDecodeError, KeyError):
print("Unable to extract message from response.")
else:
print("No response received or unable to extract message from response.")
def obtain_token(self):
# Obtain authentication token
user = os.getenv('GENTAR_USER')
password = os.getenv('GENTAR_PASSWORD')
if user is None or password is None:
sys.exit("Please export your GENTAR_USER and GENTAR_PASSWORD to the environment before running the script.")
url = self.service + "auth/signin"
headers = {'Content-Type': 'application/json', 'cache-control': 'no-cache'}
credentials = {'userName': user, 'password': password}
# Send a POST request to obtain the token
r = requests.post(url, headers=headers, json=credentials)
if r.status_code == 200:
self.token = r.json()['accessToken']
else:
r.raise_for_status()
def fetch_one_entry(self, url):
# Fetch data from a given URL using the authentication token
headers = {'Content-Type': 'application/json', 'cache-control': 'no-cache',
'Authorization': 'Bearer ' + self.token}
r = requests.get(url, headers=headers)
r.raise_for_status()
return r.json()
def revise_service(self, url, data):
headers = {'Content-Type': 'application/json', 'cache-control': 'no-cache',
'Authorization': 'Bearer ' + self.token}
r = requests.put(url, headers=headers, json=data)
r.raise_for_status()
return r.json(), r.status_code
if __name__ == '__main__':
# Instantiate the Updater class
updater = Updater()
# Read targ_rep_targeting_vector data from the specified file
updater.read_targ_rep_targeting_vector("targ_rep_targeting_vector_rr_ids.txt")
# Process targ_rep_targeting_vector and update outcomes
updater.process_targ_rep_targeting_vector_rr_ids()