스프링

 

https://okky.kr/article/390581

 



	@RequestMapping(value="/search/okky", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> searchOkky(@RequestParam(value="keyword") String keyword, 
			@RequestParam(value="maxpage", defaultValue="5") int maxpage,
			HttpServletRequest request) throws Exception{
		keyword = URLEncoder.encode(keyword, "UTF-8");
		int count = 1;
		
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet;
		
		String URL = "http://okky.kr/articles/questions?query="
				+keyword+"&sort=id&order=desc";
		
		List<Article> list = new ArrayList<Article>();
		while(true){
			
		httpGet = new HttpGet(URL);
		Document doc;
		
		ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
            public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity, "UTF-8") : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        };
		
		doc = Jsoup.parse(httpClient.execute(httpGet, responseHandler));
		Elements nodes = doc.select(".list-group-item h5");
		
		nodes.parallelStream().forEach(e -> {

			Article article = new Article();
			article.setTitle(e.text());
			article.setHref("http://okky.kr" + e.getElementsByTag("a").attr("href"));
			
			list.add(article);
		});
		
		Elements pages = doc.select(".pagination li");
		Element next = pages.last();
		
		boolean isNext = false;
		
		if(next != null)
			isNext = next.classNames().contains("next");
		
		if(!isNext || count >= maxpage)
			break;
		
		URL = "http://okky.kr" + next.getElementsByTag("a").attr("href");
		count++;
		}
		logger.info("Okky Search Request from "+request.getRemoteAddr());
		return new ResponseEntity<Object>(list, HttpStatus.ACCEPTED);
	}

 

 

about author

PHRASE

Level 60  머나먼나라

헛된 사람은 자신이 사람들에게 주는 혐오감도 모른 채 자기의 존재가 모든 사람에게 호감을 받는 줄 알고 흐믓해 하는 사람이다. -스피노자

댓글 ( 4)

댓글 남기기

작성