-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExePython.php
More file actions
109 lines (103 loc) · 3.12 KB
/
ExePython.php
File metadata and controls
109 lines (103 loc) · 3.12 KB
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
105
106
107
108
109
<?php
/**
964E
* Created by PhpStorm.
* User: think
* Date: 2016/11/17
* Time: 20:22
*/
class ExePython
{
const STEP_NUMBER = '1';
const STEP_LIST = '2';
const STEP_RECORD = '3';
/**
* 通过url,解析公众号页
* @param $param
*/
public function getWebchaNumberUrl($url) {
return $this->exeShell(self::STEP_NUMBER, $url);
}
/**
* 通过url,解析文章页
*/
public function getRecordListUrl($url) {
$result = $this->exeShell(self::STEP_LIST, $url);
if ($result === false) {
Bd_Log::trace(sprintf('class[%s] function[%s] data[%s]', __CLASS__, __FUNCTION__, $url));
return false;
}
$name = $result[0];
$number = $result[1];
$list = $result[2];
$data = array(
"webchat_number" => $number,
"webchat_name" => $name,
"record_list" => $list
);
return $data;
}
/**
* 获取文章详情
* @param $url
* @return string
*/
public function getRecordDetail($url) {
$result = $this->exeShell(self::STEP_RECORD, $url);
if ($result === false) {
Bd_Log::trace(sprintf('class[%s] function[%s] data[%s]', __CLASS__, __FUNCTION__, $url));
return false;
}
$title = array_shift($result);
$name = array_shift($result);
$info = '';
foreach($result as $item){
$info .= $item;
}
$detail = json_decode($info, JSON_UNESCAPED_UNICODE);
$data = array(
"title" => $title,
"webchat_name" => $name,
"detail" => $detail
);
Bd_Log::trace(sprintf('class[%s] function[%s] data[%s]', __CLASS__, __FUNCTION__, @json_encode($data)));
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
/**
* 执行shell命令
* @param $file
* @param $url
*/
private function exeShell($type, $argv) {
if (empty($argv) || empty($type)) {
return;
}
$file = $this->getHtmlFromUrl($argv);
$sh = "python ".dirname(__FILE__)."/parseHtml.py '".$type."' '".$file."'";
//$sh = "python ".dirname(__FILE__)."/parseHtml.py '".$type."' ".dirname(__FILE__)."'/html/".$type.".html'";
exec($sh, $output, $result);
if ($result !== 0)
{
Bd_Log::trace(sprintf('class[%s] function[%s] data[%s]', __CLASS__, __FUNCTION__, @json_encode($output)));
return false;
}
unlink($file);
return $output;
}
/**
* @param $url
* @return string
*/
private function getHtmlFromUrl($url){
$temp_url = explode('?', $url);
$temp_prefix = explode('/', $temp_url[0]);
$picName = array_pop($temp_prefix).'.html';
$tmp_save_path = dirname(__FILE__).'/html/' . $picName;
$time = 0 ;
do {
$file = Wm_Lib_ProxyCurl::curl($url,null,Strategyui_Define_Strategy::$proxyConfig);
$ret = file_put_contents($tmp_save_path, $file);
$time++;
} while ($ret === false and $time < 3);
return $tmp_save_path;
}
}