Last active
March 2, 2020 04:21
-
-
Save yusanshi/5f06d809f9d58ab4735234dc12b6828a to your computer and use it in GitHub Desktop.
Grade tools for students in USTC.
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
STUDENT_ID = '' | |
PASSWORD = '' | |
NAME_CN = '' | |
NAME_EN = '' |
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
import json | |
import requests | |
from bs4 import BeautifulSoup | |
from config import STUDENT_ID, PASSWORD, NAME_CN, NAME_EN | |
headers = { | |
'Connection': 'keep-alive', | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36', | |
} | |
class Utils: | |
def __init__(self, latest_only=False): | |
self.latest_only = latest_only | |
self.session = self._login() | |
self.raw_grade = self._get_raw_grade() | |
def _login(self): | |
login_url = 'https://passport.ustc.edu.cn/login?service=https://jw.ustc.edu.cn/ucas-sso/login' | |
data = { | |
'model': 'uplogin.jsp', | |
'service': 'https://jw.ustc.edu.cn/ucas-sso/login', | |
'warn': '', | |
'showCode': '', | |
'username': STUDENT_ID, | |
'password': PASSWORD, | |
'button': '', | |
} | |
session = requests.Session() | |
html = session.post(login_url, headers=headers, | |
data=data, allow_redirects=False) | |
session2 = requests.Session() | |
session2.get( | |
html.headers['location'], headers=headers, allow_redirects=False) | |
return session2 | |
def _get_raw_grade(self): | |
''' | |
get a dict of all courses | |
''' | |
semester_ID_url = 'https://jw.ustc.edu.cn/for-std/grade/sheet/getSemesters' | |
grade_url_base = 'https://jw.ustc.edu.cn/for-std/grade/sheet/getGradeList?trainTypeId=1&semesterIds=' | |
semester_ID = self.session.get(semester_ID_url, headers=headers) | |
semester_ID = BeautifulSoup(semester_ID.text, 'lxml') | |
semester_ID = json.loads(semester_ID.p.string) | |
semester_ID = sorted([unit['id'] for unit in semester_ID]) | |
if self.latest_only: | |
semester_ID = semester_ID[-1:] | |
semester_ID = [str(unit) for unit in semester_ID] | |
assert len(semester_ID) > 0 | |
semester_ID = ','.join(semester_ID) | |
grade_url = grade_url_base + semester_ID | |
raw_grade = self.session.get(grade_url, headers=headers) | |
raw_grade = BeautifulSoup(raw_grade.text, 'lxml') | |
return json.loads(raw_grade.p.string) | |
def save_transcript(self, filename='transcript.md'): | |
''' | |
Save my academic transcript in Markdown format. | |
''' | |
text = '# Academic Transcript\n' | |
text += '> This transcript file is automatically generated from a script by Lei Yu. The source code is available [here](https://gist.github.com/yusanshi/5f06d809f9d58ab4735234dc12b6828a).\n' | |
text += '## Overview\n' | |
text += '| Name (ZH) | Name (EN) | School | Total Credits | GPA | WAM |\n' | |
text += '| --- | --- | --- | --- | --- | --- |\n' | |
text += f"|{NAME_CN}| {NAME_EN} | USTC (University of Science and Technology of China) | {self.raw_grade['overview']['passedCredits']} | {self.raw_grade['overview']['gpa']}/4.3 | {self.raw_grade['overview']['weightedScore']}/100 |\n" | |
text += '## Detail\n' | |
text += '| Course Name (ZH) | Course Name (EN) | Credits | Grade (X/100) | Grade Point (X/4.3) | Semester |\n' | |
text += '| --- | --- | --- | --- | --- | --- |\n' | |
for i in self.raw_grade['semesters']: | |
for j in i['scores']: | |
info = [j['courseNameCh'], j['courseNameEn'], j['credits'], | |
str(j['score']) if j['score'] is not None else 'Passed', | |
str(j['gp']) if j['gp'] is not None else 'Passed', j['semesterEn']] | |
text += '| ' + ' | '.join(map(str, info)) + ' |\n' | |
with open(filename, 'w', encoding='utf-8') as f: | |
f.write(text) | |
def calc_GPA(self): | |
''' | |
Calculate GPA by myself. | |
''' | |
GPA_dict = { | |
'total_credits': 0, | |
'total_grade': 0, | |
'GPA': None | |
} | |
for i in self.raw_grade['semesters']: | |
for j in i['scores']: | |
if j['gp'] is not None: | |
GPA_dict['total_credits'] += j['credits'] | |
GPA_dict['total_grade'] += j['credits'] * j['gp'] | |
if GPA_dict['total_credits'] != 0: | |
GPA_dict['GPA'] = GPA_dict['total_grade'] / \ | |
GPA_dict['total_credits'] | |
print(GPA_dict) | |
if __name__ == "__main__": | |
instance = Utils() | |
# instance.calc_GPA() | |
instance.save_transcript() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment