ExportArticle.php 5.6 KB

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