Advertisement
Ewwe

WorldOfTrucks 1.1 - PHP 7

Jun 11th, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.73 KB | None | 0 0
  1. <?php
  2. class WorldOfTrucks
  3. {
  4.  
  5.     private $uid = 0;
  6.     private $url = "https://www.worldoftrucks.com/en/online_profile.php?id=";
  7.     private $wholecontent;
  8.  
  9.  
  10.     function __construct( int $id )
  11.     {
  12.         $this->uid = $id;
  13.         $this->wholecontent = file_get_contents( $this->url.$this->uid );
  14.     }
  15.  
  16.     public function getStats( string $which ): array
  17.     {
  18.         $data = $this->loadStatsTable($which);
  19.         $data = explode("</div>",$data);
  20.         unset($data[13]);
  21.         unset($data[14]);
  22.         $output = [
  23.             "TOTAL_JOBS" => $this->clean($data[0]),
  24.             "TOTAL_TIME" => $this->clean($data[1]),
  25.             "TOTAL_MASS" => $this->clean($data[2]),
  26.             "TOTAL_TRIP" => $this->clean($data[5]),
  27.             "AVG_TRIP" => $this->clean($data[3]),
  28.             "LONGEST_TRIP" => $this->clean($data[4]),
  29.             "DAY_STREAK" => $this->clean( $data[6] ),
  30.             "TOP_CITY" => $this->clean($data[9]),
  31.             "TOP_SOURCE" => $this->clean($data[10]),
  32.             "TOP_SUPPL" => $this->clean($data[11]),
  33.             "TOP_CARGO" => $this->clean($data[12]),
  34.         ];
  35.         return $output;
  36.     }
  37.  
  38.     public function getTruck( string $game ): array
  39.     {
  40.         $data = $this->loadTruckTable($game);
  41.         $data = explode( "</tr>" , $data);
  42.         unset($data[8]);
  43.         foreach( $data as $item)
  44.         {
  45.             $item = explode( "</td>" , $item);
  46.             unset($item[0]);
  47.             unset($item[2]);
  48.             $temp[] = $this->clean( $item[1] );
  49.         }
  50.         unset($temp[7]);
  51.         $output =[
  52.             "TRUCK" =>$this->getTruckName($game),
  53.             "CABIN" => $temp[0],
  54.             "CHASSIS" => $temp[1],
  55.             "PERFORMANCE" => $temp[2],
  56.             "TORQUE" => $temp[3],
  57.             "ENGINE" => $temp[4],
  58.             "GEARBOX" => $temp[5]
  59.         ];
  60.         return $output;
  61.     }
  62.  
  63.     private function getTruckName( string $game ): string
  64.     {
  65.         $ets = $this->get_string_between( $this->wholecontent , "Euro Truck Simulator 2" , "</p>");
  66.         $ats = $this->get_string_between( $this->wholecontent , "American Truck Simulator" , "</p>");
  67.         $output = [ "ETS" => $this->clean($ets), "ATS" => $this->clean($ats)];
  68.         return $output[$game];
  69.     }
  70.  
  71.     public function getDriverName(): string
  72.     {
  73.         $content = $this->get_string_between( $this->wholecontent , "<h2 class=\"orange\">","</h2>");
  74.         $data = $this->get_string_between( $content , "<span>","</span>");
  75.         return $data;
  76.     }
  77.  
  78.     private function loadTruckTable(string $game ): string
  79.     {
  80.         $data = $this->get_string_between( $this->wholecontent , "<h2>MY CURRENT RIDES</h2>", "<div class=\"cleaner\"></div>" );
  81.         $data = explode("<div class=\"truck-info\">",$data);
  82.         $output = [ "ETS" => $data[1],"ATS"=>$data[2]];
  83.         return $output[$game];
  84.     }
  85.  
  86.     private function loadStatsTable( string $get ): string
  87.     {
  88.         $data = $this->get_string_between( $this->wholecontent , "<h2>Full statistics</h2>", "<div class=\"cleaner\"></div>" );
  89.         $data = explode("</p>",$data);
  90.         $output = [
  91.             "ETS" => $data[2],
  92.             "ATS" => $data[3],
  93.             "GLOBAL" => $data[4]
  94.         ];
  95.         return $output[$get];
  96.     }
  97.  
  98.     private function clean( string $string ): string
  99.     {
  100.         return trim( strip_tags( $string ));
  101.     }
  102.  
  103.     private function get_string_between( string $string , string $start , string $end ): string
  104.     {
  105.         $string = ' ' . $string;
  106.         $ini = strpos($string, $start);
  107.         if ($ini == 0) return '';
  108.         $ini += strlen($start);
  109.         $len = strpos($string, $end, $ini) - $ini;
  110.         return substr($string, $ini, $len);
  111.     }
  112. }
  113.  
  114. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement