ipo
zyy
2026-01-08 4affbdf8938d321c0926bc2b1832dfc81c317ffa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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);
        }
    }
}