-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_viewer.py
More file actions
455 lines (314 loc) · 11 KB
/
chat_viewer.py
File metadata and controls
455 lines (314 loc) · 11 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
import sys
import platform
import importlib.util
import os
import json
import subprocess
import textwrap
from datetime import datetime
import shutil
import zipfile
import pydoc
from wcwidth import wcswidth
# ==============================
# Dependency Checks
# ==============================
def check_dependencies():
print("🔍 Checking requirements...\n")
# ---- Python Version ----
if sys.version_info < (3, 8):
print("❌ Python 3.8+ is required.")
print("Your version:", sys.version)
sys.exit(1)
# ---- wcwidth module ----
if importlib.util.find_spec("wcwidth") is None:
print("❌ Missing Python package: wcwidth\n")
print("Install it using:\n")
print(" pip install wcwidth\n")
sys.exit(1)
# ---- fzf binary ----
if shutil.which("fzf") is None:
os_name = platform.system()
print("❌ Missing dependency: fzf\n")
if os_name == "Linux":
print("Install fzf using:")
print(" sudo apt install fzf # Ubuntu/Debian")
print(" sudo dnf install fzf # Fedora")
print(" sudo pacman -S fzf # Arch")
elif os_name == "Darwin":
print("Install fzf using:")
print(" brew install fzf")
elif os_name == "Windows":
print("Install fzf using:")
print(" choco install fzf")
print("or use Winget:")
print(" winget install fzf")
else:
print("Please install fzf manually from:")
print(" https://github.com/junegunn/fzf")
sys.exit(1)
print("✅ All requirements satisfied.\n")
# ==============================
# TERMINAL SETTINGS
# ==============================
TERMINAL_WIDTH = shutil.get_terminal_size().columns
MAX_BUBBLE_WIDTH = int(TERMINAL_WIDTH * 0.55)
DOWNLOADS = os.path.join(os.path.expanduser("~"), "Downloads")
# ==============================
# Unicode Safe Padding
# ==============================
def pad_text(text, width):
extra = width - wcswidth(text)
return text + (" " * max(0, extra))
# ==============================
# Pager (Cross Platform)
# ==============================
def open_pager(text):
"""
Cross-platform pager replacement for less.
Works on Linux, macOS, Windows.
"""
pydoc.pager(text)
# ==============================
# Check if Takeout already extracted
# ==============================
def find_existing_takeout():
groups_path = os.path.join(DOWNLOADS, "Takeout", "Google Chat", "Groups")
if os.path.exists(groups_path):
print("✅ Found extracted Takeout folder.")
return groups_path
return None
# ==============================
# Find latest takeout-*.zip
# ==============================
def find_latest_takeout_zip():
zips = [
f
for f in os.listdir(DOWNLOADS)
if f.startswith("takeout-") and f.endswith(".zip")
]
if not zips:
return None
zips.sort(key=lambda f: os.path.getmtime(os.path.join(DOWNLOADS, f)), reverse=True)
latest = os.path.join(DOWNLOADS, zips[0])
print("✅ Found latest ZIP:", latest)
return latest
# ==============================
# Extract ZIP into Downloads
# ==============================
def extract_takeout(zip_path):
print("📦 Extracting ZIP...")
with zipfile.ZipFile(zip_path, "r") as z:
z.extractall(DOWNLOADS)
print("✅ Extraction completed.")
# ==============================
# Load messages safely
# ==============================
def load_messages(path):
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict) and "messages" in data:
return data["messages"]
if isinstance(data, list):
return data
return []
# ==============================
# Timestamp Cleaner
# ==============================
def clean_date(date_str):
try:
dt = datetime.strptime(date_str, "%A, %d %B %Y at %H:%M:%S UTC")
return dt.strftime("%Y-%m-%d %H:%M")
except:
return date_str
# ==============================
# Detect pinned messages
# ==============================
def is_pinned(msg):
labels = msg.get("message_labels", [])
return any(l.get("label_type") == "PINNED" for l in labels)
def count_pinned(messages):
return sum(1 for m in messages if is_pinned(m))
def extract_pinned(messages):
return [m for m in messages if is_pinned(m)]
# ==============================
# Auto-detect your email
# ==============================
def detect_my_email(groups_path):
email_count = {}
for folder in os.listdir(groups_path):
msg_file = os.path.join(groups_path, folder, "messages.json")
if not os.path.exists(msg_file):
continue
try:
with open(msg_file, "r", encoding="utf-8") as f:
data = json.load(f)
messages = data.get("messages", [])
for msg in messages[:200]:
creator = msg.get("creator", {})
email = creator.get("email")
if email:
email_count[email] = email_count.get(email, 0) + 1
except:
continue
if not email_count:
return None
return max(email_count, key=email_count.get)
# ==============================
# Extract DM participant name
# ==============================
def get_dm_participant(messages, my_email):
for msg in messages:
creator = msg.get("creator", {})
email = creator.get("email")
name = creator.get("name", "Unknown")
if email == my_email:
continue
if name and name != "Unknown":
return name
return "Deleted User"
# ==============================
# Extract Space Title
# ==============================
def get_space_title(folder_path):
info_file = os.path.join(folder_path, "group_info.json")
if not os.path.exists(info_file):
return None
try:
with open(info_file, "r", encoding="utf-8") as f:
data = json.load(f)
return data.get("name")
except:
return None
# ==============================
# Bubble Drawing
# ==============================
def draw_bubble(text, align="left"):
wrapped = textwrap.wrap(text, width=MAX_BUBBLE_WIDTH)
top = "┌" + "─" * (MAX_BUBBLE_WIDTH + 2) + "┐"
bottom = "└" + "─" * (MAX_BUBBLE_WIDTH + 2) + "┘"
bubble_lines = [top]
for line in wrapped:
bubble_lines.append(f"│ {pad_text(line, MAX_BUBBLE_WIDTH)} │")
bubble_lines.append(bottom)
bubble_text = "\n".join(bubble_lines)
if align == "right":
padding = TERMINAL_WIDTH - (MAX_BUBBLE_WIDTH + 4)
bubble_text = "\n".join((" " * padding + l) for l in bubble_text.splitlines())
return bubble_text
# ==============================
# Format Chat Output
# ==============================
def format_chat(messages, my_email, pinned_only=False):
output = []
if pinned_only:
output.append("📌 Showing ONLY pinned messages\n")
else:
output.append(
"Navigation:\n /PINNED → search pinned\n q → quit pager\n"
)
for msg in messages:
creator = msg.get("creator", {})
name = creator.get("name", "Unknown")
email = creator.get("email")
text = msg.get("text", "").strip()
if not text:
if is_pinned(msg):
text = "[Pinned message (non-text)]"
else:
continue
date = clean_date(msg.get("created_date", ""))
sender = "You" if email == my_email else name
align = "right" if email == my_email else "left"
pin_icon = "[PINNED] " if is_pinned(msg) else ""
header = f"{pin_icon}{sender} • {date}"
if align == "right":
header = header.rjust(TERMINAL_WIDTH)
output.append("\n" + header)
output.append(draw_bubble(text, align))
return "\n".join(output)
# ==============================
# fzf Helper
# ==============================
def run_fzf(options, prompt):
fzf = subprocess.Popen(
["fzf", "--prompt", prompt],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True,
)
selected, _ = fzf.communicate("\n".join(options))
return selected.strip()
# ==============================
# MAIN PROGRAM
# ==============================
def main():
# Step 1: Find extracted Takeout folder
groups_path = find_existing_takeout()
# Step 2: If missing → extract latest ZIP
if not groups_path:
zip_path = find_latest_takeout_zip()
if not zip_path:
print("❌ No takeout-*.zip found in Downloads.")
return
extract_takeout(zip_path)
groups_path = find_existing_takeout()
if not groups_path:
print("❌ Extracted, but Groups folder not found.")
return
print("✅ Using Groups folder:", groups_path)
# Step 3: Detect your email automatically
my_email = detect_my_email(groups_path)
if not my_email:
my_email = input("Enter your email: ").strip()
print("✅ Your email:", my_email)
# Step 4: Category selector
category = run_fzf(["DM", "SPACE", "PINNED ONLY"], "Select Category: ")
if not category:
return
pinned_mode = category.startswith("PINNED")
show_dm = category.startswith("DM")
show_space = category.startswith("SPACE")
# Step 5: Build chat list
entries = []
for folder in sorted(os.listdir(groups_path)):
folder_path = os.path.join(groups_path, folder)
msg_file = os.path.join(folder_path, "messages.json")
if not os.path.exists(msg_file):
continue
messages = load_messages(msg_file)
pinned_count = count_pinned(messages)
if pinned_mode and pinned_count == 0:
continue
if (show_dm or pinned_mode) and folder.startswith("DM"):
name = get_dm_participant(messages, my_email)
if pinned_count > 0:
name += f" (📌 {pinned_count})"
entries.append(f"DM {name:<45} | {folder}")
elif (show_space or pinned_mode) and folder.startswith("Space"):
title = get_space_title(folder_path) or folder
if pinned_count > 0:
title += f" (📌 {pinned_count})"
entries.append(f"SP {title:<45} | {folder}")
if not entries:
print("❌ No chats found.")
return
# Step 6: Chat selector
selected = run_fzf(entries, "Select Chat: ")
if not selected:
return
folder = selected.split("|")[-1].strip()
# Step 7: Load messages
msg_file = os.path.join(groups_path, folder, "messages.json")
messages = load_messages(msg_file)
if pinned_mode:
messages = extract_pinned(messages)
chat_text = format_chat(messages, my_email, pinned_only=pinned_mode)
# ✅ Cross-platform pager (no less needed)
open_pager(chat_text)
# ==============================
# RUN
# ==============================
if __name__ == "__main__":
check_dependencies()
main()