-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharchive_chat.py
345 lines (280 loc) · 11.7 KB
/
archive_chat.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
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
import argparse
import glob
import json
import os
import requests
import sys
from tqdm import tqdm
from tabulate import tabulate
def list_groups(args):
headers = {'Content-Type': 'application/json'}
page_num = 1
listing_complete = False
chats = []
while not listing_complete:
params = {
'token': args.token,
'omit': 'memberships',
'page': page_num
}
r = requests.get('https://api.groupme.com/v3/groups',
headers=headers, params=params)
current_chats = json.loads(r.content)
for chat in current_chats['response']:
chats.append((chat['name'], chat['id'], chat['messages']['count']))
page_num += 1
if len(current_chats['response']) == 0:
listing_complete = True
return chats
def list_dms(args):
headers = {'Content-Type': 'application/json'}
page_num = 1
listing_complete = False
chats = []
while not listing_complete:
params = {
'token': args.token,
'page': page_num
}
r = requests.get('https://api.groupme.com/v3/chats',
headers=headers, params=params)
current_chats = json.loads(r.content)
for chat in current_chats['response']:
chats.append((
chat['other_user']['name'],
chat['other_user']['id'],
chat['messages_count']
))
page_num += 1
if len(current_chats['response']) == 0:
listing_complete = True
return chats
def fetch_group_messages(args):
params = {
'token': args.token
}
url = 'https://api.groupme.com/v3/groups/%s' % (args.group_chat_id)
r = requests.get(url, params=params)
people = {}
messages = []
group_info = {}
response = json.loads(r.content)['response']
group_info['name'] = response['name']
group_info['description'] = response['description']
group_info['image_url'] = response['image_url']
group_info['created_at'] = response['created_at']
for member in response['members']:
people[member['user_id']] = {'name': member['nickname']}
if args.save_global_avatars:
people[member['user_id']]['avatar_url'] = member['image_url']
else:
people[member['user_id']]['avatar_url'] = None
url = 'https://api.groupme.com/v3/groups/%s/messages' % (
args.group_chat_id)
r = requests.get(url, params=params)
curr_messages = json.loads(r.content)
# TODO Check for validity of request
num_total_messages = curr_messages['response']['count']
num_fetched_messages = 0
curr_messages = curr_messages['response']['messages']
all_attachments = []
print("Fetching %d messages..." % (num_total_messages))
pbar = tqdm(total=num_total_messages)
while num_fetched_messages < num_total_messages:
num_fetched_messages += len(curr_messages)
pbar.update(len(curr_messages))
for message in curr_messages:
if message['sender_id'] not in people:
people[message['sender_id']] = {
'name': message['name'],
'avatar_url': message['avatar_url']
}
if not args.save_global_avatars and \
people[message['sender_id']]['avatar_url'] is None:
people[message['sender_id']]['avatar_url'] = \
message['avatar_url']
for att in message['attachments']:
if att['type'] == 'image' or \
att['type'] == 'video' or \
att['type'] == 'linked_image':
all_attachments.append(att['url'])
# print("[%s] %s : %s" % (
# message['created_at'], message['name'], message['text']))
messages.append({
'author': message['sender_id'],
'created_at': message['created_at'],
'text': message['text'],
'favorited_by': message['favorited_by'],
'attachments': message['attachments']
})
last_message_id = curr_messages[-1]['id']
params = {
'token': args.token,
'before_id': last_message_id,
'limit': args.num_messages_per_request
}
url = 'https://api.groupme.com/v3/groups/%s/messages' % (
args.group_chat_id)
r = requests.get(url, params=params)
if r.status_code == 304:
break
curr_messages = json.loads(r.content)
# TODO Check for validity of request
curr_messages = curr_messages['response']['messages']
pbar.close()
messages = list(reversed(messages))
return messages, people, group_info, all_attachments
def fetch_direct_messages(args):
params = {
'token': args.token,
'other_user_id': args.direct_chat_id
}
url = 'https://api.groupme.com/v3/direct_messages'
r = requests.get(url, params=params)
people = {}
messages = []
group_info = {}
curr_messages = json.loads(r.content)
# TODO Check for validity of request
num_total_messages = curr_messages['response']['count']
num_fetched_messages = 0
curr_messages = curr_messages['response']['direct_messages']
all_attachments = []
print("Fetching %d messages..." % (num_total_messages))
pbar = tqdm(total=num_total_messages)
while num_fetched_messages < num_total_messages:
num_fetched_messages += len(curr_messages)
pbar.update(len(curr_messages))
for message in curr_messages:
if message['sender_id'] not in people:
people[message['sender_id']] = {
'name': message['name'],
'avatar_url': message['avatar_url']
}
for att in message['attachments']:
if att['type'] == 'image' or \
att['type'] == 'video' or \
att['type'] == 'linked_image':
all_attachments.append(att['url'])
# print("[%s] %s : %s" % (
# message['created_at'], message['name'], message['text']))
messages.append({
'author': message['sender_id'],
'created_at': message['created_at'],
'text': message['text'],
'favorited_by': message['favorited_by'],
'attachments': message['attachments']
})
last_message_id = curr_messages[-1]['id']
params = {
'token': args.token,
'other_user_id': args.direct_chat_id,
'before_id': last_message_id,
'limit': args.num_messages_per_request
}
url = 'https://api.groupme.com/v3/direct_messages'
r = requests.get(url, params=params)
if r.status_code == 304:
break
curr_messages = json.loads(r.content)
# TODO Check for validity of request
curr_messages = curr_messages['response']['direct_messages']
pbar.close()
messages = list(reversed(messages))
group_info['name'] = people[args.direct_chat_id]['name']
group_info['image_url'] = people[args.direct_chat_id]['avatar_url']
return messages, people, group_info, all_attachments
def main():
parser = argparse.ArgumentParser(description="""GroupMe chats archiver.
By default, the app will list all of your chats that are currently
active.
""")
parser.add_argument('--token', '-t', required=True,
help="GroupMe Developer Token")
parser.add_argument('--group-chat-id', '-g', dest="group_chat_id",
help="Group chat ID to archive")
parser.add_argument('--direct-chat-id', '-d', dest="direct_chat_id",
help="Direct Message chat ID to archive")
parser.add_argument('--num-messages-per-request', '-n', default=20,
dest='num_messages_per_request',
help="Number of messages in each request. Max: 100.")
parser.add_argument('--output-dir', '-o', dest="output_dir",
help="Output directory to store archived content")
parser.add_argument('--save-global-avatars', action='store_true',
dest='save_global_avatars',
help="Use global avatars instead of " +
"chat specific user avatars")
args = parser.parse_args()
if not args.group_chat_id and not args.direct_chat_id:
print("Group chats")
print("===========")
chats = list_groups(args)
table_headers = ["Chat Name", "ID", "Number of messages"]
print(tabulate(chats, headers=table_headers))
print("")
print("Direct Messages")
print("===============")
chats = list_dms(args)
table_headers = ["Chat Name", "ID", "Number of messages"]
print(tabulate(chats, headers=table_headers))
else:
if args.group_chat_id:
messages, people, group_info, all_attachments = \
fetch_group_messages(args)
else:
messages, people, group_info, all_attachments = \
fetch_direct_messages(args)
output_dir = args.output_dir
if not output_dir:
output_dir = group_info['name']
output_dir = output_dir.replace('/', ' ')
os.makedirs(output_dir, exist_ok=True)
print("\nFetching avatars...")
avatars_path = os.path.join(output_dir, 'avatars/')
os.makedirs(avatars_path, exist_ok=True)
for k, v in tqdm(people.items()):
url = v['avatar_url']
if url:
r = requests.get("%s.avatar" % (url))
img_type = r.headers['content-type'].split('/')[1]
avatar_path = os.path.join(avatars_path,
'%s.avatar.%s' % (k, img_type))
with open(avatar_path, 'wb') as fp:
fp.write(r.content)
print("\nFetching attachments...")
attachments_path = os.path.join(output_dir, 'attachments/')
os.makedirs(attachments_path, exist_ok=True)
for att_url in tqdm(all_attachments):
file_name = att_url.split('/')[-1]
att_path = 'attachments/%s.%s' % (file_name, "*")
att_full_path = os.path.join(output_dir, att_path)
if len(glob.glob(att_full_path)) == 0:
r = requests.get(att_url)
img_type = r.headers['content-type'].split('/')[1]
att_path = 'attachments/%s.%s' % (file_name, img_type)
att_full_path = os.path.join(output_dir, att_path)
with open(att_full_path, 'wb') as fp:
fp.write(r.content)
print("\nPeople:")
table_headers = {
"id": "ID",
"name": "Name",
"avatar_url": "Avatar URL"
}
print(tabulate([dict({'id': k}, **v) for (k, v) in people.items()],
headers=table_headers))
# Save everything
people_file = os.path.join(output_dir, "people.json")
messages_file = os.path.join(output_dir, "messages.json")
group_info_file = os.path.join(output_dir, "group_info.json")
# Save people
with open(people_file, 'w', encoding='utf-8') as fp:
json.dump(people, fp, ensure_ascii=False, indent=2)
# Save messages
with open(messages_file, 'w', encoding='utf-8') as fp:
json.dump(messages, fp, ensure_ascii=False, indent=2)
# Save group information
with open(group_info_file, 'w', encoding='utf-8') as fp:
json.dump(group_info, fp, ensure_ascii=False, indent=2)
if __name__ == '__main__':
main()