| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- <?php
- class yep{
-
- public function __construct(){
-
- include "lib/backend.php";
- $this->backend = new backend("yep");
-
- include "lib/fuckhtml.php";
- $this->fuckhtml = new fuckhtml();
- }
-
- public function getfilters($page){
-
- return [
- "lang" => [
- "display" => "Language",
- "option" => [
- "any" => "Any language",
- "aa" => "Afar",
- "ab" => "Abkhazian",
- "ae" => "Avestan",
- "af" => "Afrikaans",
- "ak" => "Akan",
- "am" => "Amharic",
- "an" => "Aragonese",
- "ar" => "Arabic",
- "as" => "Assamese",
- "av" => "Avaric",
- "ay" => "Aymara",
- "az" => "Azerbaijani",
- "ba" => "Bashkir",
- "be" => "Belarusian",
- "bg" => "Bulgarian",
- "bh" => "Bihari",
- "bi" => "Bislama",
- "bm" => "Bambara",
- "bn" => "Bengali",
- "bo" => "Tibetan",
- "br" => "Breton",
- "bs" => "Bosnian",
- "ca" => "Catalan",
- "ce" => "Chechen",
- "ch" => "Chamorro",
- "co" => "Corsican",
- "cr" => "Cree",
- "cs" => "Czech",
- "cu" => "Church Slavic",
- "cv" => "Chuvash",
- "cy" => "Welsh",
- "da" => "Danish",
- "de" => "German",
- "dv" => "Divehi",
- "dz" => "Dzongkha",
- "ee" => "Ewe",
- "el" => "Greek",
- "en" => "English",
- "eo" => "Esperanto",
- "es" => "Spanish",
- "et" => "Estonian",
- "eu" => "Basque",
- "fa" => "Persian",
- "ff" => "Fulah",
- "fi" => "Finnish",
- "fj" => "Fijian",
- "fo" => "Faroese",
- "fr" => "French",
- "fy" => "Western Frisian",
- "ga" => "Irish",
- "gd" => "Scottish Gaelic",
- "gl" => "Galician",
- "gn" => "Guarani",
- "gu" => "Gujarati",
- "gv" => "Manx",
- "ha" => "Hausa",
- "he" => "Hebrew",
- "hi" => "Hindi",
- "ho" => "Hiri Motu",
- "hr" => "Croatian",
- "ht" => "Haitian",
- "hu" => "Hungarian",
- "hy" => "Armenian",
- "hz" => "Herero",
- "ia" => "Interlingua",
- "id" => "Indonesian",
- "ie" => "Interlingue",
- "ig" => "Igbo",
- "ii" => "Sichuan Yi",
- "ik" => "Inupiaq",
- "io" => "Ido",
- "is" => "Icelandic",
- "it" => "Italian",
- "iu" => "Inuktitut",
- "ja" => "Japanese",
- "jv" => "Javanese",
- "ka" => "Georgian",
- "kg" => "Kongo",
- "ki" => "Kikuyu",
- "kj" => "Kuanyama",
- "kk" => "Kazakh",
- "kl" => "Kalaallisut",
- "km" => "Central Khmer",
- "kn" => "Kannada",
- "ko" => "Korean",
- "kr" => "Kanuri",
- "ks" => "Kashmiri",
- "ku" => "Kurdish",
- "kv" => "Komi",
- "kw" => "Cornish",
- "ky" => "Kyrgyz",
- "la" => "Latin",
- "lb" => "Luxembourgish",
- "lg" => "Ganda",
- "li" => "Limburgish",
- "ln" => "Lingala",
- "lo" => "Lao",
- "lt" => "Lithuanian",
- "lu" => "Luba-Katanga",
- "lv" => "Latvian",
- "mg" => "Malagasy",
- "mh" => "Marshallese",
- "mi" => "Maori",
- "mk" => "Macedonian",
- "ml" => "Malayalam",
- "mn" => "Mongolian",
- "mr" => "Marathi",
- "ms" => "Malay",
- "mt" => "Maltese",
- "my" => "Burmese",
- "na" => "Nauru",
- "nb" => "Norwegian Bokmål",
- "nd" => "North Ndebele",
- "ne" => "Nepali",
- "ng" => "Ndonga",
- "nl" => "Dutch",
- "nn" => "Norwegian Nynorsk",
- "no" => "Norwegian",
- "nr" => "South Ndebele",
- "nv" => "Navajo",
- "ny" => "Chichewa",
- "oc" => "Occitan",
- "oj" => "Ojibwa",
- "om" => "Oromo",
- "or" => "Oriya",
- "os" => "Ossetian",
- "pa" => "Punjabi",
- "pi" => "Pali",
- "pl" => "Polish",
- "ps" => "Pashto",
- "pt" => "Portuguese",
- "qu" => "Quechua",
- "rm" => "Romansh",
- "rn" => "Rundi",
- "ro" => "Romanian",
- "ru" => "Russian",
- "rw" => "Kinyarwanda",
- "sa" => "Sanskrit",
- "sc" => "Sardinian",
- "sd" => "Sindhi",
- "se" => "Northern Sami",
- "sg" => "Sango",
- "si" => "Sinhala",
- "sk" => "Slovak",
- "sl" => "Slovenian",
- "sm" => "Samoan",
- "sn" => "Shona",
- "so" => "Somali",
- "sq" => "Albanian",
- "sr" => "Serbian",
- "ss" => "Swati",
- "st" => "Southern Sotho",
- "su" => "Sundanese",
- "sv" => "Swedish",
- "sw" => "Swahili",
- "ta" => "Tamil",
- "te" => "Telugu",
- "tg" => "Tajik",
- "th" => "Thai",
- "ti" => "Tigrinya",
- "tk" => "Turkmen",
- "tl" => "Tagalog",
- "tn" => "Tswana",
- "to" => "Tonga",
- "tr" => "Turkish",
- "ts" => "Tsonga",
- "tt" => "Tatar",
- "tw" => "Twi",
- "ty" => "Tahitian",
- "ug" => "Uyghur",
- "uk" => "Ukrainian",
- "ur" => "Urdu",
- "uz" => "Uzbek",
- "ve" => "Venda",
- "vi" => "Vietnamese",
- "vo" => "Volapük",
- "wa" => "Walloon",
- "wo" => "Wolof",
- "xh" => "Xhosa",
- "yi" => "Yiddish",
- "yo" => "Yoruba",
- "za" => "Zhuang",
- "zh" => "Chinese",
- "zh-cn" => "Chinese (Simplified)",
- "zh-tw" => "Chinese (Traditional)",
- "zu" => "Zulu"
- ]
- ],
- "nsfw" => [
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes",
- "no" => "No"
- ]
- ]
- ];
- }
-
- private function get($proxy, $url, $get = [], $use_api = false, $post_data = null, $bearer = null){
-
- $curlproc = curl_init();
-
- if($get !== []){
- $get = http_build_query($get);
- $url .= "?" . $get;
- }
-
- curl_setopt($curlproc, CURLOPT_URL, $url);
-
- // use http2
- curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
-
- curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
-
- if($use_api){
-
- $post_data = json_encode($post_data);
-
- curl_setopt($curlproc, CURLOPT_HTTPHEADER,
- ["Content-Type: application/json",
- "Authorization: Bearer $bearer",
- "Content-Length: " . strlen($post_data)]
- );
-
- curl_setopt($curlproc, CURLOPT_POST, true);
- curl_setopt($curlproc, CURLOPT_POSTFIELDS, $post_data);
- }else{
-
- curl_setopt($curlproc, CURLOPT_HTTPHEADER,
- ["User-Agent: " . config::USER_AGENT,
- "Accept: */*",
- "Accept-Language: en-US,en;q=0.5",
- "Accept-Encoding: gzip, deflate, br, zstd",
- "Referer: https://yep.com/",
- "Origin: https://yep.com",
- "DNT: 1",
- "Connection: keep-alive",
- "Sec-Fetch-Dest: empty",
- "Sec-Fetch-Mode: cors",
- "Sec-Fetch-Site: same-site",
- "Priority: u=4",
- "TE: trailers"]
- );
- }
-
- curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
- curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
- curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
- curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
-
- $this->backend->assign_proxy($curlproc, $proxy);
-
- $data = curl_exec($curlproc);
-
- if(curl_errno($curlproc)){
-
- throw new Exception(curl_error($curlproc));
- }
-
- curl_close($curlproc);
- return $data;
- }
-
-
-
- public function web($get){
-
- if(config::YEP_USE_API){
-
- return $this->web_api($get);
- }
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- $out = [
- "status" => "ok",
- "spelling" => [
- "type" => "no_correction",
- "using" => null,
- "correction" => null
- ],
- "npt" => null,
- "answer" => [],
- "web" => [],
- "image" => [],
- "video" => [],
- "news" => [],
- "related" => []
- ];
-
- // parse filters
- $filters = [
- "limit" => 100, // wwwwwwwwwwwwwww
- "query" => $search,
- ];
-
- if($get["nsfw"] == "no"){ $filters["safeSearch"] = "moderate"; }
- if($get["lang"] != "any"){ $filters["hl"] = $get["lang"]; }
-
- try{
-
- // https://api.yep.com/search?limit=20&query=asmr
- $json =
- $this->get(
- $this->backend->get_ip(),
- "https://api.yep.com/search",
- $filters
- );
-
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch JSON");
- }
-
- $this->detect_cf($json);
-
- $json = json_decode($json, true);
- //$json = json_decode(file_get_contents("scraper/yep.json"), true);
-
- if($json === null){
-
- throw new Exception("Failed to decode JSON");
- }
-
- if(isset($json[1]["correction"])){
-
- $out["spelling"] = [
- "type" => "not_many",
- "using" => $search,
- "correction" => $json[1]["correction"][1]
- ];
- }
-
- if(isset($json[1]["results"])){
- foreach($json[1]["results"] as $item){
-
- switch(strtolower($item["type"])){
-
- case "organic":
- $sublinks = [];
-
- if(isset($item["sitelinks"]["full"])){
-
- foreach($item["sitelinks"]["full"] as $link){
-
- $sublinks[] = [
- "title" => $link["title"],
- "date" => null,
- "description" =>
- $this->titledots(
- strip_tags(
- html_entity_decode(
- $link["snippet"]
- )
- )
- ),
- "url" => $link["url"]
- ];
- }
- }
-
- $out["web"][] = [
- "title" => $item["title"],
- "description" =>
- $this->titledots(
- strip_tags(
- html_entity_decode(
- $item["snippet"]
- )
- )
- ),
- "url" => $item["url"],
- "date" => null,
- "type" => "web",
- "thumb" => [
- "url" => null,
- "ratio" => null
- ],
- "sublink" => $sublinks,
- "table" => []
- ];
- break;
- }
- }
- }
-
- return $out;
- }
-
-
- private function web_api($get){
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- $out = [
- "status" => "ok",
- "spelling" => [
- "type" => "no_correction",
- "using" => null,
- "correction" => null
- ],
- "npt" => null,
- "answer" => [],
- "web" => [],
- "image" => [],
- "video" => [],
- "news" => [],
- "related" => []
- ];
-
- // parse filters
- $filters = [
- "query" => $search,
- "limit" => 100
- ];
-
- if($get["nsfw"] == "no"){ $filters["safe_search"] = true; }
- if($get["lang"] != "any"){ $filters["language"] = [ $get["lang"] ]; }
-
- // add api key
- $key_data = $this->backend->get_key();
-
- print_r($filters);
-
- try{
-
- $json =
- $this->get(
- $this->backend->get_ip($key_data["increment"]),
- "https://platform.yep.com/api/search",
- [],
- true,
- $filters,
- $key_data["key"]
- );
-
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch JSON");
- }
-
- // should never happen
- //$this->detect_cf($json);
-
- $json = json_decode($json, true);
- //$json = json_decode(file_get_contents("scraper/yep.json"), true);
-
- if($json === null){
-
- throw new Exception("Failed to decode JSON");
- }
-
- if(isset($json["error"])){
-
- throw new Exception("Yep API returned an error: " . $json["error"]);
- }
-
- if(isset($json["errors"])){
-
- throw new Exception("Yep API returned the following errors: {$json["message"]}");
- }
-
- if(
- isset($json["success"]) &&
- $json["success"] !== true
- ){
-
- throw new Exception("Yep API returned a false-y success value");
- }
-
- if(!isset($json["results"])){
-
- throw new Exception("Yep API did not return a results object");
- }
-
- foreach($json["results"] as $item){
-
- $out["web"][] = [
- "title" => $item["title"],
- "description" => $item["description"],
- "url" => $item["url"],
- "date" => null,
- "type" => "web",
- "thumb" => [
- "url" => null,
- "ratio" => null
- ],
- "sublink" => [],
- "table" => []
- ];
- }
-
- return $out;
- }
-
-
- private function detect_cf($payload){
-
- // detect cloudflare page
- $this->fuckhtml->load($payload);
-
- if(
- count(
- $this->fuckhtml
- ->getElementsByClassName(
- "cf-wrapper",
- "div"
- )
- ) !== 0
- ){
-
- throw new Exception("Blocked by Cloudflare. Please follow curl-impersonate installation instructions");
- }
- }
-
-
- private function titledots($title){
-
- $substr = substr($title, -4);
-
- if(
- strpos($substr, "...") !== false ||
- strpos($substr, "…") !== false
- ){
-
- return trim(substr($title, 0, -4));
- }
-
- return trim($title);
- }
-
- private function unshiturl($url){
-
- $newurl = parse_url($url, PHP_URL_QUERY);
- parse_str($newurl, $newurl);
-
- if(isset($newurl["url"])){
-
- return $newurl["url"];
- }
-
- return $url;
- }
- }
|