| | |
| | | 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.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | 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().eq(News::getLanguage,language)); |
| | | List<News> list = list(Wrappers.<News>query().lambda().orderByDesc(News::getCreateTime)); |
| | | |
| | | if (!CollectionUtil.isEmpty(list)) { |
| | | |
| | |
| | | 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; |
| | | } |
| | | } |