/**
* 获取每日新闻,添加到文章里面
* @param
* @return
*/
@Override
public ResponseResult getDaysNews() {
//调用每日简报的接口
String body = HttpUtil
.createGet(DAYS_NEWS_API + "?key=" + TIAN_KEY)
.execute()
.body();
JSONObject jsonObjectResult = JSON.parseObject(body);
Integer code = jsonObjectResult.getInteger("code");
if(code!=200){
log.error("请求每日新闻的接口出错,出错信息:"+body);
return ResponseResult.error();
}
log.error("请求每日新闻的接口成功:"+body);
JSONObject resultObject = jsonObjectResult.getJSONObject("result");
//获取新闻列表
JSONArray contentList = resultObject.getJSONArray("list");
//获取文章的封面信息
String strResult = restTemplate.getForObject(IMG_URL_API, String.class);
JSONObject jsonObject = JSON.parseObject(strResult);
Object imgUrl = jsonObject.get("imgurl");
//将新闻列表封装成markdown文章
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
String todayDate = simpleDateFormat.format(new Date());
String titleContent = "# "+todayDate+"新闻简要<br/>";
StringBuilder stringBuilder = new StringBuilder(titleContent);
//发送到微信群里面的信息
JSONObject contentObject = new JSONObject();
contentObject.put("todayDate",todayDate);
JSONArray array = new JSONArray();
//遍历接口返回来的json数据
contentList.forEach(item->{
JSONObject arrayObject = new JSONObject();
JSONObject itemObject = (JSONObject)item;
stringBuilder.append("??");
stringBuilder.append(itemObject.getString("mtime"));
stringBuilder.append("<br/>");
stringBuilder.append("### ");
stringBuilder.append("?????");
stringBuilder.append(itemObject.getString("title"));
stringBuilder.append("<br/>");
stringBuilder.append("????");
stringBuilder.append(itemObject.getString("digest"));
stringBuilder.append("<br/>");
stringBuilder.append("<br/>");
stringBuilder.append("---");
stringBuilder.append("<br/>");
//标题
arrayObject.put("title",itemObject.getString("title"));
//内容
arrayObject.put("digest",itemObject.getString("digest"));
//时间
arrayObject.put("mtime",itemObject.getString("mtime"));
array.add(arrayObject);
});
contentObject.put("list",array);
stringBuilder.append("(本文所有数据均来自天行数据api,如有侵权,联系即删~)");
//将拿到的内容放在Markdown中
String newContent = stringBuilder.toString();
MutableDataSet options = new MutableDataSet();
String markdown = FlexmarkHtmlConverter
.builder(options)
.build()
.convert(newContent)
.replace("lang-java","java");
//构建文章的实体
BlogArticle entity = BlogArticle.builder()
.userId(7L)
.contentMd(markdown)
.categoryId(DAYYS_NEWS)
.isOriginal(YesOrNoEnum.NO.getCode())
//转载地址
.originalUrl("https://www.tianapi.com/")
.isPublish(1)
.keywords("每日新闻,穆雄雄,新闻简报,早安")
.title(todayDate+"新闻简要")
.avatar(imgUrl.toString())
.content(newContent)
.build();
baseMapper.insert(entity);
//添加标签
tagsMapper.saveArticleTags(entity.getId(),new ArrayList<Long>() {{add(63L);}});
log.info("每日新闻添加到文章成功:"+ JSON.toJSONString(entity));
contentObject.put("aid",entity.getId());
//调用推送到微信的方法
sendWechat(contentObject);
return ResponseResult.success("发送成功");
}
|