团购API使用示例


如何获取全量团购


点评提供了4个API,方便开发者快速的获取全量的团购数据信息,分别为:

  1. 1.http://api.dianping.com/v1/metadata/get_cities_with_deals
  2. 2.http://api.dianping.com/v1/deal/get_all_id_list
  3. 3.http://api.dianping.com/v1/deal/get_batch_deals_by_id
  4. 4.http://api.dianping.com/v1/deal/get_single_deal


这里我们通过一个示例演示如何获取全部城市的团购数据,主要步骤为:

  1. 1.获取所有支持团购的城市,大概有145个
  2. 2.获取所有城市的在线团购id列表
  3. 3.根据团购id列表批量获取数据详情

获取所有支持团购的城市

     /**
     * 功能描述: 获取所有支持团购的城市
     */
     public static List requestCities(String appKey, String secret) throws JsonProcessingException,
            IOException
     {
        String apiUrl = "http://api.dianping.com/v1/metadata/get_cities_with_deals";
        String jsonResult = DemoApiTool.requestApi(apiUrl, appKey, secret, new HashMap());
        JsonNode tree = JSON_MAPPER.readTree(jsonResult);
        JsonNode citiesNode = tree.get("cities");
        List cities = new ArrayList();
        for (Iterator iterator = citiesNode.getElements(); iterator.hasNext();)
        {
            JsonNode node = iterator.next();
            cities.add(node.getTextValue());
        }
        return cities;
     }

获取所有城市的在线团购id列表

     /**
     * 功能描述:获取所有城市的DealID
     */
     public static Map> requestDealIds(String appKey, String secret, List dealCities)
            throws JsonProcessingException, IOException
     {
        Map> dealIds = new HashMap>();
        String apiUrl = "http://api.dianping.com/v1/deal/get_all_id_list";
        Map paramMap = new HashMap();
        for (String city : dealCities)
        {
            paramMap.put("city", city);
            String jsonResult = DemoApiTool.requestApi(apiUrl, appKey, secret, paramMap);
            JsonNode tree = JSON_MAPPER.readTree(jsonResult);
            JsonNode idsNode = tree.get("id_list");
            List ids = new ArrayList();
            for (Iterator iterator = idsNode.getElements(); iterator.hasNext();)
            {
                JsonNode node = iterator.next();
                ids.add(node.getTextValue());
            }
            dealIds.put(city, ids);
        }
        return dealIds;
     }

根据团购id列表批量获取数据详情

    /**
     * 功能描述:获取所有的团购的信息
     */
    public static Map>> requestDealData(String appKey, String secret,
                                                                             Map> dealIds) throws IOException
    {
        Map>> dealDatas = new HashMap>>();
        for (Entry> entry : dealIds.entrySet())
        {
            String city = entry.getKey();
            List dealIdsWithinCity = entry.getValue();
            List> dealDataList = new ArrayList>();
            // 分割id列表为40大小的子列表,便于批量获取团购信息
            List> batchIdsList = splitList(dealIdsWithinCity, 40);
            for (List batchIds : batchIdsList)
            {
                dealDataList.addAll(requestBatchDeals(appKey, secret, batchIds));
            }
            dealDatas.put(city, dealDataList);
        }
        return dealDatas;
    }

    /**
     * 功能描述:批量请求团购信息,每次最多40条,这里使用jackson-mapper来解析JSON。
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List> requestBatchDeals(String appKey, String secret,
                                                               List batchIdsList) throws JsonParseException, JsonMappingException, IOException
    {
        String batchIdsParam = StringUtils.join(batchIdsList, ',');
        String apiUrl = "http://api.dianping.com/v1/deal/get_batch_deals_by_id";
        Map paramMap = new HashMap();
        paramMap.put("deal_ids", batchIdsParam);
        String jsonResult = DemoApiTool.requestApi(apiUrl, appKey, secret, paramMap);
        Map resultMap = JSON_MAPPER.readValue(jsonResult, Map.class);
        List> deals = (List>) resultMap.get("deals");
        return deals;
    }

调用的代码

        /**
         * 调用的代码
         */
        List dealCities = requestCities(appKey, secret);
        Map> dealIds = requestDealIds(appKey, secret, dealCities);
        Map>> dealDatas = requestDealData(appKey, secret, dealIds);
        ....
	

完整的代码见:https://github.com/dianping/dianping-open-sdk/blob/master/samples/dianping-java-samples/src/main/java/com/dianping/open/samples/deal/DemoDealAPI.java


注意事项:

  1. 1.注意控制请求的频率,我们允许每秒最大20次访问,建议在10-15次左右
  2. 2.团购分类全国单和本地单,city参数输入"全国",即为全国单,对应deal.city字段也为"全国"
  3. 3.全国单可以在每个城市售卖,对于导航类网站建议把全国单分散到各个城市,每个城市的团购总数等于本地单加上全国单
  4. 4.可以根据开发者应用的特色,定制需要的城市和分类,这里是获取所有城市的所有分类的团购数据
  5. 5.假定点评所有在线团购数量为100000左右,其中全国单在20000左右,按照1秒10个请求,每个请求最多返回40条团购信息,在10分钟以内可以取完所有的团购信息