Skip to content

Instantly share code, notes, and snippets.

@yiheng0

yiheng0/Daka.kt Secret

Created Jan 22, 2021
Embed
What would you like to do?
河海大学自动打卡
package moe.yiheng
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONArray
import org.json.JSONObject
import java.lang.Exception
class Daka(val userId: String, val password: String) {
private val loginUrl = "http://ids.hhu.edu.cn/amserver/UI/Login"
private val dakaUrl = "http://form.hhu.edu.cn/pdc/formDesignApi/S/gUTwwojq"
private val ua =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75"
private val excluded = arrayListOf("USERID", "CLRQ","XM_1474","XGH_336526","SFZJH_859173")
private val client = OkHttpClient.Builder()
.cookieJar(CookieJarKt())
.build()
private fun login(id: String, pass: String) {
val requestBody = FormBody.Builder()
.add("IDToken1", id)
.add("IDToken2", pass)
.add("IDButton", "Submit")
.add("goto", "aHR0cDovL2Zvcm0uaGh1LmVkdS5jbjo4MC9wZGMvZm9ybS9saXN0")
.add("encoded", "true")
.add("inputCode", "")
.add("gx_charset", "UTF-8")
.build()
val request = Request.Builder()
.addHeader("User-Agent", ua)
.url(loginUrl)
.post(requestBody)
.build()
client.newCall(request).execute()
}
private fun genWid(form: String): String {
return findString(form,"ar _selfFormWid = '(\\w+)';") ?: throw Exception("获取wid失败")
}
private fun getForm(): String {
val request = Request.Builder()
.url(dakaUrl)
.addHeader("User-Agent", ua)
.build()
return client.newCall(request).execute().body().string()
}
private fun getDataDetail(form: String): JSONObject {
val dataDetail: String = findString(form,"dataDetail = (.+),") ?: throw Exception("获取dataDetail失败")
val jsonArray = JSONArray(dataDetail)
return jsonArray.getJSONObject(0)
}
private fun getFillDetail(form: String): JSONObject {
val fillDetail: String = findString(form,"fillDetail = (.+);") ?: throw Exception("获取fillDetail失败")
val jsonArray = JSONArray(fillDetail)
return jsonArray.getJSONObject(0)
}
fun execute() {
login(userId, password)
val form = getForm()
val wid = genWid(form)
val dataDetail = getDataDetail(form)
val fillDetail = getFillDetail(form)
val bodyBuilder = FormBody.Builder()
.add("DATETIME_CYCLE", getDate())
for (s in dataDetail.keySet()) {
bodyBuilder.add(s, dataDetail.getString(s))
}
for (s in fillDetail.keySet()) {
val data = if (fillDetail.isNull(s)) "" else fillDetail.getString(s)
if (s !in excluded) {
bodyBuilder.add(s, data)
}
}
val request = Request.Builder()
.url("http://form.hhu.edu.cn/pdc/formDesignApi/dataFormSave?wid=${wid}&userId=${userId}")
.addHeader("User-Agent", ua)
.post(bodyBuilder.build())
.build()
val response = client.newCall(request).execute()
if (!response.body().string().contains("true")) {
throw Exception("打卡失败")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment