[P2-php-svn] [754] expack:

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2010年 1月 1日 (金) 00:42:37 JST


Revision: 754
          http://sourceforge.jp/projects/p2-php/svn/view?view=rev&revision=754
Author:   rsk
Date:     2010-01-01 00:42:37 +0900 (Fri, 01 Jan 2010)

Log Message:
-----------
expack:
- P2Client::downloadDat() でモリタポを消費して生datを
  取得できるようにした。(rep2からはまだ利用できない)

Modified Paths:
--------------
    p2ex/trunk/lib/P2Client.php


-------------- next part --------------
Modified: p2ex/trunk/lib/P2Client.php
===================================================================
--- p2ex/trunk/lib/P2Client.php	2009-12-31 13:39:08 UTC (rev 753)
+++ p2ex/trunk/lib/P2Client.php	2009-12-31 15:42:37 UTC (rev 754)
@@ -7,10 +7,6 @@
 
 /**
  * p2.2ch.net ƒNƒ‰ƒCƒAƒ“ƒg
- *
- * ƒvƒƒoƒCƒ_‹K§Žž‚ɏ‘‚«ž‚Þ‚½‚߂ɐ݌v‚µ‚½B
- * ƒ‚ƒŠƒ^ƒ|‚ðÁ”‚ÄdatŽæ“¾Œ ŒÀ‚Ì–³‚¢dat—Ž‚¿ƒXƒŒƒbƒh‚Ì
- * ¶dat‚ðŽæ“¾‚Å‚«‚é‚悤‚É‚È‚Á‚½‚È‚çA‘¦‘Ήž‚·‚éB
  */
 class P2Client
 {
@@ -27,6 +23,8 @@
     const P2_ROOT_URI = 'http://p2.2ch.net/p2/';
     const SCRIPT_NAME_READ = 'read.php';
     const SCRIPT_NAME_POST = 'post.php';
+    const SCRIPT_NAME_INFO = 'info.php';
+    const SCRIPT_NAME_DAT  = 'dat.php';
 
     /**
      * User-Agent
@@ -34,21 +32,31 @@
     const HTTP_USER_AGENT = 'Monazilla/1.0 (rep2-expack-p2client)';
 
     /**
-     * ƒtƒH[ƒ€‚̃pƒ‰ƒ[ƒ^–¼
+     * HTTPƒŠƒNƒGƒXƒg‚̃pƒ‰ƒ[ƒ^–¼
      */
-    const FIELD_NAME_LOGIN_ID   = 'form_login_id';
-    const FIELD_NAME_LOGIN_PASS = 'form_login_pass';
-    const FIELD_NAME_POPUP      = 'popup';
-    const FIELD_NAME_FROM       = 'FROM';
-    const FIELD_NAME_MAIL       = 'mail';
-    const FIELD_NAME_MESSAGE    = 'MESSAGE';
-    const FIELD_NAME_BERES      = 'submit_beres';
+    const REQUEST_PARAMETER_LOGIN_ID    = 'form_login_id';
+    const REQUEST_PARAMETER_LOGIN_PASS  = 'form_login_pass';
+    const REQUEST_PARAMETER_HOST    = 'host';
+    const REQUEST_PARAMETER_BBS     = 'bbs';
+    const REQUEST_PARAMETER_KEY     = 'key';
+    const REQUEST_PARAMETER_LS      = 'ls';
+    const REQUEST_PARAMETER_FROM    = 'FROM';
+    const REQUEST_PARAMETER_MAIL    = 'mail';
+    const REQUEST_PARAMETER_MESSAGE = 'MESSAGE';
+    const REQUEST_PARAMETER_POPUP   = 'popup';
+    const REQUEST_PARAMETER_BERES   = 'submit_beres';
 
     /**
+     * “ǂݍž‚ݐ³”Û”»’è‚Ì‚½‚ß‚Ì•¶Žš—ñ
+     */
+    const NEEDLE_READ_NO_THREAD = '<b>p2 info - ”ƒT[ƒo‚©‚çÅV‚̃XƒŒƒbƒhî•ñ‚ðŽæ“¾‚Å‚«‚Ü‚¹‚ñ‚Å‚µ‚½B</b>';
+    const NEEDLE_DAT_NO_DAT = '<h4>p2 error: ‚²Žw’è‚ÌDAT‚Í‚ ‚è‚Ü‚¹‚ñ‚Å‚µ‚½</h4>';
+
+    /**
      * ‘‚«ž‚ݐ³”Û”»’è‚Ì‚½‚߂̐³‹K•\Œ»
      */
-    const REGEX_SUCCESS = '{<title>.*(?:‘‚«(?:ž|‚±)‚Ý‚Ü‚µ‚½|‘‚«ž‚ݏI—¹ - SubAll BBS).*</title>}is';
-    const REGEX_COOKIE  = '{<!-- 2ch_X:cookie -->|<title>¡ ‘‚«ž‚ÝŠm”F ¡</title>|>‘‚«ž‚ÝŠm”FB<}';
+    const REGEX_POST_SUCCESS = '{<title>.*(?:‘‚«(?:ž|‚±)‚Ý‚Ü‚µ‚½|‘‚«ž‚ݏI—¹ - SubAll BBS).*</title>}is';
+    const REGEX_POST_COOKIE  = '{<!-- 2ch_X:cookie -->|<title>¡ ‘‚«ž‚ÝŠm”F ¡</title>|>‘‚«ž‚ÝŠm”FB<}';
 
     // }}}
     // {{{ properties
@@ -177,8 +185,8 @@
             $postData[$name] = rawurlencode($value);
         }
         $postData = $this->getFormValues($dom, $form, $postData);
