-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
97 lines (65 loc) · 2.41 KB
/
common.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
#!/usr/bin/env python
import configparser
import json
import os
import requests
config = configparser.ConfigParser()
config.read(os.path.expanduser("~/.avast.ini"))
# Telegramfields
tutor1 = "0_13_20231012041710"
tutor2 = "0_14_20231012045321"
socioid = "0_16_20241120130245"
telegramfields = [tutor1, tutor2, socioid]
apiurl = f"https://{config['auth']['endpoint']}.playoffinformatica.com/api.php/api/v1.0"
headers = {"Content-Type": "application/json", "content-encoding": "gzip"}
endpoint = config["auth"]["endpoint"]
class BearerAuth(requests.auth.AuthBase):
def __init__(self, token):
self.token = token
def __call__(self, r):
r.headers["authorization"] = f"Bearer {self.token}"
return r
def gettoken(user=config["auth"]["username"], password=config["auth"]["password"]):
apiurl = (
f"https://{config['auth']['endpoint']}.playoffinformatica.com/api.php/api/v1.0"
)
# get token
loginurl = f"{apiurl}/login/colegi"
data = {"username": user, "password": password}
result = requests.post(loginurl, data=json.dumps(data), headers=headers)
return result.json()["access_token"]
def writejson(filename, data):
with open(f"data/{filename}.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
return True
def readjson(filename):
with open(f"data/{filename}.json", "r", encoding="utf-8") as f:
return json.load(f)
def addcategoria(token, socio, categoria):
"""Adds categoria to socio
Args:
token (str): token for accessing API (RW)
socio (int): Socio identifier
categoria (int): ID for category to modify
"""
headers = {"Authorization": f"Bearer {token}"}
categoriaurl = f"{apiurl}/colegiats/{socio}/modalitats"
data = {"idModalitat": categoria}
files = []
return requests.request(
"POST", categoriaurl, headers=headers, data=data, files=files
)
def delcategoria(token, socio, categoria):
"""Removes categoria from socio
Args:
token (str): token for accessing API (RW)
socio (int): Socio identifier
categoria (int): ID for category to modify
"""
headers = {"Authorization": f"Bearer {token}"}
categoriaurl = f"{apiurl}/colegiats/{socio}/modalitats/{categoria}"
data = {}
files = []
return requests.request(
"DELETE", categoriaurl, headers=headers, data=data, files=files
)