#!/usr/bin/python3
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time, json, sys
co=webdriver.FirefoxOptions()
prefs=webdriver.FirefoxProfile()
prefs.set_preference('permissions.default.image',2)
prefs.set_preference('dom.ipc.plugins.enabled.libflashplayer.so','false')
co.add_argument('-headless')
co.add_argument('--ignore-certificate-errors')
dr=webdriver.Firefox(options=co,firefox_profile=prefs)
dr.get('https://www.ewt360.com')
WebDriverWait(dr,15).until(EC.visibility_of_element_located((By.ID,'username')))
dr.execute_script('(function(){document.getElementById(\'username\').value=\''+sys.argv[1]+'\';document.getElementById(\'password\').value=\''+sys.argv[2]+'\';document.getElementsByName(\'submit\')[0].click();})()')
WebDriverWait(dr,15).until(EC.text_to_be_present_in_element((By.ID,'login'),'我的假期'))
dr.get('https://teacher.ewt360.com/bendbff/api/homeworkprod/homework/student/getStudentHomeworkDetail?homeworkId=10017426&sceneId=0&schoolId=15005&timestamp='+str(int(time.time()*1000)))
def isresponded(dd):
    return "}" in dd.page_source
def isvl(dd):
    return len(dd.find_elements_by_tag_name('video'))==1
def hs(dd):
    return len(dd.find_elements_by_class_name('ccH5vm'))==1
def gs(dd):
    return json.loads(dd.execute_script("return document.body.innerText;"))
WebDriverWait(dr,15).until(isresponded)
hw=gs(dr)
if hw!=None and hw['success']:
    for da in hw['data']['contentList']:
        als=da['contents']
        if da['hasFinished']<len(als):
            for i in als:
                if not i['finished']:
                    dr.get((i['contentUrl'].startswith('//') and 'https:' or '')+i['contentUrl'])
                    WebDriverWait(dr,30).until(isvl)
                    dr.find_element_by_class_name('ccH5PlayBtn').click()
                    WebDriverWait(dr,20).until(hs)
                    dr.find_element_by_class_name('ccH5vm').click()
                    dr.execute_script('document.getElementsByClassName("ccH5hdul")[0].children[1].click();');
                    dr.execute_script('document.getElementsByClassName("ccH5spul")[0].children[2].click();');
                    dr.execute_script("var vd=document.getElementsByTagName('video')[0];"+\
                    "vd.onpause=function(){vd.play();};"
                    "setInterval(function(){if(document.getElementById('earnest_check_count_down_num')!=null){"+\
                    "document.getElementById('earnest_check_count_down_num').parentElement.click();}},24000);");
                    print(i)
                    WebDriverWait(dr,i['duration']*2)\
                    .until(EC.visibility_of_element_located((By.CSS_SELECTOR,'div#replaybtn.adrPlayBtn')))
