-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharchive.py
276 lines (203 loc) · 7.92 KB
/
archive.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
#!/usr/bin/env python3
import requests
import json
import os
import os.path
import argparse
import sys
from collections import OrderedDict
ATTACHMENTS_DIRECTORY = "attachments/"
PROFILES_DIRECTORY = "profiles/"
MESSAGES_FILE = "messages.json"
VERBOSE = False
DEBUG = False
BASEURL = "https://api.groupme.com/v3"
def main():
global VERBOSE
global DEBUG
parser = argparse.ArgumentParser()
parser.add_argument('-g', '--group', nargs=1, type=str, metavar='group', help='Group Name', required=True)
parser.add_argument('-t', '--token', nargs=1, type=str, metavar='token', help='GroupMe API Token', required=True)
parser.add_argument('-d', '--directory', nargs=1, type=str, metavar='directory', help='Base chat directory')
#parser.add_argument('-a', '--attachments', nargs=1, type=str, metavar='group', help='Directory to store attachments')
#parser.add_argument('-p', '--profile', nargs=1, type=str, metavar='group', help='Directory to store profile pictures')
#parser.add_argument('--no-store-attachments', action='store_true', help='Do not save attachments')
#parser.add_argument('--no-store-profile-pictures', action='store_true', help='Do not save profile pictures')
parser.add_argument('--verbose', action='store_true', help='Verbose output')
parser.add_argument('--debug', action='store_true', help='Debug output')
args = parser.parse_args()
# Assign vars
GROUP_NAME = args.group[0]
if GROUP_NAME == '':
eprint('Group Name must be provided')
sys.exit(1)
TOKEN = args.token[0]
if TOKEN == '':
eprint('Token must be provided')
sys.exit(1)
if args.directory:
OUTPUT_DIRECTORY = args.directory[0]
else:
# Base output directory needs trailing slash
OUTPUT_DIRECTORY = GROUP_NAME + '/'
VERBOSE = args.verbose
DEBUG = args.debug
# Make directories
directory = os.path.dirname(OUTPUT_DIRECTORY + ATTACHMENTS_DIRECTORY)
if not os.path.exists(directory):
os.makedirs(directory)
directory = os.path.dirname(OUTPUT_DIRECTORY + PROFILES_DIRECTORY)
if not os.path.exists(directory):
os.makedirs(directory)
# Check if overwriting message file
if os.path.isfile(OUTPUT_DIRECTORY + MESSAGES_FILE):
# File exists, notify and exit
eprint('Message file already exists')
sys.exit(1)
# Get group ID
groupid = getGroupID(GROUP_NAME, TOKEN)
# Failed to find group
if groupid == None:
return 1
# Get messages
messages = getMessages(groupid, TOKEN)
# Write messages file
vprint('Writing to message file: ' + OUTPUT_DIRECTORY + MESSAGES_FILE)
with open(OUTPUT_DIRECTORY + MESSAGES_FILE, 'w') as outfile:
json.dump({'messages' : messages}, outfile, indent=4, separators=(',', ': '))
# Write attachments
saveAttachments(messages, OUTPUT_DIRECTORY + ATTACHMENTS_DIRECTORY)
# Write profile images
saveProfiles(messages, OUTPUT_DIRECTORY + PROFILES_DIRECTORY)
# Get the numeric group ID for a given group name
# Returns None if not found
# Requests groups page by page until found or end
def getGroupID(groupName, token):
PER_PAGE = 200
PAGE = 1
groupRequestURL = BASEURL + '/groups'
r = addParameters(groupRequestURL, {
'token' : token})
req_params = {'per_page' : PER_PAGE}
vprint('Requesting groups: ' + r)
result = requests.get(r, params = req_params)
jsonresult = result.json()
# Check if authorized
if result.status_code == 401:
eprint('Unauthorized: invalid token')
return None
# Check if invalid
if result.status_code != 200 or result == None:
eprint('getGroupID response invalid')
return None
if jsonresult['response'] == None:
eprint('getGroupID json result invalid')
return None
# Find the group by name
for group in jsonresult['response']:
if group['name'] == groupName:
#print(group['name'] + ' : ' + group['id'])
vprint('Found group ID: ' + group['id'])
return group['id']
eprint('Failed to find group: ' + groupName)
return None
# Get all messages for a group ID in JSON
def getMessages(groupID, token):
requestURL = BASEURL + '/groups/' + str(groupID) + '/messages'
# Make first request
vprint('Making first request: ' + requestURL)
r = addParameters(requestURL, {
'token' : token})
req_params = {'limit' : 100}
result = requests.get(r, params = req_params)
jsonresult = result.json()
res = jsonresult
count = 1
while jsonresult['response'] != None:
vprint('Making request ' + str(count) + ': ' + requestURL)
# Add to running results
messages = jsonresult['response']['messages']
res['response']['messages'] += messages
# Get earliest ID
lastid = messages[-1]['id']
vprint(str(count) + ' : ' + str(lastid))
req_params['before_id'] = lastid
# Make next request
result = requests.get(r, params = req_params)
# Check if last message
if result.status_code == 304 or result == None:
vprint('End of messages')
break
jsonresult = result.json()
count +=1
# De-duplicate
vprint('Deduplicating messages')
d = res['response']['messages']
res['response']['messages'] = [i for n, i in enumerate(d) if i not in d[n + 1:]]
# Return
return res
# Save attachments
def saveAttachments(messages, directory):
vprint('Saving attachments in: ' + directory)
for message in messages['response']['messages']:
if message['attachments'] != []:
for attachment in message['attachments']:
if attachment['type'] != 'image':
vprint("Unsupported type " + attachment['type'] + " on: " + message['id'])
continue
if attachment['type'] == 'image':
url = attachment['url']
filename = url[url.rfind("/") + 1:]
with open(directory + filename, 'wb') as handle:
response = requests.get(url, stream=True)
if not response.ok:
eprint(response)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
vprint('Wrote ' + filename)
return
# Save attachments
def saveProfiles(messages, directory):
vprint('Saving profiles in: ' + directory)
avatarlist = []
for message in messages['response']['messages']:
temp = message['avatar_url']
if temp != None:
if not temp in avatarlist:
avatarlist.append(temp)
for avatar in avatarlist:
url = avatar
filename = url[url.rfind("/") + 1:]
vprint(url + " : " + filename)
with open(directory + filename, 'wb') as handle:
response = requests.get(url, stream=True)
if not response.ok:
eprint(response)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
vprint('Wrote ' + filename)
return
# Add a dictionary of key / value pairs
def addParameters(baseurl, paramDict):
res = baseurl + ''
for key in paramDict.keys():
res += '?' + str(key) + '=' + str(paramDict[key])
return res
# Add an HTTP ? = key value parameter to a request URL
def addParameter(baseurl, paramID, paramValue):
return baseurl + '?' + paramID + '=' + paramValue
# Print verbose
def vprint(*args, **kwargs):
global VERBOSE
if VERBOSE:
print(*args, file=sys.stdout, **kwargs)
# Print an error
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# Run main
if __name__ == '__main__':
main()