-
Notifications
You must be signed in to change notification settings - Fork 1
/
BLIH.py
126 lines (112 loc) Β· 3.39 KB
/
BLIH.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
import json
import hmac
import hashlib
import urllib.request
import urllib.parse
class Request():
def __init__(self, url, method, data):
self.done = False
self.url = url
self.method = method
self.data = bytes(json.dumps(data), "utf8")
self.error = None
self.make()
def make(self):
self.request = urllib.request.Request(url=self.url, method=self.method)
self.request.add_data(self.data)
self.request.add_header("Content-Type", "application/json")
def execute(self):
self.done = True
try:
self.result = urllib.request.urlopen(self.request)
except urllib.error.HTTPError as e:
data = json.loads(e.read().decode("utf8"))
self.error = {"code": e.code, "data": data}
return False
return True
def parse(self):
if self.done and not self.error and self.result.status == 200:
try:
raw_data = json.loads(self.result.read().decode("utf8"))
data = {"code": self.result.status, "data": raw_data}
except:
return None
return data
return None
class BLIH():
def __init__(self, login, token):
self.base = "https://blih.epitech.eu/"
self.login = login
self.token = bytes(token, "utf8")
with open("./BLIHAPI.json") as data:
self.actions = json.load(data)
def make_body(self, data):
hash_signature = hmac.new(self.token, msg=bytes(self.login, 'utf-8'), digestmod=hashlib.sha512)
if data:
hash_signature.update(bytes(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')), 'utf8'))
signature = hash_signature.hexdigest()
result = {"user": self.login, "signature": signature}
if data:
result["data"] = data
return result
def get_action(self, user_mode, user_action):
if user_mode in self.actions:
for action in self.actions[user_mode]:
if action["action"] == user_action:
return action
return None
def get_route(self, action, args):
find = action["route"].find("$")
index = 0
while find != -1:
action["route"] = action["route"].replace("$", args[index], 1)
index += 1
find = action["route"].find("$")
return action["route"]
def get_data(self, action, args):
index = 0
data = {}
if action["data"]:
for arg in action["data"]:
if len(args) > index:
data[arg["name"]] = args[index]
elif not arg["optional"] and arg["default"]:
data[arg["name"]] = arg["default"]
index += 1
if data == {}:
return None
return data
def check_args(self, action, route, body):
if action["arguments"]:
route_args = len(action["arguments"])
else:
route_args = 0
data_args = 0
if action["data"]:
for arg in action["data"]:
if not arg["optional"] and not arg["default"]:
data_args += 1
if len(route) < route_args or (action["data"] and (len(body) > len(action["data"]) or len(body) < data_args)):
return None
return action
def make_args(self, action, route=[], body=[]):
result = self.check_args(action, route, body)
if not result:
return None
final_route = self.base + self.get_route(action, route)
data = self.get_data(action, body)
final_body = self.make_body(data)
request = Request(final_route, action["method"], final_body)
return request
def execute(self, user_mode, user_action, route=[], body=[]):
action = self.get_action(user_mode, user_action)
if action:
request = self.make_args(action, route, body)
if not request:
return
status = request.execute()
if status:
result = request.parse()
else:
result = request.error
return result