/
tag.php
104 lines (93 loc) · 2.95 KB
/
tag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* tag ok
*/
class tag
{
const URL='http://fundact.eastmoney.com/zhutijj/home/index?isapp=false&from=singlemessage&isappinstalled=1';
function __construct()
{
}
function index()
{
$this->setTag();
}
private static function log($msg)
{
echo date('H:i:s').' '.$msg.PHP_EOL;
}
/**
* 执行大约10分钟,无副作用,消耗网络较大,二次执行带有缓存,无需每天执行(每周执行即可)
*/
function setTag()
{
$cachefile='tagcache.tmp';
if(is_file($cachefile))
{
$result=unserialize(file_get_contents($cachefile));
(is_array($result)&&!empty($result))||exit('error cache data');
}
else
{
$html=Curl::get(self::URL);
$pattern='/\s+class="bggreen"\s+data-tp="(\w+)"\s+data-tpname="(.+?)"/';
$result=array();
if(preg_match_all($pattern, $html, $matches))
{
$tp=array_combine($matches[2],$matches[1]);
foreach ($tp as $name => $id)
{
$result[$name]=$this->getMore($name,$id);
}
}
file_put_contents($cachefile,serialize($result));
}
$this->toDb($result);
}
private function getMore($value,$id)
{
$pattern='/<span\s+class="code">(\d+)<\/span>/';
$allId=array();
for ($page=1; $page <200 ; $page++)
{
$url="http://fundact.eastmoney.com/zhutijj/home/table?tp={$id}&rs=SYL_3Y&sort=SYL_3Y&sorttype=desc&pageindex={$page}";
$html=Curl::get($url);
if(empty(trim($html)))
{
self::log("{$id} Total Page : ".($page-1));
break;
}
self::log("Get {$id} At Page {$page} Ok");
if(preg_match_all($pattern,$html,$matches))
{
$allId=array_merge($allId,$matches[1]);
}
}
return $allId;
}
//save to db
private function toDb($result)
{
foreach ($result as $name => $item)
{
foreach ($item as $code)
{
$origin=DB::getLine("SELECT * FROM `invest_product` WHERE code='{$code}'");
$comments=explode(',',$origin['comment']);
$comments=array_filter($comments,function($value){return trim($value);});
if(!in_array($name,$comments) && !empty($origin))
{
$comments[]=$name;
$comments=implode(',',$comments);
$sql="UPDATE `invest_product` SET comment='{$comments}' WHERE code ='{$code}' ";
$ret=DB::runSql($sql);
self::log("Updated {$code},Result:{$ret}");
}
else
{
self::log("Code {$code} Already Update-To-Date");
}
}
}
}
}