-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet-api5.py
203 lines (167 loc) · 7.56 KB
/
wallet-api5.py
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
import requests
from datetime import datetime
import re
import os
import sys
from dotenv import load_dotenv
from openpyxl import Workbook
from openpyxl.styles import Alignment
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
# Load environment variables
load_dotenv()
API_KEY = os.getenv("API_KEY")
INTERCOM_PROD_KEY = os.getenv('INTERCOM_PROD_KEY')
GDRIVE_FOLDER_ID = os.getenv('GDRIVE_FOLDER_ID')
def remove_html_tags(text):
if not isinstance(text, str):
return ''
return re.sub(r'<.*?>', '', text)
def sanitize_text(text):
if text:
return text.replace('\u200b', '').encode('utf-8', 'ignore').decode('utf-8')
return text
def get_intercom_conversation(conversation_id):
url = f'https://api.intercom.io/conversations/{conversation_id}'
response = requests.get(url, headers={"Authorization": f"Bearer {INTERCOM_PROD_KEY}"})
if response.status_code != 200:
print(f"Error: {response.status_code} - {response.text}")
return None
return response.json()
def get_conversation_summary(conversation):
if 'conversation_parts' in conversation:
conversation_parts = conversation['conversation_parts'].get('conversation_parts', [])
for part in conversation_parts:
if part.get('part_type') == 'conversation_summary':
return remove_html_tags(part.get('body', ''))
return conversation.get('custom_attributes', {}).get('Cristi GPT response', "No summary available")
def get_conversation_transcript(conversation):
transcript = []
if 'conversation_parts' in conversation:
conversation_parts = conversation['conversation_parts'].get('conversation_parts', [])
for part in conversation_parts:
if part.get('part_type') == 'comment':
author = part.get('author', {}).get('type', 'Unknown')
comment = remove_html_tags(part.get('body', ''))
transcript.append(f"{author}: {comment}")
return "\n".join(transcript) if transcript else "No transcript available"
def search_conversations(start_date_str, end_date_str):
try:
# Handle both "YYYY-MM-DD" and "YYYY-MM-DD HH:MM" formats
if " " in start_date_str:
start_date = datetime.strptime(start_date_str, "%Y-%m-%d %H:%M").timestamp()
else:
start_date = datetime.strptime(start_date_str, "%Y-%m-%d").timestamp()
if " " in end_date_str:
end_date = datetime.strptime(end_date_str, "%Y-%m-%d %H:%M").timestamp()
else:
end_date = datetime.strptime(end_date_str, "%Y-%m-%d").timestamp()
except ValueError as e:
print(f"Error parsing dates: {e}")
return []
url = "https://api.intercom.io/conversations/search"
headers = {
"Authorization": f"Bearer {INTERCOM_PROD_KEY}",
"Accept": "application/json",
"Content-Type": "application/json"
}
payload = {
"query": {
"operator": "AND",
"value": [
{"field": "statistics.last_close_at", "operator": ">", "value": int(start_date)},
{"field": "statistics.last_close_at", "operator": "<", "value": int(end_date)}
]
},
"pagination": {"per_page": 150}
}
all_conversations = []
next_page = None
while True:
if next_page:
payload["pagination"]["starting_after"] = next_page # Add pagination cursor
response = requests.post(url, headers=headers, json=payload)
if response.status_code != 200:
print(f"Error: {response.status_code} - {response.text}")
return all_conversations # Return whatever was retrieved so far
data = response.json()
conversations = data.get('conversations', [])
all_conversations.extend(conversations) # Append new conversations
print(f"Fetched {len(conversations)} conversations, total: {len(all_conversations)}") # Debugging output
# Handle pagination
next_page_data = data.get('pages', {}).get('next', None)
if next_page_data and "starting_after" in next_page_data:
next_page = next_page_data["starting_after"]
else:
break # No more pages to fetch
print(f"Total conversations retrieved: {len(all_conversations)}") # Final count
return all_conversations
def filter_conversations_by_wallet_api(conversations):
"""Filters conversations for the MetaMask Wallet API area and retrieves full conversation details"""
filtered_conversations = []
for conversation in conversations:
attributes = conversation.get('custom_attributes', {})
print(f"Custom Attributes: {attributes}")
# Check if the conversation belongs to "Wallet API"
if attributes.get('MetaMask area', '').strip().lower() == 'wallet api':
full_conversation = get_intercom_conversation(conversation['id'])
if full_conversation:
filtered_conversations.append(full_conversation)
return filtered_conversations
def store_conversations_to_xlsx(conversations, file_path):
"""Stores filtered Wallet API conversations into an XLSX file"""
workbook = Workbook()
sheet = workbook.active
sheet.title = "Conversations"
# Define headers without 'Wallet API Issue' since it is not a category
headers = ['conversation_id', 'summary', 'transcript']
sheet.append(headers)
for conversation in conversations:
conversation_id = conversation['id']
summary = sanitize_text(get_conversation_summary(conversation))
transcript = sanitize_text(get_conversation_transcript(conversation))
row = [conversation_id, summary, transcript]
sheet.append(row)
# Apply text wrapping to relevant columns
for col in ["B", "C"]: # Column B = Summary, Column C = Transcript
for cell in sheet[col]:
cell.alignment = Alignment(wrap_text=True)
workbook.save(file_path)
print(f"File {file_path} saved successfully.")
def upload_to_drive(file_path):
gauth = GoogleAuth()
gauth.LoadCredentialsFile("credentials.json")
if gauth.credentials is None:
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
gauth.Refresh()
else:
gauth.Authorize()
gauth.SaveCredentialsFile("credentials.json")
drive = GoogleDrive(gauth)
file = drive.CreateFile({"title": os.path.basename(file_path), "parents": [{"id": GDRIVE_FOLDER_ID}]})
file.SetContentFile(file_path)
file.Upload()
print(f"File {file_path} uploaded successfully to Google Drive.")
def main_function(start_date, end_date):
conversations = search_conversations(start_date, end_date)
if not conversations:
print("No conversations found for the provided timeframe.")
return
wallet_api_conversations = filter_conversations_by_wallet_api(conversations)
print(f"Wallet API Conversations Found: {len(wallet_api_conversations)}")
if wallet_api_conversations:
file_path = f'wallet_api_conversations_{start_date}_to_{end_date}.xlsx'
store_conversations_to_xlsx(wallet_api_conversations, file_path)
upload_to_drive(file_path)
print(f"File {file_path} uploaded successfully.")
else:
print("No wallet API-related conversations found.")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python script.py <start_date> <end_date>")
sys.exit(1)
start_date = sys.argv[1]
end_date = sys.argv[2]
print(f"Script started with start_date: {start_date} and end_date: {end_date}")
main_function(start_date, end_date)