yandex.php 29 KB

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