-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_orders.py
More file actions
463 lines (377 loc) · 17.3 KB
/
Copy pathsync_orders.py
File metadata and controls
463 lines (377 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""
Swiggy Instamart → Splitwise (Direct REST, no LLM)
Fetches Swiggy Instamart order emails from Gmail and creates Splitwise expenses.
Usage:
python sync_orders.py --days 30 --dry-run # preview without adding to Splitwise
python sync_orders.py --days 30 # actually add to Splitwise
"""
import os
import re
import sys
import json
import base64
import argparse
from datetime import datetime
from email import message_from_bytes
from email.header import decode_header
from pathlib import Path
from dotenv import load_dotenv
import httpx
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
load_dotenv()
# ── Config ────────────────────────────────────────────────────────────────────
SPLITWISE_TOKEN = os.environ.get("SPLITWISE_OAUTH_ACCESS_TOKEN")
SWIGGY_SENDERS = ["noreply@swiggy.in", "no-reply@swiggy.in"]
GMAIL_SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]
CREDENTIALS_FILE = Path(__file__).parent / "credentials.json"
TOKEN_FILE = Path(__file__).parent / "gmail_token.json"
SPLITWISE_API = "https://secure.splitwise.com/api/v3.0"
PROCESSED_FILE = Path(__file__).parent / "processed_orders.json"
# (token check happens inside main() so this file is safely importable)
# ── Gmail auth ────────────────────────────────────────────────────────────────
def get_gmail_service():
creds = None
if TOKEN_FILE.exists():
creds = Credentials.from_authorized_user_file(str(TOKEN_FILE), GMAIL_SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(str(CREDENTIALS_FILE), GMAIL_SCOPES)
creds = flow.run_local_server(port=0)
TOKEN_FILE.write_text(creds.to_json())
return build("gmail", "v1", credentials=creds)
# ── Email helpers ─────────────────────────────────────────────────────────────
def decode_header_value(value: str) -> str:
parts = decode_header(value)
return "".join(
p.decode(enc or "utf-8", errors="replace") if isinstance(p, bytes) else p
for p, enc in parts
)
def get_email_body(msg) -> str:
"""Extract plain text or stripped HTML body."""
if msg.is_multipart():
for part in msg.walk():
ct = part.get_content_type()
payload = part.get_payload(decode=True)
if not payload:
continue
text = payload.decode("utf-8", errors="replace")
if ct == "text/plain":
return text
if ct == "text/html":
import html as html_lib
text = html_lib.unescape(text)
return re.sub(r"<[^>]+>", " ", text)
else:
payload = msg.get_payload(decode=True)
if payload:
return payload.decode("utf-8", errors="replace")
return ""
# ── Swiggy email parser ───────────────────────────────────────────────────────
def parse_items(body: str) -> list[str]:
"""
Extract order items from Swiggy email body.
Handles multiple email template formats.
Returns item names only, e.g. "Paneer".
"""
# Try to isolate just the items section (various header wordings)
section_match = re.search(
r"(?:order items?|items?\s+ordered|your order(?:\s+items?)?)\s*[:\-]?\s*"
r"(.*?)"
r"(?:bill detail|subtotal|order total|grand total|you paid|delivery charge|platform fee|convenience fee|taxes & fee)",
body,
re.IGNORECASE | re.DOTALL,
)
section = section_match.group(1) if section_match else body
items = []
seen = set()
# Format A: "2 x Item Name (local name) ₹22.60" — qty present
# Format B: "Item Name (local name) ₹22.60" — no qty
item_pattern = re.compile(
r"(?:\d+\s*[xX×]\s*)?" # optional quantity prefix
r"([A-Za-z][^₹\n\r|]{3,80}?)" # item name
r"(?:\s*\([^)]{1,40}\))?" # optional local name in parens
r"\s*₹[\d,]+(?:\.\d{2})?", # price
)
# Words that indicate a fee/summary line, not a product
_GARBAGE = re.compile(
r"\b(fee|charge|tax|summary|subtotal|total|bill|handling|delivery|"
r"platform|convenience|discount|offer|saving|nbsp|gst|invoice)\b",
re.IGNORECASE,
)
for m in item_pattern.finditer(section):
name = m.group(1).strip()
name = re.sub(r"[\s\-_|]+$", "", name).strip()
if name and len(name) > 3 and not _GARBAGE.search(name) and name.lower() not in seen:
seen.add(name.lower())
items.append(name)
return items
def parse_swiggy_order(subject: str, body: str, date_str: str) -> dict | None:
"""
Extract order amount and items from a Swiggy email.
Returns dict with keys: date, amount, items — or None if parsing fails.
"""
# Skip cancelled orders
if re.search(r"cancel(l?ed|lation)", subject + " " + body, re.IGNORECASE):
print(f" ↷ Skipping cancelled order: {subject}")
return None
# Normalise whitespace
body_clean = re.sub(r"\s+", " ", body)
# Common patterns in Swiggy order emails (INR amounts)
patterns = [
r"(?:Total|Order Total|Grand Total|Amount Paid)[^\d]*₹?\s*([\d,]+(?:\.\d{2})?)",
r"₹\s*([\d,]+(?:\.\d{2})?)\s*(?:paid|charged|total)",
r"(?:Rs\.?|INR)\s*([\d,]+(?:\.\d{2})?)",
]
amount = None
for pattern in patterns:
match = re.search(pattern, body_clean, re.IGNORECASE)
if match:
amount_str = match.group(1).replace(",", "")
try:
amount = float(amount_str)
break
except ValueError:
continue
if not amount:
print(f" ⚠ Could not parse amount from: {subject}")
return None
# Parse items
items = parse_items(body_clean)
# Parse date
try:
from email.utils import parsedate_to_datetime
order_date = parsedate_to_datetime(date_str).strftime("%Y-%m-%d")
except Exception:
order_date = datetime.now().strftime("%Y-%m-%d")
return {
"date": order_date,
"amount": amount,
"items": items,
}
# ── Gmail fetch ───────────────────────────────────────────────────────────────
def fetch_swiggy_emails(service, last_n_days: int = None, since: str = None) -> list[dict]:
if since:
# Gmail after: filter expects YYYY/MM/DD
dt = datetime.strptime(since, "%d-%m-%Y")
query = f"from:({' OR '.join(SWIGGY_SENDERS)}) subject:instamart -subject:cancel after:{dt.strftime('%Y/%m/%d')}"
else:
query = f"from:({' OR '.join(SWIGGY_SENDERS)}) subject:instamart -subject:cancel newer_than:{last_n_days}d"
print(f"Gmail query: {query}\n")
# Fetch all pages
messages = []
page_token = None
while True:
kwargs = {"userId": "me", "q": query, "maxResults": 100}
if page_token:
kwargs["pageToken"] = page_token
results = service.users().messages().list(**kwargs).execute()
messages.extend(results.get("messages", []))
page_token = results.get("nextPageToken")
if not page_token:
break
print(f"Total matching emails found: {len(messages)}\n")
if not messages:
return []
orders = []
for msg_ref in messages:
msg_data = service.users().messages().get(
userId="me", id=msg_ref["id"], format="raw"
).execute()
raw = base64.urlsafe_b64decode(msg_data["raw"])
msg = message_from_bytes(raw)
sender = msg.get("From", "")
if not any(s in sender.lower() for s in SWIGGY_SENDERS):
continue # belt-and-braces check
subject = decode_header_value(msg.get("Subject", ""))
date_str = msg.get("Date", "")
body = get_email_body(msg)
order = parse_swiggy_order(subject, body, date_str)
if order:
order["email_id"] = msg_ref["id"]
orders.append(order)
return orders
# ── Processed orders tracker ──────────────────────────────────────────────────
def load_processed() -> set:
if PROCESSED_FILE.exists():
return set(json.loads(PROCESSED_FILE.read_text()))
return set()
def save_processed(ids: set):
PROCESSED_FILE.write_text(json.dumps(list(ids), indent=2))
# ── Splitwise REST calls ──────────────────────────────────────────────────────
def get_current_user() -> dict:
resp = httpx.get(
f"{SPLITWISE_API}/get_current_user",
headers={"Authorization": f"Bearer {SPLITWISE_TOKEN}"},
)
resp.raise_for_status()
return resp.json()["user"]
def get_categories() -> list:
resp = httpx.get(
f"{SPLITWISE_API}/get_categories",
headers={"Authorization": f"Bearer {SPLITWISE_TOKEN}"},
)
resp.raise_for_status()
categories = []
for group in resp.json().get("categories", []):
categories.append(group)
categories.extend(group.get("subcategories", []))
return categories
def find_grocery_category(categories: list) -> int | None:
for cat in categories:
if "grocer" in cat.get("name", "").lower():
return cat["id"]
return None
def create_group(name: str) -> int:
"""Create a new Splitwise group and return its id."""
resp = httpx.post(
f"{SPLITWISE_API}/create_group",
headers={"Authorization": f"Bearer {SPLITWISE_TOKEN}"},
data={"name": name},
)
resp.raise_for_status()
return resp.json()["group"]["id"]
def create_expense(user_id: int, group_id: int, amount: float,
description: str, category_id: int | None) -> dict:
# Fetch group members so we can split equally among all of them
grp = httpx.get(
f"{SPLITWISE_API}/get_group/{group_id}",
headers={"Authorization": f"Bearer {SPLITWISE_TOKEN}"},
)
grp.raise_for_status()
members = grp.json().get("group", {}).get("members", []) or [{"id": user_id}]
n = len(members)
per_person = round(amount / n, 2)
remainder = round(amount - per_person * (n - 1), 2) # absorbs rounding
first_line = description.split("\n")[0]
payload = {
"cost": f"{amount:.2f}",
"description": first_line,
"details": description,
"currency_code": "INR",
"group_id": group_id,
}
if category_id:
payload["category_id"] = category_id
for i, member in enumerate(members):
share = remainder if i == n - 1 else per_person
payload[f"users__{i}__user_id"] = member["id"]
payload[f"users__{i}__paid_share"] = f"{amount:.2f}" if member["id"] == user_id else "0.00"
payload[f"users__{i}__owed_share"] = f"{share:.2f}"
resp = httpx.post(
f"{SPLITWISE_API}/create_expense",
headers={"Authorization": f"Bearer {SPLITWISE_TOKEN}"},
data=payload,
)
resp.raise_for_status()
return resp.json()
def build_description(orders: list[dict]) -> str:
"""Build a single expense description listing all orders with items."""
total = sum(o["amount"] for o in orders)
sorted_orders = sorted(orders, key=lambda x: x["date"])
from_date = sorted_orders[0]["date"]
to_date = sorted_orders[-1]["date"]
title = f"Swiggy Instamart ({from_date} to {to_date})"
lines = [title, "─" * len(title)]
for o in sorted_orders:
items_str = "; ".join(o.get("items", [])) or "—"
lines.append(f"{o['date']} ₹{o['amount']:.2f} {items_str}")
lines.append("─" * 30)
lines.append(f"Total ₹{total:.2f}")
return "\n".join(lines)
# ── Expense push with split-retry ─────────────────────────────────────────────
def _push_with_retry(user_id: int, group_id: int, orders: list[dict],
category_id: int | None, depth: int = 0) -> list[str]:
"""Push orders as one combined expense; splits in half and retries on failure."""
if not orders:
return []
indent = " " * depth
total = sum(o["amount"] for o in orders)
description = build_description(orders)
try:
result = create_expense(user_id, group_id, total, description, category_id)
expenses = result.get("expenses") or []
if not expenses:
raise ValueError("API returned empty expenses list")
print(f"{indent}✓ Added ₹{total:.2f} ({len(orders)} order(s), id={expenses[0].get('id')})")
return [o["email_id"] for o in orders]
except Exception as e:
if len(orders) == 1:
print(f"{indent}✗ Failed on single order ₹{total:.2f}: {e}")
return []
mid = len(orders) // 2
print(f"{indent}↷ Failed ({e}) — splitting {len(orders)} orders into halves and retrying…")
a = _push_with_retry(user_id, group_id, orders[:mid], category_id, depth + 1)
b = _push_with_retry(user_id, group_id, orders[mid:], category_id, depth + 1)
return a + b
# ── Main ──────────────────────────────────────────────────────────────────────
def main(last_n_days: int, since: str, dry_run: bool, group_name: str,
classify: bool = False):
if not SPLITWISE_TOKEN:
print("ERROR: SPLITWISE_OAUTH_ACCESS_TOKEN not set in .env")
sys.exit(1)
print(f"{'[DRY RUN] ' if dry_run else ''}Syncing Swiggy Instamart → Splitwise\n")
# Gmail
service = get_gmail_service()
orders = fetch_swiggy_emails(service, last_n_days=last_n_days, since=since)
if not orders:
print("No Swiggy Instamart orders found. Nothing to do.")
return
print(f"Found {len(orders)} order(s):\n")
for o in orders:
print(f" {o['date']} ₹{o['amount']:.2f}")
# Skip already processed
processed = load_processed()
new_orders = [o for o in orders if o["email_id"] not in processed]
if not new_orders:
print("\nAll orders already synced to Splitwise. Nothing new to add.")
return
# Optional ML classification step
if classify:
from order_classifier import classify_and_filter
new_orders = classify_and_filter(new_orders)
if not new_orders:
print("No orders left after classification. Nothing to sync.")
return
total = sum(o["amount"] for o in new_orders)
print(f"\n{len(new_orders)} new order(s) | Total: ₹{total:.2f}\n")
if dry_run:
print("Description that would be created:")
print(build_description(new_orders))
print("\n[DRY RUN] Run without --dry-run to apply.")
return
# Splitwise
user = get_current_user()
user_id = user["id"]
print(f"Splitwise user: {user['first_name']} {user['last_name']} (id={user_id})")
categories = get_categories()
category_id = find_grocery_category(categories)
# Create group
group_id = create_group(group_name)
print(f"Created group: '{group_name}' (id={group_id})")
# Push with automatic split-retry on failure
succeeded_ids = _push_with_retry(user_id, group_id, new_orders, category_id)
for eid in succeeded_ids:
processed.add(eid)
save_processed(processed)
synced = len(succeeded_ids)
print(f"\n{'✓' if synced else '✗'} Synced {synced}/{len(new_orders)} order(s). Done.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Swiggy Instamart → Splitwise sync")
parser.add_argument("--days", type=int, default=30, help="Fetch orders from last N days (default: 30)")
parser.add_argument("--since", type=str, default=None, help="Fetch orders since date in dd-mm-yyyy format (e.g. 01-03-2026)")
parser.add_argument("--dry-run", action="store_true", help="Preview without adding to Splitwise")
parser.add_argument("--group", type=str, default="Swiggy Instamart", help="Splitwise group name to create (default: 'Swiggy Instamart')")
parser.add_argument("--classify", action="store_true", help="Use ML classifier to filter grocery/poultry orders only")
args = parser.parse_args()
if args.since:
try:
datetime.strptime(args.since, "%d-%m-%Y")
except ValueError:
print("ERROR: --since must be in dd-mm-yyyy format (e.g. 01-03-2026)")
sys.exit(1)
main(args.days, args.since, args.dry_run, args.group, args.classify)