парсер получше

This commit is contained in:
синечка ♡ 2025-03-01 02:22:35 +03:00 committed by GitHub
parent d4796a82ba
commit fa2097ebbf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,8 +7,15 @@ import openpyxl
from openpyxl import load_workbook
import config
def trim(string: str) -> str:
if string == '': return ''
try:
while string[0] == ' ': string = string[1: ]
while string[-1] == ' ': string = string[ :-1]
except: pass
return string
def update(group: str, course: str):
def update(group: str, course: str, debug: bool = false):
files = json.load(open('files.json', 'r', encoding='utf-8'))
url = files[group][course]
@ -22,12 +29,19 @@ def update(group: str, course: str):
result = {}
book = load_workbook('./temp/table.xlsx', data_only = True).active
for c in 'CDEFGHIJKLMNOPQRSTUVWXYZ':
try: typename = book[f'{c}8'].value.replace(' ','')
except: continue
usedfrom = 8
try:
typename = book[f'{c}8'].value.replace(' ','') # type: ignore
if not typename: raise Exception
except:
usedfrom = 9
try: typename = book[f'{c}9'].value.replace(' ','') # type: ignore
except: continue
if not typename: continue
# if c != 'D': continue #test
print(f'├── {typename}')
print(f'├── {typename}') if debug else ...
result[typename] = {}
last_day = ''
@ -36,11 +50,13 @@ def update(group: str, course: str):
this = {}
for r in range(9, 99):
day = book[f'A{r}'].value
for r in range(usedfrom+1, 99):
day = book[f'A{r}'].value # type: ignore
if day: day = day.capitalize()
delta = book[f'B{r}'].value
lesson = book[f'{c}{r}'].value
delta = book[f'B{r}'].value # type: ignore
lesson = book[f'{c}{r}'].value # type: ignore
print(day, delta, lesson, f'l_col:{c} row:{r}')
if day == 'Суббота': break
@ -53,9 +69,9 @@ def update(group: str, course: str):
else: EE = f'{EE[0]}{EE[1]}:{EE[2]}{EE[3]}'
delta = f'{SS} - {EE}'
# print(f'{c}{r}', day, delta, 'Lesson text...' if lesson else 'None', book[f'{c}{r}'])
print(f'{c}{r}', day, delta, 'Lesson text...' if lesson else 'None', book[f'{c}{r}']) if debug else ... # type: ignore
if day or delta or (lesson) or (not lesson and type(book[f'{c}{r}']) != openpyxl.cell.MergedCell):
if day or delta or (lesson) or (not lesson and type(book[f'{c}{r}']) != openpyxl.cell.MergedCell): # type: ignore
if day and day != last_day:
last_day = day
this[last_day] = {}
@ -65,46 +81,46 @@ def update(group: str, course: str):
this[last_day][last_delta] = []
if lesson and lesson != last_lesson:
last_lesson = lesson
d = filter(lambda _: _!='', lesson.replace('\n', ' ').split(' '))
lesson = []
for _ in d: lesson.append(trim(_))
lessonOBJ = {'type': '', 'name': '', 'info': [], 'raw': lesson, 'ok': True}
if lesson[0] == '*':
lessonOBJ['ok'] = False
this[last_day][last_delta].append(lessonOBJ)
continue
lessonOBJ['name'] = lesson.split('\n')[0]
try: lessonOBJ['type'] = lesson.split('\n')[1].split(' ')[0]
lessonOBJ['name'] = trim(lesson[0])
try: lessonOBJ['type'] = trim(lesson[1])
except:
lessonOBJ['ok'] = False
this[last_day][last_delta].append(lessonOBJ)
continue
addInfo = ' '.join(lesson.split('\n')[2:]).replace(' ', '$')
if len(lesson.split('\n')[1].split(' ')) > 1:
addInfo = ' '.join(lesson.split('\n')[1].split(' ')[1:]).replace(' ', '$') + '$$' + addInfo
addInfo = ' '.join(lesson[2:]).replace(' ', '$')
if len(lesson[1].split(' ')) > 1:
addInfo = ' '.join(lesson[1].split(' ')[1:]).replace(' ', '$') + '$$' + addInfo
for info in addInfo.split('$'):
if info == '' or info == ' ': continue
while (info[0] == ' ' or info[-1] == ' '):
if info[0] == ' ': info = info[1:]
elif info[-1] == ' ': info = info[:-1]
lessonOBJ['info'].append(info)
lessonOBJ['info'].append(trim(info))
this[last_day][last_delta].append(lessonOBJ)
else: this[last_day][last_delta].append(None)
result[typename] = this
print(f'└──── Downloaded \n')
print(f'└──── Downloaded \n') if debug else ...
json.dump(result, open(f'./timetables/{group} {course}.json'.replace(' ','_'), 'w', encoding='utf-8'), ensure_ascii=False, indent=4)
def updateAll():
def updateAll(debug: bool = false):
global config
config = reload(config)
for group, courses in config.LOAD.items():
for course in courses:
update(group, course)
update(group, course, debug)
updateAll()
# updateAll()