added guessCC for bestauto, small improvements
added guessCC for bestauto, small improvements

--- a/app/lib/Common.php
+++ b/app/lib/Common.php
@@ -56,10 +56,13 @@
 		$this->urlBase = $urlBase;
 	}
 
-	protected function getHtml ($url)
+	protected function getHtml ($url, $debug=false)
 	{
 		if ( $this->init == false ) {
 			throw new Exception ("init method must be called");
+		}
+		if ( $debug == true ) {
+			$this->debug ("fetching url ".$this->urlBase.$url, 2);
 		}
 		$cookie = tmpfile();
 		$ch = curl_init($this->urlBase.$url);
@@ -185,13 +188,22 @@
 		if ( in_array ($value, $this->fuels) ) {
 			return array_search($value, $this->fuels);
 		}
-		throw new Exception ("unlisted fuel found : ".$value);
+		throw new \Exception ("unlisted fuel found : ".$value);
 	}
 
 	private function getCc ($engineName)
 	{
 		$engineData = explode (" ", trim ($engineName));
 		return $engineData[0] * 1000;
+	}
+
+	protected function guessCC ($brandId, $modelId, $bodyId, $fuel, $carData) {
+		Db::query ("SELECT DISTINCT(`cc`) as cc, COUNT(*) as count FROM `prices` WHERE `cc`>0 AND `brand_id`='".$brandId."' AND `model_id`='".$modelId."' AND `body_id`='".$bodyId."' AND `fuel`='".$fuel."' AND `year`='".$carData['year']."' ORDER BY `cc` DESC LIMIT 1");
+		$row = Db::fetchArray();
+		if ($row['count'] >= 3) {
+			return $row['cc'];
+		}
+		return 0;
 	}
 
 	protected function getProcessedHtml ()
@@ -207,7 +219,7 @@
 	protected function match ($regex, $subject, $ignoreEmpty=false) {
 		preg_match ($regex, $subject, $match);
 		if ( count ($match) == 0 && $ignoreEmpty == false ) {
-			throw new \Exception ("regex ".$regex." result empty");
+			throw new \Exception ("regex ".$regex." result empty. subject is ".$subject);
 		}
 		return $match;
 	}
@@ -215,7 +227,7 @@
 	protected function matchAll ($regex, $subject, $ignoreEmpty=false) {
 		preg_match_all ($regex, $subject, $match);
 		if ( count ($match[0]) == 0 && $ignoreEmpty == false ) {
-			throw new \Exception ("regex ".$regex." result empty");
+			throw new \Exception ("regex ".$regex." result empty. subject is ".$subject);
 		}
 		return $match;
 	}

comments