Omkar008 commited on
Commit
2e66c76
1 Parent(s): 985f920

Update test.py

Browse files
Files changed (1) hide show
  1. test.py +15 -5
test.py CHANGED
@@ -59,22 +59,22 @@ async def auth_google(code:str):
59
  access_token = response.json().get("access_token")
60
  # if not access_token:
61
  # raise HTTPException(status_code=400, detail="Authorization code not provided")
62
- access_token_new = access_token
63
  print("Entered this function, for testing purposes")
64
  brand_name = "louis vuitton"
65
 
66
- user_info = requests.get("https://www.googleapis.com/oauth2/v1/userinfo", headers={"Authorization": f"Bearer {access_token_new}"})
67
  page_token = None
68
  messages = []
69
  user_query = f"subject:((receipt {brand_name}) OR (receipts {brand_name}) OR (reçu {brand_name}) OR (reçus {brand_name}) OR (Quittung {brand_name}) OR (Quittungen {brand_name}) OR (aankoopbon {brand_name}) OR (aankoopbonnen {brand_name}) OR (recibo {brand_name}) OR (recibos {brand_name}) OR (ricevuta {brand_name}) OR (ricevute {brand_name}) OR (ontvangstbewijs {brand_name}) OR (ontvangstbewijzen {brand_name})) has:attachment"
70
 
71
  while True:
72
  # Construct Gmail API request with pageToken
 
73
  gmail_url = f"https://www.googleapis.com/gmail/v1/users/me/messages?q={user_query}"
74
  if page_token:
75
  gmail_url += f"&pageToken={page_token}"
76
 
77
- gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token_new}"})
78
  gmail_data = gmail_response.json()
79
 
80
  # Check if there are messages in the response
@@ -87,6 +87,16 @@ async def auth_google(code:str):
87
  else:
88
  break # No more pages, exit the loop
89
 
 
 
 
 
 
 
 
 
 
 
90
  attachments = []
91
  attachment_no = 0
92
  data_new = {}
@@ -99,7 +109,7 @@ async def auth_google(code:str):
99
  print(message_id)
100
  if message_id:
101
  message_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}"
102
- message_response = requests.get(message_url, headers={"Authorization": f"Bearer {access_token_new}"})
103
  message_data = message_response.json()
104
 
105
 
@@ -117,7 +127,7 @@ async def auth_google(code:str):
117
  if "body" in part and "attachmentId" in part["body"]:
118
  attachment_id = part["body"]["attachmentId"]
119
  attachment_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}/attachments/{attachment_id}"
120
- attachment_response = requests.get(attachment_url, headers={"Authorization": f"Bearer {access_token_new}"})
121
  attachment_data = attachment_response.json()
122
  data = attachment_data.get("data")
123
  filename = part.get("filename", "untitled.txt")
 
59
  access_token = response.json().get("access_token")
60
  # if not access_token:
61
  # raise HTTPException(status_code=400, detail="Authorization code not provided")
 
62
  print("Entered this function, for testing purposes")
63
  brand_name = "louis vuitton"
64
 
65
+ user_info = requests.get("https://www.googleapis.com/oauth2/v1/userinfo", headers={"Authorization": f"Bearer {access_token}"})
66
  page_token = None
67
  messages = []
68
  user_query = f"subject:((receipt {brand_name}) OR (receipts {brand_name}) OR (reçu {brand_name}) OR (reçus {brand_name}) OR (Quittung {brand_name}) OR (Quittungen {brand_name}) OR (aankoopbon {brand_name}) OR (aankoopbonnen {brand_name}) OR (recibo {brand_name}) OR (recibos {brand_name}) OR (ricevuta {brand_name}) OR (ricevute {brand_name}) OR (ontvangstbewijs {brand_name}) OR (ontvangstbewijzen {brand_name})) has:attachment"
69
 
70
  while True:
71
  # Construct Gmail API request with pageToken
72
+
73
  gmail_url = f"https://www.googleapis.com/gmail/v1/users/me/messages?q={user_query}"
74
  if page_token:
75
  gmail_url += f"&pageToken={page_token}"
76
 
77
+ gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token}"})
78
  gmail_data = gmail_response.json()
79
 
80
  # Check if there are messages in the response
 
87
  else:
88
  break # No more pages, exit the loop
89
 
90
+ unique_thread_ids = set()
91
+
92
+ filtered_data_list = []
93
+
94
+ for entry in messages:
95
+ thread_id = entry['threadId']
96
+ if thread_id not in unique_thread_ids:
97
+ unique_thread_ids.add(thread_id)
98
+ filtered_data_list.append(entry)
99
+
100
  attachments = []
101
  attachment_no = 0
102
  data_new = {}
 
109
  print(message_id)
110
  if message_id:
111
  message_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}"
112
+ message_response = requests.get(message_url, headers={"Authorization": f"Bearer {access_token}"})
113
  message_data = message_response.json()
114
 
115
 
 
127
  if "body" in part and "attachmentId" in part["body"]:
128
  attachment_id = part["body"]["attachmentId"]
129
  attachment_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}/attachments/{attachment_id}"
130
+ attachment_response = requests.get(attachment_url, headers={"Authorization": f"Bearer {access_token}"})
131
  attachment_data = attachment_response.json()
132
  data = attachment_data.get("data")
133
  filename = part.get("filename", "untitled.txt")