forked from carolineec/informative-drawings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_helper.py
268 lines (211 loc) · 10.3 KB
/
redis_helper.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
import json
import time
from datetime import datetime
import io
import os
import redis
class RedisHelper():
def __init__(self, settings):
self.r = redis.StrictRedis(host=settings.Redis.HOST, port=settings.Redis.PORT,
db=settings.Redis.DB, password=settings.Redis.PASSWORD,
decode_responses=True)
# 주어진 key로 시작되는 값의 리스트을 리턴함
def hscan(self, map_name, key = ""):
result_list = []
keys = self.r.hkeys(map_name)
if key == "": return keys
for k in keys:
if k.startswith(key):
result_list.append(k)
return result_list
def add(self, key, value):
if type(value) != str:
value = json.dumps(value, ensure_ascii=False)
self.r.rpush(key, value)
def list(self, key, start_index = 0, end_index = -1):
return self.r.lrange(key, start_index, end_index)
#def get(self, map_name, key = None):
def get(self, *args):
if len(args) == 2:
map_name = args[0]
key = args[1]
value = self.r.hget(map_name, key)
else:
key = args[0]
value = self.r.get(key)
return value
# get -> json.loads
def get_json(self, *args):
value_json = self.get(*args)
if value_json:
value_json = json.loads(value_json)
return value_json
# 해당 map_name의 전체 dictionary를 리턴
def get_all(self, map_name):
dict_value = self.r.hgetall(map_name)
return dict_value
# get_all 중에 key, name에 해당하는 dict 값을 리턴
def get_target_dict(self, map_name, target_key, target_value):
all_dict = self.get_all(map_name)
for _, dict_value in all_dict.items():
dict_value = json.loads(dict_value)
if dict_value.get(target_key) == target_value:
return dict_value
return None
# 해당 map에 대해 value값을 주면 key값을 리턴함
def rget(self, map_name, value):
result = self.r.hgetall(map_name)
for k, v in result.items():
if type(k) == bytes: k = k.decode()
if type(v) == bytes: v = v.decode()
if v == value:
return k
return None
#def set(self, map_name, map):
def set(self, *args):
if len(args) == 3: #hset
map_name = args[0]
key = args[1]
value = args[2]
if type(value) != str:
value = json.dumps(value, ensure_ascii=False)
self.r.hset(map_name, key, value)
elif type(args[1]) == dict: #hmset
map_name = args[0]
map = args[1]
self.r.hmset(map_name, map)
else: #set
key = args[0]
value = args[1]
if type(value) != str:
value = json.dumps(value, ensure_ascii=False)
self.r.set(key, value)
def delete(self, *args):
if len(args) == 2: #hdel
map_name = args[0]
key = args[1]
self.r.hdel(map_name, key)
else: #delete
key = args[0]
self.r.delete(key)
# json value에서 특정키에 대한 값을 가져옴
def get_dict_value(self, redis_key, key, dict_key, default_value = None):
dict_value = self.get(redis_key, key)
if dict_value:
print(dict_value)
dict_value = json.loads(dict_value)
value = dict_value.get(dict_key, None)
if value:
return value
return default_value
def set_dict_value(self, redis_key, key, dict_key, value):
dict_value = self.get(redis_key, key)
if dict_value:
dict_value = json.loads(dict_value)
else:
dict_value = {}
dict_value[dict_key] = value
self.set(redis_key, key, json.dumps(dict_value, ensure_ascii=False))
def del_dict_value(self, redis_key, key, dict_key):
dict_value = self.get(redis_key, key)
if dict_value:
dict_value = json.loads(dict_value)
else:
dict_value = {}
del dict_value[dict_key]
self.set(redis_key, key, json.dumps(dict_value, ensure_ascii=False))
def get_config(self, config_key, json_value = False, default_value = None, config_type = "talk"):
key = self.HEUMTALK_CONFIG
if config_type == "noti":
key = self.NOTI_CONFIG
value = self.get(key, config_key)
if json_value and value != None:
value = json.loads(value)
if value == None:
return default_value
else:
return value
def set_config(self, config_key, value, json_value = False, config_type = "talk"):
key = self.HEUMTALK_CONFIG
if config_type == "noti":
key = self.NOTI_CONFIG
if json_value:
value = json.dumps(value, ensure_ascii = False)
self.set(key, config_key, value)
# redis 백업
def backup(self, folder_path = r"D:\NEIL\REDIS_백업"):
timestamp = datetime.now().strftime("%Y%m%d")
backup_folder = os.path.join(folder_path, timestamp)
print(backup_folder)
if not os.path.exists(backup_folder):
os.makedirs(backup_folder)
target_list = self.r.scan_iter("*")
for target in target_list:
print(target, self.r.type(target))
if self.r.type(target) == "hash":
result = self.r.hgetall(target)
json_obj = {}
for k, v in result.items():
# k = k.decode()
# v = v.decode()
json_obj[k] = v
else:
# type이 hash가 아닌 key는 skip함
continue
target = target.replace(":", "_") # 파일명을 위해 ":"->"_"로 변경
json_path = os.path.join(backup_folder, target + ".json")
with io.open(json_path, 'w', encoding='utf-8') as outfile:
outfile.write(json.dumps(json_obj, indent=3, ensure_ascii=False))
# 백업한 json 파일로부터 restore
def restore(self):
backup_file = r"D:\NEIL\REDIS_백업\2017-12-19\heumdesk_member.json"
REDIS_HEUMDESK_REGISTER_BUSINESS = "heumdesk:register_business"
json_string = common_util.read_text(backup_file)
member_dict = json.loads(json_string)
for id, line in member_dict.items():
company_dict = json.loads(line)
if "이창근" != company_dict["user_name"]:
print(id, line)
self.set(REDIS_HEUMDESK_REGISTER_BUSINESS, id, line)
# redis 일괄 값 등록
def put_values(self):
json_text = common_util.read_text(r"D:\GIT\neil_work\slack_tool\test\heum_token.txt")
token_list = json.loads(json_text)
for item in token_list:
name = item["name"]
token = item["token"]
print(name, token)
self.set(self.REDIS_SLACK_TOKEN, name, token)
def test(self):
#result = self.get(self.REDIS_SLACK_TOOL_MAIL_TO_SLACK)
#result = self.r.hgetall(self.REDIS_SLACK_TOOL_MAIL_TO_SLACK)
result = '{"[email protected]": "C4XGFF50C", "[email protected]": "C4XGFERNY", "[email protected]": "C4YNJ3AF9", "[email protected]": "C4Y6AF13Q", "[email protected]": "C4XFA3QAE", "[email protected]": "C4Y5Y964W", "[email protected]": "C4XJAR5EZ", "[email protected]": "C4XFA723C", "[email protected]": "C4Y6ADWSJ", "[email protected]": "C4Y6ANQ1L", "[email protected]": "C4ZAD1TT9", "[email protected]": "C4WPZ3E72", "[email protected]": "C4WQB6YSU", "[email protected]": "C4Y68HNK1", "[email protected]": "C4XFA4RGA", "[email protected]": "C4XFNA9R8", "[email protected]": "C4WQT4H25", "[email protected]": "C4Y6AEJ5C", "[email protected]": "C4WPZ2T6C", "[email protected]": "C4Y5Y92S2", "[email protected]": "C4XHYPFQV", "[email protected]": "C4WR56H89", "[email protected]": "C4WQT2E3T", "[email protected]": "C4WPZ23JL", "[email protected]": "C4WR57KS5", "[email protected]": "C4Y6AELKG", "[email protected]": "C4Y6LMP3R", "[email protected]": "C4XJB47RT", "[email protected]": "C4Y6AE9L6", "[email protected]": "C4WQB7E5N", "[email protected]": "C4WQT1VPB", "[email protected]": "C4XDAK403", "[email protected]": "C4XFN958A", "[email protected]": "C4Y6AEUKY", "[email protected]": "C4XDNS275", "[email protected]": "C4WPZ2PUY", "[email protected]": "C4XFA5682", "[email protected]": "C4XJAR86R", "[email protected]": "C4XFN9TD0", "[email protected]": "C4XHYMESZ", "[email protected]": "C4WPZ37S4", "[email protected]": "C4XJASPAR", "[email protected]": "C53R1S03Y", "[email protected]": "C532SG7MJ", "[email protected]": "C4XJB466R", "[email protected]": "C4XDAKKA7", "[email protected]": "C4XGTT8VA", "[email protected]": "C4XJAR86R"}'
result = json.loads(result)
print(type(result))
print(result["[email protected]"])
self.r.hmset(self.REDIS_SLACK_TOOL_MAIL_TO_SLACK_1, result)
if __name__ == "__main__":
# import common_util
import time
import config as settings
redisHelper = RedisHelper(settings)
# print(redisHelper.r.getrange(0, 2))
# exit()
# for i in range(10):
# timestamp = common_util.get_timestamp()
# dict_value = {
# "timetamp": timestamp
# }
# ret = redisHelper.add("timetamp", dict_value)
# print(ret)
# time.sleep(1)
# ret = redisHelper.list("timetamp", start_index = -3)
# print(ret)
ret = redisHelper.get_all("01090014461")
print(ret)
print(len(ret))
exit()
redisHelper.set("01090014461", "photo", "https://files.slack.com/files-pri/T05TH4T3WMD-F05TS025GHM/download/_______crop.jpg")
redisHelper.set("01090014461", "anime", "https://files.slack.com/files-pri/T05TH4T3WMD-F05TS025GHM/download/_______crop.jpg")
redisHelper.set("01090014461", "drawing", "https://files.slack.com/files-pri/T05TH4T3WMD-F05TS025GHM/download/_______crop.jpg")
redisHelper.set("01090014461", "svg", "https://files.slack.com/files-pri/T05TH4T3WMD-F05TS025GHM/download/_______crop.jpg")