-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_client.py
executable file
·95 lines (70 loc) · 3.21 KB
/
redis_client.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
import redis
import logging
import logging.config
redis_client: redis.Redis = redis.Redis(
host='localhost', port=6379, db=0, decode_responses=True)
CHAT_LOCATION_KEY = "location"
GEOS_KEY = "geos"
DESCRIPTION_KEY = "desc"
logging.config.fileConfig('logging.conf')
log = logging.getLogger('grassroot')
# ***************** #
# Current location #
# ***************** #
def set_location(chat_id, longitude, latitude):
return execute_redis_cmd(chat_id, 'HSET {} {} {},{}'.format(CHAT_LOCATION_KEY, chat_id, longitude, latitude))
def get_location(chat_id):
res = execute_redis_cmd(
chat_id, 'HGET {} {}'.format(CHAT_LOCATION_KEY, chat_id))
location = res.split(',')
if (len(location) < 2):
raise ValueError(
'Invalid format of location={} for key={}'.format(location, chat_id))
return location
# ***************** #
# `GEOADD geos 56.304558 38.135395 "group_name:description of the group_name"`
def link_group(group_name, desc, admin_id, longitude, latitude):
geoadd_res = execute_redis_cmd(admin_id, 'GEOADD {} {} {} {}'.format(
GEOS_KEY, longitude, latitude, group_name))
if geoadd_res < 1:
return geoadd_res
set_group_description(admin_id, group_name, desc)
log.info('[id=%d] successfully created group=%s admin_id=%d longitude=%f latitude=%f'.format(
admin_id, group_name, admin_id, longitude, latitude))
return add_admin(admin_id, group_name)
def search_groups_within_radius(chat_id, longitude, latitude, radius=100):
metric = 'm'
resp = execute_redis_cmd(chat_id, 'GEORADIUS {} {} {} {} {} WITHDIST'.format(
GEOS_KEY, longitude, latitude, radius, metric))
return resp
def delete_group_link(group, admin_id):
admins_ids = get_admins_ids_by(admin_id, group)
log.info('[id=%d] delete_group_link(group=%s admin_id=%d) admins: %s',
admin_id, group, admin_id, admins_ids)
if (str(admin_id) not in admins_ids):
log.warning('[id=%d] Cant delete group=%s by user=%d: He is not an admin of the group!',
admin_id, group, admin_id)
return 0
del_admins_res = execute_redis_cmd(admin_id, 'DEL {}:admins'.format(group))
if del_admins_res == 0:
return 0
del_geos_res = execute_redis_cmd(
admin_id, 'ZREM {} {}'.format(GEOS_KEY, group))
return del_geos_res
def get_admins_ids_by(chat_id, group):
return execute_redis_cmd(chat_id, 'SMEMBERS {}:admins'.format(group))
def add_admin(admin_id, group):
return execute_redis_cmd(admin_id, 'SADD {}:admins {}'.format(group, admin_id))
def set_group_description(admin_id, group_name, desc):
return execute_redis_cmd(admin_id, 'HSET {} {} {}'.format(DESCRIPTION_KEY, group_name, desc))
def get_description(admin_id, group_name):
return execute_redis_cmd(admin_id, 'HGET {} {}'.format(DESCRIPTION_KEY, group_name))
def execute_redis_cmd(admin_id, cmd):
""" Redis responses: 1 - ok, 0 - already exists, -1 - error """
try:
res = redis_client.execute_command(cmd)
log.info('[id=%d] redis cmd: `%s` res: `%s`', admin_id, cmd, res)
return res
except redis.exceptions.ResponseError as err:
log.error('[id=%d] cmd: `%s` Exception: `%s`', admin_id, cmd, err)
return -1