浙江线上大学给自己用的的刷课脚本~不知道有没有用?

2022-08-30 11:14:14 浏览数 (1)

0x01

以下是代码

  • 不知道现在还能不能用?上学那会儿还能用。需要用到的库看import....
代码语言:javascript复制
# -*- coding:utf-8 -*-
import urllib2,urllib,json,time,random,re,math
import base64,io
####
from PIL import Image
from PIL import ImageEnhance

import pytesseract

def createTimesatamp(t=32):
    strs = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"
    t = 32
    result = ""
    lens = len(strs)
    for i in range(t):
        result  = str(strs[int(math.floor(random.random() * lens))])
    result  = str(int(time.time() * 1000))
    return result

def getAuth_code():
    headers = {
    'Host': 'www.zjooc.cn',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'X-Requested-With': 'XMLHttpRequest',
    'Connection': 'keep-alive',
    'Referer': 'http://www.zjooc.cn/login?fromUrl=%2F'
    }
    host = r'http://www.zjooc.cn'
    url = host   r'/ajax?time=' createTimesatamp() '&service=/centro/api/authcode/create&params='
    request=urllib2.Request(url,headers=headers)
    response=urllib2.urlopen(request).read()
    
    json_data = json.loads(response)
    image_id = json_data['data']['id']
    image_data = json_data['data']['image']
    result = {'captchaId':image_id,
              'image_data':image_data}
    
    return json.dumps(result)

def img2value(image):
    img_b64decode= base64.b64decode(image)
    image_byte = io.BytesIO(img_b64decode)
    images = Image.open(image_byte)
    im=images.convert('L')
    im=ImageEnhance.Contrast(im)
    img=im.enhance(100) 
    out = img.resize((105,42)) 
    vcode = pytesseract.image_to_string(out,lang="eng")
    if vcode == '':
        print u'验证码:识别失败'
        vcode = 0
    else:
        print u'验证码:' vcode
    return vcode

def getAtoken(data):
    result = re.match( r'atoken=(.*?);', data, re.M|re.I)
    if result:
        return result.group(1)
    else:
        return 'error'


def login(login_name,password,captchaCode,captchaId):
    headers = {
    'Host': 'www.zjooc.cn',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'X-Requested-With': 'XMLHttpRequest',
    'Connection': 'keep-alive',
    'Referer': 'http://www.zjooc.cn/login?fromUrl=%2F',
    'Cookie':'uniqueVisitorId=你的uniqueVisitorId; lano.connect.sid=你的lano.connect.sid'
    }
    pyload = {
        "login_name": login_name,
        "password": password,
        "captchaCode":captchaCode,
        "captchaId":captchaId,
        "autoLoginTime":"7",
        "autoLoginFlag":"0"}
    host = r'http://www.zjooc.cn'
    url = host   r'/login/doLogin?time=' createTimesatamp()
    data = urllib.urlencode(pyload)
    request = urllib2.Request(url,data,headers=headers)
    response = urllib2.urlopen(request)
    response_setcookie = response.info().getheader('set-cookie')
    login_json = json.loads(response.read())
    print login_json['message']
    atoken = getAtoken(response_setcookie)
    if atoken != 'error':
        authorization_code = login_json['authorization_code']
    else:
        authorization_code = 'error'
    result = {'atoken':atoken,
              'auth_code':authorization_code}
    return json.dumps(result)


