IndexController.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\controller;
  3. use app\model\dynasty\DynastyAuthor;
  4. use app\model\dynasty\DynastyCategory;
  5. use QL\QueryList;
  6. use support\Request;
  7. class IndexController
  8. {
  9. public function index(Request $request)
  10. {
  11. $ql = QueryList::getInstance()->get("https://www.gushiji.cc/dianji/")->rules([
  12. // "name" => ['.gushi-info>h3','texts'],
  13. // "link" => ['.gushi-info>h3>a','attrs(href)'],
  14. "name" => ['.gushi-info>.cipai>ul>li','texts'],
  15. "link" => ['.gushi-info>.cipai>ul>li>a','attrs(href)'],
  16. ])->query()->getData(function ($item,$key){
  17. foreach ($item['link'] as $k=>$v){
  18. $link = explode("/",$v);
  19. $item['link'][$k] = $link[2]??'';
  20. }
  21. return $item;
  22. });
  23. $resp = $ql->all();
  24. foreach ($resp['name'] as $k=>$v){
  25. $cate = (new DynastyAuthor)->where("pinyin",$resp['link'][$k])->findOrEmpty();
  26. if ($cate->isEmpty()){
  27. $cate->insertGetId([
  28. "pinyin" => $resp['link'][$k],
  29. "name" => $v,
  30. "type" => 2,
  31. "initial" => strtoupper(substr($resp['link'][$k], 0, 1)),
  32. ]);
  33. } else {
  34. $cate->insertGetId([
  35. "pinyin" => $resp['link'][$k],
  36. "name" => $v,
  37. "type" => 2,
  38. "initial" => strtoupper(substr($resp['link'][$k], 0, 1)),
  39. ]);
  40. }
  41. }
  42. return json($resp);
  43. }
  44. protected function getAuthor()
  45. {
  46. $ql = QueryList::getInstance()->get("https://www.gushiji.cc/shiren/")->rules([
  47. "name" => ['.simple-people>li','texts'],
  48. "link" => ['.simple-people>li>a','attrs(href)'],
  49. "avatar" => ['.simple-people>li>a>img','attrs(src)'],
  50. "page" => ['#pagenum>li.info','text'],
  51. ])->query()->getData(function ($item,$key){
  52. foreach ($item['avatar'] as $k=>$v){
  53. if (empty($v)){
  54. $v = "/skin/images/nopicture.jpg";
  55. }
  56. $item['avatar'][$k] = "https://www.gushiji.cc".$v;
  57. }
  58. foreach ($item['link'] as $k=>$v){
  59. $link = explode("/",$v);
  60. $item['link'][$k] = $link[2]??'';
  61. }
  62. $poem = [];
  63. foreach ($item['name'] as $k=>$v){
  64. preg_match('/([^()()]+)[((](\d+)[))]/u', $v, $match);
  65. $item['name'][$k] = $match[1]??'';
  66. $poem[$k] = $match[2]??0;
  67. }
  68. preg_match_all('/\d+/', $item['page'], $matches);
  69. $item['page'] = $matches[0][0] ?? 0;
  70. $item['total'] = $matches[0][1] ?? 0;
  71. $item['poem'] = $poem;
  72. return $item;
  73. });
  74. return $ql->all();
  75. }
  76. public function view(Request $request)
  77. {
  78. return view('index/view', ['name' => 'webman']);
  79. }
  80. public function json(Request $request)
  81. {
  82. return json(['code' => 0, 'msg' => 'ok']);
  83. }
  84. }