package com.yami.trading.admin.task.cms;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.yami.trading.bean.cms.Infomation;
|
import com.yami.trading.common.util.DateUtil;
|
import com.yami.trading.service.cms.InfomationService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.time.Instant;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
@Slf4j
|
@Component
|
public class XueQiuInfomationGet {
|
|
@Autowired
|
private InfomationService infomationService;
|
|
|
private final AtomicInteger requestCount = new AtomicInteger(0);
|
private static final int DAILY_LIMIT = 100;
|
private static final long INTERVAL = 86400 / DAILY_LIMIT * 1000; // 864秒/次
|
|
@Scheduled(fixedRate = INTERVAL)
|
public void callApi() {
|
if (requestCount.get() < DAILY_LIMIT) {
|
// 替换为你的实际API调用逻辑
|
System.out.println("调用API (" + requestCount.incrementAndGet() + "/100) " +
|
"时间: " + System.currentTimeMillis());
|
}
|
}
|
|
@Scheduled(cron = "0 0 0 * * ?") // 每天0点重置计数器
|
public void resetCounter() {
|
requestCount.set(0);
|
}
|
|
@Scheduled(fixedRate = INTERVAL)
|
public void translate(){
|
if (requestCount.get() < DAILY_LIMIT) {
|
String apiUrl = "https://newsapi.org/v2/top-headlines?sources=bbc-news&apiKey=576edade92ff4b63b2b445a6292f3fbf";
|
try {
|
String json = sendGetRequest(apiUrl);
|
List<Infomation> newsList = new ArrayList<>();
|
|
JSONObject jsonObject = JSONObject.parseObject(json);
|
String status = jsonObject.get("status").toString();
|
if(status.equals("ok")) {
|
JSONArray articles = jsonObject.getJSONArray("articles");
|
for (Object object : articles) {
|
JSONObject article = (JSONObject) object;
|
Infomation infomation = new Infomation();
|
infomation.setTitle((String) article.get("title"));
|
infomation.setDescription((String) article.get("description"));
|
infomation.setOriginUrl((String) article.get("url"));
|
infomation.setImg((String) article.get("urlToImage"));
|
String dateStr = (String) article.get("publishedAt");
|
Instant instant = Instant.parse(dateStr);
|
infomation.setCreateTime(Date.from(instant));
|
infomation.setCreatedAt(DateUtil.formatDate(infomation.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
|
infomation.setContent((String) article.get("content"));
|
infomation.setLang("en");
|
newsList.add(infomation);
|
}
|
newsList.forEach(f->{
|
List<Infomation> list = infomationService.list(new LambdaQueryWrapper<>(Infomation.class).eq(Infomation::getTitle, f.getTitle()));
|
if(CollectionUtil.isEmpty(list)){
|
infomationService.save(f);
|
}
|
});
|
}
|
} catch (Exception e) {
|
log.error(e.getMessage());
|
System.err.println("新闻请求出错: " + e.getMessage());
|
}
|
}
|
}
|
|
public static String sendGetRequest(String urlString) throws Exception {
|
URL url = new URL(urlString);
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
// 设置请求方法
|
connection.setRequestMethod("GET");
|
|
// 设置连接超时
|
connection.setConnectTimeout(5000);
|
// 设置读取超时
|
connection.setReadTimeout(5000);
|
|
// 设置请求头(可选)
|
connection.setRequestProperty("Accept", "application/json");
|
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
|
// 获取响应代码
|
int responseCode = connection.getResponseCode();
|
System.out.println("响应状态码: " + responseCode);
|
|
// 读取响应内容
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
String inputLine;
|
StringBuilder response = new StringBuilder();
|
|
while ((inputLine = in.readLine()) != null) {
|
response.append(inputLine);
|
}
|
in.close();
|
return response.toString();
|
} else {
|
throw new RuntimeException("HTTP 请求失败: " + responseCode);
|
}
|
}
|
}
|