Skip to content

Commit

Permalink
Merge pull request #20 from kakao-asset/feature/#17-add-efk-log
Browse files Browse the repository at this point in the history
refactor: 폴더 구조 변경
  • Loading branch information
ur2e committed Dec 8, 2022
2 parents 35bb862 + 743def8 commit b21a2a3
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.kakaoasset.portfolio.controller;

import com.kakaoasset.portfolio.service.NewsStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "*")
public class NewsStockController {

@Autowired
private NewsStockService newsStockService;

@RequestMapping(value = "/main/news", method = RequestMethod.GET)
public String news(@RequestParam String stockCode) {
String result = "";

result = newsStockService.selectNewsStock(stockCode);

return result;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.kakaoasset.portfolio.controller;
import com.kakaoasset.portfolio.service.RealtimeStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "*")
public class RealtimeStockController {

@Autowired
private RealtimeStockService realtimeStockService;

@RequestMapping(value = "/main/realtime", method = RequestMethod.GET)
public String realtimeStock(@RequestParam String stock_name){

String result = null;

result = realtimeStockService.selectRealtimeStock(stock_name);

return result;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.kakaoasset.portfolio.controller;

import com.kakaoasset.portfolio.service.SearchRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
* packageName : com.kakaoasset.portfolio.elasticsearch.elasticAPI.controller
* fileName : SearchRankController
* author : Hwang
* date : 2022-11-14
*/

@RestController
@CrossOrigin(origins = "*")
public class SearchRankController {

@Autowired
private SearchRankService searchRankService;

@RequestMapping(value = "/search/rank", method = RequestMethod.GET)
public String searchRank(){

String result = null;

result = searchRankService.selectSearchRank();

return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.kakaoasset.portfolio.controller;

import com.kakaoasset.portfolio.service.SectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* packageName : com.kakaoasset.portfolio.elasticsearch.elasticAPI.controller
* fileName : SectorController
* author : Hwang
* date : 2022-11-14
*/

@RestController
@CrossOrigin(origins = "*")
public class SectorController {

@Autowired
private SectorService sectorService;

@RequestMapping(value = "/sector", method = RequestMethod.GET)
public String searchRank(@RequestParam String stock_sector){

String result = null;

result = sectorService.selectSectorStock(stock_sector);

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.kakaoasset.portfolio.service;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@Service
public class NewsStockService {
public String selectNewsStock(String stockCode) {
String result = "";
JSONArray jsonarr = new JSONArray();
String detail_url = "https://finance.daum.net/quotes/";

// make request for elasticsearch api
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add((request, body, execution) -> {
ClientHttpResponse response = execution.execute(request,body);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return response;
});

final HttpHeaders headers = new HttpHeaders();
headers.add("Referer","http://finance.daum.net");
headers.add("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36 OPR/58.0.3135.127");
final HttpEntity<?> entity = new HttpEntity<>(headers);
try {
// send request to elasticsearch

result = restTemplate.exchange("https://finance.daum.net/content/news?page=1&perPage=5&category=economy&searchType=all&keyword=" + stockCode, HttpMethod.GET, entity, String.class).getBody();
}catch (HttpClientErrorException e){
// no index
return new JSONObject("{\"error\":\"No Index\", \"index\":\""+stockCode+"\"}").toString();
}

// 뉴스 크롤링에 대한 response + 상세페이지를 보여주기 위한 newsId
JSONObject json = new JSONObject(result);
if(JSONObject.NULL != json.get("data")){
for (int i = 0; i < json.getJSONArray("data").length(); i++) {
JSONObject temp = (JSONObject) json.getJSONArray("data").get(i);
String newsId = ((JSONObject)json.getJSONArray("data").get(i)).getString("newsId");
temp.put("detail_url", detail_url + stockCode + "#news/stock/" + newsId);
jsonarr.put(temp);
}
}

return jsonarr.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.kakaoasset.portfolio.service;


import org.springframework.beans.factory.annotation.Value;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@Service
public class RealtimeStockService {

@Value("${elasticsearch.host}")
private String host;

public String selectRealtimeStock(String stock_name){
String index = "stock-data";
JSONArray jsonarr = new JSONArray();
String result = null;

// make request for elasticsearch api
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add((request, body, execution) -> {
ClientHttpResponse response = execution.execute(request,body);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return response;
});
final HttpHeaders headers = new HttpHeaders();
final HttpEntity<?> entity = new HttpEntity<>(headers);
try {
// send request to elasticsearch
result = restTemplate.exchange("http://"+host+":9200/"+index+"/_search?sort=datetime:acs&size=10000&q=" + stock_name, HttpMethod.GET, entity, String.class).getBody();
}catch (HttpClientErrorException e){
// no index
return new JSONObject("{\"error\":\"No Index\", \"index\":\""+stock_name+"\"}").toString();
}

JSONObject json = new JSONObject(result);
for (int i = 0; i < json.getJSONObject("hits").getJSONArray("hits").length(); i++) {
JSONObject temp = ((JSONObject) json.getJSONObject("hits").getJSONArray("hits").get(i)).getJSONObject("_source");
jsonarr.put(temp);
}
return jsonarr.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.kakaoasset.portfolio.service;


import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

/**
*packageName : com.kakaoasset.portfolio.elasticsearch.elasticAPI.service
* fileName : SearchRankController
* author : Hwang
* date : 2022-11-14
*/

@Service
public class SearchRankService {

@Value("${elasticsearch.host}")
private String host;

public String selectSearchRank(){

String index = "stock-rank";
JSONArray jsonarr = new JSONArray();
String result = null;

// make request for elasticsearch api
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add((request, body, execution) -> {
ClientHttpResponse response = execution.execute(request,body);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return response;
});
final HttpHeaders headers = new HttpHeaders();
final HttpEntity<?> entity = new HttpEntity<>(headers);
try {
// send request to elasticsearch

result = restTemplate.exchange("http://"+host+":9200/"+index+"/_search?sort=datetime:acs", HttpMethod.GET, entity, String.class).getBody();
}catch (HttpClientErrorException e){
// no index
return new JSONObject("{\"error\":\"No Index\"").toString();
}

JSONObject json = new JSONObject(result);

System.out.println("-------------------------------api-search-rank---------------------------");

for (int i = 0; i < json.getJSONObject("hits").getJSONArray("hits").length(); i++) {
JSONObject temp = ((JSONObject) json.getJSONObject("hits").getJSONArray("hits").get(i)).getJSONObject("_source");
jsonarr.put(temp);
}
return jsonarr.toString();
}
}
Loading

0 comments on commit b21a2a3

Please sign in to comment.