-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms_processor.py
More file actions
160 lines (136 loc) · 5.83 KB
/
forms_processor.py
File metadata and controls
160 lines (136 loc) · 5.83 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
"""
Microsoft Forms integration for processing crowdsourced credit card data
"""
import logging
import hashlib
from datetime import datetime
from typing import List
import requests
import pandas as pd
from models import (
MicrosoftFormsResponse,
CreditCardOffer,
OfferCategory
)
logger = logging.getLogger(__name__)
class FormsProcessor:
"""Process Microsoft Forms responses"""
def __init__(self, api_key: str, form_id: str):
self.api_key = api_key
self.form_id = form_id
self.base_url = "https://graph.microsoft.com/v1.0"
async def fetch_responses(self) -> List[MicrosoftFormsResponse]:
"""
Fetch responses from Microsoft Forms
Note: This requires Microsoft Graph API access.
For testing, you can also use Excel export from Forms.
"""
if not self.api_key or not self.form_id:
logger.warning("No Forms API credentials configured, using sample data")
return self._get_sample_responses()
try:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
url = f"{self.base_url}/forms/{self.form_id}/responses"
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
return self._parse_responses(data)
except Exception as e:
logger.error(f"Error fetching forms responses: {e}")
return []
def process_excel_export(self, excel_path: str) -> List[MicrosoftFormsResponse]:
"""
Process Excel export from Microsoft Forms
This is an alternative to using the API
Expected columns:
- ID
- Start time
- Completion time
- Card Name
- Bank Name
- Offer Title
- Offer Description
- Category
- Expiry Date
- Minimum Spend
- Benefit Value
"""
try:
df = pd.read_excel(excel_path)
responses = []
for _, row in df.iterrows():
response = MicrosoftFormsResponse(
response_id=str(row.get('ID', hashlib.md5(str(row).encode()).hexdigest())),
submitted_at=pd.to_datetime(row.get('Completion time', datetime.now())),
card_name=str(row.get('Card Name', '')),
bank_name=str(row.get('Bank Name', '')),
offer_title=str(row.get('Offer Title', '')),
offer_description=str(row.get('Offer Description', '')),
category=str(row.get('Category', 'other')).lower(),
expiry_date=str(row.get('Expiry Date', '')) if pd.notna(row.get('Expiry Date')) else None,
min_spend=float(row.get('Minimum Spend', 0)) if pd.notna(row.get('Minimum Spend')) else None,
benefit_value=str(row.get('Benefit Value', '')) if pd.notna(row.get('Benefit Value')) else None,
submitter_email=str(row.get('Email', '')) if pd.notna(row.get('Email')) else None
)
responses.append(response)
logger.info(f"Processed {len(responses)} responses from Excel")
return responses
except Exception as e:
logger.error(f"Error processing Excel file: {e}")
return []
def response_to_offer(self, response: MicrosoftFormsResponse) -> CreditCardOffer:
"""Convert a Forms response to a CreditCardOffer"""
# Map category string to enum
category_map = {
'dining': OfferCategory.DINING,
'travel': OfferCategory.TRAVEL,
'shopping': OfferCategory.SHOPPING,
'gas': OfferCategory.GAS,
'groceries': OfferCategory.GROCERIES,
'entertainment': OfferCategory.ENTERTAINMENT,
}
category = category_map.get(response.category.lower(), OfferCategory.OTHER)
# Assign emoji based on category
emoji_map = {
OfferCategory.DINING: "🍽️",
OfferCategory.TRAVEL: "✈️",
OfferCategory.SHOPPING: "🛍️",
OfferCategory.GAS: "⛽",
OfferCategory.GROCERIES: "🛒",
OfferCategory.ENTERTAINMENT: "🎬",
OfferCategory.OTHER: "💳"
}
# Assign gradient colors based on category
gradient_map = {
OfferCategory.DINING: ["#f97316", "#ef4444"],
OfferCategory.TRAVEL: ["#3b82f6", "#06b6d4"],
OfferCategory.SHOPPING: ["#8b5cf6", "#ec4899"],
OfferCategory.GAS: ["#06b6d4", "#0891b2"],
OfferCategory.GROCERIES: ["#10b981", "#059669"],
OfferCategory.ENTERTAINMENT: ["#ec4899", "#d946ef"],
OfferCategory.OTHER: ["#64748b", "#475569"]
}
return CreditCardOffer(
id=response.response_id,
title=response.offer_title,
description=response.offer_description,
card_name=response.card_name,
bank_name=response.bank_name,
category=category,
expiry_date=response.expiry_date,
emoji=emoji_map[category],
gradient_colors=gradient_map[category],
min_spend=response.min_spend,
created_at=response.submitted_at,
updated_at=datetime.now()
)
def _get_sample_responses(self) -> List[MicrosoftFormsResponse]:
"""Generate sample responses for testing"""
return []
def _parse_responses(self, data: dict) -> List[MicrosoftFormsResponse]:
"""Parse Microsoft Graph API response"""
# Implementation depends on actual API response structure
return []