ImportArticle.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\StudioApi;
  4. use App\Models\Article;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Http;
  7. use Illuminate\Support\Facades\Log;
  8. class ImportArticle extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. * php artisan import:article --studio=visuddhinanda --anthology=eb9e3f7f-b942-4ca4-bd6f-b7876b59a523 --token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJuYmYiOjE2OTc3Mjg2ODUsImV4cCI6MTcyOTI2NDY4NSwidWlkIjoiYmE1NDYzZjMtNzJkMS00NDEwLTg1OGUtZWFkZDEwODg0NzEzIiwiaWQiOjR9.fiXhnY2LczZ9kKVHV0FfD3AJPZt-uqM5wrDe4EhToVexdd007ebPFYssZefmchfL0mx9nF0rgHSqjNhx4P0yDA
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'import:article {--studio=} {--anthology=} {--token=}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '导入缅文tipitaka sarupa文章';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. * 分两个步骤导入
  35. * 1. 导入文章到文集
  36. * 2. 重新生成目录结构
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. if (! $this->confirm('Do you wish to continue?')) {
  43. return 0;
  44. }
  45. $token = $this->option('token');
  46. $studioName = $this->option('studio');
  47. $anthologyId = $this->option('anthology');
  48. // 先获取文章列表,建立全部目录
  49. $head = [];
  50. $strFileName = __DIR__.'/tipitaka-sarupa.csv';
  51. if (! file_exists($strFileName)) {
  52. $this->error($strFileName.'文件不存在');
  53. return 1;
  54. }
  55. if (($fp = fopen($strFileName, 'r')) === false) {
  56. $this->error("can not open csv {$strFileName}");
  57. return 0;
  58. }
  59. $this->info('打开csv文件成功');
  60. $studioId = StudioApi::getIdByName($studioName);
  61. if (! $studioId) {
  62. $this->error("can not found studio name {$studioName}");
  63. return 0;
  64. }
  65. // 导入文章
  66. $url = config('app.url').'/api/v2/article';
  67. $inputRow = 0;
  68. fseek($fp, 0);
  69. $count = 0;
  70. $success = 0;
  71. $fail = 0;
  72. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  73. if ($inputRow > 0) {
  74. $id = $data[0];
  75. $dir = $data[1];
  76. $title = $data[2];
  77. $realTitle = "[{$id}]{$title}";
  78. $content = str_replace('\n', "\n", $data[4]);
  79. $reference = str_replace(['(', ')'], ['({{ql|type=m|title=', '}})'], $data[5]);
  80. $contentCombine = "{$title}\n\n{$content}\n\n{$reference}";
  81. $percent = (int) ($inputRow * 100 / 7000);
  82. $this->info("[{$percent}%] doing ".$realTitle);
  83. // 先查是否有
  84. $hasArticle = Article::where('owner', $studioId)
  85. ->where('title', $realTitle)
  86. ->exists();
  87. if ($hasArticle) {
  88. $this->error('文章已经存在 title='.$realTitle);
  89. continue;
  90. }
  91. $count++;
  92. $this->info('新建 title='.$realTitle);
  93. sleep(2);
  94. $response = Http::withToken($token)->post($url,
  95. [
  96. 'title' => $realTitle,
  97. 'lang' => 'my',
  98. 'studio' => $studioName,
  99. 'anthologyId' => $anthologyId,
  100. ]);
  101. if ($response->ok()) {
  102. $this->info('create ok');
  103. $articleId = $response->json('data')['uid'];
  104. } else {
  105. $this->error('create article fail.'.$realTitle);
  106. Log::error('create article fail title='.$realTitle);
  107. $fail++;
  108. continue;
  109. }
  110. sleep(2);
  111. $this->info('修改 id='.$articleId);
  112. $response = Http::withToken($token)->put($url.'/'.$articleId,
  113. [
  114. 'title' => $realTitle,
  115. 'summary' => $title.'#'.$id,
  116. 'lang' => 'my',
  117. 'content' => $contentCombine,
  118. 'anthology_id' => $anthologyId,
  119. 'to_tpl' => true,
  120. 'status' => 30,
  121. ]);
  122. if ($response->ok()) {
  123. $this->info('edit ok');
  124. $success++;
  125. } else {
  126. $this->error('edit article fail');
  127. Log::error('edit article fail ', ['id' => $articleId, 'title' => $realTitle]);
  128. $fail++;
  129. }
  130. }
  131. $inputRow++;
  132. }
  133. fclose($fp);
  134. $this->info('成功='.$success.' 失败='.$fail);
  135. return 0;
  136. }
  137. }