google.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. <?php
  2. // noJS but requires login/cookie token
  3. // https://www.google.com/search?q=asmr&tbm=isch&asearch=arc&async=arc_id:srp_a,ffilt:all,ve_name:MoreResultsContainer,use_ac:false,inf:0,_id:arc-srp_a,_pms:s,_fmt:pc
  4. class google{
  5. public function __construct(){
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. include "lib/backend.php";
  9. $this->backend = new backend("google");
  10. }
  11. public function getfilters($page){
  12. $base = [
  13. "country" => [ // gl=<country> (image: cr=countryAF)
  14. "display" => "Country",
  15. "option" => [
  16. "any" => "Instance's country",
  17. "af" => "Afghanistan",
  18. "al" => "Albania",
  19. "dz" => "Algeria",
  20. "as" => "American Samoa",
  21. "ad" => "Andorra",
  22. "ao" => "Angola",
  23. "ai" => "Anguilla",
  24. "aq" => "Antarctica",
  25. "ag" => "Antigua and Barbuda",
  26. "ar" => "Argentina",
  27. "am" => "Armenia",
  28. "aw" => "Aruba",
  29. "au" => "Australia",
  30. "at" => "Austria",
  31. "az" => "Azerbaijan",
  32. "bs" => "Bahamas",
  33. "bh" => "Bahrain",
  34. "bd" => "Bangladesh",
  35. "bb" => "Barbados",
  36. "by" => "Belarus",
  37. "be" => "Belgium",
  38. "bz" => "Belize",
  39. "bj" => "Benin",
  40. "bm" => "Bermuda",
  41. "bt" => "Bhutan",
  42. "bo" => "Bolivia",
  43. "ba" => "Bosnia and Herzegovina",
  44. "bw" => "Botswana",
  45. "bv" => "Bouvet Island",
  46. "br" => "Brazil",
  47. "io" => "British Indian Ocean Territory",
  48. "bn" => "Brunei Darussalam",
  49. "bg" => "Bulgaria",
  50. "bf" => "Burkina Faso",
  51. "bi" => "Burundi",
  52. "kh" => "Cambodia",
  53. "cm" => "Cameroon",
  54. "ca" => "Canada",
  55. "cv" => "Cape Verde",
  56. "ky" => "Cayman Islands",
  57. "cf" => "Central African Republic",
  58. "td" => "Chad",
  59. "cl" => "Chile",
  60. "cn" => "China",
  61. "cx" => "Christmas Island",
  62. "cc" => "Cocos (Keeling) Islands",
  63. "co" => "Colombia",
  64. "km" => "Comoros",
  65. "cg" => "Congo",
  66. "cd" => "Congo, the Democratic Republic",
  67. "ck" => "Cook Islands",
  68. "cr" => "Costa Rica",
  69. "ci" => "Cote D'ivoire",
  70. "hr" => "Croatia",
  71. "cu" => "Cuba",
  72. "cy" => "Cyprus",
  73. "cz" => "Czech Republic",
  74. "dk" => "Denmark",
  75. "dj" => "Djibouti",
  76. "dm" => "Dominica",
  77. "do" => "Dominican Republic",
  78. "ec" => "Ecuador",
  79. "eg" => "Egypt",
  80. "sv" => "El Salvador",
  81. "gq" => "Equatorial Guinea",
  82. "er" => "Eritrea",
  83. "ee" => "Estonia",
  84. "et" => "Ethiopia",
  85. "fk" => "Falkland Islands (Malvinas)",
  86. "fo" => "Faroe Islands",
  87. "fj" => "Fiji",
  88. "fi" => "Finland",
  89. "fr" => "France",
  90. "gf" => "French Guiana",
  91. "pf" => "French Polynesia",
  92. "tf" => "French Southern Territories",
  93. "ga" => "Gabon",
  94. "gm" => "Gambia",
  95. "ge" => "Georgia",
  96. "de" => "Germany",
  97. "gh" => "Ghana",
  98. "gi" => "Gibraltar",
  99. "gr" => "Greece",
  100. "gl" => "Greenland",
  101. "gd" => "Grenada",
  102. "gp" => "Guadeloupe",
  103. "gu" => "Guam",
  104. "gt" => "Guatemala",
  105. "gn" => "Guinea",
  106. "gw" => "Guinea-Bissau",
  107. "gy" => "Guyana",
  108. "ht" => "Haiti",
  109. "hm" => "Heard Island and Mcdonald Islands",
  110. "va" => "Holy See (Vatican City State)",
  111. "hn" => "Honduras",
  112. "hk" => "Hong Kong",
  113. "hu" => "Hungary",
  114. "is" => "Iceland",
  115. "in" => "India",
  116. "id" => "Indonesia",
  117. "ir" => "Iran, Islamic Republic",
  118. "iq" => "Iraq",
  119. "ie" => "Ireland",
  120. "il" => "Israel",
  121. "it" => "Italy",
  122. "jm" => "Jamaica",
  123. "jp" => "Japan",
  124. "jo" => "Jordan",
  125. "kz" => "Kazakhstan",
  126. "ke" => "Kenya",
  127. "ki" => "Kiribati",
  128. "kp" => "Korea, Democratic People's Republic",
  129. "kr" => "Korea, Republic",
  130. "kw" => "Kuwait",
  131. "kg" => "Kyrgyzstan",
  132. "la" => "Lao People's Democratic Republic",
  133. "lv" => "Latvia",
  134. "lb" => "Lebanon",
  135. "ls" => "Lesotho",
  136. "lr" => "Liberia",
  137. "ly" => "Libyan Arab Jamahiriya",
  138. "li" => "Liechtenstein",
  139. "lt" => "Lithuania",
  140. "lu" => "Luxembourg",
  141. "mo" => "Macao",
  142. "mk" => "Macedonia, the Former Yugosalv Republic",
  143. "mg" => "Madagascar",
  144. "mw" => "Malawi",
  145. "my" => "Malaysia",
  146. "mv" => "Maldives",
  147. "ml" => "Mali",
  148. "mt" => "Malta",
  149. "mh" => "Marshall Islands",
  150. "mq" => "Martinique",
  151. "mr" => "Mauritania",
  152. "mu" => "Mauritius",
  153. "yt" => "Mayotte",
  154. "mx" => "Mexico",
  155. "fm" => "Micronesia, Federated States",
  156. "md" => "Moldova, Republic",
  157. "mc" => "Monaco",
  158. "mn" => "Mongolia",
  159. "ms" => "Montserrat",
  160. "ma" => "Morocco",
  161. "mz" => "Mozambique",
  162. "mm" => "Myanmar",
  163. "na" => "Namibia",
  164. "nr" => "Nauru",
  165. "np" => "Nepal",
  166. "nl" => "Netherlands",
  167. "an" => "Netherlands Antilles",
  168. "nc" => "New Caledonia",
  169. "nz" => "New Zealand",
  170. "ni" => "Nicaragua",
  171. "ne" => "Niger",
  172. "ng" => "Nigeria",
  173. "nu" => "Niue",
  174. "nf" => "Norfolk Island",
  175. "mp" => "Northern Mariana Islands",
  176. "no" => "Norway",
  177. "om" => "Oman",
  178. "pk" => "Pakistan",
  179. "pw" => "Palau",
  180. "ps" => "Palestinian Territory, Occupied",
  181. "pa" => "Panama",
  182. "pg" => "Papua New Guinea",
  183. "py" => "Paraguay",
  184. "pe" => "Peru",
  185. "ph" => "Philippines",
  186. "pn" => "Pitcairn",
  187. "pl" => "Poland",
  188. "pt" => "Portugal",
  189. "pr" => "Puerto Rico",
  190. "qa" => "Qatar",
  191. "re" => "Reunion",
  192. "ro" => "Romania",
  193. "ru" => "Russian Federation",
  194. "rw" => "Rwanda",
  195. "sh" => "Saint Helena",
  196. "kn" => "Saint Kitts and Nevis",
  197. "lc" => "Saint Lucia",
  198. "pm" => "Saint Pierre and Miquelon",
  199. "vc" => "Saint Vincent and the Grenadines",
  200. "ws" => "Samoa",
  201. "sm" => "San Marino",
  202. "st" => "Sao Tome and Principe",
  203. "sa" => "Saudi Arabia",
  204. "sn" => "Senegal",
  205. "cs" => "Serbia and Montenegro",
  206. "sc" => "Seychelles",
  207. "sl" => "Sierra Leone",
  208. "sg" => "Singapore",
  209. "sk" => "Slovakia",
  210. "si" => "Slovenia",
  211. "sb" => "Solomon Islands",
  212. "so" => "Somalia",
  213. "za" => "South Africa",
  214. "gs" => "South Georgia and the South Sandwich Islands",
  215. "es" => "Spain",
  216. "lk" => "Sri Lanka",
  217. "sd" => "Sudan",
  218. "sr" => "Suriname",
  219. "sj" => "Svalbard and Jan Mayen",
  220. "sz" => "Swaziland",
  221. "se" => "Sweden",
  222. "ch" => "Switzerland",
  223. "sy" => "Syrian Arab Republic",
  224. "tw" => "Taiwan, Province of China",
  225. "tj" => "Tajikistan",
  226. "tz" => "Tanzania, United Republic",
  227. "th" => "Thailand",
  228. "tl" => "Timor-Leste",
  229. "tg" => "Togo",
  230. "tk" => "Tokelau",
  231. "to" => "Tonga",
  232. "tt" => "Trinidad and Tobago",
  233. "tn" => "Tunisia",
  234. "tr" => "Turkey",
  235. "tm" => "Turkmenistan",
  236. "tc" => "Turks and Caicos Islands",
  237. "tv" => "Tuvalu",
  238. "ug" => "Uganda",
  239. "ua" => "Ukraine",
  240. "ae" => "United Arab Emirates",
  241. "uk" => "United Kingdom",
  242. "us" => "United States",
  243. "um" => "United States Minor Outlying Islands",
  244. "uy" => "Uruguay",
  245. "uz" => "Uzbekistan",
  246. "vu" => "Vanuatu",
  247. "ve" => "Venezuela",
  248. "vn" => "Viet Nam",
  249. "vg" => "Virgin Islands, British",
  250. "vi" => "Virgin Islands, U.S.",
  251. "wf" => "Wallis and Futuna",
  252. "eh" => "Western Sahara",
  253. "ye" => "Yemen",
  254. "zm" => "Zambia",
  255. "zw" => "Zimbabwe"
  256. ]
  257. ],
  258. "nsfw" => [
  259. "display" => "NSFW",
  260. "option" => [
  261. "yes" => "Yes", // safe=active
  262. "no" => "No" // safe=off
  263. ]
  264. ]
  265. ];
  266. switch($page){
  267. case "web":
  268. return array_merge(
  269. $base,
  270. [
  271. "lang" => [ // lr=<lang> (prefix lang with "lang_")
  272. "display" => "Language",
  273. "option" => [
  274. "any" => "Any language",
  275. "ar" => "Arabic",
  276. "bg" => "Bulgarian",
  277. "ca" => "Catalan",
  278. "cs" => "Czech",
  279. "da" => "Danish",
  280. "de" => "German",
  281. "el" => "Greek",
  282. "en" => "English",
  283. "es" => "Spanish",
  284. "et" => "Estonian",
  285. "fi" => "Finnish",
  286. "fr" => "French",
  287. "hr" => "Croatian",
  288. "hu" => "Hungarian",
  289. "id" => "Indonesian",
  290. "is" => "Icelandic",
  291. "it" => "Italian",
  292. "iw" => "Hebrew",
  293. "ja" => "Japanese",
  294. "ko" => "Korean",
  295. "lt" => "Lithuanian",
  296. "lv" => "Latvian",
  297. "nl" => "Dutch",
  298. "no" => "Norwegian",
  299. "pl" => "Polish",
  300. "pt" => "Portuguese",
  301. "ro" => "Romanian",
  302. "ru" => "Russian",
  303. "sk" => "Slovak",
  304. "sl" => "Slovenian",
  305. "sr" => "Serbian",
  306. "sv" => "Swedish",
  307. "tr" => "Turkish",
  308. "zh-CN" => "Chinese (Simplified)",
  309. "zh-TW" => "Chinese (Traditional)"
  310. ]
  311. ],
  312. "newer" => [ // tbs
  313. "display" => "Newer than",
  314. "option" => "_DATE"
  315. ],
  316. "older" => [
  317. "display" => "Older than",
  318. "option" => "_DATE"
  319. ],
  320. "spellcheck" => [
  321. "display" => "Spellcheck",
  322. "option" => [
  323. "yes" => "Yes",
  324. "no" => "No"
  325. ]
  326. ]
  327. ]
  328. );
  329. break;
  330. case "images":
  331. return array_merge(
  332. $base,
  333. [
  334. "time" => [ // tbs=qdr:<time>
  335. "display" => "Time posted",
  336. "option" => [
  337. "any" => "Any time",
  338. "d" => "Past 24 hours",
  339. "w" => "Past week",
  340. "m" => "Past month",
  341. "y" => "Past year"
  342. ]
  343. ],
  344. "size" => [ // imgsz
  345. "display" => "Size",
  346. "option" => [
  347. "any" => "Any size",
  348. "l" => "Large",
  349. "m" => "Medium",
  350. "i" => "Icon",
  351. "qsvga" => "Larger than 400x300",
  352. "vga" => "Larger than 640x480",
  353. "svga" => "Larger than 800x600",
  354. "xga" => "Larger than 1024x768",
  355. "2mp" => "Larger than 2MP",
  356. "4mp" => "Larger than 4MP",
  357. "6mp" => "Larger than 6MP",
  358. "8mp" => "Larger than 8MP",
  359. "10mp" => "Larger than 10MP",
  360. "12mp" => "Larger than 12MP",
  361. "15mp" => "Larger than 15MP",
  362. "20mp" => "Larger than 20MP",
  363. "40mp" => "Larger than 40MP",
  364. "70mp" => "Larger than 70MP"
  365. ]
  366. ],
  367. "ratio" => [ // imgar
  368. "display" => "Aspect ratio",
  369. "option" => [
  370. "any" => "Any ratio",
  371. "t|xt" => "Tall",
  372. "s" => "Square",
  373. "w" => "Wide",
  374. "xw" => "Panoramic"
  375. ]
  376. ],
  377. "color" => [ // imgc
  378. "display" => "Color",
  379. "option" => [
  380. "any" => "Any color",
  381. "color" => "Full color",
  382. "bnw" => "Black & white",
  383. "trans" => "Transparent",
  384. // from here, imgcolor
  385. "red" => "Red",
  386. "orange" => "Orange",
  387. "yellow" => "Yellow",
  388. "green" => "Green",
  389. "teal" => "Teal",
  390. "blue" => "Blue",
  391. "purple" => "Purple",
  392. "pink" => "Pink",
  393. "white" => "White",
  394. "gray" => "Gray",
  395. "black" => "Black",
  396. "brown" => "Brown"
  397. ]
  398. ],
  399. "type" => [ // tbs=itp:<type>
  400. "display" => "Type",
  401. "option" => [
  402. "any" => "Any type",
  403. "clipart" => "Clip Art",
  404. "lineart" => "Line Drawing",
  405. "animated" => "Animated"
  406. ]
  407. ],
  408. "format" => [ // as_filetype
  409. "display" => "Format",
  410. "option" => [
  411. "any" => "Any format",
  412. "jpg" => "JPG",
  413. "gif" => "GIF",
  414. "png" => "PNG",
  415. "bmp" => "BMP",
  416. "svg" => "SVG",
  417. "webp" => "WEBP",
  418. "ico" => "ICO",
  419. "craw" => "RAW"
  420. ]
  421. ],
  422. "rights" => [ // tbs=sur:<rights>
  423. "display" => "Usage rights",
  424. "option" => [
  425. "any" => "Any license",
  426. "cl" => "Creative Commons licenses",
  427. "ol" => "Commercial & other licenses"
  428. ]
  429. ]
  430. ]
  431. );
  432. break;
  433. case "videos":
  434. return array_merge(
  435. $base,
  436. [
  437. "newer" => [ // tbs
  438. "display" => "Newer than",
  439. "option" => "_DATE"
  440. ],
  441. "older" => [
  442. "display" => "Older than",
  443. "option" => "_DATE"
  444. ],
  445. "duration" => [
  446. "display" => "Duration",
  447. "option" => [
  448. "any" => "Any duration",
  449. "s" => "Short (0-4min)", // tbs=dur:s
  450. "m" => "Medium (4-20min)", // tbs=dur:m
  451. "l" => "Long (20+ min)" // tbs=dur:l
  452. ]
  453. ],
  454. "quality" => [
  455. "display" => "Quality",
  456. "option" => [
  457. "any" => "Any quality",
  458. "h" => "High quality" // tbs=hq:h
  459. ]
  460. ],
  461. "captions" => [
  462. "display" => "Captions",
  463. "option" => [
  464. "any" => "No preference",
  465. "yes" => "Closed captioned" // tbs=cc:1
  466. ]
  467. ]
  468. ]
  469. );
  470. break;
  471. case "news":
  472. return array_merge(
  473. $base,
  474. [
  475. "newer" => [ // tbs
  476. "display" => "Newer than",
  477. "option" => "_DATE"
  478. ],
  479. "older" => [
  480. "display" => "Older than",
  481. "option" => "_DATE"
  482. ],
  483. "sort" => [
  484. "display" => "Sort",
  485. "option" => [
  486. "relevance" => "Relevance",
  487. "date" => "Date" // sbd:1
  488. ]
  489. ]
  490. ]
  491. );
  492. break;
  493. }
  494. }
  495. private function get($proxy, $url, $get = [], $alt_ua = false){
  496. $curlproc = curl_init();
  497. if($get !== []){
  498. $get = http_build_query($get);
  499. $url .= "?" . $get;
  500. }
  501. curl_setopt($curlproc, CURLOPT_URL, $url);
  502. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  503. if($alt_ua === true){
  504. curl_setopt($curlproc, CURLOPT_HTTPHEADER, [
  505. "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 26_0_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) GSA/406.0.862495628 Mobile/15E148 Safari/604.1",
  506. "Accept: text/html, application/xml;q=0.9, */*;q=0.8",
  507. "Accept-Language: en-US,en;q=0.8",
  508. "Accept-Encoding: gzip, deflate",
  509. "Connection: Keep-Alive",
  510. "Cache-Control: no-cache"
  511. ]);
  512. }else{
  513. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  514. curl_setopt($curlproc, CURLOPT_HTTPHEADER, [
  515. "User-Agent: " . config::USER_AGENT,
  516. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  517. "Accept-Language: en-US,en;q=0.5",
  518. "Accept-Encoding: gzip",
  519. "DNT: 1",
  520. "Connection: keep-alive",
  521. "Upgrade-Insecure-Requests: 1",
  522. "Sec-Fetch-Dest: document",
  523. "Sec-Fetch-Mode: navigate",
  524. "Sec-Fetch-Site: none",
  525. "Sec-Fetch-User: ?1",
  526. "Priority: u=1",
  527. "TE: trailers"
  528. ]);
  529. }
  530. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  531. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  532. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  533. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  534. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  535. // follow redirects
  536. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  537. $this->backend->assign_proxy($curlproc, $proxy);
  538. $data = curl_exec($curlproc);
  539. if(curl_errno($curlproc)){
  540. throw new Exception(curl_error($curlproc));
  541. }
  542. curl_close($curlproc);
  543. return $data;
  544. }
  545. public function web($get){
  546. throw new Exception("There are no known ways to scrape Google's /search endpoint without JS at this time. I'm working on a method that extracts cookies from browsers. Use Google API/CSE/Yahoo JP/Startpage for google results for now.");
  547. }
  548. public function video($get){
  549. throw new Exception("There are no known ways to scrape Google's /search endpoint without JS at this time. I'm working on a method that extracts cookies from browsers. Use Google API/CSE/Yahoo JP/Startpage for google results for now.");
  550. }
  551. public function news($get){
  552. throw new Exception("There are no known ways to scrape Google's /search endpoint without JS at this time. I'm working on a method that extracts cookies from browsers. Use Google API/CSE/Yahoo JP/Startpage for google results for now.");
  553. }
  554. public function image($get){
  555. // generate parameters
  556. if($get["npt"]){
  557. [$params, $proxy] =
  558. $this->backend->get(
  559. $get["npt"],
  560. "images"
  561. );
  562. $params = json_decode($params, true);
  563. $page = $params["page"] + 1;
  564. $params = $params["params"];
  565. $params["async"] = "_fmt:json,p:1,ijn:{$page}";
  566. }else{
  567. $search = $get["s"];
  568. if(strlen($search) === 0){
  569. throw new Exception("Search term is empty!");
  570. }
  571. $proxy = $this->backend->get_ip();
  572. $country = $get["country"];
  573. $nsfw = $get["nsfw"];
  574. $time = $get["time"];
  575. $size = $get["size"];
  576. $ratio = $get["ratio"];
  577. $color = $get["color"];
  578. $type = $get["type"];
  579. $format = $get["format"];
  580. $rights = $get["rights"];
  581. $page = 0;
  582. $params = [
  583. "q" => $search,
  584. "tbm" => "isch",
  585. "asearch" => "isch",
  586. "async" => "_fmt:json,p:0,ijn:{$page}", // ijn:0 = page 1
  587. ];
  588. // country (image search uses cr instead of gl)
  589. if($country != "any"){
  590. $params["cr"] = "country" . strtoupper($country);
  591. }
  592. // nsfw
  593. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  594. // generate tbs
  595. $tbs = [];
  596. // time
  597. if($time != "any"){
  598. $tbs["qdr"] = $time;
  599. }
  600. // size
  601. if($size != "any"){
  602. $params["imgsz"] = $size;
  603. }
  604. // ratio
  605. if($ratio != "any"){
  606. $params["imgar"] = $ratio;
  607. }
  608. // color
  609. if($color != "any"){
  610. if(
  611. $color == "color" ||
  612. $color == "trans"
  613. ){
  614. $params["imgc"] = $color;
  615. }elseif($color == "bnw"){
  616. $params["imgc"] = "gray";
  617. }else{
  618. $tbs["ic"] = "specific";
  619. $tbs["isc"] = $color;
  620. }
  621. }
  622. // type
  623. if($type != "any"){
  624. $tbs["itp"] = $type;
  625. }
  626. // format
  627. if($format != "any"){
  628. $params["as_filetype"] = $format;
  629. }
  630. // rights (tbs)
  631. if($rights != "any"){
  632. $tbs["sur"] = $rights;
  633. }
  634. // append tbs
  635. if(count($tbs) !== 0){
  636. $params["tbs"] = "";
  637. foreach($tbs as $key => $value){
  638. $params["tbs"] .= $key . ":" . $value . ",";
  639. }
  640. $params["tbs"] = rtrim($params["tbs"], ",");
  641. }
  642. }
  643. try{
  644. $json =
  645. $this->get(
  646. $proxy,
  647. "https://www.google.com/search",
  648. $params
  649. );
  650. }catch(Exception $error){
  651. throw new Exception("Failed to get search page");
  652. }
  653. unset($params["async"]);
  654. //$json = file_get_contents("scraper/google.json");
  655. // detect captcha
  656. $this->fuckhtml->load($json);
  657. $this->detect_sorry();
  658. // remove xssi
  659. $json =
  660. preg_replace(
  661. '/^[^{]*/',
  662. "",
  663. $json
  664. );
  665. $json = json_decode($json, true);
  666. if($json === null){
  667. throw new Exception("Failed to decode JSON");
  668. }
  669. $out = [
  670. "status" => "ok",
  671. "npt" => null,
  672. "image" => []
  673. ];
  674. if(!isset($json["ischj"]["metadata"])){
  675. throw new Exception("Google did not return an image array");
  676. }
  677. foreach($json["ischj"]["metadata"] as $image){
  678. $out["image"][] = [
  679. "title" => $this->titledots($image["result"]["page_title"]),
  680. "source" => [
  681. [
  682. "url" => $image["original_image"]["url"],
  683. "width" => (int)$image["original_image"]["width"],
  684. "height" => (int)$image["original_image"]["height"]
  685. ],
  686. [
  687. "url" => $image["thumbnail"]["url"],
  688. "width" => (int)$image["thumbnail"]["width"],
  689. "height" => (int)$image["thumbnail"]["height"]
  690. ]
  691. ],
  692. "url" => $image["result"]["referrer_url"]
  693. ];
  694. }
  695. $page++;
  696. if(count($out["image"]) === 10){
  697. $out["npt"] =
  698. $this->backend->store(
  699. json_encode([
  700. "params" => $params,
  701. "page" => $page
  702. ]),
  703. "images",
  704. $proxy
  705. );
  706. }
  707. return $out;
  708. }
  709. private function scrape_dimg($html){
  710. // get images loaded through javascript
  711. $this->dimg = [];
  712. preg_match_all(
  713. '/function\(\){google\.ldi=({.*?});/',
  714. $html,
  715. $dimg
  716. );
  717. if(isset($dimg[1])){
  718. foreach($dimg[1] as $i){
  719. $tmp = json_decode($i, true);
  720. foreach($tmp as $key => $value){
  721. $this->dimg[$key] =
  722. $this->unshit_thumb(
  723. $value
  724. );
  725. }
  726. }
  727. }
  728. // get additional javascript base64 images
  729. preg_match_all(
  730. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  731. $html,
  732. $dimg
  733. );
  734. if(isset($dimg[1])){
  735. for($i=0; $i<count($dimg[1]); $i++){
  736. $delims = explode(",", $dimg[2][$i]);
  737. $string =
  738. $this->fuckhtml
  739. ->parseJsString(
  740. $dimg[1][$i]
  741. );
  742. foreach($delims as $delim){
  743. $this->dimg[trim($delim, "'")] = $string;
  744. }
  745. }
  746. }
  747. }
  748. private function scrape_imagearr($html){
  749. // get image links arrays
  750. preg_match_all(
  751. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  752. $html,
  753. $image_arr
  754. );
  755. $this->image_arr = [];
  756. if(isset($image_arr[1])){
  757. for($i=0; $i<count($image_arr[1]); $i++){
  758. $original =
  759. $this->fuckhtml
  760. ->parseJsString(
  761. $image_arr[5][$i]
  762. );
  763. if(
  764. preg_match(
  765. '/^x-raw-image/',
  766. $original
  767. )
  768. ){
  769. // only add thumbnail, google doesnt have OG resolution
  770. $this->image_arr[$image_arr[1][$i]] = [
  771. [
  772. "url" =>
  773. $this->unshit_thumb(
  774. $this->fuckhtml
  775. ->parseJsString(
  776. $image_arr[2][$i]
  777. )
  778. ),
  779. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  780. "height" => (int)$image_arr[6][$i]
  781. ]
  782. ];
  783. continue;
  784. }
  785. $this->image_arr[$image_arr[1][$i]] =
  786. [
  787. [
  788. "url" => $original,
  789. "width" => (int)$image_arr[7][$i],
  790. "height" => (int)$image_arr[6][$i]
  791. ],
  792. [
  793. "url" =>
  794. $this->unshit_thumb(
  795. $this->fuckhtml
  796. ->parseJsString(
  797. $image_arr[2][$i]
  798. )
  799. ),
  800. "width" => (int)$image_arr[4][$i],
  801. "height" => (int)$image_arr[3][$i]
  802. ]
  803. ];
  804. }
  805. }
  806. }
  807. private function getdimg($dimg){
  808. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  809. }
  810. private function unshit_thumb($url, $get_bigger_res = false){
  811. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  812. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  813. $parts = parse_url($url);
  814. if(
  815. isset($parts["host"]) &&
  816. preg_match(
  817. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  818. $parts["host"]
  819. )
  820. ){
  821. parse_str($parts["query"], $params);
  822. if(isset($params["q"])){
  823. if($get_bigger_res){
  824. // this method doesnt always work, but does work for wiki thumbnails
  825. return
  826. "https://" . $parts["host"] . "/images?q=tbn:" .
  827. $this->base64url_encode(
  828. substr(
  829. $this->base64url_decode(
  830. explode(
  831. ":",
  832. $params["q"])[1]
  833. ),
  834. 0,
  835. 29
  836. )
  837. );
  838. }else{
  839. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  840. }
  841. }
  842. }
  843. return $url;
  844. }
  845. private function parsestyles(){
  846. $styles = [];
  847. $style_div =
  848. $this->fuckhtml
  849. ->getElementsByTagName(
  850. "style"
  851. );
  852. $raw_styles = "";
  853. foreach($style_div as $style){
  854. $raw_styles .= $style["innerHTML"];
  855. }
  856. // filter out media/keyframe queries
  857. $raw_styles =
  858. preg_replace(
  859. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  860. "",
  861. $raw_styles
  862. );
  863. // get styles
  864. preg_match_all(
  865. '/(.+?){([\S\s]*?)}/',
  866. $raw_styles,
  867. $matches
  868. );
  869. for($i=0; $i<count($matches[1]); $i++){
  870. // get style values
  871. preg_match_all(
  872. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  873. $matches[2][$i],
  874. $values_regex
  875. );
  876. $values = [];
  877. for($k=0; $k<count($values_regex[1]); $k++){
  878. $values[trim($values_regex[1][$k])] =
  879. strtolower(trim($values_regex[2][$k]));
  880. }
  881. $names = explode(",", $matches[1][$i]);
  882. // h1,h2,h3 will each get their own array index
  883. foreach($names as $name){
  884. $name = trim($name, "}\t\n\r\0\x0B");
  885. foreach($values as $key => $value){
  886. $styles[$name][$key] = $value;
  887. }
  888. }
  889. }
  890. foreach($styles as $key => $values){
  891. $styles[$key]["_c"] = count($values);
  892. }
  893. $this->styles = $styles;
  894. // get CSS colors
  895. $this->css_colors = [];
  896. if(isset($this->styles[":root"])){
  897. foreach($this->styles[":root"] as $key => $value){
  898. $this->css_colors[$value] = strtolower($key);
  899. }
  900. }
  901. }
  902. private function getstyle($styles){
  903. $styles["_c"] = count($styles);
  904. foreach($this->styles as $style_key => $style_values){
  905. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  906. $style_key =
  907. explode(" ", $style_key);
  908. $style_key = $style_key[count($style_key) - 1];
  909. return
  910. ltrim(
  911. str_replace(
  912. [".", "#"],
  913. " ",
  914. $style_key
  915. )
  916. );
  917. }
  918. }
  919. return false;
  920. }
  921. private function getcolorvar($color){
  922. if(isset($this->css_colors[$color])){
  923. return $this->css_colors[$color];
  924. }
  925. return null;
  926. }
  927. private function unshiturl($url, $return_size = false){
  928. // decode
  929. $url =
  930. $this->fuckhtml
  931. ->getTextContent(
  932. $url
  933. );
  934. $url_parts = parse_url($url);
  935. if(isset($url_parts["query"])){
  936. parse_str($url_parts["query"], $query);
  937. }else{
  938. $query = [];
  939. }
  940. if(
  941. !isset(
  942. $url_parts["host"]
  943. ) ||
  944. stripos($url_parts["host"], "google.") !== false
  945. ){
  946. // no host, we have a tracking url
  947. if(isset($query["imgurl"])){
  948. $url = $query["imgurl"];
  949. }
  950. elseif(isset($query["q"])){
  951. $url = $query["q"];
  952. }
  953. }
  954. // rewrite URLs to remove extra tracking parameters
  955. $domain = parse_url($url, PHP_URL_HOST);
  956. if(
  957. preg_match(
  958. '/wikipedia.org$/',
  959. $domain
  960. )
  961. ){
  962. // rewrite wikipedia mobile URLs to desktop
  963. $url =
  964. $this->replacedomain(
  965. $url,
  966. preg_replace(
  967. '/([a-z0-9]+)(\.m\.)/',
  968. '$1.',
  969. $domain
  970. )
  971. );
  972. }
  973. elseif(
  974. preg_match(
  975. '/imdb\.com$|youtube\.[^.]+$/',
  976. $domain
  977. )
  978. ){
  979. // rewrite imdb and youtube mobile URLs too
  980. $url =
  981. $this->replacedomain(
  982. $url,
  983. preg_replace(
  984. '/^m\./',
  985. "",
  986. $domain
  987. )
  988. );
  989. }
  990. elseif(
  991. preg_match(
  992. '/play\.google\.[^.]+$/',
  993. $domain
  994. )
  995. ){
  996. // remove referrers from play.google.com
  997. $u_query = parse_url($url, PHP_URL_QUERY);
  998. if($u_query !== null){
  999. parse_str($u_query, $u_query);
  1000. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  1001. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  1002. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  1003. $query = http_build_query($query);
  1004. $url =
  1005. str_replace(
  1006. $u_query,
  1007. $u_query,
  1008. $url
  1009. );
  1010. }
  1011. }
  1012. elseif(
  1013. preg_match(
  1014. '/twitter\.com$/',
  1015. $domain
  1016. )
  1017. ){
  1018. // remove more referrers from twitter.com
  1019. $u_query = parse_url($url, PHP_URL_QUERY);
  1020. if($u_query !== null){
  1021. parse_str($u_query, $u_query);
  1022. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  1023. $u_query = http_build_query($u_query);
  1024. $url =
  1025. str_replace(
  1026. $oldquery,
  1027. $u_query,
  1028. $url
  1029. );
  1030. }
  1031. }
  1032. elseif(
  1033. preg_match(
  1034. '/maps\.google\.[^.]+/',
  1035. $domain
  1036. )
  1037. ){
  1038. if(stripos($url, "maps?") !== false){
  1039. $u_query = parse_url($url, PHP_URL_QUERY);
  1040. if($u_query !== null){
  1041. parse_str($u_query, $u_query);
  1042. if(isset($u_query["daddr"])){
  1043. $url =
  1044. "https://maps.google.com/maps?daddr=" .
  1045. urlencode($u_query["daddr"]);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. if($return_size){
  1051. return [
  1052. "url" => $url,
  1053. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  1054. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  1055. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  1056. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  1057. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  1058. ];
  1059. }
  1060. return $url;
  1061. }
  1062. private function replacedomain($url, $domain){
  1063. return
  1064. preg_replace(
  1065. '/(https?:\/\/)([^\/]+)/',
  1066. '$1' . $domain,
  1067. $url
  1068. );
  1069. }
  1070. private function titledots($title){
  1071. return trim($title, " .\t\n\r\0\x0B…");
  1072. }
  1073. private function hms2int($time){
  1074. $parts = explode(":", $time, 3);
  1075. $time = 0;
  1076. if(count($parts) === 3){
  1077. // hours
  1078. $time = $time + ((int)$parts[0] * 3600);
  1079. array_shift($parts);
  1080. }
  1081. if(count($parts) === 2){
  1082. // minutes
  1083. $time = $time + ((int)$parts[0] * 60);
  1084. array_shift($parts);
  1085. }
  1086. // seconds
  1087. $time = $time + (int)$parts[0];
  1088. return $time;
  1089. }
  1090. function base64url_decode($data){
  1091. $b64 = strtr($data, "-_", "+/");
  1092. $pad = strlen($b64) % 4;
  1093. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  1094. return base64_decode($b64);
  1095. }
  1096. function base64url_encode($data){
  1097. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  1098. }
  1099. private function detect_sorry(){
  1100. $captcha_form =
  1101. $this->fuckhtml
  1102. ->getElementById(
  1103. "captcha-form",
  1104. "form"
  1105. );
  1106. if($captcha_form !== false){
  1107. throw new Exception("Google returned a captcha");
  1108. }
  1109. }
  1110. }