Advertisement
Fwaky

Fetches all sorts of data from Tibia.com

Feb 22nd, 2014
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.91 KB | None | 0 0
  1. <?php
  2. /**
  3. *     Author: Johan Hultin
  4. *     Last revision: 2013-06-29 21:13:00
  5. **/
  6.  
  7. class Tibia
  8. {
  9.     private $vocations = array("None", "Druid", "Elder Druid", "Sorcerer", "Master sorcerer", "Paladin", "Royal Paladin", "Knight", "Elite knight", "Unknown");
  10.     public function vocationId($vocation){
  11.         $vocations = $this->vocations;
  12.         return array_search($vocation, $vocations);
  13.     }
  14.     /**
  15.     *     Gets the list of the character's deaths
  16.     **/
  17.     public function characterDeaths($name)
  18.     {
  19.         $html = $this->getUrl("http://www.tibia.com/community/?subtopic=characters&name=".$name);
  20.  
  21.         if (false !== stripos($html, "<b>Could not find character</b>")) {
  22.             // Throw a error here
  23.         }
  24.  
  25.         $domd = $this->getDOMDocument($html);
  26.         $domx = new DOMXPath($domd);
  27.         $rows = $domx->query("//b[text() = 'Character Deaths']/ancestor::table[1]//tr[position() > 1]");
  28.         $deaths = array();
  29.  
  30.         foreach ($rows as $row) {
  31.             $date = $row->firstChild->nodeValue;
  32.             $text = $row->lastChild->nodeValue;
  33.  
  34.             preg_match("/Died at Level (\\d+) by (.+)\\./", $text, $matches);
  35.  
  36.             $deaths[] = array(
  37.                 "date"      =>  DateTime::createFromFormat("M d Y, H:i:s T", $date),
  38.                 "level"     =>  $matches[1],
  39.                 "reason"    =>  $matches[2],
  40.             );
  41.         }
  42.  
  43.         return $deaths;
  44.     }
  45.  
  46.     /**
  47.     *     Gets information about the given character
  48.     **/
  49.     public function characterInfo($name)
  50.     {
  51.         $html = $this->getUrl("http://www.tibia.com/community/?subtopic=characters&name=".$name);
  52.  
  53.         if (false !== stripos($html, "<b>Could not find character</b>")) {
  54.             $character["not_found"] = true;
  55.         } else {
  56.             $map = array(
  57.                 "Name:" => "name",
  58.                 "Former Names:" => "former_names",
  59.                 "Sex:" => "sex",
  60.                 "Vocation:" => "vocation",
  61.                 "Level:" => "level",
  62.                 "World:" => "world",
  63.                 "Former world:" => "former_world",
  64.                 "Residence:" => "residence",
  65.                 "Achievement Points:" => "achievement_points",
  66.                 "Last login:" => "last_login",
  67.                 "Comment:" => "comment",
  68.                 "Account Status:" => "account_status",
  69.                 "Married to:" => "married_to",
  70.                 "House:" => "house",
  71.                 "Guild membership:" => "guild",
  72.                 "Comment:" => "comment",
  73.             );
  74.  
  75.             $domd = $this->getDOMDocument($html);
  76.             $domx = new DOMXPath($domd);
  77.             $character = array();
  78.  
  79.             $rows = $domx->query("//div[@class='BoxContent']/table[1]/tr[position() > 1]");
  80.             foreach ($rows as $row) {
  81.                 $name  = trim($row->firstChild->nodeValue);
  82.                 $value = trim($row->lastChild->nodeValue);
  83.  
  84.                 if (isset($map[$name])) {
  85.                     $character[$map[$name]] = $value;
  86.                 } else {
  87.                     $character[$name] = $value;
  88.                 }
  89.             }
  90.  
  91.             // value cleanup
  92.  
  93.             $character["last_login"] = DateTime::createFromFormat("M d Y, H:i:s T", $character["last_login"]);
  94.  
  95.             if (isset($character["guild"])) {
  96.                 $values = explode(" of the ", $character["guild"]);
  97.                 $character["guild"] = array(
  98.                     "name"  =>  $values[1],
  99.                     "rank"  =>  $values[0],
  100.                 );
  101.             }
  102.  
  103.             if (isset($character["house"])) {
  104.                 $values = explode(" is paid until ", $character["house"]);
  105.                 $character["house"] = $values[0];
  106.             }
  107.         }
  108.         return $character;
  109.     }
  110.  
  111.     /**
  112.     *    Return the list of characters online at the given world
  113.     **/
  114.     public function whoIsOnline($world)
  115.     {
  116.         $html = $this->getUrl("http://www.tibia.com/community/?subtopic=worlds&world=" . $world);
  117.         $domd = $this->getDOMDocument($html);
  118.  
  119.         $domx = new DOMXPath($domd);
  120.         $characters = $domx->query("//table[@class='Table2']//tr[position() > 1]");
  121.         $ret = array();
  122.  
  123.         foreach ($characters as $character) {
  124.             $name     = $domx->query("td[1]/a[@href]", $character)->item(0)->nodeValue;
  125.             $level    = $domx->query("td[2]", $character)->item(0)->nodeValue;
  126.             $vocation = $domx->query("td[3]", $character)->item(0)->nodeValue;
  127.  
  128.             $ret[] = array(
  129.                 "name"      =>  $name,
  130.                 "level"     =>  $level,
  131.                 "vocation"  =>  $vocation,
  132.             );
  133.         }
  134.  
  135.         return $ret;
  136.     }
  137.     /**
  138.     *    Retrieves a list of all worlds
  139.     **/
  140.     public function getWorlds(){
  141.         $html = $this->getUrl("http://www.tibia.com/community/?subtopic=worlds");
  142.        
  143.         $domd = $this->getDOMDocument($html);
  144.         $domx = new DOMXPath($domd);
  145.         $worlds = $domx->query("//table[@class='TableContent']//tr[position() > 1]");
  146.         $ret = array();
  147.        
  148.         foreach($worlds as $world){
  149.             $name = $domx->query("td[1]/a[@href]", $world)->item(0)->nodeValue;
  150.             $type = $domx->query("td[4]", $world)->item(0)->nodeValue;
  151.             $area = $domx->query("td[3]", $world)->item(0)->nodeValue;
  152.            
  153.             $ret[] = array(
  154.                 "name"      => $name,
  155.                 "location"  => $area,
  156.                 "worldtype" => $type,
  157.             );
  158.         }
  159.        
  160.         return $ret;
  161.     }
  162.     /**
  163.     *    Retrieves a list of all worlds
  164.     */ 
  165.     public function getHighscores($world, $type, $page){
  166.         $type = strtolower($type);
  167.         $html = $this->getUrl("http://www.tibia.com/community/?subtopic=highscores&world=".$world."&list=".$type."&page=".$page);
  168.         $domd = $this->getDOMDocument($html);
  169.         $domx = new DOMXPath($domd);
  170.         $worlds = $domx->query("//b[text() = 'Rank']/ancestor::table[1]//tr[position() > 1]");
  171.         $ret = array();
  172.         foreach($worlds as $world){
  173.             @$rank = $domx->query("td[1]", $world)->item(0)->nodeValue;
  174.             @$name = $domx->query("td[2]/a[@href]", $world)->item(0)->nodeValue;
  175.             if($type == 'experience'){
  176.                 @$level = $domx->query("td[3]", $world)->item(0)->nodeValue;
  177.                 @$skill = $domx->query("td[4]", $world)->item(0)->nodeValue;
  178.             } else {
  179.                 @$skill = $domx->query("td[3]", $world)->item(0)->nodeValue;
  180.             }
  181.             $ret[] = array(
  182.                 "name"      => @$name,
  183.                 "rank"  => @$rank,
  184.                 "value" => @$skill,
  185.                 "level" => @$level,
  186.             );
  187.         }
  188.         unset($ret[0]); // To prevent an extra row!
  189.         return $ret;
  190.     }
  191.     /**
  192.     *    Creates a DOMDocument object from a given html string
  193.     **/
  194.     private function getDOMDocument($html)
  195.     {
  196.         $domd = new DOMDocument("1.0", "utf-8");
  197.  
  198.         $replace = array(
  199.             "&#160;"    =>  " ", // non-breaking space in the page's code
  200.             chr(160)    =>  " ", // non-breaking space in character comments
  201.         );
  202.         $html = str_replace(array_keys($replace), array_values($replace), $html);
  203.  
  204.         $html = mb_convert_encoding($html, "utf-8", "iso-8859-1");
  205.  
  206.         libxml_use_internal_errors(true);
  207.         $domd->loadHTML($html);
  208.         libxml_use_internal_errors(false);
  209.  
  210.         return $domd;
  211.     }
  212.  
  213.     /**
  214.     *    Fetches a page from tibia.com and returns its body
  215.     **/
  216.     private function getUrl($url)
  217.     {
  218.         $ch = curl_init();
  219.         curl_setopt($ch, CURLOPT_URL, $url);
  220.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  221.         $output = curl_exec($ch);
  222.         return $output;
  223.     }
  224. }
  225. /* End of file tibiaparser.class.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement