import com.alibaba.fastjson2.JSONObject
import kotlinx.coroutines.*
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.RequestEntity
import org.springframework.http.ResponseEntity
import org.springframework.web.client.RestTemplate
import java.net.URI
class ActivityData {
var activityIds: List<LinkedHashMap<String, Any>>? = null
var videoIds: List<Int>? = null
var videoWatchTimes: List<Int>? = null
}
fun main() {
val activityData = ActivityData()
runBlocking {
while (true) {
// 如果数据为空,或者需要刷新,调用接口获取数据
if (activityData.activityIds == null || needRefreshData()) {
activityData.activityIds = getActivityId()
activityData.videoIds = getVideoId()
activityData.videoWatchTimes = getVideoWatchTime()
}
// 创建协程列表,用于存储所有请求的 Deferred 对象
val requests = mutableListOf<Deferred<Unit>>()
// 使用协程启动多个异步请求
activityData.activityIds?.forEachIndexed { index, activity ->
val videoId = activityData.videoIds?.get(index)
val videoWatchTime = activityData.videoWatchTimes?.get(index)
if (videoId != null && videoWatchTime != null) {
val requestDeferred = async {
sendPostRequest(activity["id"] as Int, videoId, videoWatchTime)
}
requests.add(requestDeferred)
}
}
// 等待所有请求完成
requests.awaitAll()
// 暂停10秒钟
delay(10000)
}
}
}
fun needRefreshData(): Boolean {
return false
}
suspend fun sendPostRequest(activityId: Int, videoId: Int, videoWatchTime: Int) {
val url = "http://api.jijiaoguopei.com/api/spActivity/changeActivityStatusNew"
val headers = HttpHeaders()
// 设置请求头信息
headers.add("Accept", "application/json, text/plain, */*")
headers.add("Accept-Encoding", "gzip, deflate")
headers.add("Accept-Language", "zh-CN,zh;q=0.9")
headers.add(
"Authorization",“换成你的” )
headers.add("Content-Type", "application/json;charset=UTF-8")
headers.add("Content-Length", "79")
headers.add("Host", "api.jijiaoguopei.com")
headers.add("Origin", "http://jijiaoguopei.com")
headers.add("Proxy-Connection", "keep-alive")
headers.add("Referer", "http://jijiaoguopei.com/")
headers.add(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
// 构建请求体
val requestBody = """
{
"activityId": $activityId,
"videoCompleteFlag": 0,
"videoId": $videoId,
"videoWatchTime": $videoWatchTime
}
""".trimIndent()
// 发送POST请求
val restTemplate = RestTemplate()
val requestEntity: RequestEntity<String> = RequestEntity.post(URI.create(url))
.headers(headers)
.body(requestBody)
val responseEntity: ResponseEntity<String> = withContext(Dispatchers.IO) {
restTemplate.exchange(requestEntity, String::class.java)
}
// 处理响应,你可以根据需要进行处理
println("Response: ${responseEntity.body}")
}
fun sendGetRequest(): JSONObject? {
val url =
"http://api.jijiaoguopei.com/api/spActivity/getActivityByTeacherId?id=“你拿到的ID”&page=1&limit=30&specialTopicId=252&activityClassifyId=299&courseTypeId=&compulsoryFlag=1&code="
val requestEntity: RequestEntity<String> = requestEntity(url)
val restTemplate = RestTemplate()
val responseEntity: ResponseEntity<JSONObject> = restTemplate.exchange(requestEntity, JSONObject::class.java)
return responseEntity.body
}
private fun requestEntity(url: String): RequestEntity<String> {
val headers = HttpHeaders()
headers.add("Accept", "application/json, text/plain, */*")
headers.add("Accept-Encoding", "gzip, deflate")
headers.add("Accept-Language", "zh-CN,zh;q=0.9")
headers.add(
"Authorization",
)
headers.add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
headers.add("Host", "api.jijiaoguopei.com")
headers.add("Origin", "http://jijiaoguopei.com")
headers.add("Proxy-Connection", "keep-alive")
headers.add("Referer", "http://jijiaoguopei.com/")
headers.add(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
val requestEntity: RequestEntity<String> = RequestEntity(
headers,
HttpMethod.GET,
URI.create(url)
)
return requestEntity
}
fun getVideoId(): ArrayList<Int> {
val videoIds = ArrayList<Int>()
getActivityId().forEach { it ->
val activityId = it["id"] as Int
val url = "http://api.jijiaoguopei.com/api/spActivity/getById?id=$activityId&teacherId=你拿到的ID"
val requestEntity: RequestEntity<String> = requestEntity(url)
val restTemplate = RestTemplate()
val responseEntity: ResponseEntity<JSONObject> = restTemplate.exchange(requestEntity, JSONObject::class.java)
val video = ((responseEntity.body?.get("data")) as LinkedHashMap<*, *>)["courseChapters"] as ArrayList<*>
if (video.size > 0) {
video.forEach {
it as LinkedHashMap<*, *>
val videoId = it["video"] as ArrayList<LinkedHashMap<*, *>>
videoId.forEach { ids ->
val idd = ids["id"] as Int
videoIds.add(idd)
}
}
}
}
return videoIds
}
fun getVideoWatchTime(): ArrayList<Int> {
val videoWatchTimes = ArrayList<Int>()
getActivityId().forEach {
val activityId = it["id"] as Int
val url = "http://api.jijiaoguopei.com/api/spActivity/getTaskNode?teacherId=你的ID&activityId=$activityId"
val requestEntity: RequestEntity<String> = requestEntity(url)
val restTemplate = RestTemplate()
val responseEntity: ResponseEntity<JSONObject> = restTemplate.exchange(requestEntity, JSONObject::class.java)
val data = responseEntity.body?.get("data") as LinkedHashMap<*, *>
val videoWatchTime = data["videoWatchTime"] as Int
videoWatchTimes.add(videoWatchTime)
}
return videoWatchTimes
}
private fun getActivityId(): List<LinkedHashMap<String, Any>> {
val json = sendGetRequest()
val data = json?.get("data") as LinkedHashMap<*, *>
val list = data["activityInfo"] as ArrayList<LinkedHashMap<String, Any>>
// 使用filter函数过滤列表
val filteredList = list.filter {
// 获取LinkedHashMap中的allCompleteFlag字段,并检查其值是否为0
val allCompleteFlag = it["allCompleteFlag"] as? Int
allCompleteFlag == 0
}
return filteredList
}