-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.php
139 lines (104 loc) · 3.82 KB
/
parse.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
<?php
header("Content-type: text/html; charset=utf-8");
include 'vendor/autoload.php';
use Overtrue\Pinyin\Pinyin;
function run(){
$file = 'CNBlogs_BlogBackup_131_201408_202201.xml';
$html = file_get_contents($file);
$pinyin = new Pinyin(); // 默认
preg_match_all('/<item><title>(.*)<\/title>.*<link>(.*)<\/link>.*<pubDate>(.*)<\/pubDate>.*<description>(.*)<\/description><\/item>/sU', $html, $data, PREG_SET_ORDER );
// var_dump($data);
foreach($data as $item){
// var_dump($item);exit;
// 文章正文
$content = str_replace(']]>', '', str_replace('<![CDATA[', '', $item[4]));
// 过滤非法字符
$title = preg_replace('/(\/|\\|\:|\*|\?|\"|\<|\>|\|)/', "", $item[1]);
// 时间
$datetime = date('Y-m-d H:i:s', strtotime($item[3]));
//内容合成
// $content = sprintf("---\r\ntitle: %s\r\ndate: %s\r\n---\r\n%s",$title, $datetime, $content);
$content = sprintf("---\r\ntitle: %s\r\ndate: %s\r\n---\r\n%s",$title, $datetime, $content);
$dir = 'docs/'. date('Y', strtotime($item[3]));
//转成拼音,防止文件无法创建
$filename = sprintf('%s/%s-%s.md', $dir, date('Y-m-d', strtotime($item[3])) , $pinyin->permalink($title));
// $filename = sprintf('%s/%s-%s.md', $dir, date('Y-m-d', strtotime($item[3])) , iconv('utf-8', 'GB2312', $title));
@mkdir($dir, 0777, true);
@chmod($filename, 0777);
$content = matchPic($content, $datetime);
$content = matchPicMD($content, $datetime);
file_put_contents($filename, $content);
// exit;
}
}
function matchPic($content, $datetime){
preg_match_all('/<img src="(.*)" alt=.*>/sU', $content, $pics, PREG_SET_ORDER );
foreach($pics as $pic){
$url = $pic[1];
$path_parts = pathinfo($url);
$extension = @$path_parts['extension'] ? : 'jpg';
if(!preg_match('/^http/', $url)){
$url = 'http:'.$url;
}
print_r($url." ok\n");
if(checkHttpStatus($url)){
$img_url = downPic($url, $datetime, $extension);
$content = str_replace($url, $img_url, $content);
}
}
return $content;
}
function matchPicMD($content, $datetime){
preg_match_all('/!\[\]\((.*)\)/sU', $content, $pics, PREG_SET_ORDER );
foreach($pics as $pic){
//print_r($pic);
$url = $pic[1];
$path_parts = pathinfo($url);
$extension = @$path_parts['extension'] ? : 'jpg';
if(!preg_match('/^http/', $url)){
$url = 'http:'.$url;
}
print_r($url." ok\n");
if(checkHttpStatus($url)){
$img_url = downPic($url, $datetime, $extension);
$content = str_replace($url, $img_url, $content);
}
}
return $content;
}
function downPic($url, $datetime, $extension){
$extension = $extension ? : 'jpg';
$content = file_get_contents($url);
$dir = 'docs/images/';
$filename = $dir.date('Ymd', strtotime($datetime)).uniqid().".".$extension;
@mkdir($dir, 0777, true);
file_put_contents($filename, $content);
return str_replace('docs','..',$filename);
}
/**
* 检测图片是否能访问
* @param $url
* @return bool
*/
function checkHttpStatus($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "HEAD");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
curl_setopt ($ch, CURLOPT_TIMEOUT_MS, 4000);//超时时间
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if(!$info || $info['http_code'] != '200'){
// print_r($info);
return false;
}
return true;
}
run();
//$content = '及附加费![](http://images2017.cnblogs.com/blog/663847/201710/663847-20171029132658195-1444776911.png) 家具';
//matchPicMD($content, '');