更新全网搜功能

This commit is contained in:
Alone
2024-09-23 08:55:36 +08:00
parent 3fbf8563a0
commit d7c978614b
3 changed files with 975 additions and 1 deletions
+236
View File
@@ -0,0 +1,236 @@
<?php
namespace app\api\controller;
use think\App;
use think\facade\Request;
use app\api\QfShop;
use app\model\Source as SourceModel;
use app\model\Days as DaysModel;
class Other extends QfShop
{
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new SourceModel();
$this->cookie = Config('qfshop.quark_cookie');
}
/**
* 全网搜索 该接口仅用于微信自动回复
*
* @return void
*/
public function all_search()
{
$searchdata = input('post.');
if (empty($searchdata['title'])) {
return jerr("请输入要看的内容");
}
$title = $searchdata['title'];
$map[] = ['status', '=', 1];
$map[] = ['is_delete', '=', 0];
$map[] = ['is_time', '=', 1];
$map[] = ['title|description', 'like', '%' . trim($title) . '%'];
$urls = $this->model->where($map)->field('source_id as id, title, url')->order('update_time', 'desc')->limit(5)->select()->toArray();
if (!empty($urls)) {
// 获取所有需要更新的ID
$ids = [];
foreach ($urls as $item) {
$ids[] = $item['id'];
}
// 更新数据库中的 update_time 字段
if (!empty($ids)) {
$this->model->whereIn('source_id', $ids)->update(['update_time' => time()]);
}
return jok('临时资源获取成功',$urls);
}
$searchList = []; //查询的结果集
$datas = []; //最终数据
$num_total = 2; //最多想要几条结果
$num_success = 0;
// 处理第2个源
foreach (source2($title) as $value) {
if ($num_success >= $num_total) {
break; // 有效结果数量已达到,则跳出循环
}
// 如果 URL 不存在则新增 $value
if (!$this->urlExists($searchList, $value['url'])) {
$searchList[] = $value;
$this->processUrl($value, $num_success, $datas);
}
}
// 处理第4个源
if ($num_success < $num_total) {
foreach (source4($title) as $value) {
if ($num_success >= $num_total) {
break; // 有效结果数量已达到,则跳出循环
}
// 如果 URL 不存在则新增 $value
if (!$this->urlExists($searchList, $value['url'])) {
$searchList[] = $value;
$this->processUrl($value, $num_success, $datas);
}
}
}
// 处理第3个源
if ($num_success < $num_total) {
foreach (source3($title) as $value) {
if ($num_success >= $num_total) {
break; // 有效结果数量已达到,则跳出循环
}
// 如果 URL 不存在则新增 $value
if (!$this->urlExists($searchList, $value['url'])) {
$searchList[] = $value;
$this->processUrl($value, $num_success, $datas);
}
}
}
// 处理第1个源 第一个源放最后
if ($num_success < $num_total) {
foreach (source1($title) as $value) {
if ($num_success >= $num_total) {
break; // 有效结果数量已达到,则跳出循环
}
// 如果 URL 不存在则新增 $value
if (!$this->urlExists($searchList, $value['url'])) {
$searchList[] = $value;
$this->processUrl($value, $num_success, $datas);
}
}
}
return jok('临时资源获取成功',$datas);
}
// 检查 URL 是否已存在(忽略查询参数)
public function urlExists($searchList, $urlToCheck) {
// 解析待检查的 URL
$parsedUrlToCheck = parse_url($urlToCheck);
foreach ($searchList as $item) {
$parsedUrl = parse_url($item['url']);
// 比较 scheme, host 和 path
if ($parsedUrlToCheck['scheme'] === $parsedUrl['scheme'] &&
$parsedUrlToCheck['host'] === $parsedUrl['host'] &&
$parsedUrlToCheck['path'] === $parsedUrl['path']) {
return true;
}
}
return false;
}
/**
* 临时资源转存
*
* @return void
*/
public function processUrl($value, &$num_success, &$datas)
{
$substring = strstr($value['url'], 's/');
if ($substring === false) {
return; // 模拟 continue 行为
}
$pwd_id = substr($substring, 2); // 去除 's/' 部分
$urlData = array(
'cookie' => $this->cookie,
'url' => $value['url'],
'expired_type' => 2,
'to_pdir_fid' => '', //存入目标文件
'ad_fid' => '', //分享时带上这个文件
);
$res = curlHelper(Request::domain()."/api/open/transfer", "POST", $urlData)['body'];
$res = json_decode($res, true);
if($res['code'] !== 200){
return; // 模拟 continue 行为
}
$patterns = '/^\d+\./';
$title = preg_replace($patterns, '', $value['title']);
// 添加资源到系统中
$data["title"] =$title;
$data["url"] =$res['data']['share_url'];
$data["is_type"] = determineIsType($data["url"]);
$data["fid"] =$res['data']['fid']??'';
$data["is_time"] = 1;
$data["update_time"] = time();
$data["create_time"] = time();
$this->model->insertGetId($data);
$datas[] =$data;
$num_success++;
}
/**
* 30分钟后清除临时资源
*
* @return void
*/
public function delete_search()
{
// 搜索条件
$map[] = ['is_time', '=', 1];
$map[] = ['update_time', '<=', time() - (30 * 60)];
$abc = $this->model->where($map)->select();
$this->model->where($map)->chunk(100, function ($order) {
foreach ($order as $value) {
$deles = $value->toArray();
$filelist = [];
$filelist[] = $deles['fid'];
$urlData = array(
'action_type' => 2,
'exclude_fids' => [],
'filelist' => $filelist,
);
$urlHeader = array(
'Accept: application/json, text/plain, */*',
'Accept-Language: zh-CN,zh;q=0.9',
'content-type: application/json;charset=UTF-8',
'sec-ch-ua: "Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
'sec-ch-ua-mobile: ?0',
'sec-ch-ua-platform: "Windows"',
'sec-fetch-dest: empty',
'sec-fetch-mode: cors',
'sec-fetch-site: same-site',
'Referer: https://pan.quark.cn/',
'Referrer-Policy: strict-origin-when-cross-origin',
'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'cookie: '.$this->cookie,
);
$res = curlHelper("https://drive-pc.quark.cn/1/clouddrive/file/delete?pr=ucpro&fr=pc&uc_param_str=", "POST", json_encode($urlData), $urlHeader)['body'];
$res = json_decode($res, true);
if($res['status'] == 200){
$this->model->where('fid', $deles['fid'])->delete();
}
}
});
return jok('临时资源删除成功',$abc);
}
}
+190 -1
View File
@@ -787,4 +787,193 @@ function determineIsType($url) {
// 默认值是夸克网盘,返回 0
return 0;
}
}
}
/**
* 网络资源搜索源一
* @return array
*/
function source1($title)
{
$d = [];
return $d;
}
/**
* 网络资源搜索源二(5条线路)
* 每个线路只取第一个
* @return array
*/
function source2($title)
{
$urlDefault = "http://s.kkkob.com"; //http://s.kkkob.com
$url2 = [];
$res = curlHelper($urlDefault."/v/api/getToken", "GET")['body'];
$res = json_decode($res, true);
$token = $res['token'] ?? '';
if(empty($token)){
return $url2;
}
$urlData = array(
'name' => $title,
'token' => $token
);
$urlHeader = array('Content-Type: application/json');
// 定义正则表达式模式
$pattern = '/https:\/\/pan\.quark\.cn\/[^\s]*/';
//线路2
$res = curlHelper($urlDefault."/v/api/getJuzi", "POST", json_encode($urlData), $urlHeader)['body'];
$res = json_decode($res, true);
if (!empty($res['list'] ?? [])) {
foreach ($res['list'] as $key => $value) {
if(preg_match($pattern, $value['answer'], $matches)){
// 匹配成功,$matches[0] 包含了匹配到的链接
$link = $matches[0];
$url2[] = [
'title' => preg_replace('/\s*[\(]?(夸克)?[\)]?\s*/u', '', $value['question']),
'url' => $link
];
break;
}
}
}
if(!empty($url2)){
return $url2;
}
//线路4
$res = curlHelper($urlDefault."/v/api/getXiaoyu", "POST", json_encode($urlData), $urlHeader)['body'];
$res = json_decode($res, true);
if (!empty($res['list'] ?? [])) {
foreach ($res['list'] as $key => $value) {
if(preg_match($pattern, $value['answer'], $matches)){
// 匹配成功,$matches[0] 包含了匹配到的链接
$link = $matches[0];
$url2[] = [
'title' => preg_replace('/\s*[\(]?(夸克)?[\)]?\s*/u', '', $value['question']),
'url' => $link
];
break;
}
}
}
// //线路1
// $res = curlHelper($urlDefault."/v/api/search", "POST", json_encode($urlData), $urlHeader)['body'];
// $res = json_decode($res, true);
// if (!empty($res['list'] ?? [])) {
// $item = $res['list'][0];
// // 使用正则表达式进行匹配
// if (preg_match($pattern, $item['answer'], $matches)) {
// // 匹配成功,$matches[0] 包含了匹配到的链接
// $link = $matches[0];
// $url2[] = [
// 'title' => '①'.preg_replace('/\s*[\(]?(夸克)?[\)]?\s*/u', '', $item['question']),
// 'url' => $link
// ];
// }
// }
return $url2;
}
/**
* 网络资源搜索源三
* @return array
*/
function source3($title)
{
$url3 = [];
$url = 'https://www.qileso.com/tag/quark?s='.$title;
$dom = getDom($url);
$finder = new DomXPath($dom);
// 查询class值为list-group post-list mt-3的元素
$nodes =$finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' list-group post-list mt-3 ')]//a");
if ($nodes->length > 0) {
$firstNode =$nodes->item(0);
$href = $firstNode->getAttribute('href');
$dom = getDom($href);
$finder = new DomXPath($dom);
// 查询包含特定前缀的href属性的所有<a>标签
$nodes =$finder->query("//@*[starts-with(., 'https://pan.quark.cn/s/')]");
if ($nodes->length > 0) {
$firstNode =$nodes->item(0);
$value = $firstNode->value;
// 查询<title>元素
$nodes =$finder->query("/html/head/title");
if ($nodes->length > 0) {
$titleNode =$nodes->item(0);
$titleText =$titleNode->textContent;
// 去掉 " - 奇乐搜" 部分
$title = preg_replace('/ - 奇乐搜|网盘|夸克/', '', $titleText);
}
$url3[] = [
'title' => '②'.$title,
'url' => $value
];
}
}
return $url3;
}
/**
* 网络资源搜索源四
* @return array
*/
function source4($title)
{
$url = 'https://www.pansearch.me/search?keyword='.urlencode($title).'&pan=quark';
$dom = getDom($url);
$finder = new DomXPath($dom);
// 使用 XPath 查询选择具有特定类名的元素
$nodes =$finder->query('//div[contains(concat(" ", normalize-space(@class), " "), " whitespace-pre-wrap ") and contains(concat(" ", normalize-space(@class), " "), " break-all ")]');
$results = [];
foreach ($nodes as $node) {
// 获取元素的内容,包括其子元素
$content = $node->textContent;
// Initialize an associative array to store parsed data
$parsedItem = [
'title' => '',
'url' => ''
];
// Use regular expressions to extract the title and url
if (preg_match('/名称:(.*?)\n\n描述:/s', $content, $titleMatch)) {
$parsedItem['title'] = '「推荐」'.trim($titleMatch[1]);
}
if (preg_match('/链接:(https:\/\/pan\.quark\.cn\/s\/[a-zA-Z0-9]+)/', $content, $urlMatch)) {
$parsedItem['url'] = trim($urlMatch[1]);
}
if ($parsedItem['title'] && $parsedItem['url']) {
$results[] = $parsedItem;
}
if (count($results) >= 3) {
break;
}
}
return $results;
}