def searchCourse(auth_code,atoken):
    url = r'http://www.zjooc.cn/ajax?time=' createTimesatamp() '&service=/jxxt/api/course/courseStudent/student/course&params[pageNo]=1&params[pageSize]=5&params[pageTotal]=0&params[courseDivisionType]=&params[coursePublished]=&params[courseName]=&params[publishStatus]=&params[loading]=true'
    headers = {
    'Host': 'www.zjooc.cn',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'X-Requested-With': 'XMLHttpRequest',
    'Connection': 'keep-alive',
    'Referer': 'http://www.zjooc.cn/ucenter/student/course/build/list?auth_code=' auth_code,
    'Cookie':'lano.connect.sid=你的lano.connect.sid; ckCsrfToken=你的ckCsrfToken;atoken='   atoken,
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache'}
    request=urllib2.Request(url,headers=headers)
    response=urllib2.urlopen(request).read()
    json_data = json.loads(response)
    num_school = len(json_data['data'])
    num_courseLecturer_temp = 0
    array_courseLecture_id = []
    print u'已获取,总共有'   str(num_school)   u'个学校'
    for i in range(num_school):
        print u'    已获取,第'   str(i 1)   u'个学校'
        print u'        课程类别:'   json_data['data'][i]['classifyName']
        print u'        课程名称:'   json_data['data'][i]['name']
        num_courseLecturer = len(json_data['data'][i]['courseLecturerList'])
        num_courseLecturer_temp  = num_courseLecturer
        for j in range(num_courseLecturer):
            print u'            学校名:'   json_data['data'][i]['courseLecturerList'][j]['corp']
            print u'            课程id:'   json_data['data'][i]['courseLecturerList'][j]['courseId']
            print u'            所属分院:'   json_data['data'][i]['courseLecturerList'][j]['department']
            print u'            id:'   json_data['data'][i]['courseLecturerList'][j]['id']
            print u'            姓名:'   json_data['data'][i]['courseLecturerList'][j]['name']
            array_courseLecture_id.append(json_data['data'][i]['courseLecturerList'][j]['courseId'])
    print u'总计'   str(num_courseLecturer_temp)   u'个课程'
    return array_courseLecture_id

def searchProgress(auth_code,atoken,array_courseLecture_id,num):
    url = r'http://www.zjooc.cn/ajax?time=' createTimesatamp() '&service=/jxxt/api/course/courseStudentScore/progressScore&params[courseId]='   array_courseLecture_id[num]
    headers = {
    'Host': 'www.zjooc.cn',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'X-Requested-With': 'XMLHttpRequest',
    'Connection': 'keep-alive',
    'Referer': 'http://www.zjooc.cn/ucenter/student/course/build/list?auth_code=' auth_code,
    'Cookie':'lano.connect.sid=你的lano.connect.sid; ckCsrfToken=你的ckCsrfToken;atoken='   atoken,
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache'}
    request=urllib2.Request(url,headers=headers)
    response=urllib2.urlopen(request).read()
    json_data = json.loads(response)
    print u'n已获取,当前进度'   str(float(json_data['data']['courseProgress']) * 100)   u'%'
    return float(json_data['data']['courseProgress'])

def study(auth_code,atoken,array_courseLecture_id,num):
    host = r'http://www.zjooc.cn/ajax?time=' createTimesatamp()
    url = host '&service=/jxxt/api/course/courseStudent/getStudentCourseChapters&params[pageNo]=1&params[courseId]='   array_courseLecture_id[num]   '&params[urlNeed]=0'
    headers = {
    'Host': 'www.zjooc.cn',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'X-Requested-With': 'XMLHttpRequest',
    'Connection': 'keep-alive',
    'Referer': 'http://www.zjooc.cn/ucenter/student/course/build/list?auth_code=' auth_code,
    'Cookie':'lano.connect.sid=你的lano.connect.sid; ckCsrfToken=你的ckCsrfToken;atoken=' atoken,
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache'}
    request=urllib2.Request(url,headers=headers)
    response=urllib2.urlopen(request).read()
    json_data = json.loads(response)
    len_1 = len(json_data['data'])
    count = 0
    success_count = 0
    for i in range(len_1): 
        len_2 = len(json_data['data'][i]['children']) #开始章节
        print u'---- 章节 '   str(i 1)
        for j in range(len_2):
            len_3 = len(json_data['data'][i]['children'][j]['children']) #开始单元
            print u'-------- 单元 ' str(j 1)
            for k in range(len_3):
                len_4 = len(json_data['data'][i]['children'][j]['children'][k]) #开始小节
                name = json_data['data'][i]['children'][j]['children'][k]['name']
                chapterId = json_data['data'][i]['children'][j]['children'][k]['id']
                courseId = json_data['data'][i]['children'][j]['children'][k]['courseId']
                resourceType = (json_data['data'][i]['children'][j]['children'][k]['resourceType'])
                print u'------------ 正在学习第'   str(k 1)   u'小节-《' name u'》' str(resourceType)
                count  = 1
                    
                headers_study = {
                'Host': 'www.zjooc.cn',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
                'Accept': 'application/json, text/javascript, */*; q=0.01',
                'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
                'Accept-Encoding': 'utf-8',
                'X-Requested-With': 'XMLHttpRequest',
                'Connection': 'keep-alive',
                'Referer': 'http://www.zjooc.cn/ucenter/student/course/study/' courseId '/plan/detail/' chapterId,
                'Cookie':'lano.connect.sid=你的lano.connect.sid;uniqueVisitorId=你的uniqueVisitorIdckCsrfToken=你的ckCsrfToken;atoken=' atoken,
                'Pragma': 'no-cache',
                'Cache-Control': 'no-cache'}
                headers_study_video = {
                'Host': 'www.zjooc.cn',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
                'Accept': 'application/json, text/javascript, */*; q=0.01',
                'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
                'Accept-Encoding': 'utf-8',
                'X-Requested-With': 'XMLHttpRequest',
                'Connection': 'keep-alive',
                'Referer': 'http://www.zjooc.cn/ucenter/student/course/study/' courseId '/plan/detail/' chapterId,
                'Cookie':'lano.connect.sid=你的lano.connect.sid;uniqueVisitorId=你的uniqueVisitorId;ckCsrfToken=你的ckCsrfToken;atoken=' atoken}
                if resourceType !=1:
                    study_url = host '&service=/learningmonitor/api/learning/monitor/videoPlaying&params[courseId]=' courseId '&params[chapterId]=' chapterId
                else:
                    TimeLength = (json_data['data'][i]['children'][j]['children'][k]['vedioTimeLength'])
                    study_url = host '&service=/learningmonitor/api/learning/monitor/videoPlaying&params[chapterId]=' chapterId '&params[courseId]=' courseId '&params[playTime]=' TimeLength '&params[percent]=100'
                    request = urllib2.Request(study_url,headers=headers_study_video)
                    response = urllib2.urlopen(request)
                request = urllib2.Request(study_url,headers=headers_study)
                response = urllib2.urlopen(request).read()
                json_datas = json.loads(response)

                if json_datas['resultCode'] == 0:
                    print u'---------------- 学习第'   str(k 1)   u'小节-《' name u'》 ------ 成功!'
                    success_count  = 1
                else:
                    print u'---------------- 学习第'   str(k 1)   u'小节-《' name u'》 ------ 失败!'
                time.sleep(0)
                searchProgress(auth_code,atoken,array_courseLecture_id,len(array_courseLecture_id)-1)
    print u'执行完成!' u'完成率:' str(float(success_count)/float(count)*100) '%'
    
def main(login_name,password):
    getcode_data = getAuth_code()
    code_data = json.loads(getcode_data)
    captchaCode = img2value(code_data['image_data'])
    captchaId = code_data['captchaId']
    time.sleep(1)
    login_jsondata = json.loads(login(login_name,password,captchaCode,captchaId))
    atoken = login_jsondata['atoken']
    auth_code = login_jsondata['auth_code']
    if atoken != 'error':
        array_courseLecture_id = searchCourse(auth_code,atoken)
        time.sleep(1)
        array_courseLecture_id_len = len(array_courseLecture_id)
        for i in range(array_courseLecture_id_len):
            progress = searchProgress(auth_code,atoken,array_courseLecture_id,array_courseLecture_id_len - 1)
            if progress != 1:
                study(auth_code,atoken,array_courseLecture_id,array_courseLecture_id_len - 1)
                
            else:
                print u'第'   str(i 1)   u'个课程一完成学习进度'
            
    else:
        print u'请重新登陆!'

login_name = '你的用户名'
password = '你的密码.'
old_time = time.time()
main(login_name,password)
new_time = time.time()
used_time = (new_time - old_time) * 1000
print str(used_time) ' ms'

0 人点赞