Giter Site home page Giter Site logo

mustplus.server's Introduction

gretting image

⚙️ GitHub Analytics

Achievements Isocalendar

🤝🏻 Connect with Me

mustplus.server's People

Contributors

aikov avatar dependabot[bot] avatar junyi-99 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

mustplus.server's Issues

Some suggestions for improvement of COES.py

# Author : Aikov
# Time :2019/4/30
# Status:Finished
def get_info(userid, password):
    try:
        student = Student.objects.get(student_id=userid)
    except ObjectDoesNotExist:
        student = Student.objects.create(student_id=userid)
    cookies = get_cookie(userid, password)
    if cookies == 0:
        return 0
    # Find info on personal info
    url = 'https://coes-stud.must.edu.mo/coes/StudentInfo.do'
    r = requests.get(url=url, cookies=cookies)

    # Find Chinese name
    tar = 'Name in Chinese:&nbsp;</td> <td class="blackfont"> '
    pos1 = r.text.find(tar) + tar.__len__()
    pos2 = r.text.find('  </td>', __start=pos1)
    student.name_zh = r.text[pos1:pos2]

    # Find English name
    tar = 'Name in English:&nbsp;</td> <td class="blackfont"> '
    pos1 = r.text.find(tar) + tar.__len__()
    pos2 = r.text.find('  </td>', __start=pos1)
    student.name_en = r.text[pos1:pos2]

    # Find sex
    tar = 'Gender:&nbsp;</td> <td class="blackfont">'
    pos1 = r.text.find(tar) + tar.__len__()
    pos2 = r.text.find('</td>', __start=pos1)
    student.sex = r.text[pos1:pos2]
    

student.sex 是 Boolean 属性,r.text[:] 是 str

    # Find birthday
    tar = 'Date of Birth:&nbsp;</td> <td class="blackfont">'
    pos1 = r.text.find(tar) + tar.__len__()
    pos2 = r.text.find('</td>', __start=pos1)
    date = r.text[pos1:pos2]
    date = date.split('/')
    student.birthday.day = int(date[0])
    student.birthday.mouth = int(date[1])
    student.birthday.year = int(date[2])

    # Find info on study plan
    url = 'https://coes-stud.must.edu.mo/coes/StudyPlanGroup.do'
    r = requests.get(url=url, cookies=cookies)

    # Find Faculty
    tar = 'Faculty:&nbsp;</td> <td class="blackfont">'
    pos1 = r.text.find(tar) + tar.__len__()
    pos2 = r.text.find('</td>', __start=pos1)
    name = r.text[pos1:pos2]
    try:
        faculty = Faculty.objects.get(name_en=name)
    except ObjectDoesNotExist:
        faculty = Faculty.objects.get(name_zh=name) 

如果这里也 raise 了一个 ObjectDoesNotExist 怎么办; 还有可能学校加新的faculty而我们的数据库里没有,也会导致崩溃。继续向下看,

    student.faculty_id = faculty.id

    # Find Major
    tar = 'Major:&nbsp;'
    pos = r.text.find(tar)
    # 不知道为什么两个tar之间在源码里有一个回车,这样规避一下
    tar = '</td> <td class="blackfont">  '
    pos1 = r.text.find(tar, __start=pos) + tar.__len__()
    pos2 = r.text.find('  </td>', __start=pos1)
    name = r.text[pos1:pos2]
    try:
        major = Major.objects.get(name_en=name)
    except ObjectDoesNotExist:
        major = Major.objects.get(name_zh=name) 

同理。可能逻辑上不是中文就是英文,但是实际情况非常复杂,COES可能会出问题,那么COES显示不全的时候这里就会爆炸。还有可能学校加新的major数据库里没有,也会导致崩溃

在这里不要取巧,该 catch 的 Exception 最好都 catch 到

    student.major_id = major.id
    # 已经从COES爬完了个人信息
    student.save()
    url = 'https://coes-stud.must.edu.mo/coes/logout.do' 
    requests.post(url=url, cookies=r.cookies)

如果上面text.find部分出了差错,就无法退出COES了。不要相信COES是稳定的,要认为所有东西都不可靠,我们要保证用户使用我们的服务不会出现问题。

可以尝试使用修饰器去修饰这些函数(比如写一个ensure_logout修饰器)

让这个修饰器去 handle 可能存在的 exception

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.