美文网首页开发者日记
如何从第三方导出笑话数据到自己的服务器

如何从第三方导出笑话数据到自己的服务器

作者: IT老五 | 来源:发表于2018-12-09 22:02 被阅读1次

    自己开发的app有个段子模块,是从第三方导入的,用过聚合、点睛、showapi、阿里数据等,后来都有了调用次数限制,最初还开了一年聚合的会员,但是对于非盈利个人开发者,收费还是有点稍高。于是就折腾着将第三方的笑话数据导出来,自己搭了个后台,基于简单方便且免费,选择了bmob。

    于是开始导出数据,选用的是最熟悉的java,将多个平台的数据导出。聚合、showapi都是免费的,每天导出的数据量有限,阿里充值了钱,基本可以导出数据平台的所有笑话和趣图。以下是导出数据的代码:

    // 聚合数据
    private static String doGetJuhe(int pageNum, String key) {
        String url = "http://v.juhe.cn/joke/content/text.php";
        url += "?page="+pageNum+"&pagesize=20&key="+key;
        String result = sendGetRequest(url);
        return result;
    }
    // HaoService
    private static String doGetHaoService(int pageNum,String key) {
        String url = "http://apis.haoservice.com/lifeservice/Joke/ContentList";
        url += "?key="+key+"&pagesize=10&page=+"+pageNum+"&paybyvas=false";
        String result = sendGetRequest(url);
        return result;
    }
    /**
     * 阿里数据
     * @param pageNum
     * @param path "/picJoke" "/gifJoke" "/textJoke"
     * @return
     */
    public static String doGetAli(int pageNum, String path, String appCode) {
        String host = "https://ali-joke.showapi.com";
        //String path = "/picJoke";//"/gifJoke";//"/textJoke";
        String method = "GET";
        String appcode = appCode;
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("maxResult", "50");
        querys.put("page", "" + pageNum);
        querys.put("time", "2018-07-19");
        try {
            /**
            * 重要提示如下:
            * HttpUtils请从
            * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
            * 下载
            *
            * 相应的依赖请参照
            * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
            */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            return EntityUtils.toString(response.getEntity());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    

    于是,开始折腾bmob,搭建应用审核后,建一个笑话数据库:主要字段包括 _id(自增id)、title(标题)、content(内容)、createTime(发布时间)、images(图片列表)、author(作者,与user表关联)、type(类型,笑话/趣图/文章...),其他字段(如feeds,likes,favs,addr,addrName,extra拓展字段等)
    然后,需要将之前导出的数据导入到bmob数据库中,bmob支持csv及json文件导入,按照模板格式即可,java代码如下:

    // haoservice
    private static JSONArray jsonToHaoService(JSONArray resultJa, String str) {
        try {
            JSONObject jsonO = new JSONObject(str);
            JSONArray jsonA = jsonO.optJSONArray("result");
            for(int i = 0; i < jsonA.length(); i++) {
                JSONObject jo = jsonA.optJSONObject(i);
                if(!jo.has("content") || jo.optString("content").length() < 10) {
                    continue;
                }
                JSONObject resultJo = new JSONObject();
                resultJo.put("type", "1");
                resultJo.put("title", jo.optString("title"));
                resultJo.put("content", jo.optString("content").replaceAll("&#8205;", "").replaceAll("&zwj;", "").replaceAll("&hellip;", ""));
                resultJo.put("time", jo.optString("updatetime") + ":00");
                if(resultJa == null) {
                    resultJa = new JSONArray();
                }
                resultJa.put(resultJo);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(resultJa == null) {
            return null;
        }
        return resultJa;
    }
    // 聚合数据
    private static JSONArray jsonToJuhe(JSONArray resultJa, String str) {
        try {
            JSONObject jsonO = new JSONObject(str);
            JSONObject jsonO2 = jsonO.optJSONObject("result");
            JSONArray jsonA = jsonO2.optJSONArray("data");
            for(int i = 0; i < jsonA.length(); i++) {
                JSONObject jo = jsonA.optJSONObject(i);
                if(!jo.has("content") || jo.optString("content").length() < 10) {
                    continue;
                }
                JSONObject resultJo = new JSONObject();
                resultJo.put("type", "1");
                resultJo.put("title", "");
                resultJo.put("content", jo.optString("content"));
                resultJo.put("time", jo.optString("updatetime"));
                if(resultJa == null) {
                    resultJa = new JSONArray();
                }
                resultJa.put(resultJo);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(resultJa == null) {
            return null;
        }
        return resultJa;
    }
    // 阿里图片 pic和gif
    private static JSONArray jsonToAliImage(JSONArray resultJa, String str) {
        try {           
            JSONObject jsonO = new JSONObject(str);
            JSONObject jsonO2 = jsonO.optJSONObject("showapi_res_body");
            JSONArray jsonA = jsonO2.optJSONArray("contentlist");
            for(int i = 0; i < jsonA.length(); i++) {
                JSONObject jo = jsonA.optJSONObject(i);
                if(!jo.has("img") || jo.optString("img").length() < 10) {
                    continue;
                }
                JSONObject resultJo = new JSONObject();
                resultJo.put("type", "2");
                resultJo.put("title", jo.optString("title"));
                resultJo.put("img", jo.optString("img"));
                resultJo.put("time", jo.optString("ct").substring(0, 19));
                if(resultJa == null) {
                    resultJa = new JSONArray();
                }
                resultJa.put(resultJo);
            }
        } catch (Exception e) {
            System.out.println(str);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(resultJa == null) {
            return null;
        }
        return resultJa;
    }
    // 阿里文本
    private static JSONArray jsonToAli(JSONArray resultJa, String str) {
        try {           
            JSONObject jsonO = new JSONObject(str);
            JSONObject jsonO2 = jsonO.optJSONObject("showapi_res_body");
            JSONArray jsonA = jsonO2.optJSONArray("contentlist");
            for(int i = 0; i < jsonA.length(); i++) {
                JSONObject jo = jsonA.optJSONObject(i);
                if(!jo.has("text") || jo.optString("text").length() < 10) {
                    continue;
                }
                JSONObject resultJo = new JSONObject();
                resultJo.put("type", "1");
                resultJo.put("title", jo.optString("title"));
                resultJo.put("content", jo.optString("text"));
                resultJo.put("time", jo.optString("ct").substring(0, 19));
                if(resultJa == null) {
                    resultJa = new JSONArray();
                }
                resultJa.put(resultJo);
            }
        } catch (Exception e) {
            System.out.println(str);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(resultJa == null) {
            return null;
        }
        return resultJa;
    }
    

    原文:简书ThinkinLiu 博客: IT老五

    ps:这里主要是记录下来自己以后可能用到,可能写得不是太明白,没去整理了

    相关文章

      网友评论

        本文标题:如何从第三方导出笑话数据到自己的服务器

        本文链接:https://www.haomeiwen.com/subject/znakhqtx.html