之前在吾爱破解论坛看到了Python版,然后写了一个Java版的

主要是为了学习使用Java发送GET请求和解析JSON

效果如下

默认对接WX Pusher,需要自己去申请应用,得到appToken和uid,并填入对应位置(第121行)

需要使用alibaba的fastjson包,添加方式:在Maven构建的项目中,pom.xml添加如下代码,并重新构建

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
</dependency>

需手动抓取学习通Cookie和User-Agent 抓取方法:

打开http://i.chaoxing.com/ 并登录,使用F12大法,选择网络(network),刷新页面,随便找一个chaoxing的请求

填入如下位置(第16行)

注:如果Cookie行报错,可能是因为Cookie中有引号,使用转义"\"转义即可

代码:

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) throws Exception {
        //学习通Cookie 抓取方式自己研究= =
        String Cookie="这里填入你的Cookie";
        String UA="这里填入你的UA";
        String result=send_get("http://mooc1-api.chaoxing.com/mycourse/backclazzdata?view=json&rss=1",Cookie,UA,"mooc1-api.chaoxing.com");
        System.out.println(result);
        JSONObject jsonArray=JSONArray.parseObject(result);
        //得到课程总数
        int coursenum=jsonArray.getJSONArray("channelList").size();
        //创建课程信息对象数组
        Course []course=new Course[coursenum];
        //初始化对象数组
        for (int i = 0; i < coursenum; i++) {
            course[i]=new Course();
        }
        //创建channelList JSON 对象数组
        JSONObject []channelList=new JSONObject[coursenum];
        //从channelList解析课程cpi
        for (int i = 0; i < coursenum; i++) {
            String tempstr=jsonArray.getJSONArray("channelList").get(i).toString();
            channelList[i]=JSONObject.parseObject(tempstr);
            course[i].cpi=channelList[i].get("cpi").toString();
        }
        //JSON content data 数组
        JSONObject []content=new JSONObject[coursenum];
        JSONObject []data=new JSONObject[coursenum];
        for (int i = 0; i < coursenum; i++) {
            //提取content
            content[i]=JSONArray.parseObject(channelList[i].get("content").toString());
            //创建临时JSONObject
            JSONObject temp=new JSONObject();
            try {
                //解析JSON - course
                temp=JSONArray.parseObject(content[i].get("course").toString());
                //分离JSON - data
                data[i]=JSONArray.parseObject(temp.getJSONArray("data").get(0).toString());
                //里面有courseId
                String courseSquareUrl=data[i].get("courseSquareUrl").toString();
                //解析classId
                course[i].classId=courseSquareUrl.substring(courseSquareUrl.indexOf("classId=")+8, courseSquareUrl.indexOf("&userId"));//得到courseId
                //解析name
                course[i].name=data[i].get("name").toString();
                //解析courseId
                course[i].courseId=data[i].get("id").toString();
            }
            catch (NullPointerException e){
                //如果为自己教的课会报错
                data[i]=JSONArray.parseObject(null);
            }
        }
        //最后wx推送的str
        String FinalResult="";
        for (Course temp:course) {
            //拼接作业URL
            temp.taskLink="https://mooc1-api.chaoxing.com/work/task-list?courseId=" + temp.courseId + "&classId=" + temp.classId + "&cpi=" + temp.cpi;
            //发送get请求
            result=send_get(temp.taskLink,Cookie,UA,"mooc1-api.chaoxing.com");
            //如果有未交的作业
            if (result.contains("未交"))
            {
                //解析作业名
                String name=result.substring(result.indexOf("<p>")+3,result.indexOf("</p>"));
                //解析作业状态
                String state=result.substring(result.indexOf("<span>")+6,result.indexOf("</span>"));
                //正则表达式块开始 正则匹配小时分钟
                Pattern patt = Pattern.compile("剩余.*小时");
                Matcher matcher=patt.matcher(result);
                matcher.find();
                String time="";
                time+=matcher.group();
                patt=Pattern.compile("\\d分钟");
                matcher=patt.matcher(result);
                matcher.find();
                time+=matcher.group();
                //正则表达式块结束
                //添加到FinalResult
                FinalResult+=temp.name+"\n"+name+"\n"+state+"\n"+time+"\n";
            }
        }
        //推送到WX
        send_to_wx(FinalResult);
        System.out.println(FinalResult);
    }
    public static String send_get(String URL, String Cookie, String UA,String Host) throws IOException {
        URL url = new URL(URL);
        URLConnection connection= url.openConnection();
        //设置请求头
        connection.setRequestProperty("Host",Host);
        connection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        connection.setRequestProperty("Cookie",Cookie);
        connection.setRequestProperty("User-Agent",UA);
        connection.setRequestProperty("Accept-Language","zh-cn");
        connection.setRequestProperty("Accept-Encoding","deflate, br");
        connection.setRequestProperty("Connection","keep-alive");
        connection.setDoOutput(true);
        //流读取
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
        String line,result = "";
        //一行一行读取并加入到result
        while ((line = in.readLine()) != null) {
            result += line;
        }
        in.close();
        return result;
    }
    public static void send_to_wx(String str) throws IOException {
        //WX Pusher 板块
        //appToken
        String appToken="这里填入appToken";
        //uid
        String uid="这里填入uid";
        //拼接请求
        String wxLink="http://wxpusher.zjiecode.com/api/send/message/?appToken="+appToken+"&uid="+uid+"&content="+ URLEncoder.encode(str);
        //发送请求并输出
        System.out.println(send_get(wxLink, "", "", "wxpusher.zjiecode.com"));
    }
}
class Course{
    public String name;
    public String cpi;
    public String courseId;
    public String classId;
    public String taskLink;
}

如有侵权,请联系站长,秒删。