123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170 |
- <?php
- class yandex{
-
-
- public function __construct(){
-
- include "lib/fuckhtml.php";
- $this->fuckhtml = new fuckhtml();
-
- include "lib/backend.php";
-
- }
-
- private function get($proxy, $url, $get = [], $nsfw){
-
- $curlproc = curl_init();
-
- if($get !== []){
- $get = http_build_query($get);
- $url .= "?" . $get;
- }
-
- curl_setopt($curlproc, CURLOPT_URL, $url);
-
- switch($nsfw){
- case "yes": $nsfw = "0"; break;
- case "maybe": $nsfw = "1"; break;
- case "no": $nsfw = "2"; break;
- }
-
- $headers =
- ["User-Agent: " . config::USER_AGENT,
- "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
- "Accept-Encoding: gzip",
- "Accept-Language: en-US,en;q=0.5",
- "DNT: 1",
- "Cookie: yp=1716337604.sp.family%3A{$nsfw}#1685406411.szm.1:1920x1080:1920x999",
- "Referer: https://yandex.com/images/search",
- "Connection: keep-alive",
- "Upgrade-Insecure-Requests: 1",
- "Sec-Fetch-Dest: document",
- "Sec-Fetch-Mode: navigate",
- "Sec-Fetch-Site: cross-site",
- "Upgrade-Insecure-Requests: 1"];
-
- curl_setopt($curlproc, CURLOPT_ENCODING, "");
- curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
-
- 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 getfilters($pagetype){
-
- switch($pagetype){
-
- case "web":
- return [
- "lang" => [
- "display" => "Language",
- "option" => [
- "any" => "Any language",
- "en" => "English",
- "ru" => "Russian",
- "be" => "Belorussian",
- "fr" => "French",
- "de" => "German",
- "id" => "Indonesian",
- "kk" => "Kazakh",
- "tt" => "Tatar",
- "tr" => "Turkish",
- "uk" => "Ukrainian"
- ]
- ],
- "newer" => [
- "display" => "Newer than",
- "option" => "_DATE"
- ],
- "older" => [
- "display" => "Older than",
- "option" => "_DATE"
- ]
- ];
- break;
-
- case "images":
- return
- [
- "nsfw" => [
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes",
- "maybe" => "Maybe",
- "no" => "No"
- ]
- ],
- "time" => [
- "display" => "Time posted",
- "option" => [
- "any" => "Any time",
- "week" => "Last week"
- ]
- ],
- "size" => [
- "display" => "Size",
- "option" => [
- "any" => "Any size",
- "small" => "Small",
- "medium" => "Medium",
- "large" => "Large",
- "wallpaper" => "Wallpaper"
- ]
- ],
- "color" => [
- "display" => "Colors",
- "option" => [
- "any" => "All colors",
- "color" => "Color images only",
- "gray" => "Black and white",
- "red" => "Red",
- "orange" => "Orange",
- "yellow" => "Yellow",
- "cyan" => "Cyan",
- "green" => "Green",
- "blue" => "Blue",
- "violet" => "Purple",
- "white" => "White",
- "black" => "Black"
- ]
- ],
- "type" => [
- "display" => "Type",
- "option" => [
- "any" => "All types",
- "photo" => "Photos",
- "clipart" => "White background",
- "lineart" => "Drawings and sketches",
- "face" => "People",
- "demotivator" => "Demotivators"
- ]
- ],
- "layout" => [
- "display" => "Layout",
- "option" => [
- "any" => "All layouts",
- "horizontal" => "Horizontal",
- "vertical" => "Vertical",
- "square" => "Square"
- ]
- ],
- "format" => [
- "display" => "Format",
- "option" => [
- "any" => "Any format",
- "jpeg" => "JPEG",
- "png" => "PNG",
- "gif" => "GIF"
- ]
- ]
- ];
- break;
-
- case "videos":
- return [
- "nsfw" => [
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes",
- "maybe" => "Maybe",
- "no" => "No"
- ]
- ],
- "time" => [
- "display" => "Time posted",
- "option" => [
- "any" => "Any time",
- "9" => "Recently"
- ]
- ],
- "duration" => [
- "display" => "Duration",
- "option" => [
- "any" => "Any duration",
- "short" => "Short"
- ]
- ]
- ];
- break;
- }
- }
-
- public function web($get){
-
- $this->backend = new backend("yandex_w");
-
-
-
-
-
-
-
- if($get["npt"]){
-
- [$npt, $proxy] = $this->backend->get($get["npt"], "web");
-
- $html =
- $this->get(
- $proxy,
- "https://yandex.com" . $npt,
- [],
- "yes"
- );
- }else{
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- $proxy = $this->backend->get_ip();
- $lang = $get["lang"];
- $older = $get["older"];
- $newer = $get["newer"];
-
- $params = [
- "text" => $search,
- "web" => "1",
- "frame" => "1",
- "searchid" => "3131712"
- ];
-
- if($lang != "any"){
-
- $params["lang"] = $lang;
- }
-
- if(
- $newer === false &&
- $older !== false
- ){
-
- $newer = 0;
- }
-
- if($newer !== false){
-
- $params["from_day"] = date("j", $newer);
- $params["from_month"] = date("n", $newer);
- $params["from_year"] = date("Y", $newer);
-
- if($older === false){
-
- $older = time();
- }
-
- $params["to_day"] = date("j", $older);
- $params["to_month"] = date("n", $older);
- $params["to_year"] = date("Y", $older);
- }
-
- try{
- $html =
- $this->get(
- $proxy,
- "https://yandex.com/search/site/",
- $params,
- "yes"
- );
- }catch(Exception $error){
-
- throw new Exception("Could not get search page");
- }
-
-
- }
-
- $out = [
- "status" => "ok",
- "spelling" => [
- "type" => "no_correction",
- "using" => null,
- "correction" => null
- ],
- "npt" => null,
- "answer" => [],
- "web" => [],
- "image" => [],
- "video" => [],
- "news" => [],
- "related" => []
- ];
-
- $this->fuckhtml->load($html);
-
-
- $npt =
- $this->fuckhtml
- ->getElementsByClassName(
- "b-pager__next",
- "a"
- );
-
- if(count($npt) !== 0){
-
- $out["npt"] =
- $this->backend->store(
- $this->fuckhtml
- ->getTextContent(
- $npt
- [0]
- ["attributes"]
- ["href"]
- ),
- "web",
- $proxy
- );
- }
-
-
- $items =
- $this->fuckhtml
- ->getElementsByClassName(
- "b-serp-item",
- "li"
- );
-
- foreach($items as $item){
-
- $this->fuckhtml->load($item);
-
- $link =
- $this->fuckhtml
- ->getElementsByClassName(
- "b-serp-item__title-link",
- "a"
- )[0];
-
- $out["web"][] = [
- "title" =>
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $link
- )
- ),
- "description" =>
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "b-serp-item__text",
- "div"
- )[0]
- )
- ),
- "url" =>
- $this->fuckhtml
- ->getTextContent(
- $link
- ["attributes"]
- ["href"]
- ),
- "date" => null,
- "type" => "web",
- "thumb" => [
- "url" => null,
- "ratio" => null
- ],
- "sublink" => [],
- "table" => []
- ];
- }
-
- return $out;
- }
-
- public function image($get){
-
- $this->backend = new backend("yandex_i");
-
- if($get["npt"]){
-
- [$request, $proxy] =
- $this->backend->get(
- $get["npt"],
- "images"
- );
-
- $request = json_decode($request, true);
-
- $nsfw = $request["nsfw"];
- unset($request["nsfw"]);
- }else{
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- $proxy = $this->backend->get_ip();
- $nsfw = $get["nsfw"];
- $time = $get["time"];
- $size = $get["size"];
- $color = $get["color"];
- $type = $get["type"];
- $layout = $get["layout"];
- $format = $get["format"];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $request = [
- "format" => "json",
- "request" => [
- "blocks" => [
- [
- "block" => "extra-content",
- "params" => (object)[],
- "version" => 2
- ],
- [
- "block" => "i-global__params:ajax",
- "params" => (object)[],
- "version" => 2
- ],
- [
- "block" => "search2:ajax",
- "params" => (object)[],
- "version" => 2
- ],
- [
- "block" => "preview__isWallpaper",
- "params" => (object)[],
- "version" => 2
- ],
- [
- "block" => "content_type_search",
- "params" => (object)[],
- "version" => 2
- ],
- [
- "block" => "serp-controller",
- "params" => (object)[],
- "version" => 2
- ],
- [
- "block" => "cookies_ajax",
- "params" => (object)[],
- "version" => 2
- ],
- [
- "block" => "advanced-search-block",
- "params" => (object)[],
- "version" => 2
- ]
- ],
- "metadata" => [
- "bundles" => [
- "lb" => "AS?(E<X120"
- ],
- "assets" => [
-
- "las" => "justifier-height=1;justifier-setheight=1;fitimages-height=1;justifier-fitincuts=1;react-with-dom=1;"
-
-
-
- ],
- "extraContent" => [
- "names" => [
- "i-react-ajax-adapter"
- ]
- ]
- ]
- ]
- ];
-
-
- if($time == "week"){
- $request["recent"] = "7D";
- }
-
- if($size != "any"){
-
- $request["isize"] = $size;
- }
-
- if($type != "any"){
-
- $request["type"] = $type;
- }
-
- if($color != "any"){
-
- $request["icolor"] = $color;
- }
-
- if($layout != "any"){
-
- $request["iorient"] = $layout;
- }
-
- if($format != "any"){
-
- $request["itype"] = $format;
- }
-
- $request["text"] = $search;
- $request["uinfo"] = "sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080";
-
- $request["request"] = json_encode($request["request"]);
- }
-
- try{
- $json = $this->get(
- $proxy,
- "https://yandex.com/images/search",
- $request,
- $nsfw,
- "yandex_i"
- );
- }catch(Exception $err){
-
- throw new Exception("Failed to get JSON");
- }
-
-
-
- $json = json_decode($json, true);
-
- if($json === null){
-
- throw new Exception("Failed to decode JSON");
- }
-
- if(
- isset($json["type"]) &&
- $json["type"] == "captcha"
- ){
-
- throw new Exception("Yandex blocked this 4get instance. Please try again in ~7 minutes.");
- }
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "image" => []
- ];
-
-
- $html = "";
- foreach($json["blocks"] as $block){
-
- $html .= $block["html"];
-
-
- if(
- isset($block["params"]["nextPageUrl"]) &&
- !empty($block["params"]["nextPageUrl"])
- ){
-
- $request["nsfw"] = $nsfw;
-
- if(isset($request["p"])){
-
- $request["p"]++;
- }else{
-
- $request["p"] = 1;
- }
-
- $out["npt"] =
- $this->backend->store(
- json_encode($request),
- "images",
- $proxy
- );
- }
- }
-
- $this->fuckhtml->load($html);
-
-
- $data = null;
-
- foreach(
- $this->fuckhtml
- ->getElementsByClassName(
- "Root",
- "div"
- ) as $div
- ){
-
- if(isset($div["attributes"]["data-state"])){
-
- $tmp = json_decode(
- $this->fuckhtml
- ->getTextContent(
- $div["attributes"]["data-state"]
- ),
- true
- );
-
- if(isset($tmp["initialState"]["serpList"])){
-
- $data = $tmp;
- break;
- }
- }
- }
-
- if($data === null){
-
- throw new Exception("Failed to extract JSON");
- }
-
- foreach($data["initialState"]["serpList"]["items"]["entities"] as $image){
-
- $title = [html_entity_decode($image["snippet"]["title"], ENT_QUOTES | ENT_HTML5)];
-
- if(isset($image["snippet"]["text"])){
-
- $title[] = html_entity_decode($image["snippet"]["text"], ENT_QUOTES | ENT_HTML5);
- }
- $tmp = [
- "title" =>
- $this->fuckhtml
- ->getTextContent(
- $this->titledots(
- implode(": ", $title)
- )
- ),
- "source" => [],
- "url" => htmlspecialchars_decode($image["snippet"]["url"])
- ];
-
-
- $tmp["source"][] = [
- "url" => htmlspecialchars_decode($image["viewerData"]["preview"][0]["url"]),
- "width" => (int)$image["viewerData"]["preview"][0]["w"],
- "height" => (int)$image["viewerData"]["preview"][0]["h"],
- ];
-
- foreach($image["viewerData"]["dups"] as $dup){
-
- $tmp["source"][] = [
- "url" => htmlspecialchars_decode($dup["url"]),
- "width" => (int)$dup["w"],
- "height" => (int)$dup["h"],
- ];
- }
-
- $tmp["source"][] = [
- "url" =>
- preg_replace(
- '/^\/\//',
- "https://",
- htmlspecialchars_decode($image["viewerData"]["thumb"]["url"])
- ),
- "width" => (int)$image["viewerData"]["thumb"]["w"],
- "height" => (int)$image["viewerData"]["thumb"]["h"]
- ];
-
- $out["image"][] = $tmp;
- }
-
- return $out;
- }
-
- public function video($get){
-
- $this->backend = new backend("yandex_v");
-
- if($get["npt"]){
-
- [$params, $proxy] =
- $this->backend->get(
- $get["npt"],
- "video"
- );
-
- $params = json_decode($params, true);
-
- $nsfw = $params["nsfw"];
- unset($params["nsfw"]);
- }else{
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- $proxy = $this->backend->get_ip();
- $nsfw = $get["nsfw"];
- $time = $get["time"];
- $duration = $get["duration"];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $params = [
- "tmpl_version" => "releases/frontend/video/v1.1168.0#8d942de0f4ebc4eb6b8f3c24ffbd1f8dbc5bbe63",
- "format" => "json",
- "request" => json_encode([
- "blocks" => [
- (object)[
- "block" => "extra-content",
- "params" => (object)[],
- "version" => 2
- ],
- (object)[
- "block" => "i-global__params:ajax",
- "params" => (object)[],
- "version" => 2
- ],
- (object)[
- "block" => "search2:ajax",
- "params" => (object)[],
- "version" => 2
- ],
- (object)[
- "block" => "vital-incut",
- "params" => (object)[],
- "version" => 2
- ],
- (object)[
- "block" => "content_type_search",
- "params" => (object)[],
- "version" => 2
- ],
- (object)[
- "block" => "serp-controller",
- "params" => (object)[],
- "version" => 2
- ],
- (object)[
- "block" => "cookies_ajax",
- "params" => (object)[],
- "version" => 2
- ]
- ],
- "metadata" => (object)[
- "bundles" => (object)[
- "lb" => "^G]!q<X120"
- ],
- "assets" => (object)[
- "las" => "react-with-dom=1;185.0=1;73.0=1;145.0=1;5a502a.0=1;32c342.0=1;b84ac8.0=1"
- ],
- "extraContent" => (object)[
- "names" => [
- "i-react-ajax-adapter"
- ]
- ]
- ]
- ]),
- "text" => $search
- ];
-
- if($duration != "any"){
-
- $params["duration"] = $duration;
- }
-
- if($time != "any"){
-
- $params["within"] = $time;
- }
- }
-
- try{
- $json =
- $this->get(
- $proxy,
- "https://yandex.com/video/search",
- $params,
- $nsfw,
- "yandex_v"
- );
- }catch(Exception $error){
-
- throw new Exception("Could not fetch JSON");
- }
-
- $json = json_decode($json, true);
-
- if($json === null){
-
- throw new Exception("Could not parse JSON");
- }
-
- if(!isset($json["blocks"])){
-
- throw new Exception("Yandex blocked this 4get instance. Please try again in 7~ minutes.");
- }
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "video" => [],
- "author" => [],
- "livestream" => [],
- "playlist" => [],
- "reel" => []
- ];
-
- $html = null;
- foreach($json["blocks"] as $block){
-
- if(isset($block["html"])){
-
- $html .= $block["html"];
- }
- }
-
- $this->fuckhtml->load($html);
-
- $div =
- $this->fuckhtml
- ->getElementsByTagName("div");
-
-
- $npt =
- $this->fuckhtml
- ->getElementsByClassName(
- "more more_direction_next i-bem",
- $div
- );
-
- if(count($npt) !== 0){
-
- $params["p"] = "1";
- $params["nsfw"] = $nsfw;
- $out["npt"] =
- $this->backend->store(
- json_encode($params),
- "video",
- $proxy
- );
- }
-
- $items =
- $this->fuckhtml
- ->getElementsByClassName(
- "serp-item",
- $div
- );
-
- foreach($items as $item){
-
- $data =
- json_decode(
- $this->fuckhtml
- ->getTextContent(
- $item["attributes"]["data-video"]
- ),
- true
- );
-
- $this->fuckhtml->load($item);
-
- $thumb =
- $this->fuckhtml
- ->getElementsByClassName(
- "thumb-image__image",
- "img"
- );
-
- $c = 1;
- if(count($thumb) === 0){
-
- $thumb = [
- "url" => null,
- "ratio" => null
- ];
- }else{
-
- $thumb = [
- "url" =>
- str_replace(
- "//",
- "https://",
- $this->fuckhtml
- ->getTextContent(
- $thumb
- [0]
- ["attributes"]
- ["src"]
- ),
- $c
- ),
- "ratio" => "16:9"
- ];
- }
-
- $smallinfos =
- $this->fuckhtml
- ->getElementsByClassName(
- "serp-item__sitelinks-item",
- "div"
- );
-
- $date = null;
- $views = null;
- $first = true;
-
- foreach($smallinfos as $info){
-
- if($first){
-
- $first = false;
- continue;
- }
-
- $info =
- $this->fuckhtml
- ->getTextContent(
- $info
- );
-
- if($temp_date = strtotime($info)){
-
- $date = $temp_date;
- }else{
-
- $views = $this->parseviews($info);
- }
- }
-
- $description =
- $this->fuckhtml
- ->getElementsByClassName(
- "serp-item__text serp-item__text_visibleText_always",
- "div"
- );
-
- if(count($description) === 0){
-
- $description = null;
- }else{
-
- $description =
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $description[0]
- )
- );
- }
-
- $out["video"][] = [
- "title" =>
- $this->fuckhtml
- ->getTextContent(
- $this->titledots(
- $data["title"]
- )
- ),
- "description" => $description,
- "author" => [
- "name" => null,
- "url" => null,
- "avatar" => null
- ],
- "date" => $date,
- "duration" =>
- (int)$data
- ["counters"]
- ["toHostingLoaded"]
- ["stredParams"]
- ["duration"],
- "views" => $views,
- "thumb" => $thumb,
- "url" =>
- str_replace(
- "http://",
- "https://",
- $this->fuckhtml
- ->getTextContent(
- $data["counters"]
- ["toHostingLoaded"]
- ["postfix"]
- ["href"]
- ),
- $c
- )
- ];
- }
-
- return $out;
- }
-
- private function parseviews($text){
-
- $text = explode(" ", $text);
-
- $num = (float)$text[0];
- $mod = $text[1];
-
- switch($mod){
-
- case "bln.": $num = $num * 1000000000; break;
- case "mln.": $num = $num * 1000000; break;
- case "thsd.": $num = $num * 1000; break;
- }
-
- return $num;
- }
-
- private function titledots($title){
-
- $substr = substr($title, -3);
-
- if(
- $substr == "..." ||
- $substr == "…"
- ){
-
- return trim(substr($title, 0, -3));
- }
-
- return trim($title);
- }
- }
|