Spaces:
Running
Running
Update get_gmail_data.py
Browse files- get_gmail_data.py +22 -18
get_gmail_data.py
CHANGED
@@ -56,24 +56,28 @@ class GmailDataExtractor:
|
|
56 |
receipt_query = f'(subject:"your order" OR subject:receipts OR subject:receipt OR subject: aankoopbon OR subject:reçu OR subject:invoice OR subject:invoice OR category:purchases)'
|
57 |
# if self.__user_input is not None:
|
58 |
# receipt_query = f'((subject:"your order" OR subject:receipts OR subject:receipt OR subject:invoice OR subject:invoice OR category:purchases) AND subject:{self.__user_input})&maxResults=15'
|
59 |
-
gmail_url = f"https://www.googleapis.com/gmail/v1/users/me/messages?
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
77 |
|
78 |
print(len(messages))
|
79 |
return messages
|
|
|
56 |
receipt_query = f'(subject:"your order" OR subject:receipts OR subject:receipt OR subject: aankoopbon OR subject:reçu OR subject:invoice OR subject:invoice OR category:purchases)'
|
57 |
# if self.__user_input is not None:
|
58 |
# receipt_query = f'((subject:"your order" OR subject:receipts OR subject:receipt OR subject:invoice OR subject:invoice OR category:purchases) AND subject:{self.__user_input})&maxResults=15'
|
59 |
+
gmail_url = f"https://www.googleapis.com/gmail/v1/users/me/messages?q={receipt_query}&maxResults=10"
|
60 |
+
gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token}"})
|
61 |
+
gmail_data = gmail_response.json()
|
62 |
+
messages=[]
|
63 |
+
messages.extend(gmail_data)
|
64 |
+
# def __fetch_page(url):
|
65 |
+
# response = requests.get(url, headers={"Authorization": f"Bearer {access_token}"})
|
66 |
+
# response.raise_for_status() # Raise error if the request fails
|
67 |
+
# data = response.json()
|
68 |
+
# return data.get("messages", []), data.get("nextPageToken")
|
69 |
+
|
70 |
+
# messages = []
|
71 |
+
# page_token = None
|
72 |
+
# try:
|
73 |
+
# while True:
|
74 |
+
# url = f"{gmail_url}&pageToken={page_token}" if page_token else gmail_url
|
75 |
+
# page_messages, page_token = __fetch_page(url)
|
76 |
+
# messages.extend(page_messages)
|
77 |
+
# if not page_token:
|
78 |
+
# break
|
79 |
+
# except requests.RequestException as e:
|
80 |
+
# raise RuntimeError(f"Error fetching messages from Gmail API: {str(e)}")
|
81 |
|
82 |
print(len(messages))
|
83 |
return messages
|