PoemContent.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\command;
  3. use app\model\dynasty\DynastyAuthor;
  4. use QL\QueryList;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. class PoemContent extends Command
  9. {
  10. protected static string $defaultName = 'poem:content';
  11. protected static string $defaultDescription = '采集作品详情';
  12. protected function execute(InputInterface $input, OutputInterface $output): int
  13. {
  14. try {
  15. $nextPage = 1; $done = 0; $subPage = 1;
  16. while (is_numeric($nextPage)) {
  17. $output->writeln("开始" . getDateFull());
  18. $resp = (new \app\model\poem\PoemData)->where("is_done",0)->page($nextPage,10)->select();
  19. if ($resp->isEmpty()) {
  20. $output->writeln("没有更多任务啦!===".getDateFull());
  21. $nextPage = null;
  22. return self::SUCCESS;
  23. }
  24. $model = (new \app\model\poem\PoemContent);
  25. foreach ($resp as $val) {
  26. $output->writeln("标题:{$val['title']}==作者:{$val['author_name']}===链接地址:https://www.gushiji.cc/gushi/{$val['poem_id']}.html====".getDateFull());
  27. $ql = QueryList::getInstance()->get("https://www.gushiji.cc/gushi/{$val['poem_id']}.html")->rules([
  28. "data" => [".shicicontent","text"],
  29. "tags" => [".relatedCategory>ul>li>a","texts"],
  30. "notes" => [".shici-content","texts"],
  31. ])->query()->getData(function ($item,$key){
  32. foreach ($item['notes'] as $k=>$v){
  33. if ($k == 0) {
  34. $note = splitTransAndNote($v);
  35. } else {
  36. $note = $v;
  37. }
  38. $item['notes'][$k] = $note;
  39. }
  40. return $item;
  41. });
  42. $respData = $ql->all();
  43. $poemData = $model->where("poem_id",$val['poem_id'])->findOrEmpty();
  44. if ($poemData->isEmpty()) {
  45. $output->writeln("【新增】标题:{$val['title']}====".getDateFull());
  46. $poemData->insertGetId([
  47. 'poem_id' => $val['poem_id'],
  48. "content" => $respData['data'],
  49. "trans" => $respData['notes'][0]['trans']??'',
  50. "note" => $respData['notes'][0]['note']??'',
  51. "review" => $respData['notes'][1]??'',
  52. "background" => $respData['notes'][2]??'',
  53. ]);
  54. } else {
  55. $output->writeln("【更新】标题:{$val['title']}====".getDateFull());
  56. $poemData->save([
  57. "content" => $respData['data'],
  58. "trans" => $respData['notes'][0]['trans']??'',
  59. "note" => $respData['notes'][0]['note']??'',
  60. "review" => $respData['notes'][1]??'',
  61. "background" => $respData['notes'][2]??''
  62. ]);
  63. }
  64. (new \app\model\poem\PoemData)->where('id',$val['id'])->save(['is_done'=>1,"tags" => json_encode($respData['tags'])]);
  65. }
  66. $nextPage = $nextPage+1;
  67. $output->writeln("进入下一篇=={$nextPage}==".getDateFull());
  68. }
  69. } catch (\Throwable $throwable) {
  70. echo $throwable->getMessage()."\n";
  71. }
  72. return self::SUCCESS;
  73. }
  74. }