stanley-king před 9 roky
rodič
revize
7172e74b52

+ 132 - 0
crontab/crawl/brand_operator.php

@@ -0,0 +1,132 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/1/14
+ * Time: 下午6:10
+ */
+class brand_operator
+{
+    private $tm_prop_values;
+    private $cls_tree;
+    private $mod_brand;
+    private $tm_goods;
+    private $mod_goods_common;
+
+    const brand_pid = 20000;
+
+    public function __construct()
+    {
+        $this->tm_goods = Model('tm_goods');
+        $this->tm_prop_values = Model('tm_prop_values');
+        $this->cls_tree = new class_tree();
+        $this->mod_brand = Model('brand');
+        $this->mod_goods_common = Model('goods_common');
+    }
+
+    private function brand_exist($name)
+    {
+        $result = $this->mod_brand->field('brand_id')->where(array('brand_tm_name' => array('like',"%{$name}%")))->select();
+
+        if(empty($result) || count($result) == 0) {
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+    private function create()
+    {
+        $brands = $this->tm_prop_values->field('id,cid,name')->where(array('pid' => self::brand_pid))->group('name')->select();
+
+        foreach($brands as $val)
+        {
+            $name = $val['name'];
+            $cid = $val['cid'];
+            if($this->brand_exist($name)) continue;
+
+            if($this->cls_tree->get_info($cid,$c_1,$c_2,$c_3,$cls_name))
+            {
+                $ret = $this->mod_brand->insert(array('brand_name' => $name,'brand_tm_name' => $name,'brand_class' => $cls_name));
+                if($ret == false) {
+                    Log::record("insert brand error brand_name = brand_tm_name = {$name}",Log::ERR);
+                } else {
+                    Log::record("brand_name = {$name} brand_class = {$cls_name}.",Log::DEBUG);
+                }
+            }
+        }
+    }
+
+    private function get_brand($propnames,&$brand_name)
+    {
+        $ret = $this->brand_name($propnames);
+        if(!array_key_exists(self::brand_pid,$ret)) {
+            return false;
+        } else {
+            foreach($ret[self::brand_pid]['val'] as $key => $val) {
+                $brand_name = $val;
+                break;
+            }
+        }
+        if(isset($brand_name) && !empty($brand_name))
+        {
+            $result = $this->mod_brand->where(array('brand_tm_name' => array('like',"%{$brand_name}%")))->select();
+            if(!empty($result) && count($result) > 0) {
+                return $result[0]['brand_id'];
+            }
+        }
+
+        return false;
+    }
+
+    public function proc()
+    {
+        $this->create();
+
+        $results = $this->tm_goods->field('body,title,num_iid')->limit(false)->select();
+        foreach ($results as $result)
+        {
+            $num_iid = $result['num_iid'];
+            $body = $result['body'];
+            $response = json_decode($body, true);
+            $item = $response['item_seller_get_response']['item'];
+            $props_name = $item['props_name'];
+            $brand_id = $this->get_brand($props_name,$brand_name);
+
+            if($brand_id)
+            {
+                $ret = $this->mod_goods_common->where(array('num_iid' => $num_iid))->update(array('brand_id' => $brand_id,'brand_name' => $brand_name));
+                if($ret == false) {
+                    Log::record("update goods_common brand error: num_iid={$num_iid},brand_id={$brand_id},brand_name={$brand_name}");
+                }
+            } else {
+                Log::record("update goods_common brand error: cannot find brand_id num_iid={$num_iid}");
+            }
+        }
+    }
+
+    /**
+     * @param $propnames
+     * @return array
+     */
+    private function brand_name($propnames)
+    {
+        $spec_vals = preg_split("/[;]+/", $propnames);
+
+        $ret = array();
+        foreach ($spec_vals as $sv) {
+            $data = preg_split("/[:]+/", $sv);
+            if (!empty($data)) {
+                $tmpid = $data[0];
+                $tmpvid = $data[1];
+                $tmpname = $data[2];
+                $tmvname = $data[3];
+
+                $ret[$tmpid]['name'] = $tmpname;
+                $ret[$tmpid]['val'][$tmpvid] = $tmvname;
+            }
+        }
+        return $ret;
+    }
+}

+ 7 - 1
crontab/crawl/class_tree.php

@@ -22,6 +22,10 @@ class class_tree
         $c_2 = 0;
 
         $info = $this->group_class[$cid];
+
+        if(empty($info)) {
+            return false;
+        }
         $name = $info['gc_name'];
 
         do {
@@ -47,6 +51,8 @@ class class_tree
             $cid = $info['gc_parent_id'];
             $info = $this->group_class[$cid];
         } while($deep > 0);
+
+        return true;
     }
 
     private function write_deep(&$alldata,&$childs,$deep)
@@ -68,7 +74,7 @@ class class_tree
      * @param array $condition 检索条件
      * @return array 数组类型的返回结果
      */
-    public function load_all()
+    private function load_all()
     {
         $mod_cats = Model('goods_class');
         $class_list = $mod_cats->field('*')->order('gc_parent_id asc,gc_sort asc,gc_id asc')->limit(false)->select();

+ 10 - 2
crontab/crawl/fetch_goods.php

@@ -28,6 +28,8 @@ require_once (BASE_CRONTAB_PATH . '/crawl/spec_attr_parser.php');
 require_once (BASE_CRONTAB_PATH . '/crawl/upload_file.php');
 require_once (BASE_CRONTAB_PATH . '/crawl/upload_control.php');
 require_once (BASE_CRONTAB_PATH . '/crawl/tm_pagereq.php');
+require_once(BASE_CRONTAB_PATH . '/crawl/brand_operator.php');
+
 
 echo "7\r\n";
 //require_once (BASE_CORE_PATH . '/framework/libraries/uploadfile.php');
@@ -51,6 +53,9 @@ function not_null($str)
     return (empty($str)) ? '' : $str;
 }
 
+
+
+
 //$tmreq = new tmrequest();
 //$tmreq->proc();
 //
@@ -62,6 +67,9 @@ function not_null($str)
 //
 //$spec = new spec_attr_parser();
 //$spec->proc();
+//
+//$importer = new product_importer();
+//$importer->proc_shop();
 
-$importer = new product_importer();
-$importer->proc_shop();
+$treater = new brand_operator();
+$treater->proc();

+ 30 - 19
crontab/crawl/pretreat_tmdata.php

@@ -13,6 +13,7 @@ class pretreat_tmdata
     private $tm_props;
     private $tm_prop_values;
     private $goods_class;
+    private $cls_tree;
 
     public function __construct()
     {
@@ -21,12 +22,13 @@ class pretreat_tmdata
         $this->tm_props = Model('tm_props');
         $this->tm_prop_values = Model('tm_prop_values');
         $this->goods_class = Model('goods_class');
+        $this->cls_tree = new class_tree();
     }
 
-    public function proc($create_brand)
+    public function proc()
     {
-        $this->tm_spec->where(array('num_iid > 0'))->delete();
-        $results = $this->tm_goods->field('body,title,num_iid')->where(array('imported' => 0))->limit(false)->select();
+        $this->tm_spec->where(array('num_iid > 0'))->delete(); //->where(array('imported' => 0))
+        $results = $this->tm_goods->field('body,title,num_iid')->limit(false)->select();
         foreach ($results as $result) {
             $num_iid = $result['num_iid'];
             $body = $result['body'];
@@ -43,21 +45,6 @@ class pretreat_tmdata
             $this->parse_tm_pic($item,$picnum);
             $this->tm_goods->where(array('num_iid' => $num_iid))->update(array('picnum' => $picnum));
         }
-        if($create_brand == true) {
-            $this->create_brand();
-        }
-    }
-
-    private function create_brand()
-    {
-        $barnds = $this->tm_prop_values->field('name,cid')->where(array('pid' => 20000))->group('vid')->select();
-
-        $mod_brand = Model('brand');
-        foreach($barnds as $val) {
-            $name = $val['name'];
-            $cid = $val['cid'];
-            $ret = $mod_brand->insert(array('brand_name' => $name,'brand_class' => $cid));
-        }
     }
 
     private function parse_propnames($propnames)
@@ -128,8 +115,11 @@ class pretreat_tmdata
         return $ret['gc_name'];
     }
 