-        $postData[self::FIELD_NAME_LOGIN_ID] = rawurlencode($this->_loginId);
-        $postData[self::FIELD_NAME_LOGIN_PASS] = rawurlencode($this->_loginPass);
+        $postData[self::REQUEST_PARAMETER_LOGIN_ID] = rawurlencode($this->_loginId);
+        $postData[self::REQUEST_PARAMETER_LOGIN_PASS] = rawurlencode($this->_loginPass);
 
         return $this->httpPost($uri, $postData, true);
     }
@@ -192,20 +200,19 @@
      * @param string $host
      * @param string $bbs
      * @param string $key
-     * @param string|integer $ls
+     * @param string $ls
      * @param mixed &$response
      * @return string HTTPƒŒƒXƒ|ƒ“ƒXƒ{ƒfƒB
      * @throws P2Exception
      */
-    public function readThread($host, $bbs, $key, $ls = 1, &$response = null)
+    public function readThread($host, $bbs, $key, $ls = '1', &$response = null)
     {
         $getData = array(
-            'host'  => (string)$host,
-            'bbs'   => (string)$bbs,
-            'key'   => (string)$key,
-            'ls'    => (string)$ls,
+            self::REQUEST_PARAMETER_HOST => (string)$host,
+            self::REQUEST_PARAMETER_BBS  => (string)$bbs,
+            self::REQUEST_PARAMETER_KEY  => (string)$key,
+            self::REQUEST_PARAMETER_LS   => (string)$ls,
         );
-
         $uri = self::P2_ROOT_URI . self::SCRIPT_NAME_READ;
         $response = $this->httpGet($uri, $getData);
         $dom = new P2DOM($response['body']);
@@ -218,20 +225,75 @@
             }
         }
 
+        if (strpos($response['body'], self::NEEDLE_READ_NO_THREAD) !== false) {
+            return null;
+        }
+
         return $response['body'];
     }
 
     // }}}
+    // {{{ downloadDat()
+
+    /**
+     * dat‚ðŽæ‚荞‚Þ
+     *
+     * datŽæ“¾Œ ŒÀ‚ª–³‚¢ê‡‚ÍŽ©“®‚Ń‚ƒŠƒ^ƒ|‚ðÁ”‚Ädat‚ðŽæ“¾‚·‚éB
+     * Ž¸”s‚µ‚Ä‚à‹ƒ‚©‚È‚¢B
+     *
+     * @param string $host
+     * @param string $bbs
+     * @param string $key
+     * @param mixed &$response
+     * @return string ¶dat
+     * @throws P2Exception
+     */
+    public function downloadDat($host, $bbs, $key, &$response = null)
+    {
+        // ƒXƒŒƒbƒh‚Ì—L–³‚ðŠm‚©‚߂邽‚߁A‚Ü‚¸ read.php ‚ð’@‚­B
+        // dat—Ž‚¿Œã‚ɃzƒXƒg‚ªˆÚ“]‚µ‚½ê‡AˆÚ“]Œã‚̃zƒXƒg–¼‚ŃAƒNƒZƒX‚µ‚Ä‚à
+        // ƒXƒŒƒbƒhî•ñ‚ðŽæ“¾‚Å‚«‚È‚©‚Á‚½‚Ƃ̃ƒbƒZ[ƒW‚ª•\Ž¦‚³‚ê‚éB
+        $html = $this->readThread($host, $bbs, $key, 'l1n', $response);
+        if ($html === null) {
+            return null;
+        }
+
+        // uƒ‚ƒŠƒ^ƒ|‚Åp2‚ÉŽæ‚荞‚ށvƒŠƒ“ƒN‚Ì—L–³‚𒲂ׂéB
+        // –³‚¢ê‡‚ÍdatŽæ“¾Œ ŒÀ‚ª‚ ‚é‚à‚Ì‚Æ‚·‚éB
+        $dom = new P2DOM($html);
+        $expression = './/a[contains(@href, "' . self::SCRIPT_NAME_READ . '")'
+                    . ' and contains(@href, "&moritapodat=")]';
+        $result = $dom->query($expression);
+        if ($result instanceof DOMNodeList && $result->length > 0) {
+            $anchor = $result->item(0);
+            $uri = self::P2_ROOT_URI
+                 . substr($anchor->getAttribute('href'), self::SCRIPT_NAME_READ);
+            $response = $this->httpGet($uri);
+        }
+
+        // dat‚ðŽæ“¾‚·‚éB
+        // datŽæ“¾Œ ŒÀ‚ª‚È‚¢ê‡‚⃂ƒŠƒ^ƒ|’Ê’ ‚ÌŽc‚‚ª‘«‚è‚È‚¢ê‡‚̏ˆ—‚Í’[Ü‚éB
+        $getData = array(
+            self::REQUEST_PARAMETER_HOST => (string)$host,
+            self::REQUEST_PARAMETER_BBS  => (string)$bbs,
+            self::REQUEST_PARAMETER_KEY  => (string)$key,
+        );
+        $uri = self::P2_ROOT_URI . self::SCRIPT_NAME_DAT;
+        $response = $this->httpGet($uri, $getData);
+
+        if (strpos($response['body'], self::NEEDLE_DAT_NO_DAT) !== false) {
+            return null;
+        }
+
+        return $response['body'];
+    }
+
+    // }}}
     // {{{ post()
 
     /**
      * ƒXƒŒƒbƒh‚ɏ‘‚«ž‚Þ
      *
-     * csrfId‚ðŽæ“¾‚µA‚©‚ÂŒöŽ®p2‚ÌŠù“Ç‚ðÅV‚̏ó‘Ô‚É‚·‚邽‚߁A
-     * ‚Ü‚¸ read.php ‚ð’@‚­B
-     * ’ʐM—Ê‚ðß–ñ‚Å‚«‚é‚悤‚É ls=l1n ‚Æ‚µ‚Ä‚¢‚éB
-     * popup=1 ‚͏‘‚«ž‚ÝŒã‚̃y[ƒW‚ɃŠƒ_ƒCƒŒƒNƒg‚³‚¹‚È‚¢‚½‚߁B
-     *
      * @param string $host
      * @param string $bbs
      * @param string $key
@@ -246,24 +308,32 @@
     public function post($host, $bbs, $key, $from, $mail, $message,
                          $beRes = false, &$response = null)
     {
-        $dom = new P2DOM($this->readThread($host, $bbs, $key, 'l1n', $response));
+        // csrfId‚ðŽæ“¾‚µA‚©‚ÂŒöŽ®p2‚ÌŠù“Ç‚ðÅV‚̏ó‘Ô‚É‚·‚邽‚߁A‚Ü‚¸ read.php ‚ð’@‚­B
+        // ’ʐM—Ê‚ðß–ñ‚Å‚«‚é‚悤‚É ls=l1n ‚Æ‚µ‚Ä‚¢‚éB
+        // popup=1 ‚͏‘‚«ž‚ÝŒã‚̃y[ƒW‚ɃŠƒ_ƒCƒŒƒNƒg‚³‚¹‚È‚¢‚½‚߁B
+        $html = $this->readThread($host, $bbs, $key, 'l1n', $response);
+        if ($html === null) {
+            return false;
+        }
+
+        $dom = new P2DOM($html);
         if ($form = $this->getPostForm($dom)) {
             $uri = self::P2_ROOT_URI . self::SCRIPT_NAME_POST;
 
             $postData = $this->getFormValues($dom, $form);
-            $postData[self::FIELD_NAME_POPUP]   = '1';
-            $postData[self::FIELD_NAME_FROM]    = rawurlencode($from);
-            $postData[self::FIELD_NAME_MAIL]    = rawurlencode($mail);
-            $postData[self::FIELD_NAME_MESSAGE] = rawurlencode($message);
+            $postData[self::REQUEST_PARAMETER_POPUP]   = '1';
+            $postData[self::REQUEST_PARAMETER_FROM]    = rawurlencode($from);
+            $postData[self::REQUEST_PARAMETER_MAIL]    = rawurlencode($mail);
+            $postData[self::REQUEST_PARAMETER_MESSAGE] = rawurlencode($message);
             if ($beRes) {
-                $postData[self::FIELD_NAME_BERES] = '1';
-            } elseif (array_key_exists(self::FIELD_NAME_BERES, $postData)) {
-                unset($postData[self::FIELD_NAME_BERES]);
+                $postData[self::REQUEST_PARAMETER_BERES] = '1';
+            } elseif (array_key_exists(self::REQUEST_PARAMETER_BERES, $postData)) {
+                unset($postData[self::REQUEST_PARAMETER_BERES]);
             }
 
             $response = $this->httpPost($uri, $postData, true);
 
-            if (preg_match(self::REGEX_COOKIE, $response['body'])) {
+            if (preg_match(self::REGEX_POST_COOKIE, $response['body'])) {
                 $dom = new P2DOM($response['body']);
                 $expression = './/form[contains(@action, "' . self::SCRIPT_NAME_POST . '")]';
                 $result = $dom->query($expression);
@@ -273,7 +343,7 @@
                 }
             }
 
-            return (bool)preg_match(self::REGEX_SUCCESS, $response['body']);
+            return (bool)preg_match(self::REGEX_POST_SUCCESS, $response['body']);
         } else {
             throw new P2Exception('Post form not found.');
         }



P2-php-svn メーリングリストの案内
Back to archive index