yep.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. class yep{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("yep");
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. }
  9. public function getfilters($page){
  10. return [
  11. "country" => [
  12. "display" => "Country",
  13. "option" => [
  14. "all" => "All regions",
  15. "af" => "Afghanistan",
  16. "al" => "Albania",
  17. "dz" => "Algeria",
  18. "as" => "American Samoa",
  19. "ad" => "Andorra",
  20. "ao" => "Angola",
  21. "ai" => "Anguilla",
  22. "ag" => "Antigua and Barbuda",
  23. "ar" => "Argentina",
  24. "am" => "Armenia",
  25. "aw" => "Aruba",
  26. "au" => "Australia",
  27. "at" => "Austria",
  28. "az" => "Azerbaijan",
  29. "bs" => "Bahamas",
  30. "bh" => "Bahrain",
  31. "bd" => "Bangladesh",
  32. "bb" => "Barbados",
  33. "by" => "Belarus",
  34. "be" => "Belgium",
  35. "bz" => "Belize",
  36. "bj" => "Benin",
  37. "bt" => "Bhutan",
  38. "bo" => "Bolivia",
  39. "ba" => "Bosnia and Herzegovina",
  40. "bw" => "Botswana",
  41. "br" => "Brazil",
  42. "bn" => "Brunei Darussalam",
  43. "bg" => "Bulgaria",
  44. "bf" => "Burkina Faso",
  45. "bi" => "Burundi",
  46. "cv" => "Cabo Verde",
  47. "kh" => "Cambodia",
  48. "cm" => "Cameroon",
  49. "ca" => "Canada",
  50. "ky" => "Cayman Islands",
  51. "cf" => "Central African Republic",
  52. "td" => "Chad",
  53. "cl" => "Chile",
  54. "cn" => "China",
  55. "co" => "Colombia",
  56. "cg" => "Congo",
  57. "cd" => "Congo, Democratic Republic",
  58. "ck" => "Cook Islands",
  59. "cr" => "Costa Rica",
  60. "hr" => "Croatia",
  61. "cu" => "Cuba",
  62. "cy" => "Cyprus",
  63. "cz" => "Czechia",
  64. "ci" => "Côte d'Ivoire",
  65. "dk" => "Denmark",
  66. "dj" => "Djibouti",
  67. "dm" => "Dominica",
  68. "do" => "Dominican Republic",
  69. "ec" => "Ecuador",
  70. "eg" => "Egypt",
  71. "sv" => "El Salvador",
  72. "gq" => "Equatorial Guinea",
  73. "ee" => "Estonia",
  74. "et" => "Ethiopia",
  75. "fo" => "Faroe Islands",
  76. "fj" => "Fiji",
  77. "fi" => "Finland",
  78. "fr" => "France",
  79. "gf" => "French Guiana",
  80. "pf" => "French Polynesia",
  81. "ga" => "Gabon",
  82. "gm" => "Gambia",
  83. "ge" => "Georgia",
  84. "de" => "Germany",
  85. "gh" => "Ghana",
  86. "gi" => "Gibraltar",
  87. "gr" => "Greece",
  88. "gl" => "Greenland",
  89. "gd" => "Grenada",
  90. "gp" => "Guadeloupe",
  91. "gu" => "Guam",
  92. "gt" => "Guatemala",
  93. "gg" => "Guernsey",
  94. "gn" => "Guinea",
  95. "gy" => "Guyana",
  96. "ht" => "Haiti",
  97. "hn" => "Honduras",
  98. "hk" => "Hong Kong",
  99. "hu" => "Hungary",
  100. "is" => "Iceland",
  101. "in" => "India",
  102. "id" => "Indonesia",
  103. "iq" => "Iraq",
  104. "ie" => "Ireland",
  105. "im" => "Isle of Man",
  106. "il" => "Israel",
  107. "it" => "Italy",
  108. "jm" => "Jamaica",
  109. "jp" => "Japan",
  110. "je" => "Jersey",
  111. "jo" => "Jordan",
  112. "kz" => "Kazakhstan",
  113. "ke" => "Kenya",
  114. "ki" => "Kiribati",
  115. "kw" => "Kuwait",
  116. "kg" => "Kyrgyzstan",
  117. "la" => "Lao People's Democratic Republic",
  118. "lv" => "Latvia",
  119. "lb" => "Lebanon",
  120. "ls" => "Lesotho",
  121. "ly" => "Libya",
  122. "li" => "Liechtenstein",
  123. "lt" => "Lithuania",
  124. "lu" => "Luxembourg",
  125. "mk" => "Macedonia",
  126. "mg" => "Madagascar",
  127. "mw" => "Malawi",
  128. "my" => "Malaysia",
  129. "mv" => "Maldives",
  130. "ml" => "Mali",
  131. "mt" => "Malta",
  132. "mq" => "Martinique",
  133. "mr" => "Mauritania",
  134. "mu" => "Mauritius",
  135. "yt" => "Mayotte",
  136. "mx" => "Mexico",
  137. "fm" => "Micronesia, Federated States of",
  138. "md" => "Moldova",
  139. "mc" => "Monaco",
  140. "mn" => "Mongolia",
  141. "me" => "Montenegro",
  142. "ms" => "Montserrat",
  143. "ma" => "Morocco",
  144. "mz" => "Mozambique",
  145. "mm" => "Myanmar",
  146. "na" => "Namibia",
  147. "nr" => "Nauru",
  148. "np" => "Nepal",
  149. "nl" => "Netherlands",
  150. "nc" => "New Caledonia",
  151. "nz" => "New Zealand",
  152. "ni" => "Nicaragua",
  153. "ne" => "Niger",
  154. "ng" => "Nigeria",
  155. "nu" => "Niue",
  156. "no" => "Norway",
  157. "om" => "Oman",
  158. "pk" => "Pakistan",
  159. "ps" => "Palestine, State of",
  160. "pa" => "Panama",
  161. "pg" => "Papua New Guinea",
  162. "py" => "Paraguay",
  163. "pe" => "Peru",
  164. "ph" => "Philippines",
  165. "pn" => "Pitcairn",
  166. "pl" => "Poland",
  167. "pt" => "Portugal",
  168. "pr" => "Puerto Rico",
  169. "qa" => "Qatar",
  170. "ro" => "Romania",
  171. "ru" => "Russian Federation",
  172. "rw" => "Rwanda",
  173. "re" => "Réunion",
  174. "sh" => "Saint Helena",
  175. "kn" => "Saint Kitts and Nevis",
  176. "lc" => "Saint Lucia",
  177. "vc" => "Saint Vincent and the Grenadines",
  178. "ws" => "Samoa",
  179. "sm" => "San Marino",
  180. "st" => "Sao Tome and Principe",
  181. "sa" => "Saudi Arabia",
  182. "sn" => "Senegal",
  183. "rs" => "Serbia",
  184. "sc" => "Seychelles",
  185. "sl" => "Sierra Leone",
  186. "sg" => "Singapore",
  187. "sk" => "Slovakia",
  188. "si" => "Slovenia",
  189. "sb" => "Solomon Islands",
  190. "so" => "Somalia",
  191. "kr" => "Sourth Korea",
  192. "za" => "South Africa",
  193. "es" => "Spain",
  194. "lk" => "Sri Lanka",
  195. "sr" => "Suriname",
  196. "se" => "Sweden",
  197. "ch" => "Switzerland",
  198. "tw" => "Taiwan",
  199. "tj" => "Tajikistan",
  200. "tz" => "Tanzania",
  201. "th" => "Thailand",
  202. "tl" => "Timor-Leste",
  203. "tg" => "Togo",
  204. "tk" => "Tokelau",
  205. "to" => "Tonga",
  206. "tt" => "Trinidad and Tobago",
  207. "tn" => "Tunisia",
  208. "tr" => "Turkey",
  209. "tm" => "Turkmenistan",
  210. "ug" => "Uganda",
  211. "ua" => "Ukraine",
  212. "ae" => "United Arab Emirates",
  213. "gb" => "United Kingdom",
  214. "us" => "United States",
  215. "uy" => "Uruguay",
  216. "uz" => "Uzbekistan",
  217. "vu" => "Vanuatu",
  218. "ve" => "Venezuela",
  219. "vn" => "Vietnam",
  220. "vg" => "Virgin Islands, British",
  221. "vi" => "Virgin Islands, U.S.",
  222. "ye" => "Yemen",
  223. "zm" => "Zambia",
  224. "zw" => "Zimbabwe"
  225. ]
  226. ],
  227. "nsfw" => [
  228. "display" => "NSFW",
  229. "option" => [
  230. "yes" => "Yes",
  231. "maybe" => "Maybe",
  232. "no" => "No"
  233. ]
  234. ]
  235. ];
  236. }
  237. private function get($proxy, $url, $get = []){
  238. $curlproc = curl_init();
  239. if($get !== []){
  240. $get = http_build_query($get);
  241. $url .= "?" . $get;
  242. }
  243. curl_setopt($curlproc, CURLOPT_URL, $url);
  244. // use http2
  245. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  246. // set ciphers
  247. // aes_128_gcm_sha_256,chacha20_poly1305_sha_256,aes_256_gcm_sha_384,ecdhe_ecdsa_aes_128_gcm_sha_256,ecdhe_rsa_aes_128_gcm_sha_256,ecdhe_ecdsa_chacha20_poly1305_sha_256,ecdhe_rsa_chacha20_poly1305_sha_256,ecdhe_ecdsa_aes_256_gcm_sha_384,ecdhe_rsa_aes_256_gcm_sha_384,ecdhe_ecdsa_aes_256_sha,ecdhe_ecdsa_aes_128_sha,ecdhe_rsa_aes_128_sha,ecdhe_rsa_aes_256_sha,rsa_aes_128_gcm_sha_256,rsa_aes_256_gcm_sha_384,rsa_aes_128_sha,rsa_aes_256_sha
  248. /*
  249. curl_setopt(
  250. $curlproc,
  251. CURLOPT_SSL_CIPHER_LIST,
  252. "aes_128_gcm_sha_256,chacha20_poly1305_sha_256,aes_256_gcm_sha_384,ecdhe_ecdsa_aes_128_gcm_sha_256,ecdhe_rsa_aes_128_gcm_sha_256,ecdhe_ecdsa_chacha20_poly1305_sha_256,ecdhe_rsa_chacha20_poly1305_sha_256,ecdhe_ecdsa_aes_256_gcm_sha_384,ecdhe_rsa_aes_256_gcm_sha_384,ecdhe_ecdsa_aes_256_sha,ecdhe_ecdsa_aes_128_sha,ecdhe_rsa_aes_128_sha,ecdhe_rsa_aes_256_sha,rsa_aes_128_gcm_sha_256,rsa_aes_256_gcm_sha_384,rsa_aes_128_sha,rsa_aes_256_sha"
  253. );*/
  254. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  255. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  256. ["User-Agent: " . config::USER_AGENT,
  257. "Accept: */*",
  258. "Accept-Language: en-US,en;q=0.5",
  259. "Accept-Encoding: gzip, deflate, br, zstd",
  260. "Referer: https://yep.com/",
  261. "Origin: https://yep.com",
  262. "DNT: 1",
  263. "Connection: keep-alive",
  264. "Sec-Fetch-Dest: empty",
  265. "Sec-Fetch-Mode: cors",
  266. "Sec-Fetch-Site: same-site",
  267. "Priority: u=4",
  268. "TE: trailers"]
  269. );
  270. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  271. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  272. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  273. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  274. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  275. $this->backend->assign_proxy($curlproc, $proxy);
  276. $data = curl_exec($curlproc);
  277. if(curl_errno($curlproc)){
  278. throw new Exception(curl_error($curlproc));
  279. }
  280. curl_close($curlproc);
  281. return $data;
  282. }
  283. public function web($get){
  284. $search = $get["s"];
  285. if(strlen($search) === 0){
  286. throw new Exception("Search term is empty!");
  287. }
  288. $country = $get["country"];
  289. $nsfw = $get["nsfw"];
  290. switch($nsfw){
  291. case "yes": $nsfw = "off"; break;
  292. case "maybe": $nsfw = "moderate"; break;
  293. case "no": $nsfw = "strict"; break;
  294. }
  295. $out = [
  296. "status" => "ok",
  297. "spelling" => [
  298. "type" => "no_correction",
  299. "using" => null,
  300. "correction" => null
  301. ],
  302. "npt" => null,
  303. "answer" => [],
  304. "web" => [],
  305. "image" => [],
  306. "video" => [],
  307. "news" => [],
  308. "related" => []
  309. ];
  310. try{
  311. // https://api.yep.com/fs/2/search?client=web&gl=CA&no_correct=false&q=undefined+variable+javascript&safeSearch=off&type=web
  312. $json =
  313. $this->get(
  314. $this->backend->get_ip(),
  315. "https://api.yep.com/fs/2/search",
  316. [
  317. "client" => "web",
  318. "gl" => $country == "all" ? $country : strtoupper($country),
  319. "limit" => "99999",
  320. "no_correct" => "false",
  321. "q" => $search,
  322. "safeSearch" => $nsfw,
  323. "type" => "web"
  324. ]
  325. );
  326. }catch(Exception $error){
  327. throw new Exception("Failed to fetch JSON");
  328. }
  329. $this->detect_cf($json);
  330. $json = json_decode($json, true);
  331. //$json = json_decode(file_get_contents("scraper/yep.json"), true);
  332. if($json === null){
  333. throw new Exception("Failed to decode JSON");
  334. }
  335. if(isset($json[1]["correction"])){
  336. $out["spelling"] = [
  337. "type" => "not_many",
  338. "using" => $search,
  339. "correction" => $json[1]["correction"][1]
  340. ];
  341. }
  342. if(isset($json[1]["results"])){
  343. foreach($json[1]["results"] as $item){
  344. switch(strtolower($item["type"])){
  345. case "organic":
  346. $sublinks = [];
  347. if(isset($item["sitelinks"]["full"])){
  348. foreach($item["sitelinks"]["full"] as $link){
  349. $sublinks[] = [
  350. "title" => $link["title"],
  351. "date" => null,
  352. "description" =>
  353. $this->titledots(
  354. strip_tags(
  355. html_entity_decode(
  356. $link["snippet"]
  357. )
  358. )
  359. ),
  360. "url" => $link["url"]
  361. ];
  362. }
  363. }
  364. $out["web"][] = [
  365. "title" => $item["title"],
  366. "description" =>
  367. $this->titledots(
  368. strip_tags(
  369. html_entity_decode(
  370. $item["snippet"]
  371. )
  372. )
  373. ),
  374. "url" => $item["url"],
  375. "date" => strtotime($item["first_seen"]),
  376. "type" => "web",
  377. "thumb" => [
  378. "url" => null,
  379. "ratio" => null
  380. ],
  381. "sublink" => $sublinks,
  382. "table" => []
  383. ];
  384. break;
  385. }
  386. }
  387. }
  388. if(isset($json[1]["featured_news"])){
  389. foreach($json[1]["featured_news"] as $news){
  390. $out["news"][] = [
  391. "title" => $news["title"],
  392. "description" =>
  393. $this->titledots(
  394. strip_tags(
  395. html_entity_decode(
  396. $news["snippet"]
  397. )
  398. )
  399. ),
  400. "date" => strtotime($news["first_seen"]),
  401. "thumb" =>
  402. isset($news["img"]) ?
  403. [
  404. "url" => $this->unshiturl($news["img"]),
  405. "ratio" => "16:9"
  406. ] :
  407. [
  408. "url" => null,
  409. "ratio" => null
  410. ],
  411. "url" => $news["url"]
  412. ];
  413. }
  414. }
  415. if(isset($json[1]["featured_images"])){
  416. foreach($json[1]["featured_images"] as $image){
  417. if(
  418. $image["width"] !== 0 &&
  419. $image["height"] !== 0
  420. ){
  421. $thumb_width = $image["width"] >= 260 ? 260 : $image["width"];
  422. $thumb_height = ceil($image["height"] * ($thumb_width / $image["width"]));
  423. $width = $image["width"];
  424. $height = $image["height"];
  425. }else{
  426. $thumb_width = null;
  427. $thumb_height = null;
  428. $width = null;
  429. $height = null;
  430. }
  431. $out["image"][] = [
  432. "title" => $image["title"],
  433. "source" => [
  434. [
  435. "url" => $image["image_id"],
  436. "width" => $width,
  437. "height" => $height
  438. ],
  439. [
  440. "url" => $image["src"],
  441. "width" => $thumb_width,
  442. "height" => $thumb_height
  443. ]
  444. ],
  445. "url" => $image["host_page"]
  446. ];
  447. }
  448. }
  449. return $out;
  450. }
  451. public function image($get){
  452. $search = $get["s"];
  453. if(strlen($search) === 0){
  454. throw new Exception("Search term is empty!");
  455. }
  456. $country = $get["country"];
  457. $nsfw = $get["nsfw"];
  458. switch($nsfw){
  459. case "yes": $nsfw = "off"; break;
  460. case "maybe": $nsfw = "moderate"; break;
  461. case "no": $nsfw = "strict"; break;
  462. }
  463. $out = [
  464. "status" => "ok",
  465. "npt" => null,
  466. "image" => []
  467. ];
  468. try{
  469. $json =
  470. $this->get(
  471. $this->backend->get_ip(), // no nextpage!
  472. "https://api.yep.com/fs/2/search",
  473. [
  474. "client" => "web",
  475. "gl" => $country == "all" ? $country : strtoupper($country),
  476. "no_correct" => "false",
  477. "q" => $search,
  478. "safeSearch" => $nsfw,
  479. "type" => "images"
  480. ]
  481. );
  482. }catch(Exception $error){
  483. throw new Exception("Failed to fetch JSON");
  484. }
  485. $this->detect_cf($json);
  486. $json = json_decode($json, true);
  487. if($json === null){
  488. throw new Exception("Failed to decode JSON");
  489. }
  490. if(isset($json[1]["results"])){
  491. foreach($json[1]["results"] as $item){
  492. if(
  493. $item["width"] !== 0 &&
  494. $item["height"] !== 0
  495. ){
  496. $thumb_width = $item["width"] >= 260 ? 260 : $item["width"];
  497. $thumb_height = ceil($item["height"] * ($thumb_width / $item["width"]));
  498. $width = $item["width"];
  499. $height = $item["height"];
  500. }else{
  501. $thumb_width = null;
  502. $thumb_height = null;
  503. $width = null;
  504. $height = null;
  505. }
  506. $out["image"][] = [
  507. "title" => $item["title"],
  508. "source" => [
  509. [
  510. "url" => $item["image_id"],
  511. "width" => $width,
  512. "height" => $height
  513. ],
  514. [
  515. "url" => $item["src"],
  516. "width" => $thumb_width,
  517. "height" => $thumb_height
  518. ]
  519. ],
  520. "url" => $item["host_page"]
  521. ];
  522. }
  523. }
  524. return $out;
  525. }
  526. public function news($get){
  527. $search = $get["s"];
  528. if(strlen($search) === 0){
  529. throw new Exception("Search term is empty!");
  530. }
  531. $country = $get["country"];
  532. $nsfw = $get["nsfw"];
  533. switch($nsfw){
  534. case "yes": $nsfw = "off"; break;
  535. case "maybe": $nsfw = "moderate"; break;
  536. case "no": $nsfw = "strict"; break;
  537. }
  538. $out = [
  539. "status" => "ok",
  540. "npt" => null,
  541. "news" => []
  542. ];
  543. try{
  544. // https://api.yep.com/fs/2/search?client=web&gl=CA&no_correct=false&q=undefined+variable+javascript&safeSearch=off&type=web
  545. $json =
  546. $this->get(
  547. $this->backend->get_ip(),
  548. "https://api.yep.com/fs/2/search",
  549. [
  550. "client" => "web",
  551. "gl" => $country == "all" ? $country : strtoupper($country),
  552. "limit" => "99999",
  553. "no_correct" => "false",
  554. "q" => $search,
  555. "safeSearch" => $nsfw,
  556. "type" => "news"
  557. ]
  558. );
  559. }catch(Exception $error){
  560. throw new Exception("Failed to fetch JSON");
  561. }
  562. $this->detect_cf($json);
  563. $json = json_decode($json, true);
  564. //$json = json_decode(file_get_contents("scraper/yep.json"), true);
  565. if($json === null){
  566. throw new Exception("Failed to decode JSON");
  567. }
  568. if(isset($json[1]["results"])){
  569. foreach($json[1]["results"] as $item){
  570. $out["news"][] = [
  571. "title" => $item["title"],
  572. "author" => null,
  573. "description" =>
  574. $this->titledots(
  575. strip_tags(
  576. html_entity_decode(
  577. $item["snippet"]
  578. )
  579. )
  580. ),
  581. "date" => strtotime($item["first_seen"]),
  582. "thumb" =>
  583. isset($item["img"]) ?
  584. [
  585. "url" => $this->unshiturl($item["img"]),
  586. "ratio" => "16:9"
  587. ] :
  588. [
  589. "url" => null,
  590. "ratio" => null
  591. ],
  592. "url" => $item["url"]
  593. ];
  594. }
  595. }
  596. return $out;
  597. }
  598. private function detect_cf($payload){
  599. // detect cloudflare page
  600. $this->fuckhtml->load($payload);
  601. if(
  602. count(
  603. $this->fuckhtml
  604. ->getElementsByClassName(
  605. "cf-wrapper",
  606. "div"
  607. )
  608. ) !== 0
  609. ){
  610. throw new Exception("Blocked by Cloudflare. Please follow curl-impersonate installation instructions");
  611. }
  612. }
  613. private function titledots($title){
  614. $substr = substr($title, -4);
  615. if(
  616. strpos($substr, "...") !== false ||
  617. strpos($substr, "…") !== false
  618. ){
  619. return trim(substr($title, 0, -4));
  620. }
  621. return trim($title);
  622. }
  623. private function unshiturl($url){
  624. $newurl = parse_url($url, PHP_URL_QUERY);
  625. parse_str($newurl, $newurl);
  626. if(isset($newurl["url"])){
  627. return $newurl["url"];
  628. }
  629. return $url;
  630. }
  631. }