供调试WatchBBS

 调试WatchBBS:

去掉swing部分,稍微修改了一下代码。

调试结果:成功!

理解代码程度:中

详细代码:

  1. import java.util.Date;
  2. import javax.swing.SwingUtilities;
  3. import com.heaton.bot.HTMLParser;
  4. import com.heaton.bot.HTMLTag;
  5. import com.heaton.bot.HTTPSocket;
  6. /**
  7.  * This is a simple bot program that is designed
  8.  * to work with one specific site.
  9.  */
  10. public class WatchBBS implements Runnable {
  11.   Date _latest = null;
  12.   Date _nextPoll;
  13.   Thread _thread;
  14.   String _url="http://bbs.redhome.cc/forumdisplay.php?fid=13";
  15.   String _minutes="1";
  16.   public WatchBBS()
  17.   {
  18.       Start_actionPerformed();
  19.   }
  20.   
  21.   public static void main(String[]args)
  22.   {
  23.       new WatchBBS();
  24.   }
  25.     
  26.   void Start_actionPerformed() 
  27.   { 
  28.     if ( _latest==null ) { 
  29.       _latest = getLatestDate(); 
  30.       if ( _latest==null ) 
  31.         return
  32.     } 
  33.     if ( !setNextPoll() ) 
  34.       return
  35.     _thread = new Thread(this); 
  36.     _thread.start(); 
  37.   } 
  38.   /** 
  39.    * Called when the stop button is clicked. 
  40.    * This method stops the background thread. 
  41.    * 
  42.    * @param event 
  43.  * @throws InterruptedException 
  44.    */ 
  45.   void Stop_actionPerformed(java.awt.event.ActionEvent event) throws InterruptedException 
  46.   { 
  47.     /*
  48.      * The method stop() from the type Thread is deprecated(弃用)
  49.      */
  50.     //_thread.stop(); 
  51.     //Thread.yield();
  52.       _thread.join();
  53.   } 
  54.   /**
  55.    * Called when the Poll Now button is cliked. Also
  56.    * called when the background thread determines that
  57.    * it is time to poll again.
  58.    * 当时间到,或者按下Poll键,则会执行此函数。
  59.    * 主要方法就是:再一次到网页里面抓取时间,然后与
  60.    * 上一次抓取的时间进行比较,不同则说明有新消息进来。
  61.    *
  62.    * @param event The event.
  63.    */
  64.   void Go_actionPerformed()
  65.   {
  66.     setNextPoll();
  67.     Date update = getLatestDate();
  68.     if ( !update.toString().equalsIgnoreCase(_latest.toString()) ) {
  69.       _latest = update;
  70.       Runnable doit = new Runnable()
  71.       {
  72.         public void run()
  73.         {
  74.           System.out.println("there is a new message");
  75.         }
  76.       };
  77.       SwingUtilities.invokeLater(doit);
  78.     }
  79.   }
  80.   protected Date getLatestDate()
  81.   {
  82.     HTTPSocket http;
  83.     /*
  84.     public Date(int year,
  85.             int month,
  86.             int day)已过时。 使用构造方法 Date(long date) 替代 
  87.     */
  88.     
  89.     Date latest = new Date(0);
  90.     try {
  91.       http = new HTTPSocket();
  92.       http.send(_url,null);
  93.     } catch ( Exception e ) {
  94.       e.printStackTrace();
  95.       return null;
  96.     }
  97.     HTMLParser parse = new HTMLParser();
  98.     /*
  99.      * 开始解析网页。
  100.      */
  101.     parse.source = new StringBuffer(http.getBody());
  102.     int foundTag = 0;
  103.     String date = "";
  104.     // find all the tags
  105.     while ( !parse.eof() ) 
  106.     {
  107.       char ch = parse.get();
  108.       if ( ch==0 ) 
  109.       {
  110.           /*
  111.            * 得到tag
  112.            */
  113.         HTMLTag tag = parse.getTag();
  114.         /*
  115.          * 样例如下:
  116.          * <em><a href="redirect.php?tid=347514&goto=lastpost#lastpost">2008-10-4 19:40</a></em>
  117.          */
  118.         if ( tag.getName().equalsIgnoreCase("a") ) {
  119.           foundTag = 2;
  120.           date="";
  121.           //System.out.println(tag.getName());
  122.         } 
  123.         /*
  124.          * 执行完tag<a>,继续执行,获得date为2008-10-4 19:40。
  125.          */
  126.         else if ( tag.getName().equalsIgnoreCase("/em") ) 
  127.         {
  128.           //System.out.println(tag.getName());
  129.           Date d=null
  130.           foundTag--;
  131.           if ( foundTag==1 ) 
  132.           {
  133.             /*
  134.              * 除去那些非时间参数。
  135.              */
  136.             if(date.indexOf(":")!=-1)
  137.             { 
  138.                 d = parseDate(date);
  139.             }
  140.             
  141.             
  142.             //System.out.println(d);
  143.             if ( d!=null ) 
  144.             {
  145.                 /*
  146.                  * 获得所有时间中最新的时间。
  147.                  */
  148.               if ( d.after(latest) )
  149.                 latest = d;
  150.              // System.out.println(tag.getName());
  151.              // System.out.println(latest);
  152.             }
  153.           }
  154.         }
  155.       } 
  156.       else 
  157.       {
  158.         if ( (ch=='/r') || (ch=='/n') )
  159.           ch=' ';
  160.         date+=ch;
  161.       }
  162.     }
  163.     /*
  164.      * 测验最新时间。
  165.      */
  166.     System.out.println(latest);
  167.     return latest;
  168.   }
  169.   /**
  170.    * Parse a date of the form:
  171.    * September 2, 2001 5:30 PM
  172.    * 对find的时间进行规格化。
  173.    * @param str The string form of the date.
  174.    * @return A Date object that was parsed.
  175.    */
  176.   @SuppressWarnings("deprecation")
  177. Date parseDate(String str)
  178.   {
  179.     String months[] = {"jan","feb","mar","apr","may",
  180.       "jun","jul","aug","sep","oct","nov","dec"};
  181.     Date rtn;
  182.     try {
  183.       rtn = new Date();
  184.     //    Year
  185.       String year = str.substring(0,str.indexOf('-'));
  186.       rtn.setYear(Integer.parseInt(year)-1900);
  187.       
  188.       // month
  189.       str=str.substring(str.indexOf('-')+1);
  190.       String mth = str.substring(0,str.indexOf('-'));
  191.       for ( int i=0;i<months.length;i++ ) {
  192.         if ( mth.toLowerCase().startsWith(months[i]) ) {
  193.           rtn.setMonth(i);
  194.           break;
  195.         }
  196.       }
  197.       // day
  198.       str = str.substring(str.indexOf('-')+1);
  199.       String day = str.substring(0,str.indexOf(' '));
  200.       rtn.setDate(Integer.parseInt(day));
  201.       // Hour
  202.       str = str.substring(str.indexOf(' ')+1).trim();
  203.       String hour = str.substring(0,str.indexOf(':'));
  204.       rtn.setHours(Integer.parseInt(hour));
  205.       // Minute
  206.       str = str.substring(str.indexOf(':')+1).trim();
  207.       String minutes = str.substring(0);
  208.       rtn.setMinutes(Integer.parseInt(minutes));
  209.       rtn.setSeconds(0);
  210.       // AM or PM
  211.       /*
  212.       str = str.substring(str.indexOf(' ')+1).trim();
  213.       if ( str.toUpperCase().charAt(0)=='P' )
  214.         rtn.setHours(rtn.getHours()+12);
  215.       */
  216.       return rtn;
  217.     } catch ( Exception e ) {
  218.       return null;
  219.     }
  220.   }
  221.   
  222.   /**
  223.    * This run method is called to execute
  224.    * the background thread.
  225.    */
  226.   public void run()
  227.   {
  228.     while ( true ) {
  229.       Runnable doit = new Runnable()
  230.       {
  231.         public void run()
  232.         {
  233.           Date d = new Date();
  234.           /*
  235.            * 计算剩余时间的秒数。
  236.            */
  237.           long milli = (_nextPoll.getTime()-d.getTime())/1000;
  238.           System.out.println("Will poll in " + milli + " seconds.");
  239.         }
  240.       };
  241.       /*
  242.        * 标准解释如下:
  243.          true if and only if the instant of time represented by this 
  244.          Date object is strictly earlier than the instant represented 
  245.          by when
  246.           简单来说就是_nextPoll所对应的时间要早于new Date()即此刻时间.也就是规定的
  247.           时间一过,便执行Go_actionPerformed(null)函数。
  248.       */
  249.       if ( _nextPoll.before(new Date()) ) {
  250.         Go_actionPerformed();
  251.       }
  252.       SwingUtilities.invokeLater(doit);
  253.       try {
  254.         Thread.sleep(1000);
  255.       } catch ( InterruptedException e ) {
  256.       }
  257.     }
  258.   }
  259.   /**
  260.    * Called to determine the next time
  261.    * that a poll will take place.
  262.    * 主要就是设定下一个poll(调查)的时间值。
  263.    * @return True on success, false on failure.
  264.    */
  265.   public boolean setNextPoll()
  266.   {
  267.     try {
  268.       int minutes = Integer.parseInt(_minutes);
  269.       Date d = new Date();
  270.       d.setMinutes(d.getMinutes()+minutes);
  271.       _nextPoll = d;
  272.       return true;
  273.     } catch ( Exception e ) {
  274.       e.printStackTrace();
  275.       return false;
  276.     }
  277.   }
  278. }

过时的方法还有很多啊!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值