background

Since the nuggets sign in lottery to let the small taste after bone (which is actually not what bird with table mat and badge), the small then joined the daily saving ore drawing brigade, results in 20 consecutive days off a month ago, back to before liberation in 100-200 days of ore, and still decide to leave things to the program to do, Javaer should be talking about programs

Taking action

First, let’s create a new SpringBoot project. Why use SpringBooot? Since Xiaohai is a Java backend with one year of experience, the technology stack is CTRL C, V + SpringBoot. Okay, so then we’ll add @enablesCheduling to start a scheduled task to execute our lottery code every day. Add @ SpringBootApplication (exclude = {DataSourceAutoConfiguration. Class}) to cancel the default datasource configuration, because we don’t need database support, so we can set the shelf, Start writing business.

/** * @author lizhihai * @date 2021/09/11 * Start timing task, Cancel springdatasource load * / @ EnableScheduling @ SpringBootApplication (exclude = {DataSourceAutoConfiguration. Class}) public class MyTestProApplication { public static void main(String[] args) { SpringApplication.run(MyTestProApplication.class, args); }}Copy the code

Sign-in raffle request

Nuggets sign in draw these requests are the HTTPS protocol, a certificate of request, so we do not say, write a generic use HttpURLConnection send HTTPS request method, and obtain the return value method, the reference here www.jianshu.com/p/848911df8…

/** * The common HTTPS request method is the HTTPS certificate request ** @param URL * @param method * @return * @throws Exception */ public String commnoReuqest(String url, String method,Cookies cookie) throws Exception { URL serverUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection(); conn.setRequestMethod(method); conn.setRequestProperty("Content-type", "application/json"); Conn.setrequestproperty (" user-agent ", "Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"); conn.setRequestProperty("Cookie", cookie.getValue()); / / must be set false, otherwise it will automatically redirect to redirect the address of the conn. After setInstanceFollowRedirects (false); conn.connect(); String result = getReturn(conn); return result; } /* Request the URL to obtain the returned content */ public static String getReturn(HttpURLConnection Connection) throws IOException {StringBuffer buffer  = new StringBuffer(); / / returns the input stream into a string try (InputStream InputStream = connection. The getInputStream (); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader);) { String str = null; while ((str = bufferedReader.readLine()) ! = null) { buffer.append(str); } String result = buffer.toString(); return result; }}Copy the code

The nuggets API

Then we open the familiar F12 is ow, look at the Denver nuggets signed in lottery request search API, according to every request url guess is stem what of, let’s put the finishing a good API wrote constant class, and copy the cookie down, according to each url and general request method, good encapsulation check-in, lottery, query method of ore. {“err_no”:15001,”err_msg”:” You have finished checking in today, please do not check in again “,”data”:null}, Ok Test result successful get√

public class Constans { public static final String POST = "POST"; public static final String GET = "GET"; / / URL / / sign in public static final String CHECK_IN = "https://api.juejin.cn/growth_api/v1/check_in"; / / DRAW public static final String the DRAW = "https://api.juejin.cn/growth_api/v1/lottery/draw"; / / query residual ore public static final String GET_CUR_POINT = "https://api.juejin.cn/growth_api/v1/get_cur_point"; }Copy the code
/** * check-in ** @throws Exception */ public Boolean checkIn(Cookies cookie) throws Exception {String Response = commnoReuqest(Constans.CHECK_IN, Constans.POST,cookie); logger.info("{}{}",cookie.getName(),response); boolean hasSign=! response.contains("err_no":0"); if(hasSign){ sendHttps.sendToDingDing(cookie.getName()+response); } return hasSign; Throws Exception */ public void draw(Cookies cookie) throws Exception {String Response = commnoReuqest(Constans.DRAW, Constans.POST,cookie); DrawResponce data= JSON.parseObject(response,new TypeReference<DrawResponce>(){}); String prizeName=data.getData().getLotteryName(); if(!" 66 ore ". The equals (prizeName) &&! Prizename. contains("Bug")){log.error("{} draws good stuff {}!!!!!! , cookie.getName()+" draw "+prizeName"); } logger.info(response); }Copy the code

scalability

Here the small pull open a trumpet and pull a friend to join the lottery group, so you need to complete the batch lottery operation, and considering some friend hope use lottery free every day, to save the ore, some hope every tissot, draw, so we define an enumeration class, attribute has a name, value, allin, representing the name of the friend, Friend nuggets cookie, whether the friend wants to soha lottery. This allows us to draw batches by iterating through enumerated values

/ / cookie nuggets of Cookies is said to be valid in one month, one hang up again kao over public enum Cookies {HAI1 (true, "the small sea, 183", "_ga = GA1.2.1296772436.1629686778; passport_csrf_token_default=f933ed7f9d24bfb06894114528a64af6; passport_csrf_token=f933ed7f9d24bfb06894114528a64af6; MONITOR_WEB_ID=aa936331-59e1-4607-8edf-84657bbc22cd; _gid = GA1.2.1269321208.1630996120; odin_tt=1bb7f242d137177d63dd9302d566ca5eac4c9fcb56b1dde948623c0efaf17cbecade967d875212a51b1c0e85f5643f2b08907c58b7b23435 c0f866d83a18cf6e; passport_auth_status=0481add371299d2704126689380b9849%2C703a60ebb0e24b3a72db9bdc379e399d; passport_auth_status_ss=0481add371299d2704126689380b9849%2C703a60ebb0e24b3a72db9bdc379e399d; sid_guard=a6220a3e86f808bb214cbbe6d482a529%7C1631266482%7C5184000%7CTue%2C+09-Nov-2021+09%3A34%3A42+GMT; uid_tt=ccb35b269c8a75cb1dbd63c6d37a8a53; uid_tt_ss=ccb35b269c8a75cb1dbd63c6d37a8a53; sid_tt=a6220a3e86f808bb214cbbe6d482a529; sessionid=a6220a3e86f808bb214cbbe6d482a529; sessionid_ss=a6220a3e86f808bb214cbbe6d482a529; Sid_ucp_v1 = 1.0.0 - KDZkMGY3MDVmMzIyODI0MzU3Y2IyOWRjNGFmOGUxYTQwMGIwNTA3MWMKFwi93KCx4o2gBRCyzeyJBhiwFDgCQPEHGgJsZiIgYTYyMjB hM2U4NmY4MDhiYjIxNGNiYmU2ZDQ4MmE1Mjk; Ssid_ucp_v1 = 1.0.0 - KDZkMGY3MDVmMzIyODI0MzU3Y2IyOWRjNGFmOGUxYTQwMGIwNTA3MWMKFwi93KCx4o2gBRCyzeyJBhiwFDgCQPEHGgJsZiIgYTYyMj BhM2U4NmY4MDhiYjIxNGNiYmU2ZDQ4MmE1Mjk; n_mh=KF_8KclpLr_zR7cAEjb3RtZNBW4-HCCsXj4xdrmiLXE"), HAI2 (true, "the small sea 176", "MONITOR_WEB_ID = 46 a89 e596ce - 9 ecc - 4-992 - a - 7 c6a6e0c3481; _ga = GA1.2.1129485207.1631332210; _gid = GA1.2.1280463638.1631332210; passport_csrf_token_default=79180cb48bf4906a01362c777619a03e; passport_csrf_token=79180cb48bf4906a01362c777619a03e; passport_auth_status=26b44631a00ffda2ca525997e425471b%2C; passport_auth_status_ss=26b44631a00ffda2ca525997e425471b%2C; sid_guard=da5b5f8fa2be84a74656f9ed8f0f58de%7C1631332246%7C5184000%7CWed%2C+10-Nov-2021+03%3A50%3A46+GMT; uid_tt=369af9b9a6bd0a458166702eb1a9eb76; uid_tt_ss=369af9b9a6bd0a458166702eb1a9eb76; sid_tt=da5b5f8fa2be84a74656f9ed8f0f58de; sessionid=da5b5f8fa2be84a74656f9ed8f0f58de; sessionid_ss=da5b5f8fa2be84a74656f9ed8f0f58de; Sid_ucp_v1 = 1.0.0 - KDA2ODRmYTJmODZiOThjYjBjNDhjYjZjYzMwZjY0NzNhNzM1ODdhYmIKFwjnkrCFu4yWAxCWz_CJBhiwFDgCQPEHGgJsZiIgZGE1YjV mOGZhMmJlODRhNzQ2NTZmOWVkOGYwZjU4ZGU; Ssid_ucp_v1 = 1.0.0 - KDA2ODRmYTJmODZiOThjYjBjNDhjYjZjYzMwZjY0NzNhNzM1ODdhYmIKFwjnkrCFu4yWAxCWz_CJBhiwFDgCQPEHGgJsZiIgZGE1Yj VmOGZhMmJlODRhNzQ2NTZmOWVkOGYwZjU4ZGU; n_mh=gY-EUdc0-atNYE3DFtWTsH7CHt0JMgKQkRnBimbpdn8; _gat=1"), WU(true," WU ","MONITOR_WEB_ID= DA85199D-F4D8-434B-A709-6470a7f2AE08; _ga = GA1.2.1545343137.1631330098; _gid = GA1.2.1996029296.1631330098; passport_csrf_token_default=c95cd45eb0563c42a850d971f8f8d670; passport_csrf_token=c95cd45eb0563c42a850d971f8f8d670; passport_auth_status=46573bcc67d38f6c4202117b42f9b248%2C; passport_auth_status_ss=46573bcc67d38f6c4202117b42f9b248%2C; sid_guard=ed395050035826d1ea12953bd5728b05%7C1631330346%7C5184000%7CWed%2C+10-Nov-2021+03%3A19%3A06+GMT; uid_tt=4f23e928a63e23958189356b7349ddc1; uid_tt_ss=4f23e928a63e23958189356b7349ddc1; sid_tt=ed395050035826d1ea12953bd5728b05; sessionid=ed395050035826d1ea12953bd5728b05; sessionid_ss=ed395050035826d1ea12953bd5728b05; Sid_ucp_v1 = 1.0.0 - KDAzNjEzZDNkM2FmODRhNDIxY2E3ODQ4YzJkYjY4MDI5MzczNmFjZDYKFwjnr4CNvIyqBRCqwPCJBhiwFDgCQPEHGgJsZiIgZWQzOTU wNTAwMzU4MjZkMWVhMTI5NTNiZDU3MjhiMDU; Ssid_ucp_v1 = 1.0.0 - KDAzNjEzZDNkM2FmODRhNDIxY2E3ODQ4YzJkYjY4MDI5MzczNmFjZDYKFwjnr4CNvIyqBRCqwPCJBhiwFDgCQPEHGgJsZiIgZWQzOT UwNTAwMzU4MjZkMWVhMTI5NTNiZDU3MjhiMDU; n_mh=fCN8FMn4hIUuqN_fJo4Lf9qCwiFWUh01_NgRv1iwdfQ"); private String name; // Name private String value; Private Boolean isAllIn; // Whether Cookies(Boolean isAllIn,String name, String value) {this.isAllIn=isAllIn; this.name = name; this.value = value; } public String getName() { return name; } public boolean isAllIn() { return isAllIn; } public String getValue() { return value; }}Copy the code

Timing task

Focus,, the last step of the lottery timed task, we look at the code, here we can not worry about the weekend broken sign, thus free both hands uninstall nuggets APP. Gnome male -“

@Scheduled(cron = "0 1 0 * * ?" ) public void excute() throws Exception {logger.info(" Scheduled task starts "); for(Cookies cookie:Cookies.values()){ boolean hasSign=checkIn(cookie); if(hasSign) {continue; } // First free draw draw(cookie); While (cookie.isallin ()&&getCurPoint(cookie)>=200){draw(cookie); }} logger.info(" end of execution "); }Copy the code

By the way

Here, the return of gold mining API layer by layer, using MapUtils is very cumbersome, I recommend an idea plug-in GsonFormat to directly copy the string returned by the request to generate our lottery result response class by one key, delightful.

@NoArgsConstructor @Data public class DrawResponce { @JsonProperty("err_no") private Integer errNo; @JsonProperty("err_msg") private String errMsg; @JsonProperty("data") private DataDTO data; @NoArgsConstructor @Data public static class DataDTO { @JsonProperty("id") private Integer id; @JsonProperty("lottery_id") private String lotteryId; @JsonProperty("lottery_name") private String lotteryName; @JsonProperty("lottery_type") private Integer lotteryType; @JsonProperty("lottery_image") private String lotteryImage; @JsonProperty("history_id") private String historyId; }}Copy the code

Nailing robot

I want to know what I have picked. Can I go to your company and look at your console and search log.info? So I refer to (CTRL C + V) these two articles www.jb51.cc/java/526453… , www.cnblogs.com/fugitive/p/… Docking nail, the code will not stick, quite a lot, docking my own nail group robot (IP verification method) and then I can receive several nail messages at twelve o ‘clock every day: Xiao Hai pumping 66 ore. You know, just out of the blue, crying

The last

Finally: this is the first time that the vegetable chicken small Java small sea writes an article, there are many ancestral CTRL C V operation, big guys see a joy on the line. The source code has been uploaded to github, github.com/NicholasZhi… You can send your name, allin and cookie in the comments section. For example (Nicholas Shohai, Soha/Buha, “Nicholas”) I’ll get you into the raffle