Omkar008 commited on
Commit
04546c6
1 Parent(s): 8009462

Update controllers/fetch_microsoft_mails.py

Browse files
Files changed (1) hide show
  1. controllers/fetch_microsoft_mails.py +21 -21
controllers/fetch_microsoft_mails.py CHANGED
@@ -239,27 +239,27 @@ def extract_attachments_from_mail(access_token: str, message_data: dict) -> List
239
  if attachments_response.status_code != 200 :
240
  return "No Attachments","Empty Strcutured Data"
241
  attachments = attachments_response.json().get('value', [])
242
-
243
- for attachment in attachments:
244
- attachment_id = attachment['id']
245
- filename = attachment['name']
246
- if filename.endswith(".zip") or filename.endswith(".txt") or filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".gif"):
247
- continue
248
- attachment_response = requests.get(f'https://graph.microsoft.com/v1.0/me/messages/{message_id}/attachments/{attachment_id}', headers=headers)
249
- print("Attachment Response")
250
- print()
251
- # rex = attachment_response.json()
252
- # print(rex)
253
- data = attachment_response.json()['contentBytes']
254
- print(data)
255
- raw_text = ut.extract_text_from_attachment(filename , data)
256
- print(raw_text)
257
- struct_data = ut.strcuture_document_data(raw_text)
258
- if struct_data:
259
- structured_data.append(struct_data)
260
- attachments.append(Attachment(attachment_len = len(data),filename=filename, data=data))
261
- return attachments,structured_data
262
- #Gmail data fetching below
263
  # if "payload" in message_data and "parts" in message_data["payload"]:
264
  # for part in message_data["payload"]["parts"]:
265
  # if "body" in part and "attachmentId" in part["body"]:
 
239
  if attachments_response.status_code != 200 :
240
  return "No Attachments","Empty Strcutured Data"
241
  attachments = attachments_response.json().get('value', [])
242
+ if attachments:
243
+ for attachment in attachments:
244
+ attachment_id = attachment['id']
245
+ filename = attachment['name']
246
+ if filename.endswith(".zip") or filename.endswith(".txt") or filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".gif"):
247
+ continue
248
+ attachment_response = requests.get(f'https://graph.microsoft.com/v1.0/me/messages/{message_id}/attachments/{attachment_id}', headers=headers)
249
+ print("Attachment Response")
250
+ print()
251
+ # rex = attachment_response.json()
252
+ # print(rex)
253
+ data = attachment_response.json()['contentBytes']
254
+ print(data)
255
+ raw_text = ut.extract_text_from_attachment(filename , data)
256
+ print(raw_text)
257
+ struct_data = ut.strcuture_document_data(raw_text)
258
+ if struct_data:
259
+ structured_data.append(struct_data)
260
+ attachments.append(Attachment(attachment_len = len(data),filename=filename, data=data))
261
+ return attachments,structured_data
262
+ #Gmail data fetching below
263
  # if "payload" in message_data and "parts" in message_data["payload"]:
264
  # for part in message_data["payload"]["parts"]:
265
  # if "body" in part and "attachmentId" in part["body"]: