PoemAuthor.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\command;
  3. use app\model\dynasty\Dynasty;
  4. use app\model\dynasty\DynastyAuthor;
  5. use Overtrue\Pinyin\Pinyin;
  6. use QL\QueryList;
  7. use Symfony\Component\Console\Command\Command;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use function DI\get;
  11. class PoemAuthor extends Command
  12. {
  13. protected static string $defaultName = 'poem:author';
  14. protected static string $defaultDescription = '采集作者';
  15. protected function execute(InputInterface $input, OutputInterface $output): int
  16. {
  17. try {
  18. $nextPage = 1; $done = 0; $subPage = 1;
  19. while (is_numeric($nextPage)) {
  20. $output->writeln("开始".getDateFull());
  21. $resp = (new Dynasty)->where("type",1)->page($nextPage,10)->select();
  22. if ($resp->isEmpty()) {
  23. $output->writeln("没有更多任务啦!===".getDateFull());
  24. $nextPage = null;
  25. return self::SUCCESS;
  26. }
  27. $author = (new DynastyAuthor);
  28. foreach ($resp as $key=>$val) {
  29. $output->writeln("朝代:{$val['name']}==页码:{$subPage}===链接地址:https://www.gushiji.cc/shiren/{$val['pinyin']}.html====".getDateFull());
  30. while (is_numeric($subPage)) {
  31. if ($subPage == 1) {
  32. $url = "https://www.gushiji.cc/shiren/{$val['pinyin']}.html";
  33. } else {
  34. $url = "https://www.gushiji.cc/shiren/{$val['pinyin']}_{$subPage}.html";
  35. }
  36. $ql = QueryList::getInstance()->get($url)->rules([
  37. "name" => ['.simple-people>li','texts'],
  38. "link" => ['.simple-people>li>a','attrs(href)'],
  39. "avatar" => ['.simple-people>li>a>img','attrs(src)'],
  40. "page" => ['#pagenum>li.info','text'],
  41. ])->query()->getData(function ($item,$key){
  42. foreach ($item['avatar'] as $k=>$v){
  43. if (empty($v)){
  44. $v = "/skin/images/nopicture.jpg";
  45. }
  46. $item['avatar'][$k] = "https://www.gushiji.cc".$v;
  47. }
  48. foreach ($item['link'] as $k=>$v){
  49. $link = explode("/",$v);
  50. $item['link'][$k] = $link[2]??'';
  51. }
  52. $poem = [];
  53. foreach ($item['name'] as $k=>$v){
  54. preg_match('/([^()()]+)[((](\d+)[))]/u', $v, $match);
  55. $item['name'][$k] = $match[1]??'';
  56. $poem[$k] = $match[2]??0;
  57. }
  58. preg_match_all('/\d+/', $item['page'], $matches);
  59. $item['page'] = $matches[0][0] ?? 0;
  60. $item['total'] = $matches[0][1] ?? 0;
  61. $item['poem'] = $poem;
  62. return $item;
  63. });
  64. $respData = $ql->all();
  65. if ($subPage > $respData['page']) {
  66. $output->writeln("朝代:{$val['name']}==没有作者啦==".getDateFull());
  67. $subPage = 1;
  68. break;
  69. }
  70. foreach ($respData['name'] as $k=>$v){
  71. $authorData = $author->where(['name' => $v, 'dynasty_id' => $val['id']])->findOrEmpty();
  72. if ($authorData->isEmpty()) {
  73. $output->writeln("【新增】朝代:{$val['name']}==诗人:{$v}==拼音:".trim($respData['link'][$k])."==".getDateFull());
  74. $authorData->insertGetId([
  75. "name" => $v,
  76. "poem_total" => $respData['poem'][$k],
  77. "avatar" => $respData['avatar'][$k],
  78. "dynasty_id" => $val['id'],
  79. "dynasty_name" => $val['name'],
  80. "initial" => strtoupper(substr($respData['link'][$k], 0, 1)),
  81. "pinyin" => $respData['link'][$k]
  82. ]);
  83. } else {
  84. $output->writeln("【更新】朝代:{$val['name']}==诗人:{$v}==拼音:".trim($respData['link'][$k])."==".getDateFull());
  85. $authorData->save([
  86. "name" => $v,
  87. "poem_total" => $respData['poem'][$k],
  88. "avatar" => $respData['avatar'][$k],
  89. "dynasty_id" => $val['id'],
  90. "dynasty_name" => $val['name'],
  91. "initial" => strtoupper(substr($respData['link'][$k], 0, 1)),
  92. "pinyin" => $respData['link'][$k]
  93. ]);
  94. }
  95. }
  96. $subPage = $subPage+1;
  97. $output->writeln("进入下一页=={$nextPage}-{$subPage}==".getDateFull());
  98. $output->writeln("===========================================================================");
  99. }
  100. }
  101. $nextPage = $nextPage+1;
  102. $subPage = 1;
  103. $output->writeln("进入下一页=={$nextPage}==".getDateFull());
  104. }
  105. } catch (\Throwable $throwable) {
  106. }
  107. return self::SUCCESS;
  108. }
  109. }