forked from nemoTyrant/manong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.php
365 lines (336 loc) · 8.25 KB
/
func.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
* whether it is a GET request
*/
function is_get(){
return $_SERVER['REQUEST_METHOD'] == 'GET';
}
/**
* which action is requested
*/
function action(){
// if(is_get())
// return 'index';
return isset($_GET['a']) ? $_GET['a']:'index';
}
/**
* show the index page
*/
function index(){
require 'manong.php';
exit;
}
/**
* singleton db class
*/
function get_db(){
static $db=null;
if(is_null($db)){
$db=new manongdb();
}
return $db;
}
/**
* log text
*/
$log=true;
function mylog($str){
global $log;
if($log){
if(is_array($str)){
echo "<pre>";
print_r($str);
echo "</pre>";
}else{
echo $str.'<br>';
}
}
}
/**
* return json string
*/
function ajax_return($arr){
header('Content-type:application/json;charset=utf-8');
echo json_encode($arr);
exit;
}
/**
* crawl the content
*/
function crawl(){
$number=$_POST['number'];
$mdb=get_db();
$data=$mdb->get_issue($number);
$html='';
if(empty($data)){
$html .= 'no data';
}else{
$cates=$mdb->get_cate();
foreach ($data as $val) {
// basic classification
$cate='';
foreach ($cates as $v) {
if(strlen($v) < 2){ // in case R or D matched
continue;
}
if(false !== strpos(strtoupper($val['title']), $v)){
$cate=$v;
}
}
$html .= '<div class="item">';
$html .= '<hr>';
$html .= '<form>';
$html .= "标题:<input type='text' name='title' value='{$val['title']}'><br>";
$html .= "描述:<input type='text' name='desc' value='{$val['desc']}'><br>";
$html .= "地址:<input type='text' name='href' value='{$val['href']}'><button class='openurl'>打开</button><br>";
$html .= "<input type='hidden' name='id' value='{$val['id']}'>";
$html .= "<input type='hidden' name='number' value='{$val['number']}'>";
$html .= "<input type='text' class='newcate' name='newcate' value='{$cate}'>";
$html .= "<button class='add'>添加</button>";
$html .= "<button class='del''>删除</button>";
$html .= "</form>";
$html .= "</div>";
}
}
echo $html;
}
/**
* load categories
*/
function cate(){
$mdb=get_db();
$cate=$mdb->get_cate();
$html='';
$html.='<select name="category">';
$html.='<option value="0">请选择</option>';
$catearr=[];
foreach ($cate as $val) {
$html.='<option value="'.$val.'">'.$val.'</option>';
$catearr[]=$val;
}
$html.='</select>';
ajax_return(['html'=>$html,'cate'=>$catearr]);
}
/**
* add an item to db
*/
function add(){
$cate=trim($_GET['newcate']);
$cate=$cate ? $cate:$_GET['category'];
$cate || ajax_return(['res'=>0,'msg'=>'fill category']);
$data=[
'title'=>trim($_GET['title']),
'desc'=>trim($_GET['desc']),
'href'=>trim($_GET['href']),
'number'=>trim($_GET['number']),
'category'=>strtoupper($cate),
'hash'=>md5(trim($_GET['href'])),
'addtime'=>time(),
'ctime'=>time(),
];
if(get_db()->add($data)){
ajax_return(['res'=>1,'msg'=>'success','cate'=>$data['category']]);
}else{
ajax_return(['res'=>0,'msg'=>'db error']);
}
}
/**
* delete an item from cache
*/
function del(){
$id=$_GET['id'];
$rs=get_db()->del_cache($id);
$res=$rs ? 1:0;
ajax_return(compact('res'));
}
/**
* render data to markdown text
*/
function render(){
$mdb=get_db();
$data=$mdb->get_all();
$current=$mdb->current_number();
$content="码农周刊分类整理
======
码农周刊的类别分的比较大,不易于后期查阅,所以我把每期的内容按语言或技术进行了分类整理。
码农周刊官方网址 [http://weekly.manong.io/](http://weekly.manong.io/)
一些不熟悉的领域分类可能不准确,请见谅
15期为图书推荐,请直接浏览[原地址](http://weekly.manong.io/issues/15)
56期为14年最受欢迎列表,请直接浏览[原地址](http://weekly.manong.io/issues/56)
现在已整理到第{$current}期。
号外:[@lcepy](https://github.com/lcepy) 同学基于本项目制作了ios端的阅读app《猿已阅》,并已开源,大家可以移步[这里](https://github.com/lcepy/manong-reading)参观一下。
";
// category index
// ##大纲
// [ANDROID](#ANDROID)
// [ANGULAR](#ANGULAR)
$categories=$mdb->get_cate();
if($categories){
$content.="\n";
$content.="##索引\n";
foreach ($categories as $val) {
$content.="[{$val}](#{$val}) \n";
}
}
$current_cate='';
foreach ($data as $val) {
if($val['category'] != $current_cate){
$content.="\n";
$content.="<a name=\"{$val['category']}\"></a>\n";
$content.="##".str_replace('#', '\# ', $val['category'])."\n";
$current_cate=$val['category'];
}
$content.="[{$val['title']}]({$val['href']}) \n";
}
$rs=file_put_contents('./readme.md', $content);
if($rs){
ajax_return(['res'=>1]);
}else{
ajax_return(['res'=>0,'msg'=>'输出失败']);
}
}
class manongdb{
private $pdo;
private $issue_url='http://weekly.manong.io/issues/';
function __construct($user='root',$password='',$dbname='manong'){
$this->pdo=new PDO('mysql:host=localhost;dbname='.$dbname,$user,$password);
$this->pdo->query('set names utf8');
}
/**
* get issue data
*/
function get_issue($number){
mylog('searching for cache');
$data=$this->cache($number);
if(empty($data)){
mylog('no cache,start crawling');
$data=$this->crawl($number);
if(false === $data){
die('抓取失败');
}
}
return $data;
}
/**
* get issue cache
*/
function cache($number){
$data=$this->pdo->query("select * from cache where number={$number}")->fetchAll(PDO::FETCH_ASSOC);
if($data){
mylog('cache founded');
}
return $data;
}
/**
* delete an item from cache
*/
function del_cache($id){
return $this->pdo->exec("delete from cache where id={$id}");
}
/**
* crawl the data
*/
function crawl($number){
$url=$this->issue_url.$number;
$content=file_get_contents($url);
$content=str_replace(["\r","\n"], '', $content);
$pattern='/<h4><a target="_blank" href="(.*?)">(.*?)<\/a> <\/h4>.*?<p>(.*?)<\/p>/';
$rs=preg_match_all($pattern, $content, $matches);
if($rs){
mylog('crawl finished.start parsing');
$data=array();
foreach ($matches[1] as $key => $val) {
if(false !== strpos($matches[1][$key], 'job') || false !== strpos($matches[1][$key], 'amazon')){ // get rid of job and book recommendations
continue;
}
$item=[
'title'=>strip_tags($matches[2][$key]),
'desc'=>$matches[3][$key],
'href'=>$val,
'number'=>$number,
'hash'=>md5($val)
];
//加入数据库
$id=$this->add_cache($item);
$item['id']=$id;
mylog("item {$id} added to cache");
// 加入数组
$data[]=$item;
}
return $data;
}
mylog('failed to crawl');
return false;
}
/**
* add item to cache
*/
function add_cache($item){
$sql=$this->arr2sql('cache',$item);
$this->pdo->exec($sql);
$id=$this->pdo->lastInsertId();
return $id;
}
/**
* translate array to sql
*/
function arr2sql($dbname,$arr,$updateid=null){
if($updateid){
// return "update {$dbname} set "
unset($arr['ctime']);
unset($arr['addtime']);
$sql="update {$dbname} set ";
foreach ($arr as $key => $val) {
$sql.="`{$key}`='{$val}',";
}
$sql.='ctime='.time();
$sql.=" where id={$updateid}";
return $sql;
}else{
$fields=[];
$values=[];
foreach ($arr as $key => $v) {
$fields[]="`{$key}`";
$values[]="'{$v}'";
}
return "insert into {$dbname} (".implode(',', $fields).") values(".implode(',', $values).")";
}
}
/**
* get categories
*/
function get_cate(){
$arr=[];
$rs=$this->pdo->query("select distinct category from issue order by category")->fetchAll(PDO::FETCH_ASSOC);
foreach ($rs as $val) {
$arr[]=$val['category'];
}
return $arr;
}
/**
* add an item to db or update it if exists
*/
function add($data){
$rs=$this->pdo->exec($this->arr2sql('issue',$data));
if($rs){
return $this->pdo->lastInsertId();
}
$rs=$this->pdo->query("select id from issue where hash='{$data['hash']}'")->fetch(PDO::FETCH_ASSOC);
if(isset($rs['id'])){
$update=$this->pdo->exec($this->arr2sql('issue',$data,$rs['id']));
if($update) return $rs['id'];
}
return false;
}
/**
* fetch all recorded data
*/
function get_all(){
return $this->pdo->query('select * from issue order by category,addtime')->fetchAll(PDO::FETCH_ASSOC);
}
function current_number(){
$rs=$this->pdo->query('select max(number) as max from issue')->fetch(PDO::FETCH_ASSOC);
return $rs['max'];
}
}
?>