1
0

yandex.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. <?php
  2. class yandex{
  3. /*
  4. curl functions
  5. */
  6. public function __construct(){
  7. include "lib/fuckhtml.php";
  8. $this->fuckhtml = new fuckhtml();
  9. include "lib/backend.php";
  10. // backend included in the scraper functions
  11. }
  12. private function get($proxy, $url, $get = [], $nsfw, $get_cookie = 1){
  13. $curlproc = curl_init();
  14. if($get !== []){
  15. $get = http_build_query($get);
  16. $url .= "?" . $get;
  17. }
  18. curl_setopt($curlproc, CURLOPT_URL, $url);
  19. // extract "i" cookie
  20. if($get_cookie === 0){
  21. $cookies_tmp = [];
  22. curl_setopt($curlproc, CURLOPT_HEADERFUNCTION, function($curlproc, $header) use (&$cookies_tmp){
  23. $length = strlen($header);
  24. $header = explode(":", $header, 2);
  25. if(trim(strtolower($header[0])) == "set-cookie"){
  26. $cookie_tmp = explode("=", trim($header[1]), 2);
  27. $cookies_tmp[trim($cookie_tmp[0])] =
  28. explode(";", $cookie_tmp[1], 2)[0];
  29. }
  30. return $length;
  31. });
  32. }
  33. switch($nsfw){
  34. case "yes": $nsfw = "0"; break;
  35. case "maybe": $nsfw = "1"; break;
  36. case "no": $nsfw = "2"; break;
  37. }
  38. switch($get_cookie){
  39. case 0:
  40. $cookie = "";
  41. break;
  42. case 1:
  43. $cookie = "Cookie: yp=" . (time() - 4000033) . ".szm.1:1920x1080:876x1000#" . time() . ".sp.family:" . $nsfw;
  44. break;
  45. default:
  46. $cookie = "Cookie: i=" . $get_cookie;
  47. }
  48. $headers =
  49. ["User-Agent: " . config::USER_AGENT,
  50. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  51. "Accept-Encoding: gzip",
  52. "Accept-Language: en-US,en;q=0.5",
  53. "DNT: 1",
  54. $cookie,
  55. "Referer: https://yandex.com/images/search",
  56. "Connection: keep-alive",
  57. "Upgrade-Insecure-Requests: 1",
  58. "Sec-Fetch-Dest: document",
  59. "Sec-Fetch-Mode: navigate",
  60. "Sec-Fetch-Site: cross-site",
  61. "Upgrade-Insecure-Requests: 1"];
  62. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  63. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  64. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  65. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  66. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  67. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  68. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  69. $this->backend->assign_proxy($curlproc, $proxy);
  70. $data = curl_exec($curlproc);
  71. if($get_cookie === 0){
  72. if(isset($cookies_tmp["i"])){
  73. return $cookies_tmp["i"];
  74. }else{
  75. throw new Exception("Failed to get Yandex clearance cookie");
  76. }
  77. }
  78. if(curl_errno($curlproc)){
  79. throw new Exception(curl_error($curlproc));
  80. }
  81. curl_close($curlproc);
  82. return $data;
  83. }
  84. public function getfilters($pagetype){
  85. switch($pagetype){
  86. case "web":
  87. return [
  88. "lang" => [
  89. "display" => "Language",
  90. "option" => [
  91. "any" => "Any language",
  92. "en" => "English",
  93. "ru" => "Russian",
  94. "be" => "Belorussian",
  95. "fr" => "French",
  96. "de" => "German",
  97. "id" => "Indonesian",
  98. "kk" => "Kazakh",
  99. "tt" => "Tatar",
  100. "tr" => "Turkish",
  101. "uk" => "Ukrainian"
  102. ]
  103. ],
  104. "newer" => [
  105. "display" => "Newer than",
  106. "option" => "_DATE"
  107. ],
  108. "older" => [
  109. "display" => "Older than",
  110. "option" => "_DATE"
  111. ]
  112. ];
  113. break;
  114. case "images":
  115. return
  116. [
  117. "nsfw" => [
  118. "display" => "NSFW",
  119. "option" => [
  120. "yes" => "Yes",
  121. "maybe" => "Maybe",
  122. "no" => "No"
  123. ]
  124. ],
  125. "time" => [
  126. "display" => "Time posted",
  127. "option" => [
  128. "any" => "Any time",
  129. "week" => "Last week"
  130. ]
  131. ],
  132. "size" => [
  133. "display" => "Size",
  134. "option" => [
  135. "any" => "Any size",
  136. "small" => "Small",
  137. "medium" => "Medium",
  138. "large" => "Large",
  139. "wallpaper" => "Wallpaper"
  140. ]
  141. ],
  142. "color" => [
  143. "display" => "Colors",
  144. "option" => [
  145. "any" => "All colors",
  146. "color" => "Color images only",
  147. "gray" => "Black and white",
  148. "red" => "Red",
  149. "orange" => "Orange",
  150. "yellow" => "Yellow",
  151. "cyan" => "Cyan",
  152. "green" => "Green",
  153. "blue" => "Blue",
  154. "violet" => "Purple",
  155. "white" => "White",
  156. "black" => "Black"
  157. ]
  158. ],
  159. "type" => [
  160. "display" => "Type",
  161. "option" => [
  162. "any" => "All types",
  163. "photo" => "Photos",
  164. "clipart" => "White background",
  165. "lineart" => "Drawings and sketches",
  166. "face" => "People",
  167. "demotivator" => "Demotivators"
  168. ]
  169. ],
  170. "layout" => [
  171. "display" => "Layout",
  172. "option" => [
  173. "any" => "All layouts",
  174. "horizontal" => "Horizontal",
  175. "vertical" => "Vertical",
  176. "square" => "Square"
  177. ]
  178. ],
  179. "format" => [
  180. "display" => "Format",
  181. "option" => [
  182. "any" => "Any format",
  183. "jpeg" => "JPEG",
  184. "png" => "PNG",
  185. "gif" => "GIF"
  186. ]
  187. ]
  188. ];
  189. break;
  190. case "videos":
  191. return [
  192. "nsfw" => [
  193. "display" => "NSFW",
  194. "option" => [
  195. "yes" => "Yes",
  196. "maybe" => "Maybe",
  197. "no" => "No"
  198. ]
  199. ],
  200. "time" => [
  201. "display" => "Time posted",
  202. "option" => [
  203. "any" => "Any time",
  204. "9" => "Recently"
  205. ]
  206. ],
  207. "duration" => [
  208. "display" => "Duration",
  209. "option" => [
  210. "any" => "Any duration",
  211. "short" => "Short"
  212. ]
  213. ]
  214. ];
  215. break;
  216. }
  217. }
  218. public function web($get){
  219. $this->backend = new backend("yandex_w");
  220. // has captcha
  221. // 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
  222. // https://yandex.com/search/site/?text=minecraft&web=1&frame=1&v=2.0&searchid=3131712
  223. // &within=777&from_day=26&from_month=8&from_year=2023&to_day=26&to_month=8&to_year=2023
  224. // get clearance cookie
  225. if(($cookie = apcu_fetch("yandexweb_cookie")) === false){
  226. $proxy = $this->backend->get_ip();
  227. $cookie =
  228. $this->get(
  229. $proxy,
  230. "https://yandex.ru/support2/smart-captcha/ru/",
  231. [],
  232. false,
  233. 0
  234. );
  235. apcu_store("yandexweb_cookie", $cookie);
  236. }
  237. if($get["npt"]){
  238. [$npt, $proxy] = $this->backend->get($get["npt"], "web");
  239. $html =
  240. $this->get(
  241. $proxy,
  242. "https://yandex.com" . $npt,
  243. [],
  244. "yes",
  245. $cookie
  246. );
  247. }else{
  248. $search = $get["s"];
  249. if(strlen($search) === 0){
  250. throw new Exception("Search term is empty!");
  251. }
  252. $proxy = !isset($proxy) ? $this->backend->get_ip() : $proxy;
  253. $lang = $get["lang"];
  254. $older = $get["older"];
  255. $newer = $get["newer"];
  256. $params = [
  257. "text" => $search,
  258. "web" => "1",
  259. "frame" => "1",
  260. "searchid" => "3131712"
  261. ];
  262. if($lang != "any"){
  263. $params["lang"] = $lang;
  264. }
  265. if(
  266. $newer === false &&
  267. $older !== false
  268. ){
  269. $newer = 0;
  270. }
  271. if($newer !== false){
  272. $params["from_day"] = date("j", $newer);
  273. $params["from_month"] = date("n", $newer);
  274. $params["from_year"] = date("Y", $newer);
  275. if($older === false){
  276. $older = time();
  277. }
  278. $params["to_day"] = date("j", $older);
  279. $params["to_month"] = date("n", $older);
  280. $params["to_year"] = date("Y", $older);
  281. }
  282. try{
  283. $html =
  284. $this->get(
  285. $proxy,
  286. "https://yandex.com/search/site/",
  287. $params,
  288. "yes",
  289. $cookie
  290. );
  291. }catch(Exception $error){
  292. throw new Exception("Could not get search page");
  293. }
  294. /*
  295. $handle = fopen("scraper/yandex.html", "r");
  296. $html = fread($handle, filesize("scraper/yandex.html"));
  297. fclose($handle);*/
  298. }
  299. $out = [
  300. "status" => "ok",
  301. "spelling" => [
  302. "type" => "no_correction",
  303. "using" => null,
  304. "correction" => null
  305. ],
  306. "npt" => null,
  307. "answer" => [],
  308. "web" => [],
  309. "image" => [],
  310. "video" => [],
  311. "news" => [],
  312. "related" => []
  313. ];
  314. $this->fuckhtml->load($html);
  315. // Scrape page blocked error
  316. $title =
  317. $this->fuckhtml
  318. ->getElementsByTagName("title");
  319. if(
  320. count($title) !== 0 &&
  321. $title[0]["innerHTML"] == "403"
  322. ){
  323. throw new Exception("Yandex blocked this proxy or 4get instance.");
  324. }
  325. // get nextpage
  326. $npt =
  327. $this->fuckhtml
  328. ->getElementsByClassName(
  329. "b-pager__next",
  330. "a"
  331. );
  332. if(count($npt) !== 0){
  333. $out["npt"] =
  334. $this->backend->store(
  335. $this->fuckhtml
  336. ->getTextContent(
  337. $npt
  338. [0]
  339. ["attributes"]
  340. ["href"]
  341. ),
  342. "web",
  343. $proxy
  344. );
  345. }
  346. // get items
  347. $items =
  348. $this->fuckhtml
  349. ->getElementsByClassName(
  350. "b-serp-item",
  351. "li"
  352. );
  353. foreach($items as $item){
  354. $this->fuckhtml->load($item);
  355. $link =
  356. $this->fuckhtml
  357. ->getElementsByClassName(
  358. "b-serp-item__title-link",
  359. "a"
  360. )[0];
  361. $out["web"][] = [
  362. "title" =>
  363. $this->titledots(
  364. $this->fuckhtml
  365. ->getTextContent(
  366. $link
  367. )
  368. ),
  369. "description" =>
  370. $this->titledots(
  371. $this->fuckhtml
  372. ->getTextContent(
  373. $this->fuckhtml
  374. ->getElementsByClassName(
  375. "b-serp-item__text",
  376. "div"
  377. )[0]
  378. )
  379. ),
  380. "url" =>
  381. $this->fuckhtml
  382. ->getTextContent(
  383. $link
  384. ["attributes"]
  385. ["href"]
  386. ),
  387. "date" => null,
  388. "type" => "web",
  389. "thumb" => [
  390. "url" => null,
  391. "ratio" => null
  392. ],
  393. "sublink" => [],
  394. "table" => []
  395. ];
  396. }
  397. return $out;
  398. }
  399. public function image($get){
  400. $this->backend = new backend("yandex_i");
  401. if($get["npt"]){
  402. [$request, $proxy] =
  403. $this->backend->get(
  404. $get["npt"],
  405. "images"
  406. );
  407. $request = json_decode($request, true);
  408. $nsfw = $request["nsfw"];
  409. unset($request["nsfw"]);
  410. }else{
  411. $search = $get["s"];
  412. if(strlen($search) === 0){
  413. throw new Exception("Search term is empty!");
  414. }
  415. $proxy = $this->backend->get_ip();
  416. $nsfw = $get["nsfw"];
  417. $time = $get["time"];
  418. $size = $get["size"];
  419. $color = $get["color"];
  420. $type = $get["type"];
  421. $layout = $get["layout"];
  422. $format = $get["format"];
  423. /*
  424. $handle = fopen("scraper/yandex.json", "r");
  425. $json = fread($handle, filesize("scraper/yandex.json"));
  426. fclose($handle);*/
  427. // SIZE
  428. // large
  429. // 227.0=1;203.0=1;76fe94.0=1;41d251.0=1;75.0=1;371.0=1;291.0=1;307.0=1;f797ee.0=1;1cf7c2.0=1;deca32.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&isize=large&suggest_reqid=486139416166165501540886508227485&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  430. // medium
  431. // 227.0=1;203.0=1;76fe94.0=1;41d251.0=1;75.0=1;371.0=1;291.0=1;307.0=1;f797ee.0=1;1cf7c2.0=1;deca32.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&isize=medium&suggest_reqid=486139416166165501540886508227485&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  432. // small
  433. // 227.0=1;203.0=1;76fe94.0=1;41d251.0=1;75.0=1;371.0=1;291.0=1;307.0=1;f797ee.0=1;1cf7c2.0=1;deca32.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&isize=small&suggest_reqid=486139416166165501540886508227485&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  434. // ORIENTATION
  435. // Horizontal
  436. // 227.0=1;203.0=1;76fe94.0=1;41d251.0=1;75.0=1;371.0=1;291.0=1;307.0=1;f797ee.0=1;1cf7c2.0=1;deca32.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&iorient=horizontal&suggest_reqid=486139416166165501540886508227485&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  437. // Vertical
  438. // 227.0=1;203.0=1;76fe94.0=1;41d251.0=1;75.0=1;371.0=1;291.0=1;307.0=1;f797ee.0=1;1cf7c2.0=1;deca32.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&iorient=vertical&suggest_reqid=486139416166165501540886508227485&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  439. // Square
  440. // 227.0=1;203.0=1;76fe94.0=1;41d251.0=1;75.0=1;371.0=1;291.0=1;307.0=1;f797ee.0=1;1cf7c2.0=1;deca32.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&iorient=square&suggest_reqid=486139416166165501540886508227485&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  441. // TYPE
  442. // Photos
  443. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&text=minecraft&type=photo&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  444. // White background
  445. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&text=minecraft&type=clipart&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  446. // Drawings and sketches
  447. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&text=minecraft&type=lineart&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  448. // People
  449. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&text=minecraft&type=face&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  450. // Demotivators
  451. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&text=minecraft&type=demotivator&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  452. // COLOR
  453. // Color images only
  454. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=color&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  455. // Black and white
  456. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=gray&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  457. // Red
  458. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=red&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  459. // Orange
  460. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=orange&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  461. // Yellow
  462. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=yellow&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  463. // Cyan
  464. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=cyan&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  465. // Green
  466. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=green&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  467. // Blue
  468. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=blue&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  469. // Purple
  470. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=violet&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  471. // White
  472. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=white&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  473. // Black
  474. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&icolor=black&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  475. // FORMAT
  476. // jpeg
  477. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&itype=jpg&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  478. // png
  479. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&itype=png&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  480. // gif
  481. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&itype=gifan&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  482. // RECENT
  483. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&recent=7D&text=minecraft&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  484. // WALLPAPER
  485. // 307.0=1;371.0=1;291.0=1;203.0=1;deca32.0=1;f797ee.0=1;1cf7c2.0=1;41d251.0=1;267.0=1;bde197.0=1"},"extraContent":{"names":["i-react-ajax-adapter"]}}}&yu=4861394161661655015&isize=wallpaper&text=minecraft&wp=wh16x9_1920x1080&uinfo=sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080
  486. $request = [
  487. "format" => "json",
  488. "request" => [
  489. "blocks" => [
  490. [
  491. "block" => "extra-content",
  492. "params" => (object)[],
  493. "version" => 2
  494. ],
  495. [
  496. "block" => "i-global__params:ajax",
  497. "params" => (object)[],
  498. "version" => 2
  499. ],
  500. [
  501. "block" => "search2:ajax",
  502. "params" => (object)[],
  503. "version" => 2
  504. ],
  505. [
  506. "block" => "preview__isWallpaper",
  507. "params" => (object)[],
  508. "version" => 2
  509. ],
  510. [
  511. "block" => "content_type_search",
  512. "params" => (object)[],
  513. "version" => 2
  514. ],
  515. [
  516. "block" => "serp-controller",
  517. "params" => (object)[],
  518. "version" => 2
  519. ],
  520. [
  521. "block" => "cookies_ajax",
  522. "params" => (object)[],
  523. "version" => 2
  524. ],
  525. [
  526. "block" => "advanced-search-block",
  527. "params" => (object)[],
  528. "version" => 2
  529. ]
  530. ],
  531. "metadata" => [
  532. "bundles" => [
  533. "lb" => "AS?(E<X120"
  534. ],
  535. "assets" => [
  536. // las base
  537. "las" => "justifier-height=1;justifier-setheight=1;fitimages-height=1;justifier-fitincuts=1;react-with-dom=1;"
  538. // las default
  539. //"las" => "justifier-height=1;justifier-setheight=1;fitimages-height=1;justifier-fitincuts=1;react-with-dom=1;227.0=1;203.0=1;76fe94.0=1;215f96.0=1;75.0=1"
  540. ],
  541. "extraContent" => [
  542. "names" => [
  543. "i-react-ajax-adapter"
  544. ]
  545. ]
  546. ]
  547. ]
  548. ];
  549. /*
  550. Apply filters
  551. */
  552. if($time == "week"){
  553. $request["recent"] = "7D";
  554. }
  555. if($size != "any"){
  556. $request["isize"] = $size;
  557. }
  558. if($type != "any"){
  559. $request["type"] = $type;
  560. }
  561. if($color != "any"){
  562. $request["icolor"] = $color;
  563. }
  564. if($layout != "any"){
  565. $request["iorient"] = $layout;
  566. }
  567. if($format != "any"){
  568. $request["itype"] = $format;
  569. }
  570. $request["text"] = $search;
  571. $request["uinfo"] = "sw-1920-sh-1080-ww-1125-wh-999-pd-1-wp-16x9_1920x1080";
  572. $request["request"] = json_encode($request["request"]);
  573. }
  574. try{
  575. $json = $this->get(
  576. $proxy,
  577. "https://yandex.com/images/search",
  578. $request,
  579. $nsfw,
  580. "yandex_i"
  581. );
  582. }catch(Exception $err){
  583. throw new Exception("Failed to get JSON");
  584. }
  585. /*
  586. $handle = fopen("scraper/yandex.json", "r");
  587. $json = fread($handle, filesize("scraper/yandex.json"));
  588. fclose($handle);*/
  589. $json = json_decode($json, true);
  590. if($json === null){
  591. throw new Exception("Failed to decode JSON");
  592. }
  593. if(
  594. isset($json["type"]) &&
  595. $json["type"] == "captcha"
  596. ){
  597. throw new Exception("Yandex blocked this 4get instance. Please try again in ~7 minutes.");
  598. }
  599. $out = [
  600. "status" => "ok",
  601. "npt" => null,
  602. "image" => []
  603. ];
  604. // get html
  605. $html = "";
  606. foreach($json["blocks"] as $block){
  607. $html .= $block["html"];
  608. // get next page
  609. if(
  610. isset($block["params"]["nextPageUrl"]) &&
  611. !empty($block["params"]["nextPageUrl"])
  612. ){
  613. $request["nsfw"] = $nsfw;
  614. if(isset($request["p"])){
  615. $request["p"]++;
  616. }else{
  617. $request["p"] = 1;
  618. }
  619. $out["npt"] =
  620. $this->backend->store(
  621. json_encode($request),
  622. "images",
  623. $proxy
  624. );
  625. }
  626. }
  627. $this->fuckhtml->load($html);
  628. // get search results
  629. $data = null;
  630. foreach(
  631. $this->fuckhtml
  632. ->getElementsByClassName(
  633. "Root",
  634. "div"
  635. ) as $div
  636. ){
  637. if(isset($div["attributes"]["data-state"])){
  638. $tmp = json_decode(
  639. $this->fuckhtml
  640. ->getTextContent(
  641. $div["attributes"]["data-state"]
  642. ),
  643. true
  644. );
  645. if(isset($tmp["initialState"]["serpList"])){
  646. $data = $tmp;
  647. break;
  648. }
  649. }
  650. }
  651. if($data === null){
  652. throw new Exception("Failed to extract JSON");
  653. }
  654. foreach($data["initialState"]["serpList"]["items"]["entities"] as $image){
  655. $title = [html_entity_decode($image["snippet"]["title"], ENT_QUOTES | ENT_HTML5)];
  656. if(isset($image["snippet"]["text"])){
  657. $title[] = html_entity_decode($image["snippet"]["text"], ENT_QUOTES | ENT_HTML5);
  658. }
  659. $tmp = [
  660. "title" =>
  661. $this->fuckhtml
  662. ->getTextContent(
  663. $this->titledots(
  664. implode(": ", $title)
  665. )
  666. ),
  667. "source" => [],
  668. "url" => htmlspecialchars_decode($image["snippet"]["url"])
  669. ];
  670. // add preview URL
  671. $tmp["source"][] = [
  672. "url" => htmlspecialchars_decode($image["viewerData"]["preview"][0]["url"]),
  673. "width" => (int)$image["viewerData"]["preview"][0]["w"],
  674. "height" => (int)$image["viewerData"]["preview"][0]["h"],
  675. ];
  676. foreach($image["viewerData"]["dups"] as $dup){
  677. $tmp["source"][] = [
  678. "url" => htmlspecialchars_decode($dup["url"]),
  679. "width" => (int)$dup["w"],
  680. "height" => (int)$dup["h"],
  681. ];
  682. }
  683. $tmp["source"][] = [
  684. "url" =>
  685. preg_replace(
  686. '/^\/\//',
  687. "https://",
  688. htmlspecialchars_decode($image["viewerData"]["thumb"]["url"])
  689. ),
  690. "width" => (int)$image["viewerData"]["thumb"]["w"],
  691. "height" => (int)$image["viewerData"]["thumb"]["h"]
  692. ];
  693. $out["image"][] = $tmp;
  694. }
  695. return $out;
  696. }
  697. public function video($get){
  698. $this->backend = new backend("yandex_v");
  699. if($get["npt"]){
  700. [$get, $proxy] =
  701. $this->backend->get(
  702. $get["npt"],
  703. "video"
  704. );
  705. $get = json_decode($get, true);
  706. }else{
  707. if(strlen($get["s"]) === 0){
  708. throw new Exception("Search term is empty!");
  709. }
  710. $proxy = $this->backend->get_ip();
  711. }
  712. // 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
  713. // https://yandex.com/video/search
  714. // ?text=skycamefalling
  715. // &from=tabbar
  716. // &format=json
  717. // &ncrnd=7271
  718. // &p=0
  719. // &parent-reqid=
  720. // &request={%22blocks%22%3A[{%22block%22%3A%22video-app%22%2C%22params%22%3A{}}]} {"blocks":[{"block":"video-app","params":{}}]}
  721. // &serpid=1777751040971457-16832445014469941403-balancer-l7leveler-kubr-yp-klg-151-BAL
  722. // &yu=3091577281773194415
  723. // &tmpl_version=releases-frontend-video-v1.1816.0__3bdc24e10a8a138a1194877428e220a3ca0dbc5a
  724. $params = [
  725. "text" => $get["s"],
  726. "from" => "tabbar",
  727. "format" => "json",
  728. "ncrnd" => 7271,
  729. "p" => 0,
  730. "parent-reqid" => "",
  731. "request" => json_encode((object)[
  732. "blocks" => [
  733. (object)[
  734. "block" => "video-app",
  735. "params" => (object)[]
  736. ]
  737. ]
  738. ]),
  739. "serpid" => "1777751040971457-16832445014469941403-balancer-l7leveler-kubr-yp-klg-151-BAL",
  740. "yu" => 3091577281773194415,
  741. "tmpl_version" => "releases-frontend-video-v1.1816.0__3bdc24e10a8a138a1194877428e220a3ca0dbc5a"
  742. ];
  743. if(isset($get["p"])){
  744. $params["p"] = $get["p"];
  745. }
  746. if($get["duration"] != "any"){
  747. $params["duration"] = $get["duration"];
  748. }
  749. if($get["time"] != "any"){
  750. $params["within"] = $get["time"];
  751. }
  752. /*
  753. $handle = fopen("scraper/yandex-video.json", "r");
  754. $json = fread($handle, filesize("scraper/yandex-video.json"));
  755. fclose($handle);
  756. */
  757. try{
  758. $json =
  759. $this->get(
  760. $proxy,
  761. "https://yandex.com/video/search",
  762. $params,
  763. $get["nsfw"],
  764. "yandex_v"
  765. );
  766. }catch(Exception $error){
  767. throw new Exception("Could not fetch JSON");
  768. }
  769. $json = json_decode($json, true);
  770. if($json === null){
  771. throw new Exception("Could not parse JSON");
  772. }
  773. if(!isset($json["results"]["clips"]["items"])){
  774. throw new Exception("Yandex blocked this 4get instance. Please try again in 7~ minutes.");
  775. }
  776. $out = [
  777. "status" => "ok",
  778. "npt" => null,
  779. "video" => [],
  780. "author" => [],
  781. "livestream" => [],
  782. "playlist" => [],
  783. "reel" => []
  784. ];
  785. foreach($json["results"]["clips"]["items"] as $k => $data){
  786. if(isset($data["preview"]["posterSrc"])){
  787. $poster = $data["preview"]["posterSrc"];
  788. if(
  789. preg_match(
  790. '/^\/\//',
  791. $data["preview"]["posterSrc"]
  792. )
  793. ){
  794. $poster = "https:" . $poster;
  795. }
  796. $thumb = [
  797. "ratio" => "16:9",
  798. "url" => $poster
  799. ];
  800. }else{
  801. $thumb = [
  802. "ratio" => null,
  803. "url" => null
  804. ];
  805. }
  806. $out["video"][] = [
  807. "title" => $data["relatedParams"]["text"],
  808. "description" => $this->titledots($data["description"]),
  809. "author" => [
  810. "name" =>
  811. isset($json["results"]["clips"]["dups"][$k]["host"]["secondPart"]["name"]) ?
  812. $json["results"]["clips"]["dups"][$k]["host"]["secondPart"]["name"] : null,
  813. "url" =>
  814. isset($json["results"]["clips"]["dups"][$k]["host"]["secondPart"]["origUrl"]) ?
  815. $json["results"]["clips"]["dups"][$k]["host"]["secondPart"]["origUrl"] : null,
  816. "avatar" => null
  817. ],
  818. "date" =>
  819. isset($json["results"]["clips"]["dups"][$k]["date"]) ?
  820. strtotime($json["results"]["clips"]["dups"][$k]["date"]) : null,
  821. "duration" =>
  822. isset($json["results"]["clips"]["dups"][$k]["duration"]["value"]) ?
  823. (int)$json["results"]["clips"]["dups"][$k]["duration"]["value"] : null,
  824. "views" =>
  825. isset($json["results"]["clips"]["dups"][$k]["views"]["text"]) ?
  826. $this->parseviews($json["results"]["clips"]["dups"][$k]["views"]["text"]) : null,
  827. "thumb" => $thumb,
  828. "url" =>
  829. preg_replace(
  830. '/^http:\/\//',
  831. "https://",
  832. $data["relatedParams"]["related_url"]
  833. )
  834. ];
  835. }
  836. // get npt
  837. if($json["results"]["search"]["hasNextPage"]){
  838. $get["p"] = (int)$json["results"]["search"]["currentPage"] + 1;
  839. $out["npt"] =
  840. $this->backend->store(
  841. json_encode($get),
  842. "video",
  843. $proxy
  844. );
  845. }
  846. return $out;
  847. }
  848. private function parseviews($number){
  849. // decimal should always be 1 number long
  850. $number = explode(" ", $number, 2);
  851. $number = $number[0];
  852. $unit = strtolower($number[strlen($number) - 1]);
  853. $tmp = explode(".", $number, 2);
  854. $number = (int)$number;
  855. if(count($tmp) === 2){
  856. $decimal = (int)$tmp[1];
  857. }else{
  858. $decimal = 0;
  859. }
  860. switch($unit){
  861. case "k":
  862. $exponant = 1000;
  863. break;
  864. case "m":
  865. $exponant = 1000000;
  866. break;
  867. case "b";
  868. $exponant = 1000000000;
  869. break;
  870. default:
  871. $exponant = 1;
  872. break;
  873. }
  874. return ($number * $exponant) + ($decimal * ($exponant / 10));
  875. }
  876. private function titledots($title){
  877. $substr = substr($title, -3);
  878. if(
  879. $substr == "..." ||
  880. $substr == "…"
  881. ){
  882. return trim(substr($title, 0, -3));
  883. }
  884. return trim($title);
  885. }
  886. }