Spaces:
Sleeping
Sleeping
Update test.py
Browse files
test.py
CHANGED
@@ -43,6 +43,89 @@ async def login_google():
|
|
43 |
"url_hr": oauth_url_hr
|
44 |
}
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
@app.post("/auth/google")
|
47 |
async def auth_google(request: Request):
|
48 |
|
|
|
43 |
"url_hr": oauth_url_hr
|
44 |
}
|
45 |
|
46 |
+
|
47 |
+
app.get("/test")
|
48 |
+
async def auth_google(request: Request):
|
49 |
+
|
50 |
+
data = await request.json()
|
51 |
+
code = data.get("access_token")
|
52 |
+
brand_name = data.get("brand_name")
|
53 |
+
print("Printing the access token")
|
54 |
+
print(code)
|
55 |
+
if not code:
|
56 |
+
raise HTTPException(status_code=400, detail="Authorization code not provided")
|
57 |
+
access_token_new = code
|
58 |
+
|
59 |
+
user_info = requests.get("https://www.googleapis.com/oauth2/v1/userinfo", headers={"Authorization": f"Bearer {access_token_new}"})
|
60 |
+
page_token = None
|
61 |
+
messages = []
|
62 |
+
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"
|
63 |
+
|
64 |
+
while True:
|
65 |
+
# Construct Gmail API request with pageToken
|
66 |
+
gmail_url = f"https://www.googleapis.com/gmail/v1/users/me/messages?q={user_query}"
|
67 |
+
if page_token:
|
68 |
+
gmail_url += f"&pageToken={page_token}"
|
69 |
+
|
70 |
+
gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token_new}"})
|
71 |
+
gmail_data = gmail_response.json()
|
72 |
+
|
73 |
+
# Check if there are messages in the response
|
74 |
+
if "messages" in gmail_data:
|
75 |
+
messages.extend(gmail_data["messages"])
|
76 |
+
|
77 |
+
# Check if there are more pages
|
78 |
+
if "nextPageToken" in gmail_data:
|
79 |
+
page_token = gmail_data["nextPageToken"]
|
80 |
+
else:
|
81 |
+
break # No more pages, exit the loop
|
82 |
+
|
83 |
+
attachments = []
|
84 |
+
attachment_no = 0
|
85 |
+
data_new = {}
|
86 |
+
for i,message in enumerate(messages) :
|
87 |
+
# print(i)
|
88 |
+
# print(message)
|
89 |
+
|
90 |
+
if message:
|
91 |
+
message_id = message.get("id")
|
92 |
+
print(message_id)
|
93 |
+
if message_id:
|
94 |
+
message_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}"
|
95 |
+
message_response = requests.get(message_url, headers={"Authorization": f"Bearer {access_token_new}"})
|
96 |
+
message_data = message_response.json()
|
97 |
+
|
98 |
+
|
99 |
+
if "payload" in message_data:
|
100 |
+
payload = message_data["payload"]
|
101 |
+
if "body" in payload and "data" in payload["body"]:
|
102 |
+
body_data = payload["body"]["data"]
|
103 |
+
body_content = base64.urlsafe_b64decode(body_data.encode("UTF-8")).decode("UTF-8")
|
104 |
+
print("Body Content:")
|
105 |
+
print(body_content)
|
106 |
+
|
107 |
+
# Check for parts in the message payload
|
108 |
+
if "payload" in message_data and "parts" in message_data["payload"]:
|
109 |
+
for part in message_data["payload"]["parts"]:
|
110 |
+
if "body" in part and "attachmentId" in part["body"]:
|
111 |
+
attachment_id = part["body"]["attachmentId"]
|
112 |
+
attachment_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}/attachments/{attachment_id}"
|
113 |
+
attachment_response = requests.get(attachment_url, headers={"Authorization": f"Bearer {access_token_new}"})
|
114 |
+
attachment_data = attachment_response.json()
|
115 |
+
data = attachment_data.get("data")
|
116 |
+
filename = part.get("filename", "untitled.txt")
|
117 |
+
|
118 |
+
if data:
|
119 |
+
data_new[filename]=data
|
120 |
+
attachment_content = base64.urlsafe_b64decode(data)
|
121 |
+
extracted_text = await extract_text_from_attachment(filename, attachment_content)
|
122 |
+
|
123 |
+
attachment_no+=1
|
124 |
+
|
125 |
+
return {"attachment_count":attachment_no,"attachment_content":data_new}
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
@app.post("/auth/google")
|
130 |
async def auth_google(request: Request):
|
131 |
|