Last active
June 17, 2021 12:25
-
-
Save dneprDroid/c447a79a5e5def57bcda1bfa24106fdc to your computer and use it in GitHub Desktop.
NMU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import json | |
import re | |
import time | |
from functools import reduce | |
import pyperclip | |
try: | |
from HTMLParser import HTMLParser | |
except ImportError: | |
# Python 3 | |
from html.parser import HTMLParser | |
parser = HTMLParser() | |
def parse_results(table_html): | |
table_html_decoded = parser.unescape(table_html) | |
# <td class="cell c3" style="text-align:center;">'90 / 3.0'</td> | |
# to '90 / 3.0' | |
hours_credits = re.findall(r'(?<=\"cell c5\").*?(?<=>)(.*)(?=<\/td>)', table_html_decoded) | |
courses_semesters = re.findall(r'(?<=\"cell c0\").*?(?<=>)(.*)(?=<\/td>)', table_html_decoded) | |
if len(hours_credits) == 0: | |
print('Не удалось ничего найти в ') | |
print('"%s"' % table_html) | |
return | |
assert(len(hours_credits) == len(courses_semesters), 'Invalid courses and credits count!') | |
courses = {} | |
credits_sum = 0.0 | |
for i in range(len(hours_credits)): | |
hc = hours_credits[i] | |
cs = courses_semesters[i] | |
course = cs.split('/')[0].replace(" ", "") | |
num_str = hc.split('/')[1].replace(" ", "") | |
credit_num = float(num_str) | |
credits_sum += credit_num | |
if not course in courses: | |
courses[course] = [] | |
courses[course].append(credit_num) | |
print('Общая сумма кредитов: %s' % credits_sum) | |
for course in courses: | |
print('Курс %s:' % course) | |
credits = courses[course] | |
sum = reduce(lambda a,b: a+b, credits) | |
print(' %s кредитов' % sum) | |
user_resp = input("Скопируете html c http://do.nmu.org.ua в буфер обмена\n(yes/no):") | |
if user_resp != "yes" and user_resp != "y": | |
print('Выход...') | |
exit(0) | |
input_html = pyperclip.paste() | |
try: | |
parse_results(input_html) | |
except Exception as e: | |
print('Что-то пошло не так....\n%s' % e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment