illustrate
This example can monitor Juhuasuan’s snap-up button, and automatically pop up the page when the time of Juhuasuan’s full point gathering arrives (the URL is defined by itself).
You can customize the monitoring of the last few minutes, and also speed up refresh speed through multithreading.
Source code
package com.itechzero.pricemonitor; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Date; /** * PriceMonitor.java * * @author Techzero * @Email [email protected] * @Time 2014-5-21 1:24:30 pm */ class MyThread extends Thread { public void run() { try { // The parameter here is the number of minutes of monitoring PriceMonitor.monitorButton(10); } catch (Exception e) { e.printStackTrace(); } } } }; public class PriceMonitor { // The monitored product URL private static String URL = "http://detail.ju.taobao.com/home.htm?spm=608.2214381.3.1.AdPEjn&item_id=38260927591&id=10000002781939"; // Monitoring button public static void monitorButton(int lastMinute) { int nowMinute = Integer.parseInt(new SimpleDateFormat("mm").format(new Date())); int endMinute = Integer.parseInt(new SimpleDateFormat("mm").format(new Date())) + lastMinute; while (nowMinute < endMinute) { nowMinute = Integer.parseInt(new SimpleDateFormat("mm").format(new Date())); String result[] = getCurrentButtonAndForm(URL, "gb2312").split(","); // Current button status String currentButton = result[0]; // Grab the form immediately//String form = result[1]; String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); System.out.println(nowTime + " - Now the button is " + currentButton); if (currentButton == "Get Now" || currentButton.equals("Get Now") || currentButton == "Get Now" || currentButton.equals("Get Now")) { System.out.println("Get Now!"); try { java.awt.Desktop.getDesktop().browse(new URI(URL)); } catch (Exception e) { e.printStackTrace(); } //doPost(form); break; } else if (currentButton == "Sold out" || currentButton.equals("Sold out") || currentButton.equals("Ended") || currentButton.equals("Ended")) { System.out.println("Try again next time!"); break; } else { System.out.println("Not started yet, wait!"); } } } // Get the current button status public static String getCurrentButtonAndForm(String url, String encoding) { if (url == null || "".equals(url.trim())) return null; String buttonState = ""; StringBuffer content = new StringBuffer(); boolean formFlag = false; try { // Create new URL object URL u = new URL(url); InputStream is = new BufferedInputStream(u.openStream()); InputStreamReader theHTML = new InputStreamReader(is, encoding != null ? encoding : "gb2312"); BufferedReader br = new BufferedReader(theHTML); String s = ""; while ((s = br.readLine()) != null) { if (s.indexOf("<input type=/"submit/" class=/"buyaction J_BuySubmit/" title=/"Grab/" value=/"Grab/"/>") != -1) { buttonState = "Grab/"; } else if (s.indexOf("<a href=/"#/" class=/"extra notice J_BuyButtonSub/">Team opening reminder</a>") != -1) { buttonState = "Team opening reminder"; } else if (s.indexOf("<div class=/"main-box chance /">") != -1) { buttonState = "Sold out"; } else if (s.indexOf("<span class=/"out floatright/">Sold out...</span>") != -1) { buttonState = "Sold out"; } else if (s.indexOf("<span class=/"out floatright/">Ended...</span>") != -1) { buttonState = "Ended"; } if (s.indexOf("<form class=/"J_BuySubForm/" data-ccb=/"0/" data-ques=/"0/" action") != -1) { content.append(s + "/r/n"); formFlag = true; } if (formFlag == true) { if (s.indexOf("<input name=/'_tb_token_/' type=/'hidden/' value") != -1) { content.append(s + "/r/n"); } if (s.indexOf("<input type=/"hidden/" name=/"_input_charset/" value") != -1) { content.append(s + "/r/n"); } if (s.indexOf("<input type=/"hidden/" name=/"itemId/" value") != -1) { content.append(s + "/r/n"); } if (s.indexOf("<input type=/"hidden/" name=/"id/" value") != -1) { content.append(s + "/r/n"); } if (s.indexOf("<input type=/"hidden/" name=/"id/" value") != -1) { content.append(s + "/r/n"); } if (s.indexOf("<input type=/"hidden/" name=/"tgType/" value") != -1) { content.append(s + "/r/n"); } if (s.indexOf("<input type=/"submit/" class=/"buyaction J_BuySubmit/"") != -1) { content.append(s + "/r/n"); } if (s.indexOf("<div class=/"time-banner/">") != -1) { content.append(s + "/r/n"); } } if (s.indexOf("<div class=/"time-banner/">") != -1) { break; } } br.close(); } catch (Exception e) { System.err.println(e); return "Open URL Error"; } return buttonState + "," + content; } // Submit the form public static String doPost(String form) { StringBuffer content = new StringBuffer(); try { URLConnection connection = new URL(URL).openConnection(); connection.setDoOutput(true); OutputStreamWriter os = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); os.write(form); os.flush(); os.close(); InputStream is = connection.getInputStream(); InputStreamReader theHTML = new InputStreamReader(is); BufferedReader br = new BufferedReader(theHTML); String s = ""; while ((s = br.readLine()) != null) { content.append(s + "/r/n"); } } catch (Exception e) { e.printStackTrace(); } // Return the page content returned after submitting the form return content.toString(); } // Log in public static void doLogin(String username, String password) { String form = "<form id=/"J_StaticForm/" action=/"https://login.taobao.com/member/login.jhtml/" method=/"post/" autocomplete=/"on/"><input type=/"text/" name=/"TPL_username/" id=/"TPL_username_1/" value=/"" + username + "/"><input type=/"password/" name=/"TPL_password/" id=/"TPL_password_1/" value=/"" + password + "/"><input type=/"hidden/" id=/"J_TPL_redirect_url/" name=/"TPL_redirect_url/" value=/"http://www.taobao.com/?spm=a2107.1.1000340.1.AL2Mpn/"><button type=/"submit/" id=/"J_SubmitStatic/">Login</button></form>"; doPost(form); } public static void main(String[] args) { //doLogin(); // new MyThread().start(); // new MyThread().start(); // new MyThread().start(); // new MyThread().start(); // new MyThread().start(); } }
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.