Ewwe

WorldOfTrucks 2.0

Dec 21st, 2016
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1. class WorldOfTrucks
  2. {
  3.     private $instanceID = null;
  4.     private $websiteURI = "https://worldoftrucks.com/en/online_profile.php?id=";
  5.     private $websiteData = null;
  6.     private $claimedData = null;
  7.  
  8.     public function __construct( $uid )
  9.     {
  10.         $this->instanceID = $uid;
  11.         $this->websiteData = file_get_contents( $this->websiteURI.$this->instanceID );
  12.     }
  13.  
  14.     public function fetchData()
  15.     {
  16.         $this->websiteData = $this->gsb( $this->websiteData , "Global statistics" , "cleaner" );
  17.         $this->websiteData = explode("\n",$this->websiteData);
  18.         $out = [
  19.             $this->websiteData[2],
  20.             $this->websiteData[9],
  21.             $this->websiteData[16],
  22.             $this->websiteData[23],
  23.             $this->websiteData[30]
  24.         ];
  25.         foreach( $out as $row )
  26.         {
  27.             $temp = explode("<br>",$row);
  28.             $output[] = [
  29.                 "ETS" => $this->gsb($temp[0],"right'>","</span>"),
  30.                 "ATS" => $this->gsb($temp[1],"right'>","</span>"),
  31.             ];
  32.         }
  33.         $this->claimedData = $output;
  34.         $this->normalizeUnits();
  35.     }
  36.     public function debug()
  37.     {
  38.         var_dump($this->claimedData);
  39.     }
  40.  
  41.     private function normalizeUnits()
  42.     {
  43.         $data = [
  44.             (int)round((explode(" ",$this->claimedData[1]["ATS"])[0]*1.016)),
  45.             (int)round(explode(" ",$this->claimedData[3]["ATS"])[0]*1.609344),
  46.             (int)str_replace(',','',explode(" ",$this->claimedData[4]["ETS"])[0]),
  47.             (int)round(str_replace(',','',explode(" ",$this->claimedData[4]["ATS"])[0])*1.609344)
  48.         ];
  49.         $this->claimedData[1]["ATS"] = $data[0]." t";
  50.         $this->claimedData[3]["ATS"] = $data[1]." km";
  51.         $this->claimedData[4]["ETS"] = $data[2]." km";
  52.         $this->claimedData[4]["ATS"] = $data[3]." km";
  53.     }
  54.  
  55.     public function getData( $mode = "GLOBAL" )
  56.     {
  57.         if( $mode == "GLOBAL" )
  58.         {
  59.             $out = [
  60.                 "TOTAL_JOBS" => $this->claimedData[0]["ETS"]+$this->claimedData[0]["ATS"],
  61.                 "TOTAL_MASS" => explode(" ",$this->claimedData[1]["ATS"])[0]+explode(" ",$this->claimedData[1]["ETS"])[0]." t",
  62.                 "TOTAL_DISTANCE" => (explode(" ",$this->claimedData[4]["ATS"])[0])+(explode(" ",$this->claimedData[4]["ETS"])[0])." t",
  63.                 "AVERAGE_DISTANCE" => round((explode(" ",$this->claimedData[4]["ATS"])[0]+explode(" ",$this->claimedData[4]["ETS"])[0])/( $this->claimedData[0]["ETS"]+$this->claimedData[0]["ATS"]))." km",
  64.                 "TOTAL_TIME" => (explode(" ",$this->claimedData[2]["ETS"])[0] + explode(" ",$this->claimedData[2]["ATS"])[0])." h ".( explode(" ",$this->claimedData[2]["ETS"])[2] + explode(" ",$this->claimedData[2]["ATS"])[2] )." min"
  65.             ];
  66.         }else{
  67.             $out = [
  68.                 "TOTAL_JOBS" => $this->claimedData[0][$mode],
  69.                 "TOTAL_MASS" => $this->claimedData[1][$mode],
  70.                 "TOTAL_DISTANCE" => $this->claimedData[4][$mode],
  71.                 "AVERAGE_DISTANCE" => $this->claimedData[3][$mode],
  72.                 "TOTAL_TIME" => $this->claimedData[2][$mode],
  73.             ];
  74.         }
  75.         var_dump($out);
  76.     }
  77.  
  78.     private function gsb( string $string , string $start , string $end )
  79.     {
  80.         $string = ' ' . $string;
  81.         $ini = strpos($string, $start);
  82.         if ($ini == 0) return '';
  83.         $ini += strlen($start);
  84.         $len = strpos($string, $end, $ini) - $ini;
  85.         return substr($string, $ini, $len);
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment