package link.at17.mid.tushare;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.zip.InflaterOutputStream;

import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import javax.script.ScriptEngine;
@Slf4j
public class SinaFinanceUnzip {
    
    public static ScriptEngine engine;
    public static String js = "";
    
    public static void main(String[] args) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("https://finance.sina.com.cn/realstock/company/sh600660/hisdata_klc2/klc_kl.js?d=2021_8_8")
                .get()
                .build();

        final Call call = okHttpClient.newCall(request);
        Response response = null;
        try {
            response = call.execute();
            String plain = response.body().string();
            String data = plain.substring(plain.indexOf('"') + 1, plain.lastIndexOf('"') - 1);
            
            final Base64.Decoder decoder = Base64.getDecoder();
            byte[] dataBytes = decoder.decode(data);
            
            engine.eval("");
            
            try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
                try (OutputStream outputStream = new InflaterOutputStream(os)) {
                    outputStream.write(dataBytes);
                    data = new String(os.toByteArray());
                    System.out.println(data);
                }


                } catch (IOException e) {
                    e.printStackTrace();
                }
        } catch (Exception e) {
            log.error("Request error", e);
        }
    }
}
