Exemplo n.º 1
0
 /**
  * Parses book details from the given HTML book description.
  *
  * The resulting book-details array will have a subset of the following keys:
  *  - isbn
  *  - title
  *  - authors
  *  - edition
  *  - publisher
  *  - section_id
  *  - bookstore_id
  *  - bookstore_part_number
  *  - bookstore_used_price
  *  - bookstore_new_price
  *  - type
  *  - required_status
  *
  * @param string $html HTML for a book description on the bookstore website
  * @return array An array of book details, or FALSE on error
  */
 protected function parse_book_details($html)
 {
     if (strpos($html, 'Currently no textbook') || strpos($html, 'Pre-Order')) {
         return FALSE;
     }
     $details = array();
     if (preg_match("/value\\='(\\d{8})'/", $html, $matches)) {
         $details['section_id'] = $matches[1];
     }
     if (preg_match("/ISBN\\:\\<\\/span\\>.+?(\\d+).+?\\</s", $html, $matches)) {
         $isbn = ISBN::to13(trim($matches[1]), TRUE);
         if ($isbn) {
             $details['isbn'] = $isbn;
         }
     }
     if (preg_match("/\\d{5}'\\stitle\\=\"(.+?)\"\\>.+?\\<img/s", $html, $matches)) {
         $details['title'] = $this->title_case(trim(htmlspecialchars_decode($matches[1], ENT_QUOTES)));
     }
     if (preg_match("/\\<span\\>Author:.*?\\<\\/span\\>(.+?)\\<\\/li/s", $html, $matches)) {
         $author = $this->ucname(trim($matches[1]));
         $details['authors'] = is_numeric($author) ? "" : $author;
     }
     if (preg_match("/Edition:\\<\\/span\\>(.+?)\\<br/", $html, $matches)) {
         $details['edition'] = strtolower(trim($matches[1]));
     }
     if (preg_match("/Publisher:\\<\\/span\\>(.+?)\\<br/", $html, $matches)) {
         $details['publisher'] = $this->title_case(trim($matches[1]));
     }
     if (preg_match("/productId\\=(.+?)&/", $html, $matches)) {
         $details['bookstore_id'] = trim($matches[1]);
     }
     if (preg_match("/partNumber\\=(.+?)&/", $html, $matches)) {
         $details['bookstore_part_number'] = rtrim($matches[1], "&amp;");
     }
     if (preg_match("/Used.+?(\\d{1,3}\\.\\d{2})/s", $html, $matches)) {
         $details['bookstore_used_price'] = $matches[1];
     }
     if (preg_match("/New.+?(\\d{1,3}\\.\\d{2})/s", $html, $matches)) {
         $details['bookstore_new_price'] = $matches[1];
     }
     $options = array("REQUIRED\\sPACKAGE", "RECOMMENDED\\sPACKAGE", "REQUIRED", "RECOMMENDED", "PACKAGE\\sCOMPONENT", "GO\\sTO\\sCLASS\\sFIRST", "BOOKSTORE\\sRECOMMENDED");
     preg_match("/" . implode('|', $options) . "/", $html, $matches);
     $required = trim($matches[0]);
     if ($required == "REQUIRED PACKAGE" || $required == "RECOMMENDED PACKAGE") {
         $type = Book_model::PACKAGE;
     } else {
         if ($required == "PACKAGE COMPONENT") {
             $type = Book_model::PACKAGE_COMPONENT;
         } else {
             $type = Book_model::BOOK;
         }
     }
     $details['type'] = $type;
     if ($required == "REQUIRED PACKAGE" || $required == "REQUIRED") {
         $required = Book_model::REQUIRED;
     } else {
         if ($required == "RECOMMENDED PACKAGE" || $required == "RECOMMENDED") {
             $required = Book_model::RECOMMENDED;
         } else {
             if ($required == "GO TO CLASS FIRST") {
                 $required = Book_model::GO_TO_CLASS_FIRST;
             } else {
                 if ($required == "BOOKSTORE RECOMMENDED") {
                     $required = Book_model::BOOKSTORE_RECOMMENDED;
                 } else {
                     $required = Book_model::REQUIRED;
                 }
             }
         }
     }
     $details['required_status'] = $required;
     return $details;
 }