前言

为了保存好博客日记,最终还是更相信github,索性迁移备份一波

hexo+框架 Hexo|主题 Butterfly

只迁移了文章,后续发文章我应该会两边同步,但是这篇就不发在那边了

贴代码

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
<?php

// CURL POST 请求
function post(string $url, array $params = [], array $headers = [], array $options = [])
{
$header = [
'Content-type' => 'application/json;charset="utf-8"',
'Accept' => 'application/json',
'origin' => str_replace(['https', 'http', ':', '//'], '', 'https://inis.cc')
];
$params = json_encode($params);
$headers = !empty($headers) ? array_merge($header, $headers) : $header;
$ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36';

foreach ($headers as $key => $val) $_headers[] = $key . ':' . $val;

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTPHEADER, $_headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $ua);
curl_setopt_array($curl, $options);

$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result, true);

return $result;
}

function get(string $url, array $params = [], array $headers = [], array $options = [])
{
$header = [
'Content-type' => 'application/json;',
'Accept' => 'application/json',
'origin' => str_replace(['https', 'http', ':', '//'], '', '')
];
$params = !empty($params) ? http_build_query($params) : json_encode($params);
$headers = !empty($headers) ? array_merge($header, $headers) : $header;
$ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36';

foreach ($headers as $key => $val) $_headers[] = $key . ':' . $val;

$curl = curl_init();

curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $_headers);
curl_setopt($curl, CURLOPT_URL, $url . '?' . $params);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_USERAGENT, $ua);
curl_setopt_array($curl, $options);

$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result, true);

return $result;
}

$api_url = 'https://dev.zets.cn/api/';

$params = ['field' => 'id,create_time,title,tags,abstract,content,covers,group'];

$data = get($api_url . 'article/column', $params);

foreach ($data['data'] as $key => $val) {
$title = mb_substr($val['title'], 0, 30) ?? '';
$content = $val['content'] ?? '';
$covers = (!empty($val['covers'])) ? $val['covers'] : $api_url . 'file/rand?name=imgs.txt&id=' . $val['id'];
$create_time = $val['create_time'] ?? '';

// 处理标签
$tags = implode(',', array_map(function ($tag) use ($api_url) {
$tags = get($api_url . 'tags/one', ['id' => $tag]);
return $tags['data']['name'] ?? '';
}, array_filter(explode('|', $val['tags']))));

// 处理分组
$group = implode(',', array_map(function ($groupId) use ($api_url) {
$group_one = get($api_url . 'article-group/one', ['id' => $groupId]);
return $group_one['data']['name'] ?? '';
}, array_filter(explode('|', $val['group']))));

// 处理摘要
$abstract = $val['abstract'] ?? trim(mb_substr($content, 0, 50, 'UTF-8'));

$md_content = '---
title: ' . $title . '
date: ' . date("Y-m-d H:i:s", $create_time) . '
categories: [' . $group . ']
tags: [' . $tags . ']
cover: ' . $covers . '
---
' . $content;

$file = './source/_posts/' . trim(str_replace('/', '-', $title)) . '.md';
$handle = fopen($file, 'w');

if ($handle === false) {
echo "文件打开失败。";
} else {
$result = fwrite($handle, $md_content);
if ($result === false) {
echo $title . "文件写入失败。";
} else {
echo $title . "文件写入成功。" . PHP_EOL;
}
fclose($handle);
}

}
exec('hexo generate --deploy');

结语

其实hexo也蛮好玩的