forked from Jamesits/zhihudaily
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.php
73 lines (61 loc) · 2.23 KB
/
fetch.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
<?php
/*
9月15抓取的数据库,导入即可:http:https://zhihudaily-zhihudaily.stor.sinaapp.com/daily.sql
*/
$mysql = new SaeMysql();
function cleanHtml($str){
$str = trim($str);
$str = strip_tags($str,"");
$str = preg_replace("/&.+;/","",$str);
$str = str_replace("\t","",$str);
$str = str_replace("\t","",$str);
$str = str_replace("\r\n","",$str);
$str = str_replace("\r","",$str);
$str = str_replace("\n","",$str);
$str = str_replace(" ","",$str);
$str = str_replace(" ","",$str);
$str = str_replace("查看知乎讨论"," ",$str);
$str = str_replace("查看更多讨论"," ",$str);
return trim($str);
}
function getday($day) {
if($day == 'today'){
$webcode = file_get_contents("http:https://news.at.zhihu.com/api/1.2/news/latest");
file_put_contents('saestor:https://zhihudaily/' .date('Ymd'). '.txt',$webcode);
}else{
$webcode = file_get_contents("http:https://news.at.zhihu.com/api/1.2/news/before/$day");
}
return json_decode($webcode, true);
}
function dealday($html,$mysql,$add) {
for($i=0;$i<count($html['news']);$i++){
$news = $html['news'][$i];
if(in_array($news['id'], $add)){
return false;
}
$page = json_decode(file_get_contents($news['url']), true);
$body = mysql_escape_string(cleanHtml($page['body']));
$title = mysql_escape_string($news['title']);
$true_title = "";
preg_match_all('/question-title\\">(.+?)</',$page['body'], $matches);
foreach($matches[1] as $value){
$true_title .= $value;
}
$true_title = mysql_escape_string($true_title);
$share_url = mysql_escape_string($news['share_url']);
$ga_prefix = mysql_escape_string($news['ga_prefix']);
$id = $news['id'];
$sql = "INSERT ignore INTO daily (title,true_title,share_url,ga_prefix,id,body) VALUES ('$title','$true_title','$share_url','$ga_prefix','$id','$body')";
$mysql->runSql($sql);
}
return $html['date'];
}
$data = $mysql->getData("SELECT id FROM `daily`");
$add = array();
foreach($data as $value){
$add[] = $value["id"];
}
$day = 'today';
while($day){
$day = dealday(getday($day),$mysql,$add);
}