| | |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.nq.common.ServerResponse; |
| | | import com.nq.dao.SiteNewsMapper; |
| | | import com.nq.enums.EStockType; |
| | | import com.nq.pojo.SiteNews; |
| | | import com.nq.service.ISiteNewsService; |
| | | import com.nq.utils.*; |
| | |
| | | import java.util.Base64; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | |
| | | /** |
| | | * 新闻资讯 |
| | |
| | | return ServerResponse.createBySuccess(pageInfo); |
| | | } |
| | | |
| | | /*新闻资讯-抓取*/ |
| | | @Override |
| | | public int grabNews() { |
| | | int ret = 0; |
| | | addNews(1, EStockType.IN.getStockUrl() +"stock-markets?key="+ EStockType.IN.getStockKey() +"&type=4"); |
| | | return ret; |
| | | public void grabNews() { |
| | | // 创建固定大小的线程池,根据需求调整线程数量 |
| | | ExecutorService executor = Executors.newFixedThreadPool(2); |
| | | try { |
| | | // 提交美国新闻抓取任务 |
| | | executor.submit(() -> |
| | | addNews(1, PropertiesUtil.getProperty("US_HTTP_API") + "stock-markets?key=" + PropertiesUtil.getProperty("US_KEY") + "&type=1") |
| | | ); |
| | | // 提交墨西哥新闻抓取任务 |
| | | executor.submit(() -> |
| | | addNews(1, PropertiesUtil.getProperty("MX_HTTP_API") + "stock-markets?key=" + PropertiesUtil.getProperty("MX_KEY") + "&type=1") |
| | | ); |
| | | } finally { |
| | | // 关闭线程池 |
| | | executor.shutdown(); |
| | | } |
| | | |
| | | } |
| | | |
| | | private int addNews(Integer type, String url){ |
| | | int k = 0; |
| | | private void addNews(Integer type, String url) { |
| | | try { |
| | | String newlist = HttpRequest.doGrabGet(url); |
| | | JSONArray jsonArray = JSONArray.fromObject(newlist); |
| | | if(jsonArray.size()>0){ |
| | | if (jsonArray.size() > 0) { |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JSONObject jsonObject = jsonArray.getJSONObject(i); |
| | | String newsId = jsonObject.getString("id"); |
| | | if(siteNewsMapper.getNewsBySourceIdCount(newsId) == 0){ |
| | | if (siteNewsMapper.getNewsBySourceIdCount(newsId) == 0) { |
| | | SiteNews siteNews = new SiteNews(); |
| | | siteNews.setSourceId(newsId); |
| | | siteNews.setSourceName(""); |
| | | siteNews.setTitle(jsonObject.getString("title")); |
| | | Long showTime = jsonObject.getLong("time"); |
| | | //Long showTime = jsonObject.getLong("time"); |
| | | long showTime = jsonObject.optLong("time", System.currentTimeMillis() / 1000); // 默认值,表示未找到或转换失败 |
| | | siteNews.setShowTime(new Date(showTime * 1000)); |
| | | siteNews.setImgurl(jsonObject.getString("img")); |
| | | if(jsonObject.has("img")){ |
| | | convertBase64ToImage(jsonObject.getString("img"),PropertiesUtil.getProperty("ftp.address")+newsId+".jpg"); |
| | | siteNews.setImgurl(PropertiesUtil.getProperty("ftp.server.http.prefix")+newsId+".jpg"); |
| | | } |
| | | siteNews.setDescription(jsonObject.getString("content")); |
| | | siteNews.setContent(jsonObject.getString("content")); |
| | | siteNews.setStatus(1); |
| | |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("添加新闻出现异常: " + e.getMessage()); |
| | | } |
| | | return k; |
| | | } |
| | | |
| | | |
| | | public static String convertBase64ToImage(String base64Str, String path) { |
| | | byte[] imageBytes = Base64.getDecoder().decode(base64Str); |
| | | try { |