Index.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\controller\apps\topSearch;
  3. use app\model\CardModel;
  4. use app\PluginsBase;
  5. use ErrorException;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. use think\facade\Cache;
  8. class Index extends PluginsBase
  9. {
  10. protected $ttl = 180;
  11. function __construct()
  12. {
  13. parent::__construct(app());
  14. $res = CardModel::config('topSearch', 'conf');
  15. if ($res) {
  16. if (isset($res['ttl'])) {
  17. $this->ttl = (int)$res['ttl'];
  18. }
  19. }
  20. }
  21. function save(): \think\response\Json
  22. {
  23. $this->getAdmin();
  24. $list = $this->request->post("conf");
  25. CardModel::saveConfigs("topSearch", ['conf'=>$list]);
  26. $this->clearRedisCache();
  27. return $this->success('保存成功');
  28. }
  29. function getConf(): \think\response\Json
  30. {
  31. $this->getAdmin();
  32. $res = CardModel::config("topSearch", "conf", false);
  33. if (!$res) {
  34. $res = [];
  35. }
  36. return $this->success('ok', $res);
  37. }
  38. function TopSearch(): \think\response\Json
  39. {
  40. $type = $this->request->get('type', 'baidu');
  41. switch ($type) {
  42. case 'baidu':
  43. return $this->baiduTopSearch();
  44. case 'bilibili':
  45. return $this->bilibili();
  46. case 'weibo':
  47. return $this->weibo();
  48. case 'zhiHu':
  49. return $this->zhiHu();
  50. case 'douyin':
  51. return $this->douyin();
  52. case 'toutiao':
  53. return $this->toutiao();
  54. }
  55. return $this->error('not type');
  56. }
  57. function zhiHu(): \think\response\Json
  58. {
  59. try {
  60. $c = Cache::get('zhiHuTopSearch');
  61. if ($c) {
  62. return $this->success('cache', $c);
  63. }
  64. } catch (ErrorException $e) {
  65. }
  66. $api = 'https://www.zhihu.com/api/v4/creators/rank/hot?domain=0&period=hour&limit=50&offset=0';
  67. $result = \Axios::http()->request('get', $api);
  68. $result = $result->getBody()->getContents();
  69. $result = json_decode($result, true);
  70. $arr = [];
  71. if (count($result['data']) > 0) {
  72. foreach ($result['data'] as $value) {
  73. $arr [] = array(
  74. 'title' => $value['question']['title'],
  75. 'hot' => $value['reaction']['pv'],
  76. 'url' => $value['question']['url']
  77. );
  78. }
  79. Cache::set('zhiHuTopSearch', $arr, $this->ttl);
  80. }
  81. return $this->success($arr);
  82. }
  83. //百度热搜
  84. public function baiduTopSearch(): \think\response\Json
  85. {
  86. try {
  87. $c = Cache::get('baiduTopSearch');
  88. if ($c) {
  89. return $this->success('cache', $c);
  90. }
  91. } catch (ErrorException $e) {
  92. }
  93. $result = \Axios::http()->request('get', 'https://top.baidu.com/api/board?tab=realtime');
  94. $result = $result->getBody()->getContents();
  95. $result = json_decode($result, true);
  96. if ($result['success']) {
  97. $result = $result['data']['cards'][0];
  98. }
  99. $arr = [];
  100. $tn = CardModel::config('topSearch', 'conf', false);
  101. if ($tn && isset($tn['baiduCode'])) {
  102. $tn = $tn['baiduCode'];
  103. } else {
  104. $tn = false;
  105. }
  106. $list = $result['content'];
  107. if (isset($result['topContent'])) {
  108. $top = $result['topContent'];
  109. if (count($top) > 0) {
  110. array_unshift($list, $top[0]);
  111. }
  112. foreach ($list as $k => $v) {
  113. $url = urlencode($v['word']);
  114. if ($tn) {
  115. $url .= '&tn=' . $tn;
  116. }
  117. $arr [] = array(
  118. 'title' => $v['word'],
  119. 'hot' => $v['hotScore'],
  120. 'url' => "https://www.baidu.com/s?wd={$url}"
  121. );
  122. Cache::set('baiduTopSearch', $arr, $this->ttl);
  123. }
  124. }
  125. return $this->success('new', $arr);
  126. }
  127. //哔哩哔哩热搜
  128. public function bilibili(): \think\response\Json
  129. {
  130. $arr = [];
  131. try {
  132. $c = Cache::get('bilibiliTopSearch');
  133. if ($c) {
  134. return $this->success('cache', $c);
  135. }
  136. $result = \Axios::http()->request('get', 'https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all', [
  137. 'headers' => [
  138. 'path' => '/x/web-interface/ranking/v2?',
  139. 'authority' => 'api.bilibili.com',
  140. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
  141. ]
  142. ]);
  143. $result = $result->getBody()->getContents();
  144. $result = json_decode($result, true);
  145. if ($result['code'] == 0) {
  146. $list = $result['data']['list'];
  147. if (count($list) > 0) {
  148. foreach ($list as $k => $v) {
  149. if ($k == 90) {
  150. break;
  151. }
  152. $arr [] = array(
  153. 'title' => $v['title'],
  154. 'hot' => $v['stat']['view'],
  155. 'url' => 'https://www.bilibili.com/video/' . $v['bvid']//$v['short_link'] ?? $v['short_link_v2']
  156. );
  157. }
  158. Cache::set('bilibiliTopSearch', $arr, $this->ttl);
  159. }
  160. }
  161. } catch (GuzzleException $e) {
  162. }
  163. return $this->success($arr);
  164. }
  165. //哔哩哔哩热搜
  166. public function weibo(): \think\response\Json
  167. {
  168. try {
  169. $c = Cache::get('weiboTopSearch');
  170. if ($c) {
  171. return $this->success('cache', $c);
  172. }
  173. } catch (ErrorException $e) {
  174. }
  175. $result = \Axios::http()->request('get', 'https://weibo.com/ajax/statuses/hot_band');
  176. $result = $result->getBody()->getContents();
  177. $result = json_decode($result, true);
  178. $arr = [];
  179. if ($result['ok'] == 1) {
  180. $list = $result['data']['band_list'];
  181. if (count($list) > 0) {
  182. foreach ($list as $k => $v) {
  183. $arr [] = array(
  184. 'title' => $v['word'],
  185. 'hot' => $v['raw_hot'] ?? $v['num'],
  186. 'url' => 'https://s.weibo.com/weiboo?q=' . $v['word']
  187. );
  188. }
  189. Cache::set('weiboTopSearch', $arr, $this->ttl);
  190. }
  191. }
  192. return $this->success($arr);
  193. }
  194. //哔哩哔哩热搜
  195. public function toutiao(): \think\response\Json
  196. {
  197. try {
  198. $c = Cache::get('toutiaoTopSearch');
  199. if ($c) {
  200. return $this->success('cache', $c);
  201. }
  202. } catch (ErrorException $e) {
  203. }
  204. $result = \Axios::http()->request('get', 'https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc');
  205. $result = $result->getBody()->getContents();
  206. $result = json_decode($result, true);
  207. $arr = [];
  208. if ($result['status'] == 'success') {
  209. $list = $result['data'];
  210. if (count($list) > 0) {
  211. foreach ($list as $k => $v) {
  212. $arr [] = array(
  213. 'title' => $v['Title'],
  214. 'hot' => $v['HotValue'],
  215. 'url' => $v['Url']
  216. );
  217. }
  218. Cache::set('toutiaoTopSearch', $arr, $this->ttl);
  219. }
  220. }
  221. return $this->success($arr);
  222. }
  223. function douyin()
  224. {
  225. try {
  226. $c = Cache::get('douyinTopSearch');
  227. if ($c) {
  228. return $this->success('cache', $c);
  229. }
  230. } catch (ErrorException $e) {
  231. }
  232. $result = \Axios::http()->request('get', 'https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/word/?reflow_source=reflow_page');
  233. $result = $result->getBody()->getContents();
  234. $result = json_decode($result, true);
  235. $arr = [];
  236. $list = $result['word_list'];
  237. if (count($list) > 0) {
  238. foreach ($list as $k => $v) {
  239. $arr [] = array(
  240. 'title' => $v['word'],
  241. 'hot' => $v['hot_value'] ?? $v['num'],
  242. 'url' => 'https://www.douyin.com/search/' . $v['word']
  243. );
  244. }
  245. Cache::set('douyinTopSearch', $arr, $this->ttl);
  246. }
  247. return $this->success($arr);
  248. }
  249. public function clearRedisCache(): \think\response\Json
  250. {
  251. Cache::delete('bilibiliTopSearch');
  252. Cache::delete('baiduTopSearch');
  253. Cache::delete('weiboTopSearch');
  254. Cache::delete('zhiHuTopSearch');
  255. Cache::delete('douyinTopSearch');
  256. return $this->success('刷新完毕');
  257. }
  258. }