+
     private function download($url)
     {
+        static $pngext = array('png');
+
         $info = pathinfo($url);
         $name = md5($url) . '.' . $info['extension'];
 
@@ -140,7 +130,26 @@ class pretreat_tmdata
             exec("wget -O $path $pic_url");
         }
 
-        if(file_exists($path)) {
+        if(file_exists($path))
+        {
+            $fh = fopen($path, "rb");
+            $head = fread($fh, 8);
+            fclose($fh);
+
+            if(empty($head)) {
+                unlink($path);
+                Log::record("{$path} file is empty.",Log::ERR);
+                return false;
+            }
+
+            if(!in_array($info['extension'],$pngext))
+            {
+                $arr = unpack("C4", $head);
+                if($arr[1] == 137 &&  $arr[2] == 80 && $arr[3] == 78 && $arr[4] == 71) {
+                    Log::record("{$path} is a png file",Log::ERR);
+                }
+            }
+
             return true;
         } else {
             return false;
@@ -180,6 +189,8 @@ class pretreat_tmdata
             {
                 if($this->download($pic)) {
                     $arpic[$name] = 1;
+                } else {
+                    Log::record("download pic {$pic}.",Log::ERR);
                 }
             } else {
                 //$arpic[$name] += 1;

+ 0 - 7
fcgi_run.php

@@ -18,10 +18,6 @@ function run_fcgi()
     require_once(BASE_ROOT_PATH.'/mobile/index.php');
     Base::mobile_init();
 
-//    if (!extension_loaded("fcgi")) {
-//        Log::record("skip\n",Log::DEBUG);
-//    }
-
     while(($ret = fcgi_accept()) >= 0)
     {
         ob_start();
@@ -55,11 +51,8 @@ function run_fcgi()
         fcgi_finish();
     }
 
-    Log::record("4\n");
     fcgi_fini();
-    Log::record("5\n");
 }
 
 run_fcgi();
-
 ?>

+ 1 - 6
run.php

@@ -3,16 +3,12 @@
 
 define('InShopNC',true);
 
-//defined('InShopNC') or exit('Access Invalid!');
-
 define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
 
 echo BASE_ROOT_PATH."\n";
 
 require_once (BASE_ROOT_PATH . '/fooder.php');
 
-echo "2\r\n";
-
 $gfcgi_run = 2;
 
 if($gfcgi_run == 0) { //模拟请求
@@ -26,12 +22,11 @@ if($gfcgi_run == 0) { //模拟请求
 } else {
     define('StartTime', microtime(true));
     define('TIMESTAMP', time());
-    echo '3\r\n';
+
     Base::run_test();
     $file = BASE_CRONTAB_PATH . '/crawl/fetch_goods.php';
 
     require $file;
-    echo '4\r\n';
 }
 ?>