added body
added body

--- a/app/lib/AutovitFetcher.php
+++ b/app/lib/AutovitFetcher.php
@@ -90,15 +90,16 @@
 				$carLink = addslashes ($this->domain.$carMatch[1]);
 
 				$carDetails = $this->getBrandModel ($carMatch[2]);
-
-				$brandId = $this->storeCarBrand ($carDetails['brand']);
-				$modelId = $this->storeCarModel ($carDetails['model'], $brandId);
-
 				$carData = $this->getCarData ($matchData[1][$key]);
 				$carData['price'] = str_replace (" ", "", $price);
+
+				$bodyId = $this->storeCarBody ($carData['body']);
+				$brandId = $this->storeCarBrand ($carDetails['brand']);
+				$modelId = $this->storeCarModel ($carDetails['model'], $brandId, $bodyId);
+
 				$engineId = $this->storeCarEngine ($carData['engine'], $brandId, $modelId);
 
-				$this->storeCarPrice ($brandId, $modelId, $engineId, $carLink, $carData);
+				$this->storeCarPrice ($brandId, $modelId, $engineId, $bodyId, $carLink, $carData);
 			}
 			$this->debug ("memory usage: ".$this->getMemoryUsage(), 2);
 			$seconds = mt_rand (1, 5);

--- a/app/lib/Common.php
+++ b/app/lib/Common.php
@@ -29,6 +29,7 @@
 	private $cacheBrands = array ();
 	private $cacheModels = array ();
 	private $cacheEngine = array ();
+	private $cacheBody = array ();
 
 	protected function __construct ()
 	{
@@ -62,7 +63,6 @@
 		}
 		$cookie = tmpfile();
 		$ch = curl_init($this->urlBase.$url);
-
 		$options = array(
 			CURLOPT_CONNECTTIMEOUT => 20 ,
 			CURLOPT_USERAGENT => $this->userAgent,
@@ -88,6 +88,8 @@
 		$this->processedHtml = preg_replace ("/\r\n/i", "", $this->processedHtml);
 		$this->processedHtml = preg_replace ("/\n/i", "", $this->processedHtml);
 		$this->processedHtml = preg_replace ("/\r/i", "", $this->processedHtml);
+		$this->processedHtml = preg_replace ("/\t/i", " ", $this->processedHtml);
+		$this->processedHtml = preg_replace ("/([ ]+)/i", " ", $this->processedHtml);
 	}
 
 	protected function storeCarBrand ($brandName)
@@ -108,18 +110,18 @@
 		return $id;
 	}
 
-	protected function storeCarModel ($modelName, $brandId)
+	protected function storeCarModel ($modelName, $brandId, $bodyId)
 	{
 		$modelName = trim ($modelName);
-		$value = $brandId.$modelName;
+		$value = $brandId.$bodyId.$modelName;
 		if ( in_array ($value, $this->cacheModels) ) {
 			return array_search($value, $this->cacheModels);
 		}
-		Db::query ("SELECT `id` FROM `model` WHERE `brand_id`='".$brandId."' AND `modelName`='".$modelName."'");
-		if ($row = Db::fetchArray()) {
-			$id = $row['id'];
-		} else {
-			Db::query ("INSERT INTO `model` (`id`, `brand_id`, `modelName`, `active`) VALUES ('NULL', '".$brandId."', '".$modelName."', '1')");
+		Db::query ("SELECT `id` FROM `model` WHERE `brand_id`='".$brandId."' AND `body_id`='".$bodyId."' AND `modelName`='".$modelName."'");
+		if ($row = Db::fetchArray()) {
+			$id = $row['id'];
+		} else {
+			Db::query ("INSERT INTO `model` (`id`, `brand_id`, `body_id`, `modelName`, `active`) VALUES ('NULL', '".$brandId."', '".$bodyId."', '".$modelName."', '1')");
 			$id = Db::getId ();
 		}
 		$this->cacheModels[$id] = $value;
@@ -146,15 +148,33 @@
 		return $id;
 	}
 
-	protected function storeCarPrice ($brandId, $modelId, $engineId, $carLink, $carData)
+	protected function storeCarBody ($bodyName)
+	{
+		$bodyName = trim ($bodyName);
+		$value = $bodyName;
+		if ( in_array ($value, $this->cacheBody) ) {
+			return array_search($value, $this->cacheBody);
+		}
+		Db::query ("SELECT `id` FROM `body` WHERE `bodyName`='".$bodyName."'");
+		if ($row = Db::fetchArray()) {
+			$id = $row['id'];
+		} else {
+			Db::query ("INSERT INTO `body` (`id`, `bodyName`, `active`) VALUES ('NULL', '".$bodyName."', '1')");
+			$id = Db::getId ();
+		}
+		$this->cacheBody[$id] = $value;
+		return $id;
+	}
+
+	protected function storeCarPrice ($brandId, $modelId, $engineId, $bodyId, $carLink, $carData)
 	{
 		$cc = $this->getCc($carData['engine']);
 		$fuel = $this->getFuel ($carData['engine']);
 		Db::query ("SELECT `id` FROM `prices` WHERE `brand_id`='".$brandId."' AND `model_id`='".$modelId."' AND `carLink`='".$carLink."'");
 		if ($row = Db::fetchArray()) {
 		} else {
-			Db::query("INSERT INTO `prices` (`id`, `brand_id`, `model_id`, `engine_id`, `carLink`, `year`, `fuel`, `cc`, `km`, `price`, `active`)
-			VALUES ('NULL', '".$brandId."', '".$modelId."', '".$engineId."', '".$carLink."', '".$carData['year']."', '".$fuel."', '".$cc."', '".$carData['km']."', '".$carData['price']."', '1')");
+			Db::query("INSERT INTO `prices` (`id`, `brand_id`, `model_id`, `engine_id`, `body_id`, `carLink`, `year`, `fuel`, `cc`, `km`, `price`, `active`)
+			VALUES ('NULL', '".$brandId."', '".$modelId."', '".$engineId."', '".$bodyId."', '".$carLink."', '".$carData['year']."', '".$fuel."', '".$cc."', '".$carData['km']."', '".$carData['price']."', '1')");
 		}
 	}
 
@@ -179,6 +199,11 @@
 		return $this->processedHtml;
 	}
 
+	protected function getOriginalHtml ()
+	{
+		return $this->originalHtml;
+	}
+
 	protected function getMemoryUsage ()
 	{
 		$size = memory_get_usage(true);
@@ -186,6 +211,11 @@
 		return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
 	}
 
+	protected function getFuels ()
+	{
+		return $this->fuels;
+	}
+
 	protected function debug ($message, $indent=1)
 	{
 		echo str_repeat("\t", $indent)." - ".$message."\r\n";

file:a/fetch.php -> file:b/fetch.php
--- a/fetch.php
+++ b/fetch.php
@@ -12,8 +12,12 @@
 
 require (appRoot."app/autoload.php");
 
-use carstats\AutovitFetcher;
-$autovit = new carstats\AutovitFetcher;
-$autovit->batch ();
+//use carstats\AutovitFetcher;
+//$autovit = new carstats\AutovitFetcher;
+//$autovit->batch ();
+
+use carstats\BestautoFetcher;
+$bestauto = new carstats\BestautoFetcher;
+$bestauto->batch ();
 
 ?>

comments