Repository navigation
Expand file tree
/
Copy pathconvert.py
More file actions
605 lines (517 loc) · 20.3 KB
/
Copy pathconvert.py
File metadata and controls
605 lines (517 loc) · 20.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env python3
"""
ChatGPT Export Reader
Converts your ChatGPT data export into readable, searchable conversations.
Usage:
1. Export your data from ChatGPT (Settings > Data Controls > Export)
2. Unzip and find conversations.json
3. Put this script in the same folder as conversations.json
4. Run: python convert.py
5. Open conversations/INDEX.html in your browser
"""
import json
import os
import re
import html
from datetime import datetime
from pathlib import Path
from collections import defaultdict
from urllib.parse import quote
def sanitize_filename(title, max_length=50):
"""Create a safe filename from a conversation title."""
if not title:
title = "Untitled"
safe = re.sub(r'[<>:"/\\|?*]', '', title)
safe = re.sub(r'\s+', ' ', safe).strip()
if len(safe) > max_length:
safe = safe[:max_length].rsplit(' ', 1)[0]
return safe or "Untitled"
def timestamp_to_datetime(ts):
"""Convert Unix timestamp to datetime."""
if ts is None:
return None
try:
return datetime.fromtimestamp(ts)
except:
return None
def extract_message_text(message):
"""Extract readable text from a message object."""
if not message or 'content' not in message:
return None
content = message['content']
content_type = content.get('content_type', '')
if content_type in ('text', 'multimodal_text'):
parts = content.get('parts', [])
text_parts = []
for part in parts:
if isinstance(part, str):
text_parts.append(part)
elif isinstance(part, dict):
if part.get('content_type') == 'image_asset_pointer':
text_parts.append('[Image]')
elif 'text' in part:
text_parts.append(part['text'])
return '\n'.join(text_parts) if text_parts else None
elif content_type == 'code':
return f"```\n{content.get('text', '')}\n```"
return None
def build_message_thread(mapping):
"""Build an ordered list of messages from the mapping structure.
Uses iterative traversal to handle deeply nested or circular
conversation trees without hitting Python's recursion limit.
"""
if not mapping:
return []
# Rebuild the tree from each node's `parent` pointer. Older exports also
# carried a `children` list on every node; exports from October 2026
# onwards do not (issue #3), so the parent pointers are the only reliable
# source. Where a node does still carry `children`, keep any ids from it
# that the parent pointers missed.
children_map = defaultdict(list)
nodes = {}
for node_id, node in mapping.items():
nodes[node_id] = node
parent = node.get('parent')
if parent:
children_map[parent].append(node_id)
for node_id, node in mapping.items():
for child_id in node.get('children', []) or []:
if child_id in mapping and child_id not in children_map[node_id]:
children_map[node_id].append(child_id)
def node_time(node_id):
message = nodes.get(node_id, {}).get('message') or {}
return message.get('create_time') or 0
# Regenerated replies give a node several children; walk them oldest first.
for node_id in children_map:
children_map[node_id].sort(key=node_time)
# Find the root(s).
# Normally exactly one node has parent: None. Some export variants have
# been seen with a "dangling" parent id on the root (a parent that isn't
# present in the mapping), which would previously make us find no root
# and silently emit an empty conversation. Treat any node whose parent
# is missing from the mapping as a root.
roots = []
for node_id, node in mapping.items():
parent = node.get('parent')
if parent is None or parent not in nodes:
roots.append(node_id)
messages = []
visited = set()
if not roots:
# Last resort (e.g. a cycle with no discoverable root): emit all
# messages in create_time order rather than losing the words.
timed = []
for node_id, node in mapping.items():
message = node.get('message')
if not message:
continue
author = message.get('author', {}).get('role', 'unknown')
text = extract_message_text(message)
if text and author in ('user', 'assistant', 'tool'):
timed.append({
'role': author,
'text': text,
'time': message.get('create_time')
})
timed.sort(key=lambda m: m['time'] or 0)
return timed
# Iterative depth-first traversal using a stack
# We push children in reverse order so the first child is processed first
stack = list(reversed(roots))
while stack:
node_id = stack.pop()
if node_id in visited:
continue
visited.add(node_id)
node = nodes.get(node_id)
if not node:
continue
message = node.get('message')
if message:
author = message.get('author', {}).get('role', 'unknown')
text = extract_message_text(message)
create_time = message.get('create_time')
if text and author in ('user', 'assistant', 'tool'):
messages.append({
'role': author,
'text': text,
'time': create_time
})
# Push children in reverse order so first child is popped first.
# Use the map rebuilt from parent pointers, never the node's own
# `children` key — newer exports don't have one (issue #3).
children = children_map.get(node_id, [])
for child_id in reversed(children):
if child_id not in visited:
stack.append(child_id)
return messages
def conversation_to_markdown(conv):
"""Convert a conversation to Markdown format."""
title = conv.get('title', 'Untitled Conversation')
create_time = timestamp_to_datetime(conv.get('create_time'))
update_time = timestamp_to_datetime(conv.get('update_time'))
lines = []
lines.append(f"# {title}")
lines.append("")
if create_time:
lines.append(f"**Created:** {create_time.strftime('%Y-%m-%d %H:%M')}")
if update_time:
lines.append(f"**Last Updated:** {update_time.strftime('%Y-%m-%d %H:%M')}")
lines.append("")
lines.append("---")
lines.append("")
messages = build_message_thread(conv.get('mapping', {}))
for msg in messages:
role = msg['role']
text = msg['text']
if role == 'user':
lines.append("## You")
elif role == 'assistant':
lines.append("## ChatGPT")
elif role == 'tool':
lines.append("## Tool Output")
else:
lines.append(f"## {role.title()}")
lines.append("")
lines.append(text)
lines.append("")
lines.append("---")
lines.append("")
return '\n'.join(lines), len(messages)
def get_first_user_message(mapping):
"""Get the first user message as a preview."""
if not mapping:
return ''
for node_id, node in mapping.items():
message = node.get('message')
if message and message.get('author', {}).get('role') == 'user':
text = extract_message_text(message)
if text:
return text.replace('\n', ' ').strip()[:150]
return ''
def count_messages(mapping):
"""Count user and assistant messages."""
count = 0
if not mapping:
return 0
for node_id, node in mapping.items():
message = node.get('message')
if message:
role = message.get('author', {}).get('role', '')
if role in ('user', 'assistant'):
count += 1
return count
def escape_html(s):
"""Escape HTML special characters."""
return html.escape(s) if s else ''
def escape_js_string(s):
"""Escape a string for use in JavaScript/HTML attributes."""
if not s:
return ''
return s.replace('\\', '\\\\').replace('"', '"').replace('\n', ' ').replace('\r', '').replace('<', '<').replace('>', '>')
def generate_html_index(conv_data, output_file):
"""Generate the searchable HTML index."""
by_month = defaultdict(list)
for c in conv_data:
by_month[c['month']].append(c)
for month in by_month:
by_month[month].sort(key=lambda x: x['date'] or datetime.min, reverse=True)
html_content = f'''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My ChatGPT Conversations</title>
<style>
* {{ box-sizing: border-box; }}
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
color: #333;
}}
h1 {{
color: #10a37f;
border-bottom: 3px solid #10a37f;
padding-bottom: 10px;
}}
.stats {{
background: #fff;
padding: 15px 20px;
border-radius: 8px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}}
.search-box {{
width: 100%;
padding: 12px 16px;
font-size: 16px;
border: 2px solid #ddd;
border-radius: 8px;
margin-bottom: 20px;
}}
.search-box:focus {{
outline: none;
border-color: #10a37f;
}}
.month-section {{
background: #fff;
border-radius: 8px;
margin-bottom: 15px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
overflow: hidden;
}}
.month-header {{
background: #10a37f;
color: white;
padding: 12px 20px;
font-size: 18px;
font-weight: 600;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
}}
.month-header:hover {{ background: #0d8a6a; }}
.month-header .count {{
background: rgba(255,255,255,0.2);
padding: 4px 10px;
border-radius: 12px;
font-size: 14px;
}}
.month-header .toggle {{ font-size: 14px; }}
.month-content {{ padding: 0; }}
.month-content.collapsed {{ display: none; }}
.conversation {{
padding: 15px 20px;
border-bottom: 1px solid #eee;
transition: background 0.2s;
}}
.conversation:last-child {{ border-bottom: none; }}
.conversation:hover {{ background: #f9f9f9; }}
.conversation a {{
color: #10a37f;
text-decoration: none;
font-weight: 600;
font-size: 16px;
}}
.conversation a:hover {{ text-decoration: underline; }}
.conversation .meta {{
color: #888;
font-size: 13px;
margin-top: 4px;
}}
.conversation .preview {{
color: #666;
font-size: 14px;
margin-top: 8px;
font-style: italic;
line-height: 1.4;
}}
.hidden {{ display: none; }}
.no-results {{
text-align: center;
padding: 40px;
color: #888;
font-size: 18px;
background: #fff;
border-radius: 8px;
}}
</style>
</head>
<body>
<h1>My ChatGPT Conversations</h1>
<div class="stats">
<strong>Total Conversations:</strong> {len(conv_data)} |
<strong>Showing:</strong> <span id="showing-count">{len(conv_data)}</span>
</div>
<input type="text" class="search-box" id="search" placeholder="Search conversations by title or content...">
<div id="conversations-container">
'''
for month in sorted(by_month.keys(), reverse=True):
convs = by_month[month]
if month != 'Unknown':
try:
month_display = datetime.strptime(month, '%Y-%m').strftime('%B %Y')
except:
month_display = month
else:
month_display = 'Unknown Date'
html_content += f''' <div class="month-section" data-month="{month}">
<div class="month-header" onclick="toggleMonth(this)">
<span>{month_display}</span>
<span><span class="count">{len(convs)} conversations</span> <span class="toggle">▼</span></span>
</div>
<div class="month-content">
'''
for c in convs:
date_str = c['date'].strftime('%Y-%m-%d') if c['date'] else 'Unknown'
title_escaped = escape_html(c['title'])
preview_escaped = escape_html(c['preview'])
title_search = escape_js_string(c['title'].lower())
preview_search = escape_js_string(c['preview'].lower())
href = f"./{quote(c['filename'])}"
html_content += f''' <div class="conversation" data-title="{title_search}" data-preview="{preview_search}">
<a href="{href}">{title_escaped}</a>
<div class="meta">{date_str} • {c['message_count']} messages</div>
'''
if preview_escaped:
html_content += f''' <div class="preview">"{preview_escaped}..."</div>
'''
html_content += ''' </div>
'''
html_content += ''' </div>
</div>
'''
html_content += ''' </div>
<div id="no-results" class="no-results hidden">No conversations found matching your search.</div>
<script>
function toggleMonth(header) {
const content = header.nextElementSibling;
const toggle = header.querySelector('.toggle');
content.classList.toggle('collapsed');
toggle.textContent = content.classList.contains('collapsed') ? '►' : '▼';
}
const searchBox = document.getElementById('search');
const noResults = document.getElementById('no-results');
const showingCount = document.getElementById('showing-count');
searchBox.addEventListener('input', function() {
const query = this.value.toLowerCase().trim();
let visibleCount = 0;
document.querySelectorAll('.month-section').forEach(section => {
let monthVisible = false;
section.querySelectorAll('.conversation').forEach(conv => {
const title = conv.dataset.title || '';
const preview = conv.dataset.preview || '';
const matches = !query || title.includes(query) || preview.includes(query);
conv.classList.toggle('hidden', !matches);
if (matches) {
monthVisible = true;
visibleCount++;
}
});
section.classList.toggle('hidden', !monthVisible);
if (query && monthVisible) {
section.querySelector('.month-content').classList.remove('collapsed');
section.querySelector('.toggle').textContent = '▼';
}
});
showingCount.textContent = visibleCount;
noResults.classList.toggle('hidden', visibleCount > 0);
});
</script>
</body>
</html>
'''
with open(output_file, 'w', encoding='utf-8') as f:
f.write(html_content)
def main():
script_dir = Path(__file__).parent
output_dir = script_dir / 'conversations'
input_files = []
single_file = script_dir / 'conversations.json'
if single_file.exists():
input_files = [single_file]
else:
index = 0
while True:
chunk_file = script_dir / f'conversations-{index:03d}.json'
if not chunk_file.exists():
break
input_files.append(chunk_file)
index += 1
if not input_files:
print("=" * 60)
print("ERROR: No conversation export file found!")
print("=" * 60)
print()
print("To use this tool:")
print("1. Go to ChatGPT -> Settings -> Data Controls -> Export")
print("2. Wait for the email with your download link")
print("3. Download and unzip the export")
print("4. Copy conversations.json or conversations-000.json to this folder")
print("5. Run this script again")
print()
return
print()
print("=" * 60)
print(" ChatGPT Export Reader")
print("=" * 60)
print()
if len(input_files) == 1:
print(f"Reading {input_files[0]}...")
else:
print(f"Reading {len(input_files)} chunked conversation files...")
print("(This may take a moment for large exports)")
print()
conversations = []
for input_file in input_files:
with open(input_file, 'r', encoding='utf-8') as f:
file_conversations = json.load(f)
if isinstance(file_conversations, list):
conversations.extend(file_conversations)
else:
print(f"Warning: Skipping unexpected JSON format in {input_file}")
print(f"Found {len(conversations)} conversations")
output_dir.mkdir(exist_ok=True)
conv_data = []
filenames_used = set()
empty_conversations = 0
for i, conv in enumerate(conversations):
title = conv.get('title', 'Untitled')
create_time = timestamp_to_datetime(conv.get('create_time'))
date_prefix = create_time.strftime('%Y%m%d') if create_time else '00000000'
safe_title = sanitize_filename(title)
filename = f"{date_prefix}_{safe_title}.md"
# Handle duplicate filenames
counter = 1
while filename in filenames_used:
filename = f"{date_prefix}_{safe_title}_{counter}.md"
counter += 1
filenames_used.add(filename)
filepath = output_dir / filename
# Convert and save
markdown, rendered_count = conversation_to_markdown(conv)
if rendered_count == 0 and conv.get('mapping'):
empty_conversations += 1
with open(filepath, 'w', encoding='utf-8') as f:
f.write(markdown)
preview = get_first_user_message(conv.get('mapping', {}))
message_count = count_messages(conv.get('mapping', {}))
conv_data.append({
'title': title,
'date': create_time,
'filename': filename,
'preview': preview,
'message_count': message_count,
'month': create_time.strftime('%Y-%m') if create_time else 'Unknown'
})
if (i + 1) % 100 == 0:
print(f" Processed {i + 1}/{len(conversations)}...")
print()
print("Creating searchable index...")
generate_html_index(conv_data, output_dir / 'INDEX.html')
print()
print("=" * 60)
print(" Done!")
print("=" * 60)
print()
if empty_conversations:
print(f" WARNING: {empty_conversations} conversation(s) produced no message content")
print(" (metadata only). If this affects conversations you know have")
print(" messages, please open an issue at:")
print(" https://github.com/marelucent/chatgpt-export-reader/issues")
print(" and mention when you exported your data.")
print()
print(f" Conversations exported: {len(conversations)}")
print(f" Output folder: {output_dir}")
print()
print(" To view your conversations:")
print(f" Open: {output_dir / 'INDEX.html'}")
print()
print(" (Just double-click the file to open it in your browser)")
print()
if __name__ == '__main__':
main()