peter
2025-07-16 076a457c7c7d005075aa8247ee0b214e94418786
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
128
129
130
package com.yami.trading.service.cms.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.trading.bean.cms.Cms;
import com.yami.trading.bean.cms.News;
import com.yami.trading.bean.cms.dto.NewsDto;
import com.yami.trading.common.util.GoogleAuthenticator;
import com.yami.trading.dao.cms.CmsMapper;
import com.yami.trading.dao.cms.NewsMapper;
import com.yami.trading.service.cms.CmsService;
import com.yami.trading.service.cms.NewsSerivce;
import com.yami.trading.util.GoogleTranslateUtil;
import jodd.util.PropertiesUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
 
@Service
public class NewsSerivceImpl extends ServiceImpl<NewsMapper, News> implements NewsSerivce {
 
    @Value("${http.prefix}")
    private String httpPrefix;
    @Override
    public Page<NewsDto> pageNews(Page page,String title,String language,String userCode) {
        return baseMapper.pageNews(page,title,language,userCode);
    }
 
    @Override
    public News getIndex(String language) {
        List<News> list  = list(Wrappers.<News>query().lambda().orderByDesc(News::getCreateTime));
 
        if (!CollectionUtil.isEmpty(list)) {
 
            Date date = new Date();
 
            for (News news : list) {
 
                if ((null != news.getStartTime() && date.before(news.getStartTime()))
                        || (null != news.getEndTime() && news.getEndTime().before(date))) {
                    continue;
                }
 
                if (news.isIndexTop() && !news.isPopUp()) {
                    list.add(news);
                }
            }
        }
 
        if (CollectionUtils.isEmpty(list))
            return null;
        return list.get(0);
 
    }
 
    @Override
    public void grabNews() {
 
        addNews(1, "http://api-in-pro.js-stock.top/stock-markets?key=xKChgi47AP1NMwMeYI3c&type=1&country_id=14");
    }
 
    private void addNews(Integer type, String url) {
        // 使用CloseableHttpClient发送请求
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            // 创建HttpGet请求
            HttpGet request = new HttpGet(url);
 
            // 执行请求并获取响应
            HttpResponse response = client.execute(request);
 
            // 输出响应状态码
            System.out.println("响应状态: " + response.getStatusLine());
            GoogleTranslateUtil googleTranslateUtil = new GoogleTranslateUtil();
 
            // 获取响应内容并转换成字符串
            String content = EntityUtils.toString(response.getEntity());
            JSONArray jsonArray = new JSONArray(content);
            if (jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String newsId = jsonObject.getString("id");
                    if (list(Wrappers.<News>query().lambda().eq(News::getSourceId,newsId)).size() == 0) {
                        News siteNews = new News();
                        siteNews.setSourceId(newsId);
                        siteNews.setContent(googleTranslateUtil.translate("en","zh-TW",jsonObject.getString("content")));
                        siteNews.setTitle(googleTranslateUtil.translate("en", "zh-TW",jsonObject.getString("title")));
                        if(jsonObject.has("img")){
                            convertBase64ToImage(jsonObject.getString("img"), com.yami.trading.common.util.PropertiesUtil.getProperty("loca.images.dir")  + "/" +newsId+".jpg");
                            siteNews.setImgUrl(httpPrefix + newsId+".jpg");
                        }
                        siteNews.setCreateTime(new Date());
                        save(siteNews);
                    }
                }
            }
        } catch (Exception e) {
            // 捕获并打印异常
            e.printStackTrace();
            log.error("添加新闻出现异常: " + e.getMessage());
        }
    }
 
    public static String  convertBase64ToImage(String base64Str, String path) {
        byte[] imageBytes = Base64.getDecoder().decode(base64Str);
        try {
            File file = new File(path);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(imageBytes);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  path;
    }
}