-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.py
52 lines (38 loc) · 1.2 KB
/
library.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
import random
import base64 # base64.urlsafe_b64encode
import time
import json
def load_config(loc="./config.json"):
with open(loc, "r") as infile:
return json.load(infile)
def create_seeded_hash(blur):
b = base64.b64encode(hex(random.randint(0, 1000))[2:])
t = base64.b64encode(hex(int(time.time() % 1000))[2:])
z = base64.b64encode(hex(blur)[2:])
return "{}{}{}".format(b, t, z)
def create_hash(blur):
x = hex(blur)[2:]
x = x.encode()
z = base64.b64encode(x)
# print("{}".format(z))
return str(z.decode())
def incr_redis(r, settings):
key = "{}_inc:int".format(settings["root"])
num = r.incr(key)
num = int(num)
return num
def set_redis(r, settings, blur, url, ttl=604800): # TTL = 7 days
_key = "{}:{}:url".format(settings["root"], blur)
_res = r.set(_key, url)
_ex = r.expire(_key, ttl)
# print(_res, _ex)
# key = "{}:hash".format(settings["root"])
# res = r.hset(key, blur, url)
return _res
def get_url(r, settings, blur):
_key = "{}:{}:url".format(settings["root"], blur)
_res = r.get(_key)
# print(_res)
# key = "{}:hash".format(settings["root"])
# res = r.hget(key, blur)
return _res