Files
4get/scraper/yandex.php
T
2026-12-04 02:38:43 -05:00

1213 lines
28 KiB
PHP

<?php
class yandex{
/*
curl functions
*/
public function __construct(){
include "lib/fuckhtml.php";
$this->fuckhtml = new fuckhtml();
include "lib/backend.php";
// backend included in the scraper functions
}
private function web_get($proxy, $url, $get = [], $nsfw, $get_cookie = 1){
$curlproc = curl_init();
if($get !== []){
$get = http_build_query($get);
$url .= "?" . $get;
}
curl_setopt($curlproc, CURLOPT_URL, $url);
// extract "i" cookie
if($get_cookie === 0){
$cookies_tmp = [];
curl_setopt($curlproc, CURLOPT_HEADERFUNCTION, function($curlproc, $header) use (&$cookies_tmp){
$length = strlen($header);
$header = explode(":", $header, 2);
if(trim(strtolower($header[0])) == "set-cookie"){
$cookie_tmp = explode("=", trim($header[1]), 2);
$cookies_tmp[trim($cookie_tmp[0])] =
explode(";", $cookie_tmp[1], 2)[0];
}
return $length;
});
}
switch($nsfw){
case "yes": $nsfw = "0"; break;
case "maybe": $nsfw = "1"; break;
case "no": $nsfw = "2"; break;
}
switch($get_cookie){
case 0:
$cookie = "";
break;
case 1:
$cookie = "Cookie: yp=" . (time() - 4000033) . ".szm.1:1920x1080:876x1000#" . time() . ".sp.family:" . $nsfw;
break;
default:
$cookie = "Cookie: i=" . $get_cookie;
}
$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,
"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, ""); // default 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($get_cookie === 0){
if(isset($cookies_tmp["i"])){
return $cookies_tmp["i"];
}else{
throw new Exception("Failed to get Yandex clearance cookie");
}
}
if(curl_errno($curlproc)){
throw new Exception(curl_error($curlproc));
}
curl_close($curlproc);
return $data;
}
private function get($session, $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",
($session !== null ? "Cookie: {$session}" : "Cookie: yp=" . (time() - random_int(3000000, 4000000)) . ".szm.1:1920x1080:876x1000#" . time() . ".sp.family:{$nsfw}"),
"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, ""); // default 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;
}
private function get_4play($proxy, $url){
if(config::FPLAY_EXTERNAL_ENDPOINT === null){
throw new Exception("Yandex returned a captcha. The instance maintainer should configure 4play to solve them");
}
try{
$curlproc = curl_init();
$url =
config::FPLAY_EXTERNAL_ENDPOINT .
"?url=" . urlencode($url) .
"&pw=" . urlencode(config::FPLAY_PASSWORD) .
"&container=" .// urlencode($container) .
"&proxy=" . urlencode($proxy) .
"&url_match=" . urlencode("\/images\/search\?") .
//"&fail_url_match=" . urlencode("\/sorry\/");
"&js_eval_url_match=" . urlencode("\/showcaptcha\?") .
"&js_eval=" . urlencode('document.getElementsByClassName("CheckboxCaptcha-Button")[0].click();') .
"&js_eval_isolated=" . "1" .
"&return_cookies=" . "1";
$header_out = [];
curl_setopt($curlproc, CURLOPT_HEADERFUNCTION, function($curlproc, $header) use (&$header_out){
$length = strlen($header);
$header_out[] = trim($header);
return $length;
});
curl_setopt($curlproc, CURLOPT_URL, $url);
curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($curlproc, CURLOPT_HTTPHEADER, [
"User-Agent: " . config::USER_AGENT_FRIENDLY,
"Accept: text/plain"
]);
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, 20); // low timeout for if we fail the captcha
curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
$this->backend->assign_proxy($curlproc, $proxy);
$data = curl_exec($curlproc);
if(curl_errno($curlproc)){
throw new Exception(curl_error($curlproc));
}
curl_close($curlproc);
}catch(Exception $error){
throw new Exception("4play timed out. Captcha solve may have failed or render API offline");
}
// check for failstate
if(
str_contains(
$header_out[0],
"400"
)
){
throw new Exception("Page renderer returned an error: " . $data);
}
// parse headers
$cookies = [];
foreach($header_out as $header){
$header = explode(":", $header, 2);
if(strtolower($header[0]) == "x-cookies"){
$cookies = json_decode(trim($header[1]), true);
break;
}
}
$cookie_str = [];
// return cookies as string
foreach($cookies as $cookie){
$cookie_str[] = "{$cookie["name"]}={$cookie["value"]};";
}
return [
"data" => $data,
"cookie" => implode(" ", $cookie_str)
];
}
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");
// has captcha
// https://yandex.com/search/touch/?text=lol&app_platform=android&appsearch_header=1&ui=webmobileapp.yandex&app_version=23070603&app_id=ru.yandex.searchplugin&search_source=yandexcom_touch_native&clid=2218567
// https://yandex.com/search/site/?text=minecraft&web=1&frame=1&v=2.0&searchid=3131712
// &within=777&from_day=26&from_month=8&from_year=2023&to_day=26&to_month=8&to_year=2023
// get clearance cookie
if(($cookie = apcu_fetch("yandexweb_cookie")) === false){
$proxy = $this->backend->get_ip();
$cookie =
$this->web_get(
$proxy,
"https://yandex.ru/support2/smart-captcha/ru/",
[],
false,
0
);
apcu_store("yandexweb_cookie", $cookie);
}
if($get["npt"]){
[$npt, $proxy] = $this->backend->get($get["npt"], "web");
$html =
$this->get(
$proxy,
"https://yandex.com" . $npt,
[],
"yes",
$cookie
);
}else{
$search = $get["s"];
if(strlen($search) === 0){
throw new Exception("Search term is empty!");
}
$proxy = !isset($proxy) ? $this->backend->get_ip() : $proxy;
$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->web_get(
$proxy,
"https://yandex.com/search/site/",
$params,
"yes",
$cookie
);
}catch(Exception $error){
throw new Exception("Could not get search page");
}
/*
$handle = fopen("scraper/yandex.html", "r");
$html = fread($handle, filesize("scraper/yandex.html"));
fclose($handle);*/
}
$out = [
"status" => "ok",
"spelling" => [
"type" => "no_correction",
"using" => null,
"correction" => null
],
"npt" => null,
"answer" => [],
"web" => [],
"image" => [],
"video" => [],
"news" => [],
"related" => []
];
$this->fuckhtml->load($html);
// Scrape page blocked error
$title =
$this->fuckhtml
->getElementsByTagName("title");
if(
count($title) !== 0 &&
$title[0]["innerHTML"] == "403"
){
throw new Exception("Yandex blocked this proxy or 4get instance.");
}
// get nextpage
$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
);
}
// get items
$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"]);
$session = $request["session"];
unset($request["session"]);
}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"];
$session = null;
/*
$handle = fopen("scraper/yandex.json", "r");
$json = fread($handle, filesize("scraper/yandex.json"));
fclose($handle);*/
// https://yandex.com/images/search
// ?tmpl_version=releases-frontend-images-v1.1907.0__a0cb31dc1f0f45dcc1749ab4c8e2c2306f77697c
// &format=json
// &request=%7B%22blocks%22%3A%5B%7B%22block%22%3A%22extra-content%22%2C%22params%22%3A%7B%7D%2C%22version%22%3A2%7D%2C%7B%22block%22%3A%7B%22block%22%3A%22i-react-ajax-adapter%3Aajax%22%7D%2C%22params%22%3A%7B%22type%22%3A%22ImagesApp%22%2C%22ajaxKey%22%3A%22serpList%2Ffetch%22%7D%2C%22version%22%3A2%7D%5D%7D
// &yu=3091577281773194415
// &from=tabbar
// &images_pagination=CocEEoAEAAAAAIkAAAAAAAAAgAAA4GAAgAAAAAAAgAABIAAAIAAAJAAAAAAgAAAAAQAMARACAAAQAAQAAAEEEAAAhAEAAAAgAAAAAAAAAAAAEAAAAAAAAAAABABAAAAAAAEAABAAAAAAAAAAAAAAAAAAAAIAgAAFAEAQAAAAAAEBEAAAAAEBAAABAAlAAAAIAAAAASQAIIACgAIAAAAACAEACQEAAAAAACAAAQAIABAAAgAIAUAAIAAQAIAAAAAAAAAAAAACAIAgAAAAAAEEQIAEAgACCQAAAAAAAABAIAAAAAAAEkAAAEAAAAAAAQAAQACAEAAAAAIAAEAACAAAAAAAQAAAgAAAggAAIEAAKAiAAAAAAAAAAAGABAAAAEEAAAAAAAAAIAAAIAABAAAAABAAiAACAAAAAgAoIAAIgAAAAAMAAAAAAACAAAAAAAAAgAACAAAAAAAgAAAAAKAABAgAAAAAAAAAAAAQgAAAAoAAAQAAAAAAAABAAAAAAAAAMAAAFAAAAAABAAAAAQBIAACAIIAQAACAAAAAAggAAICAAAkAAgAAAAAAAAAAACAAABCAAAAAgCBAAIAgAAAAAQAAAgCABABQAAQAAAAAAAAAAAgQAAIAAQAABAAAAAAAAABAAAAAAAAAAACAAAAIAgAgAAAAAAAAgAAAAAAAAAAAAIAAEAAYKCAB
// &serpListType=horizontal
// &serpid=_6RI0KRHmknuwEJ3BjRy7w
// &text=higurashi+wallpapers
// &uinfo=sw-1920-sh-1080-ww-1305-wh-997-pd-1-wp-16x9_1920x1080
/*
{"blocks":[
{
"block":"extra-content",
"params":{},
"version":2
},
{
"block":{
"block":"i-react-ajax-adapter:ajax"
},
"params":{
"type":"ImagesApp",
"ajaxKey":"serpList/fetch"
},
"version":2
}
]}
*/
$request = [
"tmpl_version" => "releases-frontend-images-v1.1907.0__a0cb31dc1f0f45dcc1749ab4c8e2c2306f77697c",
"format" => "json",
"request" => json_encode([
"blocks" => [
[
"block" => "extra-content",
"params" => (object)[],
"version" => 2
],
[
"block" => [
"block" => "i-react-ajax-adapter:ajax"
],
"params" => [
"type" => "ImagesApp",
"ajaxKey" => "serpList/fetch",
],
"version" => 2
],
]
]),
"from" => "tabbar",
//"images_pagination" =>
"serpListType" => "horizontal",
"text" => $search,
"uinfo" => "sw-1920-sh-1080-ww-1305-wh-997-pd-1-wp-16x9_1920x1080"
];
/*
Apply filters
*/
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;
}
}
try{
$json = $this->get(
$session,
$proxy,
"https://yandex.com/images/search",
$request,
$nsfw
);
}catch(Exception $err){
throw new Exception("Failed to get JSON");
}
/*
$handle = fopen("scraper/yandex.json", "r");
$json = fread($handle, filesize("scraper/yandex.json"));
fclose($handle);*/
$json = json_decode($json, true);
if($json === null){
throw new Exception("Failed to decode JSON");
}
if(
isset($json["type"]) &&
$json["type"] == "captcha"
){
if($session !== null){
throw new Exception("Yandex keeps returning captchas. Aborting.");
}
$session = $this->solve_captcha($proxy, $json["captcha"]["captcha-page"]);
if($session === false){
throw new Exception("4play attempted to solve the JS challenge but was met with an image captcha");
}
// try again
try{
$json = $this->get(
$session,
$proxy,
"https://yandex.com/images/search",
$request,
$nsfw
);
}catch(Exception $err){
throw new Exception("Failed to get un-captchawalled JSON");
}
$json = json_decode($json, true);
if($json === null){
throw new Exception("Failed to decode un-captchawalled JSON");
}
}
$out = [
"status" => "ok",
"npt" => null,
"image" => []
];
$keys = false;
$ref = 0;
// get keys
foreach($json["blocks"] as $block){
if(isset($block["params"]["adapterData"]["serpList"]["items"]["keys"])){
$keys = &$block["params"]["adapterData"]["serpList"]["items"]["keys"];
$ref++;
break;
}
}
if(!is_array($keys)){
throw new Exception("Failed to find keys to iterate over");
}
foreach($keys as $key){
$target = &$json["blocks"][$ref]["params"]["adapterData"]["serpList"]["items"]["entities"][$key];
$sources = [];
foreach(
array_merge(
$target["viewerData"]["preview"],
$target["viewerData"]["dups"]
)
as $image){
// remove dups
foreach($sources as $source){
if($source["url"] == $image["url"]){ continue 2; }
}
// add
$sources[] = [
"url" => $image["url"],
"width" => (int)$image["w"],
"height" => (int)$image["h"]
];
}
// sort by image size
usort($sources, fn($a, $b) => $b["width"] <=> $a["width"]);
// add thumbnail
$sources[] = [
"url" => "https:" . $target["viewerData"]["thumb"]["url"],
"width" => (int)$target["viewerData"]["thumb"]["w"],
"height" => (int)$target["viewerData"]["thumb"]["h"]
];
$title = [];
if(
isset($target["snippet"]["title"]) &&
!empty($target["snippet"]["title"])
){
$title[] =
$this->titledots(
$target["snippet"]["title"]
);
}
if(
isset($target["viewerData"]["snippet"]["text"]) &&
!empty($target["viewerData"]["snippet"]["text"])
){
$title[] =
$this->titledots(
strip_tags(
$target["viewerData"]["snippet"]["text"]
)
);
}
$out["image"][] = [
"title" => implode(": ", $title),
"source" => $sources,
"url" => $target["snippet"]["url"]
];
}
// get npt
if(isset($json["blocks"][$ref]["params"]["adapterData"]["serpList"]["navi"]["next"])){
if(!isset($request["p"])){
$request["p"] = 1;
}else{
$request["p"]++;
}
$request["nsfw"] = $nsfw;
$request["session"] = $session;
$out["npt"] =
$this->backend->store(
json_encode($request),
"image",
$proxy
);
}
return $out;
}
public function video($get){
$this->backend = new backend("yandex_v");
if($get["npt"]){
[$get, $proxy] =
$this->backend->get(
$get["npt"],
"video"
);
$get = json_decode($get, true);
}else{
if(strlen($get["s"]) === 0){
throw new Exception("Search term is empty!");
}
$proxy = $this->backend->get_ip();
}
// https://yandex.com/video/search?text=skycamefalling&from=tabbar&format=json&ncrnd=7271&p=0&parent-reqid=&request={%22blocks%22%3A[{%22block%22%3A%22video-app%22%2C%22params%22%3A{}}]}&serpid=1777751040971457-16832445014469941403-balancer-l7leveler-kubr-yp-klg-151-BAL&yu=3091577281773194415&tmpl_version=releases-frontend-video-v1.1816.0__3bdc24e10a8a138a1194877428e220a3ca0dbc5a
// https://yandex.com/video/search
// ?text=skycamefalling
// &from=tabbar
// &format=json
// &ncrnd=7271
// &p=0
// &parent-reqid=
// &request={%22blocks%22%3A[{%22block%22%3A%22video-app%22%2C%22params%22%3A{}}]} {"blocks":[{"block":"video-app","params":{}}]}
// &serpid=1777751040971457-16832445014469941403-balancer-l7leveler-kubr-yp-klg-151-BAL
// &yu=3091577281773194415
// &tmpl_version=releases-frontend-video-v1.1816.0__3bdc24e10a8a138a1194877428e220a3ca0dbc5a
$params = [
"text" => $get["s"],
"from" => "tabbar",
"format" => "json",
"ncrnd" => 7271,
"p" => 0,
"parent-reqid" => "",
"request" => json_encode((object)[
"blocks" => [
(object)[
"block" => "video-app",
"params" => (object)[]
]
]
]),
"serpid" => "1777751040971457-16832445014469941403-balancer-l7leveler-kubr-yp-klg-151-BAL",
"yu" => 3091577281773194415,
"tmpl_version" => "releases-frontend-video-v1.1816.0__3bdc24e10a8a138a1194877428e220a3ca0dbc5a"
];
if(isset($get["p"])){
$params["p"] = $get["p"];
}
if($get["duration"] != "any"){
$params["duration"] = $get["duration"];
}
if($get["time"] != "any"){
$params["within"] = $get["time"];
}
/*
$handle = fopen("scraper/yandex-video.json", "r");
$json = fread($handle, filesize("scraper/yandex-video.json"));
fclose($handle);
*/
try{
$json =
$this->web_get(
$proxy,
"https://yandex.com/video/search",
$params,
$get["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["results"]["clips"]["items"])){
throw new Exception("Yandex blocked this 4get instance. Please try again in 7~ minutes.");
}
$out = [
"status" => "ok",
"npt" => null,
"video" => [],
"author" => [],
"livestream" => [],
"playlist" => [],
"reel" => []
];
foreach($json["results"]["clips"]["items"] as $k => $data){
if(isset($data["preview"]["posterSrc"])){
$poster = $data["preview"]["posterSrc"];
if(
preg_match(
'/^\/\//',
$data["preview"]["posterSrc"]
)
){
$poster = "https:" . $poster;
}
$thumb = [
"ratio" => "16:9",
"url" => $poster
];
}else{
$thumb = [
"ratio" => null,
"url" => null
];
}
$out["video"][] = [
"title" => $data["relatedParams"]["text"],
"description" => $this->titledots($data["description"]),
"author" => [
"name" =>
isset($json["results"]["clips"]["dups"][$k]["host"]["secondPart"]["name"]) ?
$json["results"]["clips"]["dups"][$k]["host"]["secondPart"]["name"] : null,
"url" =>
isset($json["results"]["clips"]["dups"][$k]["host"]["secondPart"]["origUrl"]) ?
$json["results"]["clips"]["dups"][$k]["host"]["secondPart"]["origUrl"] : null,
"avatar" => null
],
"date" =>
isset($json["results"]["clips"]["dups"][$k]["date"]) ?
strtotime($json["results"]["clips"]["dups"][$k]["date"]) : null,
"duration" =>
isset($json["results"]["clips"]["dups"][$k]["duration"]["value"]) ?
(int)$json["results"]["clips"]["dups"][$k]["duration"]["value"] : null,
"views" =>
isset($json["results"]["clips"]["dups"][$k]["views"]["text"]) ?
$this->parseviews($json["results"]["clips"]["dups"][$k]["views"]["text"]) : null,
"thumb" => $thumb,
"url" =>
preg_replace(
'/^http:\/\//',
"https://",
$data["relatedParams"]["related_url"]
)
];
}
// get npt
if($json["results"]["search"]["hasNextPage"]){
$get["p"] = (int)$json["results"]["search"]["currentPage"] + 1;
$out["npt"] =
$this->backend->store(
json_encode($get),
"video",
$proxy
);
}
return $out;
}
private function parseviews($number){
// decimal should always be 1 number long
$number = explode(" ", $number, 2);
$number = $number[0];
$unit = strtolower($number[strlen($number) - 1]);
$tmp = explode(".", $number, 2);
$number = (int)$number;
if(count($tmp) === 2){
$decimal = (int)$tmp[1];
}else{
$decimal = 0;
}
switch($unit){
case "k":
$exponant = 1000;
break;
case "m":
$exponant = 1000000;
break;
case "b";
$exponant = 1000000000;
break;
default:
$exponant = 1;
break;
}
return ($number * $exponant) + ($decimal * ($exponant / 10));
}
private function titledots($title){
$substr = substr($title, -3);
if(
$substr == "..." ||
$substr == "…"
){
return trim(substr($title, 0, -3));
}
return trim($title);
}
private function solve_captcha($proxy, $captcha_url){
// https://yandex.com/showcaptcha?form-fb-hint=2.4.3a20b88c2e3d330a3c0e25a74135697f737300b15a83a73d502fc1f4b12458b536d7a8d6e2064642c7adac96279549d226833ebb381d74daca3a2454&mt=50E3FC3606D54AD20960EED7617BA353D2FB573258E7C759CE8C834CCE683F62AA2B98B8A7D491C5F4E1B90AF69704130A84C9652D13D4E67DCAF46D3CAA78A27FC6157D915D5AE534846C2D098335C6F831532FB8487D27F6120ABC5713EDA9AFDA4ABA14C04F10E2478C34FA2641DF5768249B7BCB314907F9E28794D6384ACB87FBB95A433A45B71266E2E8744695660027A55889AA186D08D378BB6B0FA14490CE5D&retpath=aHR0cHM6Ly95YW5kZXguY29tL2ltYWdlcy9zZWFyY2g_ZnJvbT10YWJiYXImcD0xJnNlcnBMaXN0VHlwZT1ob3Jpem9udGFsJnNlcnBpZD1YTV9mamh5RlVyc0U3ejFOVDFaWlpRJnRleHQ9bmFuYW5pZ2h0cmF5JnRtcGxfdmVyc2lvbj1yZWxlYXNlcy1mcm9udGVuZC1pbWFnZXMtdjEuMTkwNi4wX19jYTZlYmZiZGRkODIwZGRjMTkzMTM2OWJjOTZhYTE5NWQ0ODcxMjI5JnVpbmZvPXN3LTE5MjAtc2gtMTA4MC13dy0xMzA1LXdoLTk5Ny1wZC0xLXdwLTE2eDlfMTkyMHgxMDgwJnV0bV9yZWZlcnJlcj1odHRwcyUzQSUyRiUyRnlhbmRleC5jb20lMkZpbWFnZXMlMkZzZWFyY2glM0Zmcm9tJTNEdGFiYmFyJTI2dGV4dCUzRG5hbmFuaWdodHJheQ%2C%2C_7ae1ea9ca30de369b51cedba38493bd7&t=2%252F1796347471%252F2c7eee85cd849cc18df2b7505200496f&u=9870373719380275251&s=686715091a223e6294320f9b1e457000
// document.getElementsByClassName("CheckboxCaptcha-Button")[0].click();
$session = $this->get_4play($proxy, $captcha_url);
if($session["cookie"] != ""){
return $session["cookie"];
}
return false;
}
}