ExportArticle.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 df6c6609-6fc1-42d0-9ef1-535ef3e702c9 1234 --origin=true --channel=7fea264d-7a26-40f8-bef7-bc95102760fb --format=docx --anthology=697c9169-cb9d-4a60-8848-92745e467bab --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. $options = [
  48. 'queryId'=>$this->argument('query_id'),
  49. 'format'=>$this->option('format'),
  50. 'debug'=>$this->option('debug'),
  51. 'filename'=>'article',
  52. ];
  53. $upload = new ExportDownload($options);
  54. MdRender::init();
  55. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
  56. 'delimiters' => '[[ ]]',
  57. 'escape'=>function ($value){
  58. return $value;
  59. }));
  60. $sections = array();
  61. $articles = array();
  62. $article = $this->fetch($this->argument('id'));
  63. if(!$article){
  64. return 1;
  65. }
  66. $bookMeta = array();
  67. $bookMeta['book_author'] = "";
  68. $bookMeta['book_title'] = $article['title_text'];
  69. $articles[] = [
  70. 'level'=>1,
  71. 'title'=>$article['title_text'],
  72. 'content'=>isset($article['html'])?$article['html']:'',
  73. ];
  74. $progress = 0.1;
  75. $this->info($upload->setStatus($progress,'export article content title='.$article['title_text']));
  76. if(isset($article['toc']) && count($article['toc'])>0){
  77. $this->info('has sub article '. count($article['toc']));
  78. $step = 0.8 / count($article['toc']);
  79. $baseLevel = 0;
  80. foreach ($article['toc'] as $key => $value) {
  81. if($baseLevel === 0){
  82. $baseLevel = $value['level'] - 2;
  83. }
  84. $progress += $step;
  85. $this->info($upload->setStatus($progress,'exporting article title='.$value['title']));
  86. $article = $this->fetch($value['key']);
  87. if(!$article){
  88. $this->info($upload->setStatus($progress,'exporting article fail title='.$value['title']));
  89. continue;
  90. }
  91. $this->info($upload->setStatus($progress,'exporting article success title='.$article['title_text']));
  92. $articles[] = [
  93. 'level'=>$value['level']-$baseLevel,
  94. 'title'=>$article['title_text'],
  95. 'content'=>isset($article['html'])?$article['html']:'',
  96. ];
  97. }
  98. }
  99. $sections[] = [
  100. 'name'=>'articles',
  101. 'body'=>['articles'=>$articles],
  102. ];
  103. $this->info($upload->setStatus(0.9,'export article content done'));
  104. Log::debug('导出结束');
  105. $upload->upload('article',$sections,$bookMeta);
  106. $this->info($upload->setStatus(1,'export article done'));
  107. return 0;
  108. }
  109. private function fetch($articleId){
  110. $api = config('mint.server.api.bamboo');
  111. $basicUrl = $api . '/v2/article/';
  112. $url = $basicUrl . $articleId;;
  113. $this->info('http request url='.$url);
  114. $urlParam = [
  115. 'mode' => 'read',
  116. 'format' => 'markdown',
  117. 'anthology'=> $this->option('anthology'),
  118. 'channel' => $this->option('channel'),
  119. 'origin' => 'true' /*$this->option('origin')*/,
  120. 'paragraph' => true,
  121. ];
  122. Log::debug('export article http request',['url'=>$url,'param'=>$urlParam]);
  123. if($this->option('token')){
  124. $response = Http::withToken($this->option('token'))->get($url,$urlParam);
  125. }else{
  126. $response = Http::get($url,$urlParam);
  127. }
  128. if($response->failed()){
  129. $this->error('http request error'.$response->json('message'));
  130. Log::error('http request error',['error'=>$response->json('message')]);
  131. return false;
  132. }
  133. if(!$response->json('ok')){
  134. $this->error('http request error'.$response->json('message'));
  135. return false;
  136. }
  137. $article = $response->json('data');
  138. return $article;
  139. }
  140. }