Java tutorial
/* * Copyright 2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.cuizuoli.appranking.service; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.exception.ExceptionUtils; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.springframework.stereotype.Service; import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.RestTemplate; import cn.cuizuoli.appranking.enumeration.Category; import cn.cuizuoli.appranking.enumeration.Country; import cn.cuizuoli.appranking.enumeration.DeviceType; import cn.cuizuoli.appranking.enumeration.FeedType; import cn.cuizuoli.appranking.enumeration.MediaType; import cn.cuizuoli.appranking.model.AppRanking; /** * GooglePlayService * @author cuizuoli */ @Slf4j @Service public class GooglePlayService { private final static String GOOGLE_PLAY_HOT_URL = "https://play.google.com/store/apps/collection/%s?start=0&num=50"; private final static String GOOGLE_PLAY_URL = "https://play.google.com/store/apps/category/%s/collection/%s?start=0&num=50"; private final static String GOOGLE_PLAY_DOMAIN = "https://play.google.com"; @Resource private RestTemplate appRankingRestTemplate; /** * getHotUrl * @param feedType * @return */ public String getHotUrl(FeedType feedType) { if (feedType.getMediaType() == MediaType.GOOGLE) { return String.format(GOOGLE_PLAY_HOT_URL, feedType.getCode()); } return StringUtils.EMPTY; } /** * getUrl * @param feedType * @param category * @return */ public String getUrl(FeedType feedType, Category category) { if (feedType.getMediaType() == MediaType.GOOGLE) { return String.format(GOOGLE_PLAY_URL, category.getCode(), feedType.getCode()); } return StringUtils.EMPTY; } /** * getAppRankingList * @param feedType * @return */ public List<AppRanking> getAppRankingList(FeedType feedType, Category category) { List<AppRanking> appRankingList = new ArrayList<AppRanking>(); try { if (feedType.getMediaType() == MediaType.GOOGLE) { String url = StringUtils.EMPTY; if (category == Category.ALL) { url = getHotUrl(feedType); } else { url = getUrl(feedType, category); } log.info("Google Play -> " + url); if (StringUtils.isNotBlank(url)) { Document doc = appRankingRestTemplate.getForObject(url, Document.class); Elements elements = doc.select(".card-list>.card"); Iterator<Element> iterator = elements.iterator(); int i = 1; while (iterator.hasNext()) { Element element = iterator.next(); String appId = element.attr("data-docid"); String name = element.select(".details .title").attr("title"); String uri = element.select(".details .title").attr("href"); String artist = element.select(".details .subtitle").attr("title"); String price = element.select(".details button.price.buy>span").text(); String image170 = element.select(".cover .cover-image").attr("data-cover-small"); String image340 = element.select(".cover .cover-image").attr("data-cover-large"); AppRanking appRanking = new AppRanking(); appRanking.setAppId(appId); appRanking.setDeviceType(DeviceType.ANDROID); appRanking.setCountry(Country.JAPAN); appRanking.setMediaType(MediaType.GOOGLE); appRanking.setFeedType(feedType); appRanking.setRanking(i); appRanking.setTitle(name + " - " + artist); appRanking.setCategory(category.getCode()); appRanking.setUri(GOOGLE_PLAY_DOMAIN + uri); appRanking.setName(name); appRanking.setArtist(artist); appRanking.setPrice(price); appRanking.setImage53(image170); appRanking.setImage75(image170); appRanking.setImage100(image340); appRankingList.add(appRanking); i++; } } } } catch (HttpStatusCodeException e) { log.error(ExceptionUtils.getFullStackTrace(e)); } catch (Exception e) { log.error(ExceptionUtils.getFullStackTrace(e)); } return appRankingList; } }