This repository was archived by the owner on Dec 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunacs.py
More file actions
300 lines (263 loc) · 13.4 KB
/
unacs.py
File metadata and controls
300 lines (263 loc) · 13.4 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import re
import sys
import json
import time
import random
import asyncio
import aiohttp
import aiofiles
import requests
import pandas as pd
from tqdm import tqdm
from pathlib import Path
from bs4 import BeautifulSoup
from tools.config import Config
from tools.profile import Profile
from tools.curriculum import Curriculum
from tools.kiyos_auth import AtilimAuth
from tqdm.asyncio import tqdm as async_tqdm
from tools.helpers import user_interactions, select_from_response_data
from tools.exceptions import NoCoursesAvailableError, NotGraduatedError
class Unacs:
@staticmethod
def login():
session = AtilimAuth().login()
AtilimAuth().load_cookies(session, Config.unacs_cookie_keys, 'unacs')
session.headers.update(
{
'origin': Config.kiyos_url,
'referer': Config.kiyos_url
}
)
if 'Connection' in session.headers:
del session.headers['Connection']
saml_page = session.get(Config.unacs_login_url)
saml_page_tree = BeautifulSoup(saml_page.content, 'html.parser')
saml_value = saml_page_tree.find('input', attrs={'name': 'SAMLResponse'})['value']
relay_state_value = saml_page_tree.find('input', attrs={'name': 'RelayState'})['value']
payload = {'SAMLResponse': saml_value, 'RelayState': relay_state_value}
unacs_auth_page = session.post(Config.unacs_auth_url, data=payload, allow_redirects=False)
auth_token_url = unacs_auth_page.headers['Location']
token = re.sub(pattern=r'^.*tokenreader\?t=', repl='', string=auth_token_url)
session.headers.update({'Authorization': f'Bearer {token}'})
AtilimAuth().save_cookies(session, Config.unacs_cookie_keys, 'unacs')
return session
@staticmethod
def get_opened_area_elective_courses():
_, __, department = Profile.get_profile_data()
department = ' '.join(department.split()[:2])
print(f'You are studying in {department}. Do you want to keep going with that?')
action = user_interactions()
if action == 0:
major_curr_filepath = Config.get_curriculum_filepath(department)
if major_curr_filepath.exists():
print(f'\nCurriculum file found at {major_curr_filepath}. Do you want to update it?')
major_action = user_interactions()
if major_action == 0:
curriculum_webpage = str(input('Enter Curriculum Webpage URL: '))
Curriculum(curriculum_webpage).download()
else:
curriculum_webpage = str(input('Enter Curriculum Webpage URL: '))
Curriculum(curriculum_webpage).download()
department_name, area_elective_courses_ids = Curriculum.load_curriculum_data(major_curr_filepath)
else:
curriculum_webpage = str(input('Enter Curriculum Webpage URL: '))
user_req_department = Curriculum(curriculum_webpage).get_department_name()
curriculum_filepath = Config.get_curriculum_filepath(user_req_department)
if curriculum_filepath.exists():
print(f'Curriculum file found at {curriculum_filepath}. Do you want to update it?')
alter_action = user_interactions()
if alter_action == 0:
Curriculum(curriculum_webpage).download()
else:
Curriculum(curriculum_webpage).download()
department_name, area_elective_courses_ids = Curriculum.load_curriculum_data(curriculum_filepath)
session = Unacs.login()
session.headers['referer'] = Config.unacs_opened_courses_referer_url
terms_list = session.get(Config.unacs_term_courses_url)
if terms_list.status_code == 403:
raise ConnectionError('Have you graduated? If your answer is yes, you cannot reach the website.')
if terms_list.json()['success'] is False:
raise ConnectionError('Something goes wrong. Try again...')
current_term_card = terms_list.json()['responseData']
# current_term_name_tr = 'ad'
current_term_name_en, current_term = select_from_response_data(current_term_card, 'aD_EN', 'id')
temp_file = Config.get_unacs_current_term_opened_courses_temporary_filepath(department_name, current_term)
if temp_file.exists():
temp_file.unlink(missing_ok=True)
test_payload = {
'BOLUM_ID': 141, # mechatronics
'DERS_ID': -1,
'DONEM_ID': current_term,
'FAKULTE_ID': 2, # engineering
}
test_request = session.post(Config.unacs_opened_courses_url, json=test_payload)
if not test_request.json()['responseData']:
raise NoCoursesAvailableError(f'There are no available courses in {current_term_name_en}')
with (tqdm(total=len(area_elective_courses_ids),
desc=f'Fetching Courses', position=0) as pbar):
for course_id in area_elective_courses_ids:
payload = {
'BOLUM_ID': -1,
'DERS_ID': course_id,
'DONEM_ID': current_term,
'FAKULTE_ID': -1,
}
course = session.post(Config.unacs_opened_courses_url, json=payload)
if course.status_code == 200:
course = course.json()['responseData']
if course:
with open(temp_file, 'a+', encoding='utf-8') as f:
json.dump(course, f, ensure_ascii=False, indent=4)
wait_time = random.uniform(1.0, 1.8)
info = f'Waiting for {wait_time:.2f} seconds before the next request...'
pbar.set_postfix(info=info)
time.sleep(wait_time)
pbar.update()
area_elective_csv_file = Config.get_unacs_area_elective_opened_courses_csv_filepath(department_name,
current_term)
textfile_name = Config.get_unacs_area_elective_opened_courses_txt_filepath(department_name, current_term)
if area_elective_csv_file.exists():
area_elective_csv_file.unlink(missing_ok=True)
textfile_name.unlink(missing_ok=True)
with open(temp_file, 'r', encoding='utf-8') as f:
course = re.sub(pattern=r'\]\[', repl=',', string=f.read())
opened_courses = json.loads(course)
df = pd.DataFrame(opened_courses)
df.to_csv(area_elective_csv_file, index=False)
course_codes = df['derS_KODU']
course_names = df['derS_ADI']
sections = df['subE_NO']
teachers = df['ogretiM_UYELER']
teacher_type = df['ogretiM_UYE_PERSONEL_TUR']
teacher_email = df['oU_EMAIL']
quotas = df['toplaM_KONTENJAN']
course_credits = df['kredi']
course_ects_credits = df['akts']
for (
course_code,
course_name,
section,
teacher,
teacher_type,
teacher_email,
quota,
course_credit,
course_ects_credit,
) in zip(
course_codes,
course_names,
sections,
teachers,
teacher_type,
teacher_email,
quotas,
course_credits,
course_ects_credits,
):
if department_name in str(quota):
try:
pattern = (
rf'{department_name}\(Lisans\)\(İngilizce\) : (\d+) / (\d+)'
)
kota = re.search(pattern, quota).group(0)
kota = kota.split('/')[1].strip()
output_opened = (
f'{course_code}|{course_name}|0/{kota}|{section}|{teacher}|{teacher_type}|'
f'{teacher_email}|{course_credit} credit|{course_ects_credit} akts/ects'
)
with open(
textfile_name, 'a+', encoding='utf-8'
) as textfile_true:
textfile_true.write('-' * len(output_opened) + '\n')
textfile_true.write(output_opened + '\n')
textfile_true.write('-' * len(output_opened) + '\n')
except Exception as e:
print(
f'Skipping Exception: {e} in {__file__} line {sys.exc_info()[-1].tb_lineno}'
)
pass
else:
output_not_opened = (
f'Opened without Quotas for your Department: {course_code}|{course_name}|'
f'{section}|{teacher}|'
f'{teacher_type}|{teacher_email}|{course_credit} '
f'credit|{course_ects_credit} akts/ects'
)
with open(textfile_name, 'a+', encoding='utf-8') as textfile_false:
textfile_false.write(output_not_opened + '\n')
print('You can find the available area elective courses in the files mentioned below.')
print(f'TXT File Created at {textfile_name}')
print(f'CSV File Created at {area_elective_csv_file}')
temp_file.unlink()
@classmethod
def download_graduation_photos(cls):
filepath = Config.get_unacs_graduation_photos_folderpath()
session = cls.login()
session.headers.update(
{
'origin': Config.unacs_url,
'referer': Config.unacs_url,
'priority': 'u=1, i',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept': 'application/json, text/plain, */*'
}
)
check_graduate = session.get(Config.unacs_check_graduation_student_url)
if check_graduate.status_code == 200:
is_graduate = check_graduate.json()['responseData']
if is_graduate is False:
raise NotGraduatedError('You must have attended the graduation ceremony to download photos !')
file_names_server = session.get(Config.unacs_graduation_photos_thumbnails_url)
if file_names_server.status_code != 200:
raise ConnectionError(f'Failed to fetch thumbnail urls with {file_names_server.status_code} status code')
if file_names_server.json()['success'] is False:
raise ConnectionError('Something goes wrong. Try Again...')
file_name_list = file_names_server.json()['responseData']
# Delete already downloaded files from server response
existing_files = {f.name for f in filepath.iterdir() if f.is_file()}
file_names = [file for file in file_name_list if Path(file).name not in existing_files]
if len(file_names) == 0:
print('All photos have been downloaded!')
return
async def download_links(url: str, a_session, progress_bar, semaphore):
async with semaphore:
a_session.headers.update({'referer': '/mezuniyet-fotograf'})
filename = url.split('/')[-1]
response = await a_session.get(url)
if response.status == 401:
async_tqdm.write('Session expired. Re-logging...')
session = cls.login()
a_session.headers.update(session.headers)
response = await a_session.get(url)
if response.status == 429:
async_tqdm.write('Too many requests. Waiting for a while...')
await asyncio.sleep(random.uniform(4, 7))
response = await a_session.get(url)
try:
if response.status == 200:
image_data = await response.read()
async with aiofiles.open(f'{filepath}/{filename}', 'wb') as file:
await file.write(image_data)
wait_time = random.uniform(1, 2)
info = f'Waiting for {wait_time:.2f} seconds before the next request...'
progress_bar.set_postfix(info=info)
progress_bar.update(1)
await asyncio.sleep(wait_time)
else:
async_tqdm.write(f'Failed to download image from {url} with status {response.status}')
except Exception as e:
async_tqdm.write(f"Error fetching {url}: {e}")
async def get_graduation_photos():
semaphore = asyncio.Semaphore(8)
async with aiohttp.ClientSession() as async_session:
sync_sess_headers = session.headers
async_session.headers.update(sync_sess_headers)
tasks = []
with async_tqdm(total=len(file_names)) as pbar:
for file_name in file_names:
url = f'{Config.unacs_graduation_photo_by_name_url}{file_name}'
task = asyncio.create_task(download_links(url, async_session, pbar, semaphore))
tasks.append(task)
await asyncio.gather(*tasks)
asyncio.run(get_graduation_photos())