yushun-o
5/7/2020 - 1:50 AM

getHomework.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import chromedriver_binary

LOGIN_URL = 'https://itc-lms.ecc.u-tokyo.ac.jp/saml/login?disco=true'
COURSE_URL_PARAM = 'https://itc-lms.ecc.u-tokyo.ac.jp/lms/course?idnumber='


def get_task_list(user_email, user_password):
    task_list = []
    driver, wait = login(user_email=user_email, user_password=user_password)
    course_ids = get_course_ids(driver=driver, wait=wait)

    for course_id in course_ids:
        # course_idを"ID"とすると
        # https://itc-lms.ecc.u-tokyo.ac.jp/lms/course?idnumber=IDがリンク
        driver.get(COURSE_URL_PARAM + course_id)
        course_name = driver.find_element_by_class_name('page_name_txt').text
        report_list = get_report_list(driver=driver, course_name=course_name)
        test_list = get_test_list(driver=driver, course_name=course_name)
        task_list.extend(report_list)
        task_list.extend(test_list)

    return task_list


def login(user_email, user_password):
    task_list = []  # これを結局返す Course クラスのインスタンスを追加
    # ブラウザーを起動
    options = Options()
    options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')

    driver = webdriver.Chrome(options=options)
    driver.get(LOGIN_URL)
    print("current : " + driver.current_url)
    # wait until submitButton is clickable
    wait = WebDriverWait(driver, 10)
    button_element = wait.until(EC.element_to_be_clickable((By.ID, 'submitButton')))
    driver.find_element_by_id('userNameInput').send_keys(user_email)
    driver.find_element_by_id('passwordInput').send_keys(user_password)
    button_element.click()
    return driver, wait


def get_report_list(driver, course_name):
    report_list = []
    report_elements = driver.find_elements_by_class_name('report_list_line')
    for report_element in report_elements:
        report_title = report_element.find_element_by_class_name('break').text
        report_time_end: str = report_element.find_element_by_class_name('timeEnd').text
        submit_status = report_element.find_element_by_class_name('submitStatus').text
        if submit_status == '未提出':
            report = Task(course_name=course_name,
                          task_title=report_title,
                          task_time_end=report_time_end,
                          task_type=0)  # task_type = 0 のときはレポート
            report_list.append(report)
    return report_list


def get_test_list(driver, course_name):
    test_list = []

    test_elements = driver.find_elements_by_class_name('examination_list_line')
    for test_element in test_elements:
        test_title = test_element.find_element_by_class_name('break').text
        test_time_end: str = test_element.find_elements_by_class_name('result_list_txt')[1].text
        test_time_end = test_time_end[test_time_end.index('~') + 2:]
        text_content: str = str(test_element.find_element_by_class_name('contorl_menu').get_attribute("textContent"))
        if '受験する' in text_content:
            test_list.append(Task(course_name=course_name,
                                  task_title=test_title,
                                  task_time_end=test_time_end,
                                  task_type=1))  # task_type = 1 のときテスト
    return test_list


def get_course_ids(driver, wait):
    # wait until course_on_timetable is clickable
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'course_on_timetable')))
    course_elements = driver.find_elements_by_class_name('course_on_timetable')
    course_ids = list()
    for course_element in course_elements:  # type : selenium.webdriver.remote.webelement.WebElement
        course_ids.append(course_element.get_attribute('id'))
    return course_ids


def get_dummy_report_list(user_email, user_password):
    task_list = []
    task_list.append(Task('講座1', '講座1のレポート1つめ', "2020/04/20 08:00", 0))
    task_list.append(Task('講座2', '講座2のレポートだよ', '2020/04/27 06:00', 0))
    task_list.append(Task('講座3', '講座3のレポート1個目', '2020/05/04 13:00', 0))
    return task_list


class Task:
    def __init__(self, course_name, task_title, task_time_end, task_type: int):
        self.task_type = task_type  # 0 : report ,1 : test  に対応
        self.course_name = course_name
        self.task_title = task_title
        self.task_time_end = task_time_end