Răsfoiți Sursa

filename -> query_id

visuddhinanda 2 ani în urmă
părinte
comite
dc65585581
1 a modificat fișierele cu 68 adăugiri și 49 ștergeri
  1. 68 49
      app/Console/Commands/ExportArticle.php

+ 68 - 49
app/Console/Commands/ExportArticle.php

@@ -18,10 +18,10 @@ class ExportArticle extends Command
     /**
      * The name and signature of the console command.
      * php artisan export:article 78c22ad3-58e2-4cf0-b979-67783ca3a375 123 --channel=7fea264d-7a26-40f8-bef7-bc95102760fb --format=html
-     * php artisan export:article 4732bcae-fb9d-4db4-b6b7-e8d0aa882f30 1234 --channel=7fea264d-7a26-40f8-bef7-bc95102760fb --anthology=eb9e3f7f-b942-4ca4-bd6f-b7876b59a523 --format=html
+     * 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
      * @var string
      */
-    protected $signature = 'export:article {id} {filename} {--anthology=} {--channel=}  {--origin=false} {--translation=true} {--format=tex} {--debug}';
+    protected $signature = 'export:article {id} {query_id} {--token=} {--anthology=} {--channel=}  {--origin=false} {--translation=true} {--format=tex} {--debug}';
 
     /**
      * The console command description.
@@ -53,10 +53,10 @@ class ExportArticle extends Command
             return 0;
         }
         $upload = new ExportDownload([
-            'filename'=>$this->argument('filename').'.zip',
+            'queryId'=>$this->argument('query_id'),
             'format'=>$this->option('format'),
             'debug'=>$this->option('debug'),
-            'real_filename'=>'article',
+            'filename'=>'article',
         ]);
 
         MdRender::init();
@@ -66,26 +66,16 @@ class ExportArticle extends Command
                                             return $value;
                                         }));
 
-        $api = 'http://127.0.0.1:8000/api';
-        $basicUrl = $api . '/v2/article/';
+
         $sections = array();
         $articles = array();
 
-        $url =  $basicUrl . $this->argument('id');
-        $this->info('http request url='.$url);
-        $response = Http::get($url,[
-                'mode' => 'read',
-                'format' => 'html',
-                'anthology'=> $this->option('anthology'),
-                'channel' => $this->option('channel'),
-            ]);
-        if($response->failed()){
-            $this->error('http request error');
-        }
-        if(!$response->json('ok')){
-            $this->error('http request error'.$response->json('message'));
+
+        $article = $this->fetch($this->argument('id'));
+        if(!$article){
+            return 1;
         }
-        $article = $response->json('data');
+
         $bookMeta = array();
         $bookMeta['book_author'] = "";
         $bookMeta['book_title'] = $article['title_text'];
@@ -95,45 +85,74 @@ class ExportArticle extends Command
             'title'=>$article['title_text'],
             'content'=>isset($article['html'])?$article['html']:'',
         ];
-        if(isset($article['toc'])){
+        $progress = 0.1;
+        $this->info($upload->setStatus($progress,'export article content title='.$article['title_text']));
+
+        if(isset($article['toc']) && count($article['toc'])>0){
             $this->info('has sub article '. count($article['toc']));
-        }
-        foreach ($article['toc'] as $key => $value) {
-            $url =  $basicUrl . $value['key'];
-            $this->info('http request url='.$url);
-            $response = Http::get($url,[
-                    'mode' => 'read',
-                    'format' => 'html',
-                    'anthology'=> $this->option('anthology'),
-                    'channel' => $this->option('channel'),
-                ]);
-            if($response->failed()){
-                $this->error('http request error');
-                $this->error('http request error'.$response->json('message'));
-                continue;
-            }
-            if(!$response->json('ok')){
-                $this->error('http request error'.$response->json('message'));
-                continue;
+            $step = 0.8 / count($article['toc']);
+            $baseLevel = 0;
+            foreach ($article['toc'] as $key => $value) {
+                if($baseLevel === 0){
+                    $baseLevel = $value['level'] - 2;
+                }
+                $progress += $step;
+                $this->info($upload->setStatus($progress,'exporting article title='.$value['title']));
+                $article = $this->fetch($value['key']);
+                if(!$article){
+                    $this->info($upload->setStatus($progress,'exporting article fail title='.$value['title']));
+                    continue;
+                }
+                $this->info($upload->setStatus($progress,'exporting article success title='.$article['title_text']));
+                $articles[] = [
+                    'level'=>$value['level']-$baseLevel,
+                    'title'=>$article['title_text'],
+                    'content'=>isset($article['html'])?$article['html']:'',
+                ];
             }
-            $article = $response->json('data');
-            $this->info('sub article title='.$article['title_text']);
-            $articles[] = [
-                'level'=>$value['level'],
-                'title'=>$article['title_text'],
-                'content'=>isset($article['html'])?$article['html']:'',
-            ];
         }
+
         $sections[] = [
-            'name'=>'first',
+            'name'=>'articles',
             'body'=>['articles'=>$articles],
         ];
-        $this->info($upload->setStatus(0.9,'export content done'));
+        $this->info($upload->setStatus(0.9,'export article content done'));
         Log::debug('导出结束');
 
 
         $upload->upload('article',$sections,$bookMeta);
-
+        $this->info($upload->setStatus(1,'export article done'));
         return 0;
     }
+
+    private function fetch($articleId){
+        $api = config('mint.server.api.bamboo');
+        $basicUrl = $api . '/v2/article/';
+        $url =  $basicUrl . $articleId;;
+        $this->info('http request url='.$url);
+        $urlParam = [
+                'mode' => 'read',
+                'format' => 'html',
+                'anthology'=> $this->option('anthology'),
+                'channel' => $this->option('channel'),
+        ];
+
+        if($this->option('token')){
+            $response = Http::withToken($this->option('token'))->get($url,$urlParam);
+        }else{
+            $response = Http::get($url,$urlParam);
+        }
+
+        if($response->failed()){
+            $this->error('http request error'.$response->json('message'));
+            Log::error('http request error'.$response->json('message'));
+            return false;
+        }
+        if(!$response->json('ok')){
+            $this->error('http request error'.$response->json('message'));
+            return false;
+        }
+        $article = $response->json('data');
+        return $article;
+    }
 }