import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class TQDataProcessor {
private static final String ADMIN_UIN = "9690838";
private static final String KEY = "bjtiandijinghua-key";
private static final String TOKEN_URL = "http://webservice.edu.tq.cn/webservice/getAccessToken";
private static final String BUSINESS_URL_TEMPLATE = "http://webservice.edu.tq.cn/webservice/phoneRecord/list?access_token=%s&admin_uin=%s&pageSize=2000&pageNum=1&id=&start_time=%d&end_time=%d";
public static void main(String[] args) {
try {
long currentTimestamp = getCurrentTimestamp();
String sign = generateSign(currentTimestamp);
String accessToken = getAccessToken(currentTimestamp, sign);
long startTime = getStartOfDayTimestamp();
long endTime = getEndOfDayTimestamp();
String businessData = getBusinessData(accessToken, startTime, endTime);
saveJsonToFile(businessData, "business_data.json");
System.out.println("JSON 文件已生成:business_data.json");
} catch (Exception e) {
e.printStackTrace();
}
}
private static long getCurrentTimestamp() {
return System.currentTimeMillis() / 1000;
}
private static String generateSign(long timestamp) throws NoSuchAlgorithmException {
String input = ADMIN_UIN + "$" + KEY + "$" + timestamp;
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
private static String getAccessToken(long timestamp, String sign) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(TOKEN_URL);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode requestBody = objectMapper.createObjectNode();
requestBody.put("admin_uin", ADMIN_UIN);
requestBody.put("ctime", timestamp);
requestBody.put("sign", sign);
StringEntity entity = new StringEntity(requestBody.toString(), StandardCharsets.UTF_8);
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json");
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
ObjectNode responseJson = objectMapper.readValue(responseString, ObjectNode.class);
return responseJson.get("access_token").asText();
}
private static String getBusinessData(String accessToken, long startTime, long endTime) throws IOException {
String businessUrl = String.format(BUSINESS_URL_TEMPLATE, accessToken, ADMIN_UIN, startTime, endTime);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(businessUrl);
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
return EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
}
private static void saveJsonToFile(String json, String fileName) throws IOException {
try (FileWriter fileWriter = new FileWriter(fileName)) {
fileWriter.write(json);
}
}
private static long getStartOfDayTimestamp() {
LocalDate today = LocalDate.now();
LocalDateTime startOfDay = today.atStartOfDay();
return startOfDay.atZone(ZoneId.systemDefault()).toEpochSecond();
}
private static long getEndOfDayTimestamp() {
LocalDate today = LocalDate.now();
LocalDateTime endOfDay = today.atTime(14, 59, 59);
return endOfDay.atZone(ZoneId.systemDefault()).toEpochSecond();
}
}