-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
161 lines (114 loc) · 4.05 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
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
#!/usr/bin/env python
import configparser
import json
import os
import dateutil.parser
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]
fechacambio = "0_17_20250221121130"
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, extra=False):
"""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}
if extra:
data.update(extra)
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
)
def escribecampo(token, socioid, campo, valor=""):
"""Escribe campo personalizado de socio
Args:
token (_type_): Token para operaciones
socioid (_type_): idAssociat
campo (_type_): Campo personalizado
valor (_type_): Valor a establecer o vacío para borrar
Returns:
_type_: _description_
"""
comurl = f"{apiurl}/colegiats/{socioid}/campsdinamics"
headers = {"Authorization": f"Bearer {token}"}
data = {f"{campo}": "{valor}"}
files = []
return requests.request("PUT", comurl, headers=headers, data=data, files=files)
def calcular_proximo_recibo(fecha):
"""_summary_
Args:
fecha (datetime): Fecha for today
Returns:
str: fecha
"""
meses_cobro = sorted(
set([9, 11, 1, 3, 5])
) # Meses de cobro (septiembre, noviembre, enero, marzo, mayo)
fecha = dateutil.parser.parse(fecha)
dia = fecha.day
mes = fecha.month
año = fecha.year
if dia < 5:
dia_cobro = 5
if mes in meses_cobro:
return f"{dia_cobro:0d2}/{mes:02d}/{año}"
else:
mes_cobro = next((m for m in meses_cobro if m > mes), None)
if mes_cobro is None:
mes_cobro = meses_cobro[0]
año += 1
return f"{dia_cobro:02d}/{mes_cobro:02d}/{año}"
else:
mes_cobro = next((m for m in meses_cobro if m > mes), None)
if mes_cobro is None:
mes_cobro = meses_cobro[0]
año += 1
return f"05/{mes_cobro:02d}/{año}"