yep.php 15 KB

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