1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  @Transactional
    public void saveDayItemURL(){
        List<UpbitCoin> upbitCoins=upbitCoinRepository.findAll();
        for(UpbitCoin upbitCoin: upbitCoins){
            String code=upbitCoin.getMarket();
            String DayURL="https://crix-api-cdn.upbit.com/v1/crix/candles/days?" +
                    "code=CRIX.UPBIT."+code+
                    "&count=10";
            System.out.println(DayURL);
            DateTimeFormatter format = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
            try{
                URL postUrl = new URL(DayURL);
                HttpURLConnection con = (HttpURLConnection)postUrl.openConnection();
                Object obj = JSONValue.parse(new InputStreamReader(con.getInputStream()));
                JSONArray jObj = (JSONArray) obj;
                ObjectMapper om = new ObjectMapper();
                for (int i = 0; i < jObj.size(); i++) {
                    String data=jObj.get(i).toString();
                    JSONParser parser = new JSONParser();
                    JSONObject jsonObject = (JSONObject) parser.parse(data);
                    OffsetDateTime candleDateTime= getOffsetDateTime(format, jsonObject, "candleDateTime");
                    OffsetDateTime candleDateTimeKst= getOffsetDateTime(format, jsonObject, "candleDateTimeKst");
                    Double openingPrice= getaDouble(jsonObject, "openingPrice");
                    Double highPrice= getaDouble(jsonObject, "highPrice");
                    Double lowPrice= getaDouble(jsonObject, "lowPrice");
                    Double tradePrice= getaDouble(jsonObject, "tradePrice");
                    Double candleAccTradeVolume= getaDouble(jsonObject, "candleAccTradeVolume");
                    Double candleAccTradePrice= getaDouble(jsonObject, "candleAccTradePrice");
                    Long timestamp=Long.parseLong(String.valueOf(jsonObject.get("timestamp")));
                    Double prevClosingPrice= getaDouble(jsonObject, "prevClosingPrice");
                    ChangePriceStatus change=ChangePriceStatus.valueOf((String)jsonObject.get("change"));
                    Double changePrice= getaDouble(jsonObject, "changePrice");
                    Double signedChangePrice= getaDouble(jsonObject, "signedChangePrice");
                    Double changeRate= getParseDouble(jsonObject, "changeRate");
                    Double signedChangeRate= getParseDouble(jsonObject, "signedChangeRate");
 
                    DayItemSaveDto dayItemSaveDto=new DayItemSaveDto().builder()
                            .upbitCoin(upbitCoin)
                            .code(code)
                            .candleDateTime(candleDateTime)
                            .candleDateTimeKst(candleDateTimeKst)
                            .openingPrice(openingPrice)
                            .highPrice(highPrice)
                            .lowPrice(lowPrice)
                            .tradePrice(tradePrice)
                            .candleAccTradeVolume(candleAccTradeVolume)
                            .candleAccTradePrice(candleAccTradePrice)
                            .timestamp(timestamp)
                            .prevClosingPrice(prevClosingPrice)
                            .change(change)
                            .changePrice(changePrice)
                            .signedChangePrice(signedChangePrice)
                            .changeRate(changeRate)
                            .signedChangeRate(signedChangeRate)
                            .build();
                    dayItemRepository.save(dayItemSaveDto.toEntity());
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }
cs

위 코드는 upbit의 code를 가지고 업비트에서 제공하는 일별 정보를 dayitem이라는 엔티티를 이용하여 정보를 모두 넣는 기능을 하는 함수이다. 

회원이 코인을 주문하는 기능을 추가하려는 계획을 짜고, db 설계를 하고 있는 중인데, 코인의 일별정보를 이렇게 객체를 생성해서 따로 담아두는 코드를 내가 짠 이유를 모르겠다. 크롤링 데이터를 긁어와서 db에 저장하는 연습을 한 것 같은데 사실 dayitem이라는 객체가 왜 필요한지 좀더 곰곰히 생각해보고 필요가 없다면, 삭제를 해야겠다고 생각 중이다.

 

만약 저장을한다면 싱글테이블 구조로 상속받는 구조를 만드는게 더 나을 수 있을 것 같다.

 

+ Recent posts