使用到的项目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

    代码:

    1. <?php
    2. namespace App\Console\Commands\Shell;
    3. use Illuminate\Console\Command;
    4. /**
    5. * 语雀知识库导出脚本
    6. * 使用说明:
    7. * 改脚本依赖于语雀文档工具 yuque2book
    8. * 使用说明见 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
    9. * Class YuqueBackUp
    10. * @package App\Console\Commands\Shell
    11. */
    12. class YuqueBackUp extends Command
    13. {
    14. /**
    15. * The name and signature of the console command.
    16. *
    17. * @var string
    18. */
    19. protected $signature = 'Shell:YuqueBackUp';
    20. /**
    21. * The console command description.
    22. *
    23. * @var string
    24. */
    25. protected $description = '语雀备份';
    26. private $token = '{}';
    27. private $savePath = '{}';
    28. private $userId = '{}';
    29. /**
    30. * Create a new command instance.
    31. *
    32. * @return void
    33. */
    34. public function __construct()
    35. {
    36. parent::__construct();
    37. }
    38. /**
    39. * Execute the console command.
    40. *
    41. * @return mixed
    42. */
    43. public function handle()
    44. {
    45. # 判断备份目录是否存在
    46. if (!is_dir($this->savePath)) {
    47. mkdir($this->savePath, 0777, true);
    48. }
    49. $repos = $this->getRepoList();
    50. $repos = json_decode($repos, true);
    51. $slugList = array_map(function ($v) {
    52. return $v['slug'];
    53. }, $repos['data']);
    54. foreach ($slugList as $k => $slug) {
    55. if($slug == 'pw0zfo'){
    56. continue;
    57. }
    58. $shell = "cd {$this->savePath} && yuque2book -t {$this->token} https://www.yuque.com/{$this->userId}/{$slug} -l";
    59. echo $k + 1 . "\t" . $shell . PHP_EOL;
    60. var_dump(shell_exec($shell));
    61. }
    62. # 备份到git
    63. $shell = "cd {$this->savePath} && /usr/bin/git add . && /usr/bin/git commit -m 'yuque bak at'$(date +\"\%Y\%m\%d\") && /usr/bin/git push";
    64. echo $shell . PHP_EOL;
    65. var_dump(shell_exec($shell));
    66. }
    67. /**
    68. * 获取知识库列表
    69. * @date 2020-04-20 19:27
    70. * @return bool|string
    71. */
    72. private function getRepoList()
    73. {
    74. $curl = curl_init();
    75. curl_setopt_array($curl, array(
    76. CURLOPT_URL => "https://www.yuque.com/api/v2/users/{$this->userId}/repos",
    77. CURLOPT_RETURNTRANSFER => true,
    78. CURLOPT_ENCODING => "",
    79. CURLOPT_MAXREDIRS => 10,
    80. CURLOPT_TIMEOUT => 0,
    81. CURLOPT_FOLLOWLOCATION => true,
    82. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    83. CURLOPT_CUSTOMREQUEST => "GET",
    84. CURLOPT_HTTPHEADER => array(
    85. "Content-Type: application/json",
    86. "X-Auth-Token: " . $this->token,
    87. "User-Agent: {}"
    88. ),
    89. ));
    90. $response = curl_exec($curl);
    91. curl_close($curl);
    92. return $response;
    93. }
    94. }