google.php 28 KB

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