package com.board.gd.domain.stock;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;

/**
 * Created by godong9 on 2017. 5. 7..
 */

@Transactional(readOnly = true)
@Service
public class StockService {
    @Autowired
    private StockRepository stockRepository;

    @Transactional(readOnly = false)
    public void parseHtmlAndSave() throws IOException {
        String kospiPath = "/board/files/kospi.html";
        saveStockCodeAndName(kospiPath);

        String kodaqPath = "/board/files/kosdaq.html";
        saveStockCodeAndName(kodaqPath);
    }

    @Transactional(readOnly = false)
    public void saveStockCodeAndName(String filePath) throws IOException {
        File file = new File(System.getProperty("user.dir") + filePath);
        Document doc = Jsoup.parse(file, "EUC-KR");

        Elements trs = doc.select(".tbScY tr");
        for (Element e: trs) {
            Elements code = e.select("td:nth-of-type(2)");
            if (!code.isEmpty()) {
                Elements name = e.select("td.tL");
                String codeStr = code.text();
                String nameStr = name.text();
                nameStr = nameStr.replaceAll("[#*]", "");

                stockRepository.save(Stock.builder()
                        .name(nameStr)
                        .code(codeStr)
                        .build());
            }
        }
    }
}

@@ -332,29 +332,8 @@
 l;%0A%0A
-import java.io.File;%0A
 impo
@@ -659,128 +659,307 @@
  = %22
-/board/files/kospi.html%22;%0A        saveStockCodeAndName(kospiPath);%0A%0A        String kodaqPath = %22/board/files/kosdaq.html
+https://www.hdable.co.kr/go.able?linkcd=s03090010P1I1&gubun=0&actionType=G&radiotype=1&findKeyword=&searchGubun=1%22;%0A        saveStockCodeAndName(kospiPath);%0A%0A        String kodaqPath = %22https://www.hdable.co.kr/go.able?linkcd=s03090010P1I1&gubun=0&actionType=G&radiotype=2&findKeyword=&searchGubun=1
 %22;%0A 
@@ -1086,24 +1086,19 @@
 (String 
-filePath
+uri
 ) throws
@@ -1116,81 +1116,8 @@
 n %7B%0A
-        File file = new File(System.getProperty(%22user.dir%22) + filePath);%0A
     
@@ -1145,28 +1145,25 @@
 oup.
-parse(file, %22EUC-KR%22
+connect(uri).get(
 );%0A%0A

Update StockService