Handle.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Ku\Spider;
  3. use Dever;
  4. class Handle
  5. {
  6. public function createProduct($id = false)
  7. {
  8. $id = Dever::input('id', $id);
  9. $product = Dever::db('ku/product')->one($id);
  10. $spider = $this;
  11. $param = array();
  12. if ($product && $product['link']) {
  13. $update = false;
  14. if (!$product['name'] || !$product['pic'] || !$product['price']) {
  15. $update = true;
  16. }
  17. if ($update) {
  18. $data = $spider->get($product['link']);
  19. if ($data) {
  20. $param['name'] = $product['name'] ? $product['name'] : $data['name'];
  21. $param['pic'] = $product['pic'] ? $product['pic'] : $this->pic($data);
  22. $param['price'] = $product['price'] ? $product['price'] : $data['price'];
  23. }
  24. }
  25. Dever::config('base')->hook = true;
  26. if ($param) {
  27. $param['where_id'] = $id;
  28. Dever::db('ku/product')->update($param);
  29. }
  30. }
  31. }
  32. private function pic($result)
  33. {
  34. if (isset($result['pic']) && $result['pic']) {
  35. if (!strstr($result['pic'], 'http')) {
  36. $result['pic'] = 'http:' . $result['pic'];
  37. }
  38. $pic = Dever::load('upload/save')->copy($result['pic']);
  39. if (isset($pic['url']) && $pic['url']) {
  40. $result['pic'] = $pic['url'];
  41. }
  42. }
  43. return $result['pic'];
  44. }
  45. public function get($link)
  46. {
  47. $type = $this->getType($link);
  48. if ($type) {
  49. $link = trim($link, "\r\n");
  50. $link = $type->link($link);
  51. $data = $this->filter(Dever::curl($link));
  52. $result = $this->rule($type->rule(), $data, $type);
  53. return $result;
  54. }
  55. return false;
  56. }
  57. private function getType($link)
  58. {
  59. $type = '';
  60. if (strpos($link, 'taobao.com')) {
  61. $type = 'taobao';
  62. } elseif (strpos($link, 'tmall.com')) {
  63. $type = '';
  64. } elseif (strpos($link, 'youzan.com')) {
  65. $type = 'youzan';
  66. }
  67. if (!$type) {
  68. return false;
  69. }
  70. return Dever::load('ku/spider/' . $type);
  71. }
  72. private function rule($rule, $data, $type)
  73. {
  74. $result = array();
  75. foreach ($rule as $key => $value) {
  76. $m = 1;
  77. if (is_array($value)) {
  78. $m = $value[1];
  79. $value = $value[0];
  80. }
  81. if (strstr($value, '/')) {
  82. $value = str_replace('/', '\/', $value);
  83. }
  84. preg_match_all('/'.$value.'/i', $data, $matches);
  85. $result[$key] = '';
  86. if (isset($matches[$m][0])) {
  87. $result[$key] = trim($matches[$m][0]);
  88. $result[$key] = $type->replace($key, $result[$key]);
  89. $result[$key] = str_replace(array(' ', "\t"), '', $result[$key]);
  90. }
  91. }
  92. return $result;
  93. }
  94. private function filter($string)
  95. {
  96. $encode = mb_detect_encoding($string, array('GB2312','GBK','UTF-8'));
  97. $config = array('GB2312', 'GBK', 'EUC-CN', 'CP936');
  98. if (in_array($encode, $config)) {
  99. $string = mb_convert_encoding($string, 'UTF-8', 'GBK');
  100. }
  101. $string = str_replace(PHP_EOL, '', $string);
  102. return $string;
  103. }
  104. }