welcome to xlongwei.com

欢迎大家一起学习、交流、分享


QQ:9167702333 邮箱:admin@xlongwei.com

jokes 笑口常开


分类 Java   关键字 分享   标签 java   web   algorithm   发布 hongwei  1452145195820
注意 转载须保留原文链接,译文链接,作者译者等信息。  
jokes提供日常所需的笑话欣赏,数据来源于ShowAPI提供的免费笑话接口,demo见:http://cms.xlongwei.com/demo/jokes.html

ShowAPI免费笑话接口:
笑话大全:https://www.showapi.com/api/lookPoint/341
来福岛笑话:https://www.showapi.com/api/lookPoint/107

type标记来源接口和图文类型,jokes是分页列表(Map是对笑话条目的封装)
public String jokes(HttpServletRequest request, Model model) {
String type = RequestUtil.getStringParam(request, "type", "jokes-text");
List<Map> jokes = ShowApiUtil.jokes(type);
model.addAttribute("type", type);
model.addAttribute("jokes", jokes);
return "demo/jokes";
}

请求ShowAPI笑话数据,本地适当缓存部分内容。部分源码:JSONUtiljokes.vm
public static List<Map> jokes(String type) {
if(StringUtil.isBlank(type)) type = "laifudao-text";
switch(type){
case "laifudao-text":
case "laifudao-img":
logger.info("laifudao post for type: "+type);//来福岛笑话没有分页参数,每次请求都是新的数据
return JSONUtil.getList(post("http://route.showapi.com/107-"+("laifudao-text".equals(type)?"32":"33"), null), "showapi_res_body.list", Map.class);
case "jokes-text":
case "jokes-img":
String key = type+".pages", cache="jokes";
int pages = NumberUtil.parseInt(CacheUtil.get(cache, key), 1000);
int page =RandomUtils.nextInt(pages)+1;//随机某页数据
String post = CacheUtil.get(cache, key+"."+page);//缓存某页数据
if(StringUtil.isBlank(post)){
Map<String, String> params = new HashMap<>();
params.put("time", "2015-01-01");
params.put("page", String.valueOf(page));
post = post("http://route.showapi.com/341-"+("jokes-text".equals(type)?"1":"2"), params);
CacheUtil.set(cache, key+"."+page, post);//缓存某页数据
int allPages = JSONUtil.getInt(post, "showapi_res_body.allPages");
if(allPages>pages) CacheUtil.set(cache, key, String.valueOf(allPages));//更新总页数
logger.info("jokes post for type: "+type+", allPages: "+allPages+", updatePages: "+(allPages>pages));
}else{
logger.info("jokes cached for type: "+type);
}
return JSONUtil.getList(post, "showapi_res_body.contentlist", Map.class);
}
logger.warn("not supportted jokes type: "+type);
return null;
}

微信公众号访问:微信扫码关注公众号“xlongwei”,发送消息“笑话”,即可收到:“笑话欣赏,供您一乐”。