IndexController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/gushi/ed7378.html")->rules([
  12. "data" => [".shicicontent","text"],
  13. "tags" => [".relatedCategory>ul>li>a","texts"],
  14. "notes" => [".shici-content","texts"],
  15. ])->query()->getData(function ($item,$key){
  16. foreach ($item['notes'] as $k=>$v){
  17. if ($k == 0) {
  18. $note = splitTransAndNote($v);
  19. } else {
  20. $note = $v;
  21. }
  22. $item['notes'][$k] = $note;
  23. }
  24. return $item;
  25. });
  26. $resp = $ql->all();
  27. return json($resp);
  28. }
  29. protected function getPoem()
  30. {
  31. $ql = QueryList::getInstance()->get("https://www.gushiji.cc/dianji/")->rules([
  32. // "name" => ['.gushi-info>h3','texts'],
  33. // "link" => ['.gushi-info>h3>a','attrs(href)'],
  34. "name" => ['.gushi-info>.cipai>ul>li','texts'],
  35. "link" => ['.gushi-info>.cipai>ul>li>a','attrs(href)'],
  36. ])->query()->getData(function ($item,$key){
  37. foreach ($item['link'] as $k=>$v){
  38. $link = explode("/",$v);
  39. $item['link'][$k] = $link[2]??'';
  40. }
  41. return $item;
  42. });
  43. $resp = $ql->all();
  44. foreach ($resp['name'] as $k=>$v){
  45. $cate = (new DynastyAuthor)->where("pinyin",$resp['link'][$k])->findOrEmpty();
  46. if ($cate->isEmpty()){
  47. $cate->insertGetId([
  48. "pinyin" => $resp['link'][$k],
  49. "name" => $v,
  50. "type" => 2,
  51. "initial" => strtoupper(substr($resp['link'][$k], 0, 1)),
  52. ]);
  53. } else {
  54. $cate->insertGetId([
  55. "pinyin" => $resp['link'][$k],
  56. "name" => $v,
  57. "type" => 2,
  58. "initial" => strtoupper(substr($resp['link'][$k], 0, 1)),
  59. ]);
  60. }
  61. }
  62. return json($resp);
  63. }
  64. protected function getAuthor()
  65. {
  66. $ql = QueryList::getInstance()->get("https://www.gushiji.cc/shiren/")->rules([
  67. "name" => ['.simple-people>li','texts'],
  68. "link" => ['.simple-people>li>a','attrs(href)'],
  69. "avatar" => ['.simple-people>li>a>img','attrs(src)'],
  70. "page" => ['#pagenum>li.info','text'],
  71. ])->query()->getData(function ($item,$key){
  72. foreach ($item['avatar'] as $k=>$v){
  73. if (empty($v)){
  74. $v = "/skin/images/nopicture.jpg";
  75. }
  76. $item['avatar'][$k] = "https://www.gushiji.cc".$v;
  77. }
  78. foreach ($item['link'] as $k=>$v){
  79. $link = explode("/",$v);
  80. $item['link'][$k] = $link[2]??'';
  81. }
  82. $poem = [];
  83. foreach ($item['name'] as $k=>$v){
  84. preg_match('/([^()()]+)[((](\d+)[))]/u', $v, $match);
  85. $item['name'][$k] = $match[1]??'';
  86. $poem[$k] = $match[2]??0;
  87. }
  88. preg_match_all('/\d+/', $item['page'], $matches);
  89. $item['page'] = $matches[0][0] ?? 0;
  90. $item['total'] = $matches[0][1] ?? 0;
  91. $item['poem'] = $poem;
  92. return $item;
  93. });
  94. return $ql->all();
  95. }
  96. public function view(Request $request)
  97. {
  98. return view('index/view', ['name' => 'webman']);
  99. }
  100. public function json(Request $request)
  101. {
  102. return json(['code' => 0, 'msg' => 'ok']);
  103. }
  104. }