PoemData.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 PoemData extends Command
  9. {
  10. protected static string $defaultName = 'poem:data';
  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 DynastyAuthor)->page($nextPage,10)->select();
  19. if ($resp->isEmpty()) {
  20. $output->writeln("没有更多任务啦!===".getDateFull());
  21. $nextPage = null;
  22. return self::SUCCESS;
  23. }
  24. $poem = (new \app\model\poem\PoemData);
  25. foreach ($resp as $key=>$val) {
  26. while (is_numeric($subPage)) {
  27. if ($subPage == 1) {
  28. $url = "https://www.gushiji.cc/shiren/{$val['pinyin']}/";
  29. } else {
  30. $url = "https://www.gushiji.cc/shiren/{$val['pinyin']}/p{$subPage}.html";
  31. }
  32. $output->writeln("诗人:{$val['name']}==页码:{$subPage}===链接地址:{$url}====".getDateFull());
  33. $ql = QueryList::getInstance()->get($url)->rules([
  34. "page" => ['#pagenum>li.info','text'],
  35. "name" => ['.simple-shiciqu>li','texts'],
  36. "link" => ['.simple-shiciqu>li>a','attrs(href)'],
  37. ])->query()->getData(function ($item,$key){
  38. preg_match_all('/\d+/', $item['page'], $matches);
  39. $item['page'] = $matches[0][0] ?? 0;
  40. $item['total'] = $matches[0][1] ?? 0;
  41. foreach ($item['link'] as $k=>$v){
  42. preg_match('/\/([^\/]+)\.html$/', $v, $match);
  43. $item['link'][$k] = $match[1]??'';
  44. }
  45. return $item;
  46. });
  47. $respData = $ql->all();
  48. if ($subPage > $respData['page']) {
  49. $output->writeln("作者:{$val['name']}==没有作品啦==".getDateFull());
  50. $subPage = 1;
  51. break;
  52. }
  53. foreach ($respData['name'] as $k=>$v) {
  54. $authorData = $poem->where(['poem_id' => $respData['link'][$k]])->findOrEmpty();
  55. if ($authorData->isEmpty()) {
  56. $output->writeln("【新增】作者作品:{$val['name']}==标题:{$v}====".getDateFull());
  57. $authorData->insertGetId([
  58. "title" => $v,
  59. "poem_id" => $respData['link'][$k],
  60. "author_id" => $val['id'],
  61. "author_name" => $val['name'],
  62. "dynasty_id" => $val['dynasty_id'],
  63. "dynasty_name" => $val['dynasty_name'],
  64. ]);
  65. } else {
  66. $output->writeln("【更新】作者作品:{$val['name']}==标题:{$v}====".getDateFull());
  67. $authorData->save([
  68. "title" => $v,
  69. "poem_id" => $respData['link'][$k],
  70. "author_id" => $val['id'],
  71. "author_name" => $val['name'],
  72. "dynasty_id" => $val['dynasty_id'],
  73. "dynasty_name" => $val['dynasty_name'],
  74. ]);
  75. }
  76. }
  77. $subPage = $subPage+1;
  78. $output->writeln("进入下一页=={$nextPage}-{$subPage}==".getDateFull());
  79. $output->writeln("===========================================================================");
  80. }
  81. }
  82. $nextPage = $nextPage+1;
  83. $subPage = 1;
  84. $output->writeln("进入下一个作者=={$nextPage}==".getDateFull());
  85. }
  86. } catch (\Throwable $throwable) {
  87. echo $throwable->getMessage()."\n";
  88. }
  89. return self::SUCCESS;
  90. }
  91. }