from selenium import webdriver
import time,json,math,requests
wd = webdriver.Chrome('.\chromedriver.exe')

# 设置最大等待时长为 10秒
wd.implicitly_wait(20)
wd.get('http://sdjn.yxlearning.com/')
input('回车继续!!!!!!!')

xuexi=wd.find_element_by_css_selector('body > div.home > div > div.icon.menu80000-006 > span > a').click()
wodexuexi=wd.find_element_by_css_selector('#_nav_header > div:nth-child(2) > ul > li.dropdown.apply-entrance > a').click()
xuexizhongxin=wd.find_element_by_id('header_learning').click()

wd.find_elements_by_xpath('//*[@li-index="2"]/a')[0].click()
kecheng_list=wd.find_elements_by_xpath('//*[@class="row dd"]')
time.sleep(2)
cookies = wd.get_cookies()#获取cookie
cookie=cookies[0]['name']+'='+cookies[0]['value']
heders={'Cookie': 'projectId=g104; '+cookie}

url='http://sdjn.gxk.yxlearning.com/train/cms/study/find-my-study-info.gson?studyStatus=1'
res=requests.get(url=url,headers=heders)#获取学习中课程列表
res=json.loads(res.text)
id_list=res['attribute']['list']

for i in range(len(id_list)):
    myClassId=id_list[i]['myClassId']
    wd.get('http://sdjn.gxk.yxlearning.com/learning/index?myClassId=%s'%myClassId)
    url = 'http://sdjn.gxk.yxlearning.com/train/class/my-class.gson?myClassId=%s' % myClassId
    res = requests.get(url=url, headers={'Cookie': 'projectId=g104; ' + cookie}).text#获取课程课件的相关信息
    res = json.loads(res)
    myClassCourseRPList = res['attribute']['data']['myClassCourseRPList']
    for a in range(len(res['attribute']['data']['myClassCourseRPList'])):
        videoRPs = myClassCourseRPList[a]['videoRPs']
        for i in range(len(videoRPs)):
            print(str(videoRPs[i]['videoName'])+'  '+str(videoRPs[i]['learnSpeed'])+'%'+' 开始！！！')
            if videoRPs[i]['learnSpeed']<100.0:
                t=int(videoRPs[i]['learnRate']) - int(videoRPs[i]['watchTimeLength'])
                print(str(t)+'秒')
                try:
                    wd.find_element_by_id(videoRPs[i]['myClassCourseVideoId']).click()
                    wd.find_element_by_css_selector('div > div.pv-skin-blue.pv-video-bottom.pv-subtitle-hide.pv-base-control > div.pv-controls > div.pv-controls-right > div:nth-child(2) > button').click()
                    wd.find_element_by_css_selector('div > div.pv-video-wrap > div.pv-cover > span').click()
                except:
                    print('播放失败')
                    pass
                url = 'http://v.polyv.net/uc/exam/get?vid={}'.format(videoRPs[i]['videoSource'])
                res = requests.get(url).text
                res = json.loads(res)
                q_list = []
                t_list = []
                y = 0
                for o in range(len(res)):
                    showTime = res[o]['showTime'][4:8]
                    answer=res[o]['answer']
                    t_list.append(answer)
                for u in range(len(t_list)):
                    q_list.append(list(t_list[u]))
                q=0
                while q<math.ceil(t/20):
                    try:
                        if not wd.find_elements_by_css_selector('div > div.pv-ask-modal-wrap > div > div.pv-ask-foot > button.pv-ask-submit'):
                            pass
                        else:
                            list1=q_list[y]
                            for e in list1:
                                try:
                                    if e== 'A':
                                        wd.find_elements_by_css_selector('div > div.pv-ask-modal-wrap > div > div.pv-ask-content.pv-ask-content-noimg > div.pv-ask-right > div > div:nth-child(2) > label')[0].click()
                                    elif e == 'B':
                                        wd.find_element_by_css_selector('div > div.pv-ask-modal-wrap > div > div.pv-ask-content.pv-ask-content-noimg > div.pv-ask-right > div > div:nth-child(3) > label').click()
                                    elif e == 'C':
                                        wd.find_element_by_css_selector('div > div.pv-ask-modal-wrap > div > div.pv-ask-content.pv-ask-content-noimg > div.pv-ask-right > div > div:nth-child(4) > label').click()
                                    elif e == 'D':
                                        wd.find_element_by_css_selector('div > div.pv-ask-modal-wrap > div > div.pv-ask-content.pv-ask-content-noimg > div.pv-ask-right > div > div:nth-child(5) > label').click()
                                    elif e == 'E':
                                        wd.find_element_by_css_selector('div > div.pv-ask-modal-wrap > div > div.pv-ask-content.pv-ask-content-noimg > div.pv-ask-right > div > div:nth-child(6) > label').click()
                                    elif e == '正':
                                        wd.find_elements_by_css_selector('div > div.pv-ask-modal-wrap > div > div.pv-ask-content.pv-ask-content-noimg > div.pv-ask-right > div > div:nth-child(2) > label')[0].click()
                                    elif e == '错':
                                        wd.find_element_by_css_selector('div > div.pv-ask-modal-wrap > div > div.pv-ask-content.pv-ask-content-noimg > div.pv-ask-right > div > div:nth-child(3) > label').click()
                                    elif e == '确':
                                        pass
                                    elif e=='误':
                                        pass
                                except:
                                    pass
                            try:
                                y+=1
                                wd.find_elements_by_css_selector('div > div.pv-ask-modal-wrap > div > div.pv-ask-foot > button.pv-ask-submit')[0].click()#提交
                                wd.find_elements_by_css_selector('div > div.pv-ask-modal-wrap.pv-ask-modal-answer > div > div.pv-ask-foot > button')[0].click()#关闭
                            except:
                                print('回答错误')
                                pass
                    except:
                        print('出错了')
                        pass
                    q+=1
                print(videoRPs[i]['videoName']+' 完成')
            else:
                print(videoRPs[i]['videoName']+' 完成')
wd.close()#关闭浏览器



