custom brand replacements, script for each entry in database
custom brand replacements, script for each entry in database

--- a/app/lib/AutovitFetcher.php
+++ b/app/lib/AutovitFetcher.php
@@ -8,6 +8,7 @@
 
 class AutovitFetcher extends Common {
 
+	protected $script = "autovit";
 	public $startPage = 1;
 	public $totalPages = 1;
 	private $carBrands = array ();

--- a/app/lib/BestautoFetcher.php
+++ b/app/lib/BestautoFetcher.php
@@ -8,6 +8,7 @@
 
 class BestautoFetcher extends Common {
 
+	protected $script = "bestauto";
 	public $startPage = 1;
 	public $totalPages = 1;
 	private $carBrands = array ();
@@ -31,6 +32,9 @@
 		37 => 6,
 		3 => 11
 	);
+	private $brandReplacements = array (
+		"Vw"	=>	"Volkswagen"
+	);
 
 	public function __construct ()
 	{
@@ -49,11 +53,26 @@
 		$brandsOptions = $this->match ("|ddlMarca\" onchange\=\"(.*)\"\>(.*)\<\/select\>|isU", $this->getProcessedHtml());
 		$brandsMatch = $this->matchAll ("|\<option value\=\"([0-9]+)\"\>(.*)\<\/option\>|isU", $brandsOptions[2]);
 		unset ($brandsMatch[2][0]);
-		$this->carBrands = $brandsMatch[2];
+		$this->carBrands = $this->customBrandReplace ($brandsMatch[2]);
+	}
+
+	protected function customBrandReplace ($brands)
+	{
+		foreach ($this->brandReplacements as $value => $replacement) {
+			if ( is_array ($brands) ) {
+				$brands = array_map(function ($v) use ($value, $replacement) {
+				    return $v == $value ? $replacement : $v;
+				}, $brands);
+			} else {
+				$brands = str_replace ($value, $replacement, $brands);
+			}
+		}
+		return $brands;
 	}
 
 	protected function getBrandModel ($model)
 	{
+		$model = $this->customBrandReplace ($model);
 		foreach ($this->carBrands as $brand) {
 			if ( preg_match ("/^".$brand."/i", $model) ) {
 				return array (
@@ -78,6 +97,7 @@
 
 	public function batch ()
 	{
+		exit;
 		$this->getTotalPages ();
 
 		foreach ($this->bodyAssoc as $scat => $bodyId ) {

--- a/app/lib/Common.php
+++ b/app/lib/Common.php
@@ -106,7 +106,7 @@
 		if ($row = Db::fetchArray()) {
 			$id = $row['id'];
 		} else {
-			Db::query ("INSERT INTO `brand` (`id`, `brandName`, `active`) VALUES ('NULL', '".$brandName."', '1')");
+			Db::query ("INSERT INTO `brand` (`id`, `script`, `brandName`, `active`) VALUES ('NULL', '".$this->script."', '".$brandName."', '1')");
 			$id = Db::getId ();
 		}
 		$this->cacheBrands[$id] = $value;
@@ -124,7 +124,7 @@
 		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')");
+			Db::query ("INSERT INTO `model` (`id`, `script`, `brand_id`, `body_id`, `modelName`, `active`) VALUES ('NULL', '".$this->script."', '".$brandId."', '".$bodyId."', '".$modelName."', '1')");
 			$id = Db::getId ();
 		}
 		$this->cacheModels[$id] = $value;
@@ -144,7 +144,7 @@
 		if ($row = Db::fetchArray()) {
 			$id = $row['id'];
 		} else {
-			Db::query ("INSERT INTO `engine` (`id`, `brand_id`, `model_id`, `engineName`, `fuel`, `capacity`, `active`) VALUES ('NULL', '".$brandId."', '".$modelId."', '".$engineName."', '".$fuel."', '".$cc."', '1')");
+			Db::query ("INSERT INTO `engine` (`id`, `script`, `brand_id`, `model_id`, `engineName`, `fuel`, `capacity`, `active`) VALUES ('NULL', '".$this->script."', '".$brandId."', '".$modelId."', '".$engineName."', '".$fuel."', '".$cc."', '1')");
 			$id = Db::getId ();
 		}
 		$this->cacheEngine[$id] = $value;
@@ -176,8 +176,8 @@
 		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`, `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')");
+			Db::query("INSERT INTO `prices` (`id`, `script`, `brand_id`, `model_id`, `engine_id`, `body_id`, `carLink`, `year`, `fuel`, `cc`, `km`, `price`, `active`)
+			VALUES ('NULL', '".$this->script."', '".$brandId."', '".$modelId."', '".$engineId."', '".$bodyId."', '".$carLink."', '".$carData['year']."', '".$fuel."', '".$cc."', '".$carData['km']."', '".$carData['price']."', '1')");
 		}
 	}
 

comments