ExportArticle.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Str;
  7. use Illuminate\Support\Facades\Storage;
  8. use App\Tools\RedisClusters;
  9. use App\Tools\ExportDownload;
  10. use App\Http\Api\MdRender;
  11. class ExportArticle extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. * php artisan export:article 78c22ad3-58e2-4cf0-b979-67783ca3a375 123 --channel=7fea264d-7a26-40f8-bef7-bc95102760fb --format=html
  16. * php artisan export:article 4732bcae-fb9d-4db4-b6b7-e8d0aa882f30 1234 --channel=7fea264d-7a26-40f8-bef7-bc95102760fb --anthology=eb9e3f7f-b942-4ca4-bd6f-b7876b59a523 --format=html --token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJuYmYiOjE2OTc3Mjg2ODUsImV4cCI6MTcyOTI2NDY4NSwidWlkIjoiYmE1NDYzZjMtNzJkMS00NDEwLTg1OGUtZWFkZDEwODg0NzEzIiwiaWQiOjR9.fiXhnY2LczZ9kKVHV0FfD3AJPZt-uqM5wrDe4EhToVexdd007ebPFYssZefmchfL0mx9nF0rgHSqjNhx4P0yDA
  17. * @var string
  18. */
  19. protected $signature = 'export:article {id} {query_id} {--token=} {--anthology=} {--channel=} {--origin=false} {--translation=true} {--format=tex} {--debug}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Command description';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. $this->info('task export chapter start');
  43. Log::debug('task export chapter start');
  44. if(\App\Tools\Tools::isStop()){
  45. return 0;
  46. }
  47. $upload = new ExportDownload([
  48. 'queryId'=>$this->argument('query_id'),
  49. 'format'=>$this->option('format'),
  50. 'debug'=>$this->option('debug'),
  51. 'filename'=>'article',
  52. ]);
  53. MdRender::init();
  54. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
  55. 'delimiters' => '[[ ]]',
  56. 'escape'=>function ($value){
  57. return $value;
  58. }));
  59. $sections = array();
  60. $articles = array();
  61. $article = $this->fetch($this->argument('id'));
  62. if(!$article){
  63. return 1;
  64. }
  65. $bookMeta = array();
  66. $bookMeta['book_author'] = "";
  67. $bookMeta['book_title'] = $article['title_text'];
  68. $articles[] = [
  69. 'level'=>1,
  70. 'title'=>$article['title_text'],
  71. 'content'=>isset($article['html'])?$article['html']:'',
  72. ];
  73. $progress = 0.1;
  74. $this->info($upload->setStatus($progress,'export article content title='.$article['title_text']));
  75. if(isset($article['toc']) && count($article['toc'])>0){
  76. $this->info('has sub article '. count($article['toc']));
  77. $step = 0.8 / count($article['toc']);
  78. $baseLevel = 0;
  79. foreach ($article['toc'] as $key => $value) {
  80. if($baseLevel === 0){
  81. $baseLevel = $value['level'] - 2;
  82. }
  83. $progress += $step;
  84. $this->info($upload->setStatus($progress,'exporting article title='.$value['title']));
  85. $article = $this->fetch($value['key']);
  86. if(!$article){
  87. $this->info($upload->setStatus($progress,'exporting article fail title='.$value['title']));
  88. continue;
  89. }
  90. $this->info($upload->setStatus($progress,'exporting article success title='.$article['title_text']));
  91. $articles[] = [
  92. 'level'=>$value['level']-$baseLevel,
  93. 'title'=>$article['title_text'],
  94. 'content'=>isset($article['html'])?$article['html']:'',
  95. ];
  96. }
  97. }
  98. $sections[] = [
  99. 'name'=>'articles',
  100. 'body'=>['articles'=>$articles],
  101. ];
  102. $this->info($upload->setStatus(0.9,'export article content done'));
  103. Log::debug('导出结束');
  104. $upload->upload('article',$sections,$bookMeta);
  105. $this->info($upload->setStatus(1,'export article done'));
  106. return 0;
  107. }
  108. private function fetch($articleId){
  109. $api = config('mint.server.api.bamboo');
  110. $basicUrl = $api . '/v2/article/';
  111. $url = $basicUrl . $articleId;;
  112. $this->info('http request url='.$url);
  113. $urlParam = [
  114. 'mode' => 'read',
  115. 'format' => 'html',
  116. 'anthology'=> $this->option('anthology'),
  117. 'channel' => $this->option('channel'),
  118. ];
  119. if($this->option('token')){
  120. $response = Http::withToken($this->option('token'))->get($url,$urlParam);
  121. }else{
  122. $response = Http::get($url,$urlParam);
  123. }
  124. if($response->failed()){
  125. $this->error('http request error'.$response->json('message'));
  126. Log::error('http request error'.$response->json('message'));
  127. return false;
  128. }
  129. if(!$response->json('ok')){
  130. $this->error('http request error'.$response->json('message'));
  131. return false;
  132. }
  133. $article = $response->json('data');
  134. return $article;
  135. }
  136. }