-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
306 lines (269 loc) · 11.4 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import os
import json
import io
import csv
import base64
import tempfile
import requests
import functions_framework
from google.cloud import firestore, storage
from cloudevents.http import CloudEvent
# Maximum payload size for the API call (1MB).
MAX_PAYLOAD_SIZE = 1048576 # bytes
@functions_framework.cloud_event
def process_file_v2(cloudevent: CloudEvent):
"""
Cloud Function (Gen2) entry point to process a CSV file.
Steps:
1. Download CSV from Cloud Storage.
2. Get config from Firestore to identify phone columns.
3. Extract phone numbers and batch them so that each API call payload is below 1MB.
4. Call the blacklist API in batches and aggregate the "supression" results.
5. If no suppressed numbers are returned, simply store the original file as cleanFilePath.
6. Otherwise, process the CSV:
- Create a clean file (emptying any suppressed phone numbers).
- Create a blacklisted file (only retaining suppressed numbers per lead, merging duplicates).
7. Upload generated files to Cloud Storage and update Firestore.
"""
# --- Step 1. Decode the Pub/Sub message ---
try:
data = base64.b64decode(cloudevent.data['message']['data']).decode('utf-8')
message = json.loads(data)
except Exception as e:
print(f"Error decoding message: {e}")
return
file_id = message.get("fileId")
file_name = message.get("fileName")
bucket_name = message.get("bucket")
config_doc_path = message.get("configDocumentPath")
if not all([file_id, file_name, bucket_name, config_doc_path]):
print("Missing one or more required fields in the message.")
return
print(f"Processing fileId: {file_id}, fileName: {file_name}, bucket: {bucket_name}, config path: {config_doc_path}")
# --- Step 2. Initialize Firestore and Storage clients ---
fs_client = firestore.Client()
storage_client = storage.Client()
# Fetch configuration document from Firestore.
config_ref = fs_client.document(config_doc_path)
config_snapshot = config_ref.get()
if not config_snapshot.exists:
print(f"Configuration document not found: {config_doc_path}")
return
config = config_snapshot.to_dict()
# Retrieve dynamic configuration details.
phone_indexes = config.get("phoneColumnIndexes", [])
has_header = config.get("hasHeaderRow", True)
# --- Step 3. Download and parse the CSV file ---
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(os.path.join('uploads', file_name))
try:
file_content = blob.download_as_bytes()
except Exception as e:
print(f"Error downloading file: {e}")
return
decoded_file = file_content.decode('utf-8')
csv_lines = decoded_file.splitlines()
reader = csv.reader(csv_lines)
rows = list(reader)
if not rows:
print("Empty CSV file.")
return
# Separate header from data rows if needed.
if has_header:
header = rows[0]
data_rows = rows[1:]
else:
header = None
data_rows = rows
# --- Step 4. Extract unique phone numbers ---
phone_set = set()
for row in data_rows:
for idx in phone_indexes:
if idx < len(row):
phone = row[idx].strip()
if phone:
phone_set.add(phone)
phone_list = list(phone_set)
print(f"Extracted {len(phone_list)} unique phone numbers for API lookup.")
# --- Step 5. Call the blacklist API in batches ---
try:
suppressed_list = call_blacklist_lookup_batched(phone_list)
except Exception as e:
print(f"Error calling blacklist API: {e}")
return
suppressed_set = set(suppressed_list)
print(f"Aggregated suppressed numbers: {suppressed_set}")
# Update Firestore stage.
config_ref.update({
"status": {
"stage": "API_CALLED",
"lastUpdated": firestore.SERVER_TIMESTAMP
}
})
# --- Special Case: No suppressed numbers ---
if not suppressed_set:
print("No suppressed numbers returned. Uploading original file as cleanFilePath.")
output_bucket_name = os.getenv("OUTPUT_BUCKET")
if not output_bucket_name:
print("OUTPUT_BUCKET environment variable is not set.")
return
output_bucket = storage_client.bucket(output_bucket_name)
# Upload the original file content as the clean file.
base_name, ext = os.path.splitext(file_name)
clean_file_name = f"{base_name}_clean{ext}"
clean_blob_name = f"{file_id}/{clean_file_name}"
clean_blob = output_bucket.blob(clean_blob_name)
clean_blob.upload_from_string(decoded_file, content_type="text/csv")
# Update Firestore with output path.
config_ref.update({
"results" : {
"total": len(phone_list),
"clean": len(phone_list),
"dnc": 0
},
"outputFiles": {
"cleanFilePath": clean_blob_name,
"blacklistedFilePath": "" # No blacklisted file produced.
},
"status": {
"stage": "DONE",
"lastUpdated": firestore.SERVER_TIMESTAMP
}
})
print(f"Processed file_id={file_id}. Clean file uploaded to: {clean_blob_name}")
return
# --- Step 6. Process CSV rows for clean and blacklisted outputs ---
clean_rows = []
suppressed_dict = {} # Group suppressed rows by lead_id (assumed to be in column 0)
# Add header if present.
if header:
clean_rows.append(header)
suppressed_header = header[:] # Copy for suppressed file
else:
suppressed_header = None
for row in data_rows:
clean_row = list(row) # For clean file: clear suppressed phones.
suppressed_row = list(row) # For blacklisted file: retain only suppressed phones.
row_has_suppressed = False
for idx in phone_indexes:
if idx < len(row):
phone_val = row[idx].strip()
if phone_val in suppressed_set:
# For clean file, empty the cell.
clean_row[idx] = ""
# For suppressed file, keep the suppressed number.
suppressed_row[idx] = phone_val
row_has_suppressed = True
else:
# Clear non-suppressed phone cells in the suppressed file.
suppressed_row[idx] = ""
clean_rows.append(clean_row)
# Group rows with suppressed numbers by lead_id.
if row_has_suppressed:
lead_id = row[0] if len(row) > 0 else None
if lead_id in suppressed_dict:
existing_row = suppressed_dict[lead_id]
for idx in phone_indexes:
# Merge suppressed phone numbers for the same lead.
if idx < len(row) and not existing_row[idx] and suppressed_row[idx]:
existing_row[idx] = suppressed_row[idx]
suppressed_dict[lead_id] = existing_row
else:
suppressed_dict[lead_id] = suppressed_row
# Build suppressed file rows.
suppressed_rows = []
if suppressed_header:
suppressed_rows.append(suppressed_header)
suppressed_rows.extend(suppressed_dict.values())
# --- Step 7. Write the output CSV files in memory ---
clean_csv_output = io.StringIO()
writer_clean = csv.writer(clean_csv_output)
for row in clean_rows:
writer_clean.writerow(row)
clean_csv_content = clean_csv_output.getvalue()
suppressed_csv_output = io.StringIO()
writer_suppressed = csv.writer(suppressed_csv_output)
if suppressed_header:
writer_suppressed.writerow(suppressed_header)
for row in (list(suppressed_dict.values()) if not suppressed_header else suppressed_rows[1:]):
writer_suppressed.writerow(row)
suppressed_csv_content = suppressed_csv_output.getvalue()
# --- Step 8. Upload generated files to Cloud Storage ---
output_bucket_name = os.getenv("OUTPUT_BUCKET")
if not output_bucket_name:
print("OUTPUT_BUCKET environment variable is not set.")
return
output_bucket = storage_client.bucket(output_bucket_name)
base_name, ext = os.path.splitext(file_name)
clean_file_name = f"{base_name}_clean{ext}"
suppressed_file_name = f"{base_name}_blacklisted{ext}"
clean_blob_name = f"{file_id}/{clean_file_name}"
suppressed_blob_name = f"{file_id}/{suppressed_file_name}"
clean_blob = output_bucket.blob(clean_blob_name)
suppressed_blob = output_bucket.blob(suppressed_blob_name)
clean_blob.upload_from_string(clean_csv_content, content_type="text/csv")
suppressed_blob.upload_from_string(suppressed_csv_content, content_type="text/csv")
output_paths = {
"cleanFilePath": clean_blob_name,
"blacklistedFilePath": suppressed_blob_name
}
# --- Step 9. Update Firestore with output file paths and final status ---
config_ref.update({
"results": {
"total": len(phone_list),
"clean": len(phone_list) - len(suppressed_set),
"dnc": len(suppressed_set)
},
"outputFiles": output_paths,
"status": {
"stage": "DONE",
"lastUpdated": firestore.SERVER_TIMESTAMP
}
})
print(f"Processed file_id={file_id}. Clean file uploaded to: {clean_blob_name}")
print(f"Processed file_id={file_id}. Blacklisted file uploaded to: {suppressed_blob_name}")
def call_blacklist_lookup_batched(phone_list):
"""
Breaks the phone_list into batches such that each JSON payload ({"phones": batch})
is less than MAX_PAYLOAD_SIZE (1MB). It then calls the API for each batch and
aggregates the suppressed numbers.
"""
batches = []
current_batch = []
for phone in phone_list:
current_batch.append(phone)
payload = {"phones": current_batch}
payload_str = json.dumps(payload)
# Check size in bytes; if over the limit, remove the last phone and finalize the batch.
if len(payload_str.encode('utf-8')) > MAX_PAYLOAD_SIZE:
current_batch.pop()
if current_batch:
batches.append(current_batch)
current_batch = [phone]
if current_batch:
batches.append(current_batch)
suppressed = set()
for batch in batches:
suppressed_batch = call_blacklist_lookup(batch)
suppressed.update(suppressed_batch)
return list(suppressed)
def call_blacklist_lookup(phone_batch):
"""
Calls the external blacklist lookup API with a JSON payload containing the given phone_batch.
API URL example:
https://api.blacklistalliance.net/bulklookup?key=HxWQvKK2g8MyDz7XFGZN&ver=v1&resp=json
Returns the list of suppressed phone numbers from the "supression" key.
"""
api_key = os.getenv("BLACKLIST_API_KEY")
api_url = f"https://api.blacklistalliance.net/bulklookup?key={api_key}&ver=v1&resp=json"
payload = {"phones": phone_batch}
headers = {
"accept": "application/json",
"content-type": "application/json"
}
response = requests.post(api_url, json=payload, headers=headers)
if response.status_code != 200:
raise Exception(f"Blacklist API call failed: {response.status_code} - {response.text}")
response_json = response.json()
# Retrieve suppressed numbers from the API response.
return response_json.get("supression", [])