zj
2024-04-23 b7d0c5e9ddfedc068d2f82fab530e71043a7f4af
src/main/java/com/nq/service/impl/SiteNewsServiceImpl.java
@@ -3,11 +3,12 @@
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nq.common.ServerResponse;
import com.nq.constant.StockConstant;
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 com.nq.utils.http.HttpRequest;
import com.nq.utils.translate.GoogleTranslateUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@@ -17,6 +18,10 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Date;
import java.util.List;
@@ -134,18 +139,11 @@
    /*新闻资讯-抓取*/
    @Override
    public int grabNews() {
        int ret = 0;
        //新闻类型:1、财经要闻,2、经济数据,3、全球股市,4、7*24全球,5、商品资讯,6、上市公司,7、全球央行
         addNews(1, StockConstant.HTTP_API +"stock-markets?key="+ StockConstant.KEY +"&type=4");
         addNews(2, StockConstant.HTTP_API+"stock-markets?key="+ StockConstant.KEY +"&type=5");
         addNews(4, StockConstant.HTTP_API+"stock-markets?key="+ StockConstant.KEY +"&type=6");
        return ret;
            int ret = 0;
         addNews(1, EStockType.IN.getStockUrl() +"stock-markets?key="+ EStockType.IN.getStockKey() +"&type=4");
         return ret;
    }
    /*
    *抓取新闻专用
    * type:新闻类型:1、财经要闻,2、经济数据,3、全球股市,4、7*24全球,5、商品资讯,6、上市公司,7、全球央行
    * */
    private int addNews(Integer type, String url){
        int k = 0;
        try {
@@ -163,7 +161,8 @@
                        Long showTime = jsonObject.getLong("time");
                        siteNews.setShowTime(new Date(showTime));
                        if(jsonObject.has("img")){
                            siteNews.setImgurl(jsonObject.getString("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"));
@@ -179,4 +178,18 @@
        return k;
    }
    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;
    }
}