-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_zulip_data.py
180 lines (136 loc) · 5.35 KB
/
get_zulip_data.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May 17 11:05:48 2022
pull in image pngs on firebase indexed on zulip thread, and get associated emoji reactions
@author: jack
"""
import os
# import time
import zulip
import wget
import pickle
# make database files
with open('config.ini', 'r') as f:
config = f.read()
image_data_path = config.split('image_database_directory=')[1].split('\n')[0]
zuliprc_name = config.split('zuliprc_name=')[1].split('\n')[0]
classifybot_name = config.split('classifybot_name=')[1].split('\n')[0]
scanbot_address = config.split('scanbot_address=')[1].split('\n')[0]
scanbot_stream = config.split('scanbot_stream=')[1].split('\n')[0]
try:
os.mkdir(image_data_path)
except:
print('image_data dir already exists')
if not 'batch_0' in os.listdir(image_data_path):
os.mkdir(os.path.join(image_data_path, 'batch_0'))
## specify zuliprc file
zuliprc_path = os.path.join(os.getcwd(), zuliprc_name)
client = zulip.Client(config_file=zuliprc_path)
## specify hard-coded classifybot name
# classifybot_name = 'classifybot'
## specify stream & scanbot email address to read messages:
request = {}
## define the narrow
request['narrow'] = [
{"operator": "sender", "operand": scanbot_address},
{"operator": "stream", "operand": scanbot_stream},
# {"operator": "topic", "operand": "survey"},
]
## get id of first unread message
request['anchor'] = 'first_unread'
request['num_before'] = 0
request['num_after'] = 0
##go for oldest
# request['anchor'] = 'oldest'
# request['num_before'] = 0
# request['num_after'] = 1
result = client.get_messages(request)
print('first_unread anchor')
print(result)
first_unread_id = result['messages'][0]['id']
## get id of newest message
request['anchor'] = 'newest'
request['num_before'] = 1
request['num_after'] = 0
result = client.get_messages(request)
print('newest anchor')
print(result)
newest_message_id = result['messages'][0]['id']
to_mark_read = []
keep_unread = []
for message_id in range(first_unread_id, newest_message_id, 100):
request['anchor'] = message_id
request['num_before'] = 0
request['num_after'] = 100
## check which batch folder to put images in
folders = os.listdir(image_data_path)
max_batch_index = 0
for name in folders:
if name.split('_')[0] == 'batch':
batch_index = int(name.split('_')[1])
if batch_index > max_batch_index:
max_batch_index = batch_index
batch_path = os.path.join(image_data_path, 'batch_' + str(max_batch_index))
if len(os.listdir(batch_path)) > 256:
batch_path = os.path.join(image_data_path, 'batch_' + str(max_batch_index+1))
os.mkdir(batch_path)
pickle.dump(True, open('retrain_flag.pkl', 'wb'))
try:
batch_labels = pickle.load(open(os.path.join(batch_path,'file_labels.pkl'), 'rb'))
except:
print('no batch labels ' + batch_path)
batch_labels = {}
results = client.get_messages(request)
if results['result'] == 'success':
for message in results['messages']:
if '<div class="message_inline_image">' in message['content'] and 'read' not in message['flags']:
url = message['content'].split('<a href="')[1].split('">')[0].replace('&', '&')
labels = []
for reaction in message['reactions']:
if reaction['user']['full_name'] != classifybot_name:
labels.append(reaction['emoji_name'])
if len(labels) > 0 and '.png' in url:
try:
if not url.split('/scanbot/')[1].split('?')[0] in os.listdir(batch_path):
filename = wget.download(url=url, out=batch_path)
keyname = str(os.path.join(batch_path.split('/')[-1], filename.split('/')[-1]))
batch_labels[keyname] = labels
except:
pass
## mark the message as read
to_mark_read.append(message['id'])
else:
keep_unread.append(message['id'])
else: ## message doesn't have an image in it; mark read
to_mark_read.append(message['id'])
else:
print(results)
## dump the batch_labels
pickle.dump(batch_labels, open(os.path.join(batch_path, 'file_labels.pkl'), 'wb'))
## now mark all read
if len(to_mark_read) > 0:
mkrd_request = {
'messages': to_mark_read,
'op': 'add',
'flag': 'read',
}
result = client.update_message_flags(mkrd_request)
print(result)
# ## put a sleep in here to not hit the API rate limit
# print('the slow loop to add eyes to all mark-read files')
# for msg_id in to_mark_read:
# react_request = {
# 'message_id': msg_id,
# 'emoji_name': 'eyes',
# }
# result = client.add_reaction(react_request)
# time.sleep(.3)
## mark messages that haven't been labelled unread
mkunread_request = {
'messages': keep_unread,
'op': 'remove',
'flag': 'read',
}
result = client.update_message_flags(mkunread_request)
print(result)