fix yandex pagination
This commit is contained in:
1 parent
2b06b1cf64
commit
6a0ea4c00f
3 files changed
+361
-22
No files matched your search
+215
-7
@@ -14,7 +14,7 @@ class yandex{
|
||||
// backend included in the scraper functions
|
||||
}
|
||||
|
||||
private function get($proxy, $url, $get = [], $nsfw, $get_cookie = 1){
|
||||
private function web_get($proxy, $url, $get = [], $nsfw, $get_cookie = 1){
|
||||
|
||||
$curlproc = curl_init();
|
||||
|
||||
@@ -115,6 +115,163 @@ class yandex{
|
||||
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}" : null),
|
||||
"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){
|
||||
@@ -270,7 +427,7 @@ class yandex{
|
||||
$proxy = $this->backend->get_ip();
|
||||
|
||||
$cookie =
|
||||
$this->get(
|
||||
$this->web_get(
|
||||
$proxy,
|
||||
"https://yandex.ru/support2/smart-captcha/ru/",
|
||||
[],
|
||||
@@ -344,7 +501,7 @@ class yandex{
|
||||
|
||||
try{
|
||||
$html =
|
||||
$this->get(
|
||||
$this->web_get(
|
||||
$proxy,
|
||||
"https://yandex.com/search/site/",
|
||||
$params,
|
||||
@@ -492,6 +649,9 @@ class yandex{
|
||||
|
||||
$nsfw = $request["nsfw"];
|
||||
unset($request["nsfw"]);
|
||||
|
||||
$session = $request["session"];
|
||||
unset($request["session"]);
|
||||
}else{
|
||||
|
||||
$search = $get["s"];
|
||||
@@ -508,6 +668,7 @@ class yandex{
|
||||
$type = $get["type"];
|
||||
$layout = $get["layout"];
|
||||
$format = $get["format"];
|
||||
$session = null;
|
||||
/*
|
||||
$handle = fopen("scraper/yandex.json", "r");
|
||||
$json = fread($handle, filesize("scraper/yandex.json"));
|
||||
@@ -705,11 +866,11 @@ class yandex{
|
||||
|
||||
try{
|
||||
$json = $this->get(
|
||||
$session,
|
||||
$proxy,
|
||||
"https://yandex.com/images/search",
|
||||
$request,
|
||||
$nsfw,
|
||||
"yandex_i"
|
||||
$nsfw
|
||||
);
|
||||
}catch(Exception $err){
|
||||
|
||||
@@ -733,7 +894,38 @@ class yandex{
|
||||
$json["type"] == "captcha"
|
||||
){
|
||||
|
||||
throw new Exception("Yandex blocked this 4get instance. Please try again in ~7 minutes.");
|
||||
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 = [
|
||||
@@ -754,6 +946,7 @@ class yandex{
|
||||
){
|
||||
|
||||
$request["nsfw"] = $nsfw;
|
||||
$request["session"] = $session;
|
||||
|
||||
if(isset($request["p"])){
|
||||
|
||||
@@ -940,7 +1133,7 @@ class yandex{
|
||||
*/
|
||||
try{
|
||||
$json =
|
||||
$this->get(
|
||||
$this->web_get(
|
||||
$proxy,
|
||||
"https://yandex.com/video/search",
|
||||
$params,
|
||||
@@ -1104,4 +1297,19 @@ class yandex{
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user