欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现淘宝秒杀聚划算抢购自动提醒源码

程序员文章站 2023-11-13 10:52:04
说明 本实例能够监控聚划算的抢购按钮,在聚划算整点聚的时间到达时自动弹开页面(url自己定义)。 可以自定义监控持续分钟数,同时还可以通过多线程加快刷新速度。 源码...

说明

本实例能够监控聚划算的抢购按钮,在聚划算整点聚的时间到达时自动弹开页面(url自己定义)。

可以自定义监控持续分钟数,同时还可以通过多线程加快刷新速度。

源码

package com.itechzero.pricemonitor; 
 
import java.io.bufferedinputstream; 
import java.io.bufferedreader; 
import java.io.inputstream; 
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 techzero@163.com 
 * @time 2014-5-21 下午1:24:30 
 */ 
class mythread extends thread { 
  public void run() { 
    try { 
      // 此处参数为监控持续分钟数 
      pricemonitor.monitorbutton(10); 
    } catch (exception e) { 
      e.printstacktrace(); 
    } 
  } 
}; 
 
public class pricemonitor { 
  // 监控的商品url 
  private static string url = "http://detail.ju.taobao.com/home.htm?spm=608.2214381.3.1.adpejn&item_id=38260927591&id=10000002781939"; 
 
  // 监视按钮 
  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(","); 
      // 当前按钮状态 
      string currentbutton = result[0]; 
      // 马上抢 表单 
      //string form = result[1]; 
      string nowtime = new simpledateformat("yyyy-mm-dd hh:mm:ss").format(new date()); 
      system.out.println(nowtime + " - 现在按钮是 " + currentbutton); 
 
      if (currentbutton == "马上抢" || currentbutton.equals("马上抢") || currentbutton == "还有机会" || currentbutton.equals("还有机会")) { 
        system.out.println("赶紧下单!"); 
        try { 
          java.awt.desktop.getdesktop().browse(new uri(url)); 
        } catch (exception e) { 
          e.printstacktrace(); 
        } 
        //dopost(form); 
        break; 
      } else if (currentbutton == "卖光了" || currentbutton.equals("卖光了") || currentbutton.equals("已结束") || currentbutton.equals("已结束")) { 
        system.out.println("下次再试吧!"); 
        break; 
      } else { 
        system.out.println("还没开始呢,再等等吧!"); 
      } 
    } 
  } 
 
  // 获取当前按钮状态 
  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 { 
      // 新建url对象 
      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=\"马上抢\" value=\"马上抢\"/>") != -1) { 
          buttonstate = "马上抢"; 
        } else if (s.indexof("<a href=\"#\" class=\"extra notice j_buybuttonsub\">开团提醒</a>") != -1) { 
          buttonstate = "开团提醒"; 
        } else if (s.indexof("<div class=\"main-box chance \">") != -1) { 
          buttonstate = "还有机会"; 
        } else if (s.indexof("<span class=\"out floatright\">卖光了...</span>") != -1) { 
          buttonstate = "卖光了"; 
        } else if (s.indexof("<span class=\"out floatright\">已结束...</span>") != -1) { 
          buttonstate = "已结束"; 
        } 
        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=\"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("</form>") != -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; 
  } 
 
  // 提交表单 
  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 content.tostring(); 
  } 
   
  // 登录 
  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\">登 录</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(); 
    // new mythread().start(); 
    // new mythread().start(); 
    new mythread().start(); 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。