-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbackup.js
217 lines (176 loc) · 5.63 KB
/
backup.js
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
function generateOffsets(startOffset, total) {
const interval = 20;
const start = startOffset + interval;
const offsets = [];
for (let i = start; i <= total; i += interval) {
offsets.push(i);
}
return offsets;
}
function sleep(ms = 1000) {
return new Promise((resolve, reject) => setTimeout(resolve, ms));
}
function parseConversation(rawConversation) {
const title = rawConversation.title;
const create_time = rawConversation.create_time;
const mapping = rawConversation.mapping;
const keys = Object.keys(mapping);
const messages = [];
for (const k of keys) {
const msgPayload = mapping[k];
const msg = msgPayload.message;
if (!msg) continue;
const role = msg.author.role;
const content = msg.content.parts;
const model = msg.metadata.model_slug;
const create_time = msg.create_time;
messages.push({
role,
content,
model,
create_time,
});
}
return {
messages,
create_time,
title,
};
}
function getRequestCount(total, startOffset, stopOffset) {
if (stopOffset === -1) return total;
return stopOffset - startOffset;
}
function logProgress(total, messages, offset) {
const progress = Math.round((messages / total) * 100);
console.log(`GPT-BACKUP::PROGRESS::${progress}%::OFFSET::${offset}`);
}
function getDateFormat(date) {
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const day = ('0' + date.getDate()).slice(-2);
const hours = ('0' + date.getHours()).slice(-2);
const minutes = ('0' + date.getMinutes()).slice(-2);
const seconds = ('0' + date.getSeconds()).slice(-2);
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
}
function downloadJson(data) {
const jsonString = JSON.stringify(data, null, 2);
const jsonBlob = new Blob([jsonString], { type: 'application/json' });
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(jsonBlob);
downloadLink.download = `gpt-backup-${getDateFormat(new Date())}.json`;
document.body.appendChild(downloadLink);
downloadLink.click();
return new Promise((resolve, reject) => {
setTimeout(() => {
document.body.removeChild(downloadLink);
URL.revokeObjectURL(downloadLink.href);
resolve();
}, 150);
});
}
async function loadToken() {
const res = await fetch('https://chat.openai.com/api/auth/session');
if (!res.ok) {
throw new Error('failed to fetch token');
}
const json = await res.json();
return json.accessToken;
}
async function getConversationIds(token, offset = 0) {
const res = await fetch(
`https://chat.openai.com/backend-api/conversations?offset=${offset}&limit=20`,
{
headers: {
authorization: `Bearer ${token}`,
},
},
);
if (!res.ok) {
throw new Error('failed to fetch conversation ids');
}
const json = await res.json();
return {
items: json.items.map((item) => ({ ...item, offset })),
total: json.total,
};
}
async function fetchConversation(token, id, maxAttempts = 3, attempt = 1) {
const INITIAL_BACKOFF = 10000;
const BACKOFF_MULTIPLIER = 2;
try {
const res = await fetch(
`https://chat.openai.com/backend-api/conversation/${id}`,
{
headers: {
authorization: `Bearer ${token}`,
},
},
);
if (!res.ok) {
throw new Error('Unsuccessful response');
}
return res.json();
} catch (error) {
if (attempt >= maxAttempts) {
throw new Error(`Failed to fetch conversation after ${maxAttempts} attempts.`);
} else {
var backoff = INITIAL_BACKOFF * Math.pow(BACKOFF_MULTIPLIER, attempt);
console.log(`Error. Retrying in ${backoff}ms.`);
await sleep(backoff);
return fetchConversation(token, id, maxAttempts, attempt + 1);
}
}
}
async function getAllConversations(startOffset, stopOffset) {
const token = await loadToken();
// get first batch
const { total, items: allItems } = await getConversationIds(
token,
startOffset,
);
// generate offsets
const offsets = generateOffsets(startOffset, total);
// don't spam api
// fetch all offsets
for (const offset of offsets) {
// stop at offset
if (offset === stopOffset) break;
await sleep();
const { items } = await getConversationIds(token, offset);
allItems.push.apply(allItems, items);
}
const lastOffset =
stopOffset === -1 ? offsets[offsets.length - 1] : stopOffset;
const allConversations = [];
const requested = getRequestCount(total, startOffset, stopOffset);
console.log(`GPT-BACKUP::STARTING::TOTAL-OFFSETS::${lastOffset}`);
console.log(`GPT-BACKUP::STARTING::REQUESTED-MESSAGES::${requested}`);
console.log(`GPT-BACKUP::STARTING::TOTAL-MESSAGES::${total}`);
for (const item of allItems) {
// 60 conversations/min
await sleep(1000);
// log progress
if (allConversations.length % 20 === 0) {
logProgress(requested, allConversations.length, item.offset);
}
const rawConversation = await fetchConversation(token, item.id);
const conversation = parseConversation(rawConversation);
allConversations.push(conversation);
}
logProgress(requested, allConversations.length, lastOffset);
return allConversations;
}
async function main(startOffset, stopOffset) {
const allConversations = await getAllConversations(startOffset, stopOffset);
await downloadJson(allConversations);
}
// customize if you need to continue from a previous run
// increments of 20
const START_OFFSET = 0;
// set to -1 to run through all messages
const STOP_OFFSET = -1;
main(START_OFFSET, STOP_OFFSET)
.then(() => console.log('GPT-BACKUP::DONE'))
.catch((e) => console.error(e));