记一次学校防疫自动打卡

记一次自动打卡的开发过程

现在疫情越来越严重,学校每天都要求健康打卡,然而每次打卡都要输一次地址,还要填写重复的内容,在此不得不问一下学校的网络开发人员,怎么就不加个有个默认选项呢。既然改变不了别人,不如自己写一个自动打卡,改变一下自己。

1、使用到的工具

  • Charles - 抓包
  • Chrome - 访问网页
  • IDEA - 撸代码

分析:学校的防疫打卡需要在微信公众号上进入,其实在浏览器打开之后,发现就是一个vue写的前端,后台估计用java 写的web 服务,做了负载均衡。

2、爬虫分析

  • 打开网页代理
  • 打开Charles

在chrome开始一波猛如虎的请求,先分析登录接口,获取cookie。带着cookie拿下用户信息的地址,最后带着地址和打卡信息完成打卡。说来也简单,现在的登录爬虫啥的差不多都是这样的流程。这一步拿到登录接口、用户信息接口、打卡接口。

3、开始撸代码

鉴于对Java比较熟悉,所以采用Java,使用maven管理工程。

配置pom.xml

<dependencies>
  <dependency>
    <groupId>com.squareup.okhttp3groupId>
    <artifactId>okhttpartifactId>
    <version>3.8.1version>
  dependency>
  <dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
    <version>1.2.36version>
  dependency>
dependencies>

使用okhttp进行网络访问,fastjson解析用户数据。

  • 登录拿cookie
public String login(String url, String json) throws IOException {
  MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  OkHttpClient client = new OkHttpClient();
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
    .url(url)
    .post(body)
    .build();
  Response response = client.newCall(request).execute();
  List<String> aspxauth = response.headers("Set-Cookie");
  LoginReturn loginReturn = com.alibaba.fastjson.JSON.parseObject(response.body().string(), LoginReturn.class);
  String token = loginReturn.getData().getAccessToken();
  for (String item :
       aspxauth) {
    System.out.println(item);
  }
  return "TOKEN=" + token + "; " + aspxauth.get(3);
}

向登录接口提交用户密码,获取到cookie,其实可以直接用session代理,不过我是后来才知道的。

  • 带着cookie拿到用户信息
public User getUserInfo(String url, String cookie) throws IOException {
  OkHttpClient client = new OkHttpClient();
  Request request = new Request.Builder()
  .url(url)
  .header("Cookie", cookie)
  .build();
  Response response = client.newCall(request).execute();
  return JSON.parseObject(response.body().string(), User.class);
}

这一步很简单,就是解析麻烦。

  • 带着cookie和用户地址打卡
private void clockIn() throws IOException{
  //1、登录获取Cookie
  LoginTest loginTest = new LoginTest();
  String code = "your number";
  String password = "your password";
  String url = "http://fangkong.hnu.edu.cn/api/v1/account/login";
  String json = "{\"Code\":\""+code+"\",\"" + code + "\":\""+password+"\",\"WechatUserinfoCode\":null}";
  String cookie = loginTest.login(url, json);
  //2、获取用户信息
  UserInfoTest userInfoTest = new UserInfoTest();
  String userUrl = "http://fangkong.hnu.edu.cn/api/v1/user/iscompletebaseinfo";
  User user = userInfoTest.getUserInfo(userUrl, cookie);
  //3、利用用户信息打卡
  String addInfo = "{\"Temperature\":null,\"RealProvince\":\""+ user.getData().getHomeProvince() +"\",\"RealCity\":\"" + user.getData().getHomeCity() + "\",\"RealCounty\":\""+ user.getData().getHomeCounty() +"\",\"RealAddress\":\""+ user.getData().getHomeAddress() +"\",\"IsUnusual\":\"0\",\"UnusualInfo\":\"\",\"IsTouch\":\"0\",\"IsInsulated\":\"0\",\"IsSuspected\":\"0\",\"IsDiagnosis\":\"0\",\"tripinfolist\":[{\"aTripDate\":\"\",\"FromAdr\":\"\",\"ToAdr\":\"\",\"Number\":\"\",\"trippersoninfolist\":[]}],\"toucherinfolist\":[],\"dailyinfo\":{\"IsVia\":\"0\",\"DateTrip\":\"\"},\"IsInCampus\":\"0\",\"IsViaHuBei\":\"0\",\"IsViaWuHan\":\"0\",\"InsulatedAddress\":\"\",\"TouchInfo\":\"\",\"IsNormalTemperature\":\"1\",\"Longitude\":null,\"Latitude\":null}";
  String addUrl = "http://fangkong.hnu.edu.cn/api/v1/clockinlog/add";
  Response response = add(addUrl, addInfo, cookie);
  ClockInResult result = com.alibaba.fastjson.JSON.parseObject(response.body().string(), ClockInResult.class);
  System.out.println(result.getMsg());
}

成功打卡会显示成功

重复打卡会显示用户今天已经打过卡

重复会显示用户未登录

  • 开启一个定时任务,每天定时打开
private void TimerClockIn() {
       //新建定时任务
       TimerTask timerTask = new TimerTask() {
           @Override
           public void run() {
               System.out.println("时间=" + new Date());
               //todo 打卡
               try {
                   clockIn();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       };
       //设置执行时间
       Calendar calendar = Calendar.getInstance();
       int year = calendar.get(Calendar.YEAR);
       int month = calendar.get(Calendar.MONTH);
       int day = calendar.get(Calendar.DAY_OF_MONTH);//今天
       //明天的6:31:00执行,
       calendar.set(year, month, day, 6, 31, 0);
       calendar.add(Calendar.DATE, 1); //明天
       Date date = calendar.getTime();
       Timer timer = new Timer();
       System.out.println(date);
       timer.schedule(timerTask, date);
   }

开启之后,项目会一直运行,定时打卡。也可以用maven打包,放到云服务器上运行。

你可能感兴趣的:(java,java)