使用到的项目yuque2book: https://github.com/yuque-helper/yuque2book/wiki/%E8%AF%AD%E9%9B%80%E6%96%87%E6%A1%A3%E5%B7%A5%E5%85%B7
参考文档:https://www.yuque.com/yuque/developer/api
代码:
<?php
namespace App\Console\Commands\Shell;
use Illuminate\Console\Command;
/**
* 语雀知识库导出脚本
* 使用说明:
* 改脚本依赖于语雀文档工具 yuque2book
* 使用说明见 https://github.com/yuque-helper/yuque2book/wiki/%E8%AF%AD%E9%9B%80%E6%96%87%E6%A1%A3%E5%B7%A5%E5%85%B7
* Class YuqueBackUp
* @package App\Console\Commands\Shell
*/
class YuqueBackUp extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'Shell:YuqueBackUp';
/**
* The console command description.
*
* @var string
*/
protected $description = '语雀备份';
private $token = '{}';
private $savePath = '{}';
private $userId = '{}';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
# 判断备份目录是否存在
if (!is_dir($this->savePath)) {
mkdir($this->savePath, 0777, true);
}
$repos = $this->getRepoList();
$repos = json_decode($repos, true);
$slugList = array_map(function ($v) {
return $v['slug'];
}, $repos['data']);
foreach ($slugList as $k => $slug) {
if($slug == 'pw0zfo'){
continue;
}
$shell = "cd {$this->savePath} && yuque2book -t {$this->token} https://www.yuque.com/{$this->userId}/{$slug} -l";
echo $k + 1 . "\t" . $shell . PHP_EOL;
var_dump(shell_exec($shell));
}
# 备份到git
$shell = "cd {$this->savePath} && /usr/bin/git add . && /usr/bin/git commit -m 'yuque bak at'$(date +\"\%Y\%m\%d\") && /usr/bin/git push";
echo $shell . PHP_EOL;
var_dump(shell_exec($shell));
}
/**
* 获取知识库列表
* @date 2020-04-20 19:27
* @return bool|string
*/
private function getRepoList()
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.yuque.com/api/v2/users/{$this->userId}/repos",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"X-Auth-Token: " . $this->token,
"User-Agent: {}"
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}