2008年12月4日 星期四

Java FunP 自動推文程式

測試了一個禮拜應該是沒有問題了, 發佈出來讓大家參考, 使用此程式先要有apache HttpClient相關套件(請參閱之前發文運用Apache HttpClient實作Get與Post動作)

流程為: 登入FunP之後記住Cookie之外, 進入其他頁面必須還要傳送not_rem_login與expires等驗證資訊, 成功取得頁面內容後使用Regular Expression取得文章代號, 就可以進行推文與收藏的動作, 如果想自己執行網頁流程看看傳輸內容的話建議使用SmartSniff, 可以看見POST或GET傳輸內容與表頭。

FunpAutoLogin:

package org.sam.funp;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class FunpAutoLogin
{
    private HttpClient client;
    private FunpPusher pusher;
    private FunpAddFavo addfavo;

    public FunpAutoLogin()
    {
        pusher = new FunpPusher();
        addfavo = new FunpAddFavo();

        // set up the HttpClient object.
        client = new HttpClient();
        // host configuration.
        client.getHostConfiguration().setHost("funp.com", 80, "http");
    }

    public void login()
    {
        PostMethod method = new PostMethod("/account/login.ajax.php");

        // Cookie設置.
        Header newCookieHeader = method.getResponseHeader("Set-Cookie");
        Header currentCookieHeader = method.getRequestHeader("Cookie");
        if (newCookieHeader != null) {
            if (currentCookieHeader == null) {
                method.setRequestHeader("Cookie", newCookieHeader.getValue());
            } else {
                method.setRequestHeader("Cookie", currentCookieHeader.getValue() + "; " + newCookieHeader.getValue());
            }
        }
        // 表頭設置.
        method.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.4.154.25 Safari/525.19");  
        method.setRequestHeader("Referer", "http://funp.com/push/");  
        method.setRequestHeader("Cache-Control", "max-age=0");  
        method.setRequestHeader("X-Prototype-Version", "1.5.1.1");  
        method.setRequestHeader("X-Requested-With", "XMLHttpRequest");  
        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");  
        method.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");  
        method.setRequestHeader("Accept-Encoding", "gzip,deflate,bzip2,sdch");  
        method.setRequestHeader("X-SDCH", "Chrome 0.4.154.25");  
        method.setRequestHeader("Accept-Language", "zh-TW,zh,en-US,en");  
        method.setRequestHeader("Accept-Charset", "Big5,*,utf-8");  
        method.setRequestHeader("Host", "funp.com");  
        method.setRequestHeader("Content-Length", "69");  
        method.setRequestHeader("Connection", "Keep-Alive");  
    
        // POST傳送資訊.
        NameValuePair login = new NameValuePair("titlebar_login", "");  
        NameValuePair email = new NameValuePair("titlebar_id", your account);  
        NameValuePair passwd = new NameValuePair("titlebar_password", your passwd);  
        NameValuePair no = new NameValuePair("_", "");  
        method.setRequestBody(new NameValuePair[] { login, email, passwd, no });  
  
        try {  
            client.executeMethod(method);  
     
            // get response status.
            int state = method.getStatusCode();  
            if (state == HttpStatus.SC_OK) {  
                System.err.println("> login success.");  
                method.releaseConnection();  
      
                // 每頁推完休息120秒.
                for(int i = 1; i <= 4; i++){  
                    System.err.println("> browsing page " + i + ".");  
                    getPushDetail(1, 1, i);  
                    try{  
                        Thread.sleep(120000);  
                    }catch(Exception e){  
                        e.printStackTrace();  
                    }  
                }  
            } else {  
                System.err.println("> login failed, funP system might be crashed.");  
            }  
        } catch (HttpException httpexc) {  
            System.err.println(">> fatal protocol violation: " + httpexc.getMessage());  
            httpexc.printStackTrace();  
        } catch (IOException ioexc) {  
            System.err.println(">> fatal transport error: " + ioexc.getMessage());  
            ioexc.printStackTrace();  
        }  
    }

    private void getPushDetail(int hot, int stars, int page)
    {
        // 推文首頁.
        // GetMethod get = new GetMethod("/push/index.php?hot=" + hot + "&stars=" + stars + "&page=" + page);

        // 好友推文.
        GetMethod get = new GetMethod("/push/index.php?friend=post&star=&page=" + page);

        // 傳送已登入驗證
        get.setRequestHeader("not_rem_login", "1");
        get.setRequestHeader("expires", "Sun, 28-Dec-2012 13:21:53 GMT");
        get.setRequestHeader("path", "/");
        get.setRequestHeader("domain", "funp.com");
        try {
            client.executeMethod(get);
            String response = readSource(get).toLowerCase().replaceAll("\r", "").replaceAll("\n", "").replaceAll("\t", "");
            String searchDescriptionReg = "<div.class=['\"]description['\"].id=['\"]description_([0-9]+)['\"]>";
            Pattern pattern = Pattern.compile(searchDescriptionReg); // use regular expression to parse image src.
            Matcher matcher = pattern.matcher(response);
            while (matcher.find()) {
                try {
                    String id = matcher.group(1);
                    String description = matcher.group(2);

                    // 推文.
                    pusher.push(client, id, hot, stars, page);
                    // 加入最愛.
                    addfavo.add(client, id, hot, stars, page);

                    // 顯示進度
                    System.out.println("> id: " + id + ", description: " + description);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (HttpException httpexc) {
            System.err.println(">> Fatal protocol violation: " + httpexc.getMessage());
            httpexc.printStackTrace();
        } catch (IOException ioexc) {
            System.err.println(">> Fatal transport error: " + ioexc.getMessage());
            ioexc.printStackTrace();
        } finally {
            get.releaseConnection();
        }
    }

    // read response html source.
    private String readSource(HttpMethodBase method) throws IOException
    {
        BufferedInputStream inputStream = new BufferedInputStream(method.getResponseBodyAsStream());
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        char[] buff = new char[1024];
        StringBuilder htmlSource = new StringBuilder();
        int c = 0;
        while ((c = bufferedReader.read(buff, 0, 1024)) != -1) {
            htmlSource.append(buff, 0, c);
        }
        // deal with the response.
        String response = htmlSource.toString();

        return response;
    }

    public static void main(String[] args) throws Exception
    {
        new FunpAutoLogin().login();
    }
}

FunpPusher:

package org.sam.funp;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class FunpPusher
{
    public FunpPusher()
    {
        System.out.println("> pusher created.");
    }

    public void push(HttpClient client, String no, int hot, int star, int page)
    {
        PostMethod method = new PostMethod("/push/push.ajax.php");

        method.setRequestHeader("not_rem_login", "1");
        method.setRequestHeader("expires", "Sun, 28-Dec-2012 13:21:53 GMT");
        method.setRequestHeader("path", "/");
        method.setRequestHeader("domain", "funp.com");

        method.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.4.154.25 Safari/525.19");
        method.setRequestHeader("Referer", "http://funp.com/push/index.php?hot=" + hot + "&stars=" + star + "&page=" + page);
        method.setRequestHeader("X-Prototype-Version", "1.5.1.1");
        method.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        method.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");
        method.setRequestHeader("Accept-Encoding", "gzip,deflate,bzip2,sdch");
        method.setRequestHeader("X-SDCH", "Chrome 0.4.154.25");
        method.setRequestHeader("Accept-Language", "zh-TW,zh,en-US,en");
        method.setRequestHeader("Accept-Charset", "Big5,*,utf-8");
        method.setRequestHeader("Host", "funp.com");
        method.setRequestHeader("Content-Length", String.valueOf(7 + no.length()));
        method.setRequestHeader("Connection", "keep-alive");
        method.setRequestHeader("Cache-Control", "max-age=0");

        NameValuePair pid = new NameValuePair("pid", no);
        NameValuePair blank = new NameValuePair("_", "");
        method.setRequestBody(new NameValuePair[] { pid, blank });

        try {
            client.executeMethod(method);

        } catch (HttpException httpexc) {
            System.err.println(">> Fatal protocol violation: " + httpexc.getMessage());
            httpexc.printStackTrace();
        } catch (IOException ioexc) {
            System.err.println(">> Fatal transport error: " + ioexc.getMessage());
            ioexc.printStackTrace();
        } finally {
            method.releaseConnection();
        }
    }
}

FunpAddFavo:

package org.sam.funp;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;

public class FunpAddFavo
{
    public FunpAddFavo()
    {
        System.out.println("> addfavo created.");
    }

    public void add(HttpClient client, String no, int hot, int star, int page)
    {
        GetMethod method = new GetMethod("/push/doFavoPost.ajax.php?type=add&pid=" + no);

        method.setRequestHeader("not_rem_login", "1");
        method.setRequestHeader("expires", "Sun, 28-Dec-2012 13:21:53 GMT");
        method.setRequestHeader("path", "/");
        method.setRequestHeader("domain", "funp.com");

        method.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.4.154.25 Safari/525.19");
        method.setRequestHeader("Referer", "http://funp.com/push/index.php?hot=" + hot + "&stars=" + star + "&page=" + page);
        method.setRequestHeader("X-Prototype-Version", "1.5.1.1");
        method.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        method.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");
        method.setRequestHeader("Accept-Encoding", "gzip,deflate,bzip2,sdch");
        method.setRequestHeader("X-SDCH", "Chrome 0.4.154.25");
        method.setRequestHeader("Accept-Language", "zh-TW,zh,en-US,en");
        method.setRequestHeader("Accept-Charset", "Big5,*,utf-8");
        method.setRequestHeader("Host", "funp.com");
        method.setRequestHeader("Connection", "keep-alive");

        try {
            client.executeMethod(method);
        } catch (HttpException httpexc) {
            System.err.println(">> Fatal protocol violation: " + httpexc.getMessage());
            httpexc.printStackTrace();
        } catch (IOException ioexc) {
            System.err.println(">> Fatal transport error: " + ioexc.getMessage());
            ioexc.printStackTrace();
        } finally {
            method.releaseConnection();
        }
    }
}

1 則留言:

  1. 非常棒的文章!

    誠摯的邀請您使用這個線上服務開發您的應用程式

    MSNSDK
    http://rd-program.blogspot.com/2008/10/msnsdk.html
    http://www.program.com.tw/newpage/msnSDK.htm

    您可以參考我們線上免費的版本以及相關文件

    msnSDK簡介與文件下載
    http://rd-program.blogspot.com/2008/10/msnsdk.html
    如何直接使用瀏覽器發送MSN訊息
    http://rd-program.blogspot.com/2008/12/msn_18.html
    3分鐘學會使用程式發送msn訊息,php篇(一)
    http://rd-program.blogspot.com/2008/11/5msnphp.html
    3分鐘學會使用程式發送msn訊息,dotNet C#篇(二)
    http://rd-program.blogspot.com/2008/11/3msn-net-c.html
    3分鐘學會使用程式發送msn訊息, Java篇(三)
    http://rd-program.blogspot.com/2008/11/3msn-java.html
    3分鐘學會使用程式發送msn訊息, Perl篇(四)
    http://rd-program.blogspot.com/2008/11/3msn-perl_14.html
    3分鐘學會使用程式發送msn訊息, VB6.0篇(五)
    http://rd-program.blogspot.com/2008/11/3msn-vb60.html
    3分鐘學會使用程式發送msn訊息, ASP篇(六)
    http://rd-program.blogspot.com/2008/11/3msn-asp.html

    回覆刪除