1
0

coccoc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. class coccoc{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("coccoc");
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. }
  9. private function get($proxy, $url, $get = []){
  10. $curlproc = curl_init();
  11. if($get !== []){
  12. $get = http_build_query($get);
  13. $url .= "?" . $get;
  14. }
  15. curl_setopt($curlproc, CURLOPT_URL, $url);
  16. // http2 bypass
  17. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  18. curl_setopt($curlproc, CURLOPT_HTTPHEADER, [
  19. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  20. "Accept-Language: en-US,en;q=0.5",
  21. "Accept-Encoding: gzip, deflate, br, zstd",
  22. "DNT: 1",
  23. "Sec-GPC: 1",
  24. "Connection: keep-alive",
  25. //"Cookie: _contentAB_15040_vi=V-06_01; split_test_search=new_search; uid=L_bauXyZBY1B; vid=uCVQJQSTgb9QGT3o; ls=1753742684; serp_version=29223843/7621a70; savedS=direct",
  26. "Upgrade-Insecure-Requests: 1",
  27. "Sec-Fetch-Dest: document",
  28. "Sec-Fetch-Mode: navigate",
  29. "Sec-Fetch-Site: cross-site",
  30. "Priority: u=0, i"
  31. ]);
  32. $this->backend->assign_proxy($curlproc, $proxy);
  33. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  34. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  35. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  36. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  37. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  38. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  39. $data = curl_exec($curlproc);
  40. if(curl_errno($curlproc)){
  41. throw new Exception(curl_error($curlproc));
  42. }
  43. curl_close($curlproc);
  44. return $data;
  45. }
  46. public function getfilters($pagetype){
  47. return [
  48. "nsfw" => [
  49. "display" => "NSFW",
  50. "option" => [
  51. "yes" => "Yes", // nsfw by default????
  52. "no" => "No" // &safe=1
  53. ]
  54. ],
  55. "time" => [
  56. "display" => "Time posted",
  57. "option" => [
  58. "any" => "Any time",
  59. "1w" => "1 week ago",
  60. "2w" => "2 weeks ago",
  61. "1m" => "1 month ago",
  62. "3m" => "3 months ago",
  63. "6m" => "6 months ago",
  64. "1Y" => "1 year ago"
  65. ]
  66. ],
  67. "filter" => [
  68. "display" => "Remove duplicates",
  69. "option" => [
  70. "no" => "No",
  71. "yes" => "Yes" // &filter=0
  72. ]
  73. ]
  74. ];
  75. }
  76. public function web($get){
  77. if($get["npt"]){
  78. [$query, $proxy] =
  79. $this->backend->get(
  80. $get["npt"],
  81. "web"
  82. );
  83. $query = json_decode($query, true);
  84. }else{
  85. $proxy = $this->backend->get_ip();
  86. $query = [
  87. "query" => $get["s"]
  88. ];
  89. // add filters
  90. if($get["nsfw"] == "no"){
  91. $query["safe"] = 1;
  92. }
  93. if($get["time"] != "any"){
  94. $query["tbs"] = $get["time"];
  95. }
  96. if($get["filter"] == "yes"){
  97. $query["filter"] = 0;
  98. }
  99. }
  100. try{
  101. $html =
  102. $this->get(
  103. $proxy,
  104. "https://coccoc.com/search",
  105. $query
  106. );
  107. }catch(Exception $error){
  108. throw new Exception("Failed to get search page");
  109. }
  110. //$html = file_get_contents("scraper/coccoc.html");
  111. $html = explode("window.composerResponse", $html, 2);
  112. if(count($html) !== 2){
  113. throw new Exception("Failed to grep window.composerResponse");
  114. }
  115. $html =
  116. json_decode(
  117. $this->fuckhtml
  118. ->extract_json(
  119. ltrim($html[1], " =")
  120. ),
  121. true
  122. );
  123. if($html === null){
  124. throw new Exception("Failed to decode JSON");
  125. }
  126. if(
  127. isset($html["captcha"]) &&
  128. (int)$html["captcha"] === 1
  129. ){
  130. throw new Exception("Coc Coc returned a Captcha");
  131. }
  132. if(!isset($html["search"]["search_results"])){
  133. throw new Exception("Coc Coc did not return a search_results object");
  134. }
  135. $out = [
  136. "status" => "ok",
  137. "spelling" => [
  138. "type" => "no_correction",
  139. "using" => null,
  140. "correction" => null
  141. ],
  142. "npt" => null,
  143. "answer" => [],
  144. "web" => [],
  145. "image" => [],
  146. "video" => [],
  147. "news" => [],
  148. "related" => []
  149. ];
  150. // word correction
  151. foreach($html["top"] as $element){
  152. if(isset($element["spellChecker"][0]["query"])){
  153. $out["spelling"] = [
  154. "type" => "not_many",
  155. "using" => $html["search"]["query"],
  156. "correction" => $element["spellChecker"][0]["query"]
  157. ];
  158. }
  159. }
  160. foreach($html["search"]["search_results"] as $result){
  161. if(isset($result["type"])){
  162. switch($result["type"]){
  163. //
  164. // Related searches
  165. //
  166. case "related_queries":
  167. $out["related"] = $result["queries"];
  168. continue 2;
  169. //
  170. // Videos
  171. //
  172. case "video_hits":
  173. foreach($result["results"] as $video){
  174. if(
  175. isset($video["image_url"]) &&
  176. !empty($video["image_url"])
  177. ){
  178. $thumb = [
  179. "ratio" => "16:9",
  180. "url" => $video["image_url"]
  181. ];
  182. }else{
  183. $thumb = [
  184. "ratio" => null,
  185. "url" => null
  186. ];
  187. }
  188. $out["video"][] = [
  189. "title" =>
  190. $this->titledots(
  191. $this->fuckhtml
  192. ->getTextContent(
  193. $video["title"]
  194. )
  195. ),
  196. "description" => null,
  197. "author" => [
  198. "name" => $video["uploader"],
  199. "url" => null,
  200. "avatar" => null
  201. ],
  202. "date" => (int)$video["date"],
  203. "duration" => (int)$video["duration"],
  204. "views" => null,
  205. "thumb" => $thumb,
  206. "url" => $video["url"]
  207. ];
  208. }
  209. continue 2;
  210. }
  211. }
  212. if(
  213. !isset($result["title"]) ||
  214. !isset($result["url"])
  215. ){
  216. // should not happen
  217. continue;
  218. }
  219. if(isset($result["rich"]["data"]["image_url"])){
  220. $thumb = [
  221. "url" => $result["rich"]["data"]["image_url"],
  222. "ratio" => "16:9"
  223. ];
  224. }else{
  225. $thumb = [
  226. "url" => null,
  227. "ratio" => null
  228. ];
  229. }
  230. $sublinks = [];
  231. if(isset($result["rich"]["data"]["linked_docs"])){
  232. foreach($result["rich"]["data"]["linked_docs"] as $sub){
  233. $sublinks[] = [
  234. "title" =>
  235. $this->titledots(
  236. $this->fuckhtml
  237. ->getTextContent(
  238. $sub["title"]
  239. )
  240. ),
  241. "description" =>
  242. $this->titledots(
  243. $this->fuckhtml
  244. ->getTextContent(
  245. $sub["content"]
  246. )
  247. ),
  248. "date" => null,
  249. "url" => $sub["url"]
  250. ];
  251. }
  252. }
  253. // get date
  254. if(isset($result["date"])){
  255. $date = (int)$result["date"];
  256. }else{
  257. $date = null;
  258. }
  259. // probe for metadata
  260. $table = [];
  261. if(isset($result["rich"]["data"]["rating"])){
  262. $table["Rating"] = $result["rich"]["data"]["rating"];
  263. if(isset($result["rich"]["data"]["num_rating"])){
  264. $table["Rating"] .= " (" . number_format($result["rich"]["data"]["num_rating"]) . " ratings)";
  265. }
  266. }
  267. if(isset($result["rich"]["data"]["views"])){
  268. $table["Views"] = number_format($result["rich"]["data"]["views"]);
  269. }
  270. if(isset($result["rich"]["data"]["duration"])){
  271. $table["Duration"] = $this->int2hms($result["rich"]["data"]["duration"]);
  272. }
  273. if(isset($result["rich"]["data"]["channel_name"])){
  274. $table["Author"] = $result["rich"]["data"]["channel_name"];
  275. }
  276. if(isset($result["rich"]["data"]["video_quality"])){
  277. $table["Quality"] = $result["rich"]["data"]["video_quality"];
  278. }
  279. if(isset($result["rich"]["data"]["category"])){
  280. $table["Category"] = $result["rich"]["data"]["category"];
  281. }
  282. $out["web"][] = [
  283. "title" =>
  284. $this->titledots(
  285. $this->fuckhtml
  286. ->getTextContent(
  287. $result["title"]
  288. )
  289. ),
  290. "description" =>
  291. $this->titledots(
  292. $this->fuckhtml
  293. ->getTextContent(
  294. $result["content"]
  295. )
  296. ),
  297. "url" => $result["url"],
  298. "date" => $date,
  299. "type" => "web",
  300. "thumb" => $thumb,
  301. "sublink" => $sublinks,
  302. "table" => $table
  303. ];
  304. }
  305. //
  306. // Get wikipedia head
  307. //
  308. if(isset($html["right"])){
  309. foreach($html["right"] as $wiki){
  310. $description = [];
  311. if(isset($wiki["short_intro"])){
  312. $description[] =
  313. [
  314. "type" => "quote",
  315. "value" => $wiki["short_intro"],
  316. ];
  317. }
  318. if(isset($wiki["intro"])){
  319. $description[] =
  320. [
  321. "type" => "text",
  322. "value" => $wiki["intro"],
  323. ];
  324. }
  325. // get table elements
  326. $table = [];
  327. if(isset($wiki["fields"])){
  328. foreach($wiki["fields"] as $element){
  329. $table[$element["title"]] = implode(", ", $element["value"]);
  330. }
  331. }
  332. // get sublinks
  333. $sublinks = [];
  334. if(isset($wiki["website"])){
  335. if(
  336. preg_match(
  337. '/^http/',
  338. $wiki["website"]
  339. ) === 0
  340. ){
  341. $sublinks["Website"] = "https://" . $wiki["website"];
  342. }else{
  343. $sublinks["Website"] = $wiki["website"];
  344. }
  345. }
  346. foreach($wiki["profiles"] as $sitename => $url){
  347. $sitename = explode("_", $sitename);
  348. $sitename = ucfirst($sitename[count($sitename) - 1]);
  349. $sublinks[$sitename] = $url;
  350. }
  351. $out["answer"][] = [
  352. "title" =>
  353. $this->titledots(
  354. $wiki["title"]
  355. ),
  356. "description" => $description,
  357. "url" => null,
  358. "thumb" => isset($wiki["image"]["contentUrl"]) ? $wiki["image"]["contentUrl"] : null,
  359. "table" => $table,
  360. "sublink" => $sublinks
  361. ];
  362. }
  363. }
  364. // get next page
  365. if((int)$html["search"]["page"] < (int)$html["search"]["max_page"]){
  366. // https://coccoc.com/composer?_=1754021153532&p=0&q=zbabduiqwhduwqhdnwq&reqid=bwcAs00q&s=direct&apiV=1
  367. // ^json endpoint, but we can just do &page=2 lol
  368. if(!isset($query["page"])){
  369. $query["page"] = 2;
  370. }else{
  371. $query["page"]++;
  372. }
  373. $out["npt"] =
  374. $this->backend
  375. ->store(
  376. json_encode($query),
  377. "web",
  378. $proxy
  379. );
  380. }
  381. return $out;
  382. }
  383. public function video($get){
  384. //$html = file_get_contents("scraper/coccoc.html");
  385. if($get["npt"]){
  386. [$query, $proxy] =
  387. $this->backend->get(
  388. $get["npt"],
  389. "videos"
  390. );
  391. $query = json_decode($query, true);
  392. }else{
  393. $proxy = $this->backend->get_ip();
  394. $query = [
  395. "query" => $get["s"],
  396. "tbm" => "vid"
  397. ];
  398. // add filters
  399. if($get["nsfw"] == "no"){
  400. $query["safe"] = 1;
  401. }
  402. if($get["time"] != "any"){
  403. $query["tbs"] = $get["time"];
  404. }
  405. if($get["filter"] == "yes"){
  406. $query["filter"] = 0;
  407. }
  408. }
  409. try{
  410. $html =
  411. $this->get(
  412. $proxy,
  413. "https://coccoc.com/search",
  414. $query
  415. );
  416. }catch(Exception $error){
  417. throw new Exception("Failed to get search page");
  418. }
  419. $html = explode("window.composerResponse", $html, 2);
  420. if(count($html) !== 2){
  421. throw new Exception("Failed to grep window.composerResponse");
  422. }
  423. $html =
  424. json_decode(
  425. $this->fuckhtml
  426. ->extract_json(
  427. ltrim($html[1], " =")
  428. ),
  429. true
  430. );
  431. if($html === null){
  432. throw new Exception("Failed to decode JSON");
  433. }
  434. $out = [
  435. "status" => "ok",
  436. "npt" => null,
  437. "video" => [],
  438. "author" => [],
  439. "livestream" => [],
  440. "playlist" => [],
  441. "reel" => []
  442. ];
  443. if(!isset($html["search_video"]["search_results"])){
  444. if(isset($html["search_video"]["error"]["title"])){
  445. if($html["search_video"]["error"]["title"] == "Không tìm thấy kết quả nào"){
  446. return $out;
  447. }
  448. throw new Exception("Coc Coc returned an error: " . $html["search_video"]["error"]["title"]);
  449. }
  450. throw new Exception("Coc Coc did not supply a search_results object");
  451. }
  452. foreach($html["search_video"]["search_results"] as $video){
  453. if(isset($video["rich"]["data"]["image_url"])){
  454. $thumb = [
  455. "ratio" => "16:9",
  456. "url" => $video["rich"]["data"]["image_url"]
  457. ];
  458. }else{
  459. $thumb = [
  460. "ratio" => null,
  461. "url" => null
  462. ];
  463. }
  464. $out["video"][] = [
  465. "title" =>
  466. $this->titledots(
  467. $this->fuckhtml
  468. ->getTextContent(
  469. $video["title"]
  470. )
  471. ),
  472. "description" =>
  473. $this->titledots(
  474. $this->fuckhtml
  475. ->getTextContent(
  476. $video["content"]
  477. )
  478. ),
  479. "author" => [
  480. "name" =>
  481. isset($video["rich"]["data"]["channel_name"]) ?
  482. $video["rich"]["data"]["channel_name"] : null,
  483. "url" => null,
  484. "avatar" => null
  485. ],
  486. "date" =>
  487. isset($video["date"]) ?
  488. $video["date"] : null,
  489. "duration" =>
  490. isset($video["rich"]["data"]["duration"]) ?
  491. (int)$video["rich"]["data"]["duration"] : null,
  492. "views" => null,
  493. "thumb" => $thumb,
  494. "url" => $video["url"]
  495. ];
  496. }
  497. // get next page
  498. if((int)$html["search_video"]["page"] < (int)$html["search_video"]["max_page"]){
  499. if(!isset($query["page"])){
  500. $query["page"] = 2;
  501. }else{
  502. $query["page"]++;
  503. }
  504. $out["npt"] =
  505. $this->backend
  506. ->store(
  507. json_encode($query),
  508. "videos",
  509. $proxy
  510. );
  511. }
  512. return $out;
  513. }
  514. private function titledots($title){
  515. return trim($title, " .\t\n\r\0\x0B…");
  516. }
  517. private function int2hms($seconds){
  518. $hours = floor($seconds / 3600);
  519. $minutes = floor(($seconds % 3600) / 60);
  520. $seconds = $seconds % 60;
  521. return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
  522. }
  523. }