Omkar008 commited on
Commit
88bdc0b
1 Parent(s): a4eab37

Update get_gmail_data.py

Browse files
Files changed (1) hide show
  1. 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?maxResults=10&q={receipt_query}"
60
- def __fetch_page(url):
61
- response = requests.get(url, headers={"Authorization": f"Bearer {access_token}"})
62
- response.raise_for_status() # Raise error if the request fails
63
- data = response.json()
64
- return data.get("messages", []), data.get("nextPageToken")
65
-
66
- messages = []
67
- page_token = None
68
- try:
69
- while True:
70
- url = f"{gmail_url}&pageToken={page_token}" if page_token else gmail_url
71
- page_messages, page_token = __fetch_page(url)
72
- messages.extend(page_messages)
73
- if not page_token:
74
- break
75
- except requests.RequestException as e:
76
- raise RuntimeError(f"Error fetching messages from Gmail API: {str(e)}")
 
 
 
 
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