-
Notifications
You must be signed in to change notification settings - Fork 0
/
pedatren.py
127 lines (109 loc) · 3.69 KB
/
pedatren.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
import requests
import json
import base64
import sys
from requests.auth import HTTPBasicAuth
from app import logger
import platform
class Login():
__url = 'https://api.pedatren.nuruljadid.app/'
# __url = 'http://207.148.75.98:3000/api/v1/'
def __init__(self):
try:
open("token.txt", "r")
except IOError:
open("token.txt", "w")
with open('token.txt', 'r') as f:
token = f.read()
self.__token = token
self.__url = Login.__url
@property
def headers(self):
header = {
'content-type': 'application/json',
'connection': 'keep-alive',
'User-Agent': 'Update Induk ' + platform.system() + ' ' + platform.node(),
'x-token': self.__token,
}
return header
@property
def url(self):
return self.__url
def login(self, username, password):
data = requests.get(self.url+'auth/login',
auth=(username, password), headers=self.headers)
if data.status_code == 200:
self.__token = data.headers['x-token']
with open('token.txt', 'w') as f:
f.write(self.__token)
else:
print("Username atau Password salah!")
sys.exit(0)
def cekLogin(self):
data = requests.get(self.url+'auth/login', headers=self.headers)
return data.status_code
@property
def token(self):
if self.cekLogin() != 200:
self.login()
return self.__token
def level(self):
user = self.token.split(".")[0]
user += "=" * ((4 - len(user) % 4) % 4)
level = json.loads(base64.b64decode(user))['scope'][1]
return level
@property
def urlUser(self):
lev = self.level()
if 'lembaga' in lev:
urlUser = "{}{}/".format(self.url, lev.replace('-', '/'))
else:
with open("token.txt", "w") as f:
f.write("")
print ("Mohon Maaf hanya untuk aku Lembaga")
sys.exit(0)
return urlUser
class Pedatren(Login):
def person(self, uuid):
with requests.get(self.url+'person/{}'.format(uuid), headers=self.headers) as f:
return json.loads(f.content)
def updateInduk(self, uuid, induk, id_lembaga, id_pendidikan, tanggal_masuk):
payloads = {
"nomor_induk": induk,
"id_lembaga": id_lembaga,
"tanggal_mulai": tanggal_masuk,
}
try:
updateSiswa = requests.put(
self.url+'person/{}/pendidikan/{}'.format(uuid, id_pendidikan),
data=json.dumps(payloads), headers=self.headers,
)
return updateSiswa.status_code
except Exception as e:
logger.exception(e)
def all_pelajar(self): # , kelas=None, jurusan=None, jenis_kelamin=None):
# try:
params = {
'page': '1',
'limit': '1000',
}
f = requests.get(self.urlUser+'pelajar',
headers=self.headers, params=params)
total = f.headers['x-pagination-total-page']
json_file = []
for r in range(1, int(total)+1):
params['page'] = str(r)
f = requests.get(self.urlUser+'pelajar',
headers=self.headers, params=params)
to_json = json.loads(f.content)
json_file.append(to_json)
json_file = [j for i in json_file for j in i]
return json_file
def cetakExcel(data):
from openpyxl import Workbook
wb = Workbook(write_only=True)
ws = wb.create_sheet()
sheet = wb.active
for i in data:
sheet.append(i)
wb.save("data_siswa.xlsx")