zyy
2025-10-29 c464451dab3467a0aace7402a39a6e9ad1b503b5
trading-order-admin/src/main/java/com/yami/trading/admin/task/cms/XueQiuInfomationGet.java
@@ -1,73 +1,127 @@
package com.yami.trading.admin.task.cms;
import java.util.List;
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.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.trading.admin.facade.MachineTranslationService;
import com.yami.trading.bean.cms.Infomation;
import com.yami.trading.common.config.ThreadPool;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.service.cms.InfomationService;
import cn.hutool.core.bean.BeanUtil;
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;
    @Autowired
    private MachineTranslationService translationService;
    private ThreadPoolTaskExecutor taskExecutor=ThreadPool.getApplicationThreadPool();
//    @Scheduled(cron = "0 0/30 * ? * *")
//    public void crawl(){
//        infomationService.getInformation();
//    }
    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(cron = "0 */5 * ? * *")
    public void translate(){
        QueryWrapper<Infomation> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("translate");
        queryWrapper.eq("lang", "zh-CN");
        List<Infomation> list = infomationService.list(queryWrapper);
        for(Infomation infomation : list){
                try{
                    String description = infomation.getDescription();
                    String source = infomation.getSource();
                    Infomation infomationEn = BeanUtil.copyProperties(infomation, Infomation.class);
                    infomationEn.setUuid(null);
                    if(StringUtils.isNotEmpty(description)){
                        String translate = translationService.translate(description);
                        if(translate == null){
                            return;
                        }
                        infomationEn.setDescription(translate);
                    }
                    if(StringUtils.isNotEmpty(source)){
                        String translate = translationService.translate(source);
                        if(translate == null){
                            return;
                        }
                        infomationEn.setSource(translate);
                    }
                    infomationEn.setLang("en");
                    infomationService.save(infomationEn);
                    infomation.setTranslate("1");
                    infomationService.updateById(infomation);
                }catch (Exception e){
                    log.info("翻译报错", e);
                }
    @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);
        }
    }
}