ExportArticle.php 5.5 KB

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