KKSU
2024-04-17 829f629756e5402bcd3e9f6ef1f9c6dbffbabb11
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.xcong.excoin.quartz.job;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.yunding.dao.YdBasicSettingDao;
import com.xcong.excoin.modules.yunding.entity.YdBasicSettingEntity;
import com.xcong.excoin.modules.yunding.service.XchProfitService;
import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
 
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
 
/**
 * @author xxx
 **/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app", name = "xch-job", havingValue = "true")
public class XchBaseDataUpdateJob {
 
    @Autowired
    private YdBasicSettingDao ydBasicSettingDao;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private XchProfitService xchProfitService;
 
//        @Scheduled(cron = "0/1 * * * * ? ")
//    @Scheduled(cron = "0 0/10 * * * ? ")
//    public void baseDataUpdate() {
//        log.info("基础数据更新任务");
//        // 请求价格等数据 "https://api2.chiaexplorer.com/blockchainSummary"
//        String result = pyExec();
//        JSONObject xchPrice24HObj = (JSONObject) JSONObject.parse(result);
//        List<YdBasicSettingEntity> list = ydBasicSettingDao.selectList(null);
//        if (CollUtil.isNotEmpty(list)) {
//            YdBasicSettingEntity settingEntity = list.get(0);
//            List<String> xchPrice24H = JSONObject.parseArray(xchPrice24HObj.getString("data"), String.class);
//
//            BigDecimal newestPrice = new BigDecimal(xchPrice24H.get(xchPrice24H.size() - 1));
//
//            redisUtils.set("XCH_NEW_PRICE", newestPrice);
//            settingEntity.setCurrentPrice(newestPrice);
//            ydBasicSettingDao.updateById(settingEntity);
//        }
//    }
 
    private String getUrlResponse(String url) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        // 模拟浏览器
        String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
        try {
            System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
            URL request = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) request.openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(30000);
            connection.setConnectTimeout(30000);
            connection.setRequestProperty("User-agent", userAgent);
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return result;
    }
 
    //    @Scheduled(cron = "0 0 23 * * ?")
    public void xchProfitJob() {
        log.info("XCH收益返利任务");
//        xchProfitService.xchProfitDistributor();
    }
 
    //    @Scheduled(cron = "0 0 22 * * ?")
    public void usdtProfitJob() {
        log.info("USDT返利任务");
//        xchProfitService.agentUsdtProfitDistributor();
    }
 
    public static void main(String[] args) {
//        System.out.println(HttpRequest.get("https://www.chiaexplorer.com").execute().body());
//        System.out.println(execCurl(cmds));
    }
 
    private String execCurl(String url) {
        String[] cmds = {"curl", url
                , "-H", "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\""
                , "-H", "Accept: application/json, text/plain, */*"
                , "-H", "Referer: https://www.chiaexplorer.com/"
                , "-H", "sec-ch-ua-mobile: ?0"
                , "-H", "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
                , " --compressed"};
 
        ProcessBuilder process = new ProcessBuilder(cmds);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            return builder.toString();
 
        } catch (IOException e) {
            System.out.print("error");
            e.printStackTrace();
        }
        return null;
    }
 
    public String pyExec() {
        String result = "";
        String cmd = "python2.7 /home/javaweb/yunding/xch.py";
//        String cmd = "python /Users/helius/Desktop/xch.py";
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            process.waitFor();
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            result = input.readLine();
            input.close();
            ir.close();
        } catch (IOException | InterruptedException e) {
            log.error("python执行异常", e);
        }
        return result;
    }
 
}