google.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470
  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. // it broke again. lasted 3 months
  546. // lets hope for another solid 3 month
  547. $out = [
  548. "status" => "ok",
  549. "spelling" => [
  550. "type" => "no_correction",
  551. "using" => null,
  552. "correction" => null
  553. ],
  554. "npt" => null,
  555. "answer" => [],
  556. "web" => [],
  557. "image" => [],
  558. "video" => [],
  559. "news" => [],
  560. "related" => []
  561. ];
  562. if($get["npt"]){
  563. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  564. try{
  565. $html =
  566. $this->get(
  567. $proxy,
  568. "https://www.google.com" . $get,
  569. [],
  570. true
  571. );
  572. }catch(Exception $error){
  573. throw new Exception("Failed to get HTML");
  574. }
  575. }else{
  576. $search = $get["s"];
  577. $country = $get["country"];
  578. $nsfw = $get["nsfw"];
  579. $lang = $get["lang"];
  580. $older = $get["older"];
  581. $newer = $get["newer"];
  582. $spellcheck = $get["spellcheck"];
  583. $proxy = $this->backend->get_ip();
  584. $offset = 0;
  585. $params = [
  586. "q" => $search,
  587. "hl" => "en",
  588. "udm" => 14
  589. ];
  590. // country
  591. if($country != "any"){
  592. $params["gl"] = $country;
  593. }
  594. // nsfw
  595. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  596. // language
  597. if($lang != "any"){
  598. $params["lr"] = "lang_" . $lang;
  599. }
  600. // generate tbs
  601. $tbs = [];
  602. // get date
  603. $older = $older === false ? null : date("m/d/Y", $older);
  604. $newer = $newer === false ? null : date("m/d/Y", $newer);
  605. if(
  606. $older !== null ||
  607. $newer !== null
  608. ){
  609. $tbs["cdr"] = "1";
  610. $tbs["cd_min"] = $newer;
  611. $tbs["cd_max"] = $older;
  612. }
  613. // spellcheck filter
  614. if($spellcheck == "no"){
  615. $params["nfpr"] = "1";
  616. }
  617. if(count($tbs) !== 0){
  618. $params["tbs"] = "";
  619. foreach($tbs as $key => $value){
  620. $params["tbs"] .= $key . ":" . $value . ",";
  621. }
  622. $params["tbs"] = rtrim($params["tbs"], ",");
  623. }
  624. try{
  625. $html =
  626. $this->get(
  627. $proxy,
  628. "https://www.google.com/search",
  629. $params,
  630. true
  631. );
  632. }catch(Exception $error){
  633. throw new Exception("Failed to get HTML");
  634. }
  635. //$html = file_get_contents("scraper/google.html");
  636. }
  637. // init
  638. $this->fuckhtml->load($html);
  639. $this->detect_sorry();
  640. $this->parsestyles();
  641. // get javascript images
  642. $this->scrape_dimg($html);
  643. $this->scrape_imagearr($html);
  644. // get next page
  645. $npt =
  646. $this->fuckhtml
  647. ->getElementsByAttributeValue(
  648. "aria-label",
  649. "More search results",
  650. "a"
  651. );
  652. if(count($npt) === 0){
  653. // maybe we have the npt object from 2nd page, probe for that
  654. $npt =
  655. $this->fuckhtml
  656. ->getElementsByAttributeValue(
  657. "aria-label",
  658. "Next page",
  659. "a"
  660. );
  661. }
  662. if(count($npt) !== 0){
  663. $out["npt"] =
  664. $this->backend->store(
  665. $this->fuckhtml
  666. ->getTextContent(
  667. $npt[0]["attributes"]["href"]
  668. ),
  669. "web",
  670. $proxy
  671. );
  672. }
  673. // outer div is .MjjYud
  674. // inner div always contain role="presentation"
  675. $outer =
  676. $this->fuckhtml
  677. ->getElementsByClassName(
  678. "MjjYud",
  679. "div"
  680. );
  681. // used later
  682. $fancycontainer_class =
  683. explode(
  684. " ",
  685. $this->getstyle([
  686. "padding-top" => "4px",
  687. "padding-bottom" => "calc(12px*1)"
  688. ]),
  689. 2
  690. );
  691. if(count($fancycontainer_class) === 2){
  692. $fancycontainer_class = $fancycontainer_class[1];
  693. }else{
  694. $fancycontainer_class = false;
  695. }
  696. foreach($outer as $container){
  697. $this->fuckhtml->load($container);
  698. // probe for search result
  699. $title =
  700. $this->fuckhtml
  701. ->getElementsByAttributeValue(
  702. "role",
  703. "link",
  704. "div"
  705. );
  706. if(count($title) !== 0){
  707. // we found a search result
  708. $title =
  709. $this->titledots(
  710. $this->fuckhtml
  711. ->getTextContent(
  712. $title[0]
  713. )
  714. );
  715. // get url
  716. $sprobe =
  717. $this->fuckhtml
  718. ->getElementsByTagName(
  719. "a"
  720. );
  721. $link = null;
  722. foreach($sprobe as $possible_link){
  723. if(
  724. isset($possible_link["attributes"]["href"]) &&
  725. preg_match(
  726. '/^\/url\?q=/',
  727. $possible_link["attributes"]["href"]
  728. )
  729. ){
  730. $link =
  731. $this->fuckhtml
  732. ->getTextContent(
  733. $possible_link["attributes"]["href"]
  734. );
  735. break;
  736. }
  737. }
  738. if($link === null){
  739. // should not happen
  740. continue;
  741. }
  742. // get description
  743. // as usual, theres a thousand fucking possible divs for this one
  744. // probe for youtube-like description
  745. $description =
  746. $this->fuckhtml
  747. ->getElementsByClassName(
  748. $this->getstyle([
  749. "align-items" => "flex-start",
  750. "display" => "flex",
  751. "justify-content" => "center",
  752. "padding" => "7px 12px",
  753. "padding-right" => "0",
  754. "padding-top" => "0"
  755. ]),
  756. "div"
  757. );
  758. $ratio = "16:9";
  759. if(count($description) === 0){
  760. // fail. find the one with the image on the right handside
  761. $description =
  762. $this->fuckhtml
  763. ->getElementsByAttributeValue(
  764. "style",
  765. "padding-top:2px;padding-right:8px;padding-left:16px;padding-bottom:12px",
  766. "div"
  767. );
  768. $ratio = "1:1";
  769. if(count($description) === 0){
  770. // fail. find the one that is used the most
  771. $description =
  772. $this->fuckhtml
  773. ->getElementsByAttributeValue(
  774. "style",
  775. "-webkit-line-clamp:3",
  776. "div"
  777. );
  778. if(count($description) === 0){
  779. // last fail. this one appears with divs that have prices
  780. $description =
  781. $this->fuckhtml
  782. ->getElementsByAttributeValue(
  783. "style",
  784. "max-width:100vw;grid-area:nke7rc;padding-top:2px;padding-right:8px;padding-left:16px;padding-bottom:6px",
  785. "div"
  786. );
  787. }
  788. }
  789. }
  790. if(count($description) === 0){
  791. // should not happen but whatever
  792. $description = null;
  793. }else{
  794. $description =
  795. $this->titledots(
  796. $this->fuckhtml
  797. ->getTextContent(
  798. $description[0]
  799. )
  800. );
  801. }
  802. // probe for date
  803. $desc2 = explode("—", $description, 2);
  804. $time = null;
  805. if(count($desc2) === 2){
  806. $time = strtotime($desc2[0]);
  807. if(
  808. strlen($desc2[0]) < 16 &&
  809. $time !== false
  810. ){
  811. $description = ltrim($desc2[1]);
  812. }else{
  813. $time = null;
  814. }
  815. }
  816. $thumb = [
  817. "ratio" => null,
  818. "url" => null,
  819. ];
  820. // get thumbnail
  821. $images =
  822. $this->fuckhtml
  823. ->getElementsByTagName(
  824. "img"
  825. );
  826. foreach($images as $image){
  827. if(isset($image["attributes"]["id"])){
  828. $thumb = [
  829. "ratio" => $ratio,
  830. "url" => $this->getdimg($image["attributes"]["id"])
  831. ];
  832. }
  833. }
  834. // get sublinks
  835. $sublinks = [];
  836. // probe for the fancy version
  837. if($fancycontainer_class !== false){
  838. $fancycontainer =
  839. $this->fuckhtml
  840. ->getElementsByClassName(
  841. $fancycontainer_class,
  842. "div"
  843. );
  844. }
  845. if(
  846. $fancycontainer_class !== false &&
  847. count($fancycontainer) !== 0
  848. ){
  849. $this->fuckhtml->load($fancycontainer[0]);
  850. $as =
  851. $this->fuckhtml
  852. ->getElementsByTagName(
  853. "a"
  854. );
  855. foreach($as as $a){
  856. $sublinks[] = [
  857. "title" =>
  858. $this->fuckhtml
  859. ->getTextContent(
  860. $a
  861. ),
  862. "description" => null,
  863. "date" => null,
  864. "url" =>
  865. $this->unshiturl(
  866. $a["attributes"]["href"]
  867. )
  868. ];
  869. }
  870. }
  871. $out["web"][] = [
  872. "title" => $title,
  873. "description" => $description,
  874. "url" => $this->unshiturl($link),
  875. "date" => $time,
  876. "type" => "web",
  877. "thumb" => $thumb,
  878. "sublink" => $sublinks,
  879. "table" => []
  880. ];
  881. continue;
  882. }
  883. // probe for containers with a title header
  884. $title_header =
  885. $this->fuckhtml
  886. ->getElementsByClassName(
  887. $this->getstyle([
  888. "display" => "flex",
  889. "flex-wrap" => "wrap",
  890. "position" => "relative",
  891. "padding" => "16px"
  892. ])
  893. );
  894. if(count($title_header) !== 0){
  895. $title_header =
  896. strtolower(
  897. $this->fuckhtml
  898. ->getTextContent(
  899. $title_header[0]
  900. )
  901. );
  902. switch($title_header){
  903. case "people also search for":
  904. // get all related searches
  905. $relateds =
  906. $this->fuckhtml
  907. ->getElementsByClassName(
  908. $this->getstyle([
  909. "display" => "flex",
  910. "height" => "100%",
  911. "flex-direction" => "column",
  912. "max-width" => "100%"
  913. ])
  914. );
  915. foreach($relateds as $r){
  916. $out["related"][] =
  917. $this->fuckhtml
  918. ->getTextContent(
  919. $r
  920. );
  921. }
  922. break;
  923. }
  924. continue;
  925. }
  926. }
  927. $out["related"] = array_values(array_unique($out["related"]));
  928. return $out;
  929. }
  930. public function video($get){
  931. if($get["npt"]){
  932. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  933. $params = json_decode($params, true);
  934. }else{
  935. $search = $get["s"];
  936. $country = $get["country"];
  937. $nsfw = $get["nsfw"];
  938. $older = $get["older"];
  939. $newer = $get["newer"];
  940. $duration = $get["duration"];
  941. $quality = $get["quality"];
  942. $captions = $get["captions"];
  943. $proxy = $this->backend->get_ip();
  944. $params = [
  945. "q" => $search,
  946. "udm" => "7",
  947. "hl" => "en",
  948. "num" => 20
  949. ];
  950. // country
  951. if($country != "any"){
  952. $params["gl"] = $country;
  953. }
  954. // nsfw
  955. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  956. $tbs = [];
  957. // get date
  958. $older = $older === false ? null : date("m/d/Y", $older);
  959. $newer = $newer === false ? null : date("m/d/Y", $newer);
  960. if(
  961. $older !== null ||
  962. $newer !== null
  963. ){
  964. $tbs["cdr"] = "1";
  965. $tbs["cd_min"] = $newer;
  966. $tbs["cd_max"] = $older;
  967. }
  968. // duration
  969. if($duration != "any"){
  970. $tbs[] = "dur:" . $duration;
  971. }
  972. // quality
  973. if($quality != "any"){
  974. $tbs[] = "hq:" . $quality;
  975. }
  976. // captions
  977. if($captions != "any"){
  978. $tbs[] = "cc:" . $captions;
  979. }
  980. // append tbs
  981. if(count($tbs) !== 0){
  982. $params["tbs"] =
  983. implode(",", $tbs);
  984. }
  985. }
  986. try{
  987. $html =
  988. $this->get(
  989. $proxy,
  990. "https://www.google.com/search",
  991. $params
  992. );
  993. }catch(Exception $error){
  994. throw new Exception("Failed to get HTML");
  995. }
  996. if(!isset($params["start"])){
  997. $params["start"] = 0;
  998. }
  999. $params["start"] += 20;
  1000. $this->fuckhtml->load($html);
  1001. //
  1002. // Parse web video page
  1003. //
  1004. $this->detect_sorry();
  1005. // parse all <style> tags
  1006. $this->parsestyles();
  1007. // get javascript images
  1008. $this->scrape_dimg($html);
  1009. $this->scrape_imagearr($html);
  1010. $out = [
  1011. "status" => "ok",
  1012. "npt" =>
  1013. $this->backend->store(
  1014. json_encode($params),
  1015. "videos",
  1016. $proxy
  1017. ),
  1018. "video" => [],
  1019. "author" => [],
  1020. "livestream" => [],
  1021. "playlist" => [],
  1022. "reel" => []
  1023. ];
  1024. $search_div =
  1025. $this->fuckhtml
  1026. ->getElementById(
  1027. "center_col"
  1028. );
  1029. if($search_div === false){
  1030. throw new Exception("Failed to grep search div");
  1031. }
  1032. $this->fuckhtml->load($search_div);
  1033. $results =
  1034. $this->fuckhtml
  1035. ->getElementsByClassName(
  1036. $this->getstyle([
  1037. "margin" => "0px 0px 30px"
  1038. ]),
  1039. "div"
  1040. );
  1041. foreach($results as $result){
  1042. $this->fuckhtml->load($result);
  1043. $url =
  1044. $this->fuckhtml
  1045. ->getElementsByTagName(
  1046. "a"
  1047. );
  1048. if(count($url) === 0){
  1049. // no url, weird, continue
  1050. continue;
  1051. }
  1052. $title =
  1053. $this->fuckhtml
  1054. ->getElementsByTagName(
  1055. "h3"
  1056. );
  1057. if(count($title) === 0){
  1058. // no title, weird, continue
  1059. continue;
  1060. }
  1061. // get description
  1062. $description =
  1063. $this->fuckhtml
  1064. ->getElementsByClassName(
  1065. $this->getstyle([
  1066. "-webkit-box-orient" => "vertical",
  1067. "display" => "-webkit-box",
  1068. "-webkit-line-clamp" => "2",
  1069. "overflow" => "hidden",
  1070. "word-break" => "break-word"
  1071. ]),
  1072. "div"
  1073. );
  1074. if(count($description) === 0){
  1075. $description = null;
  1076. }else{
  1077. $description =
  1078. html_entity_decode(
  1079. $this->titledots(
  1080. $this->fuckhtml
  1081. ->getTextContent(
  1082. $description[0]
  1083. )
  1084. )
  1085. );
  1086. }
  1087. // get author + date posted
  1088. $metadiv =
  1089. $this->fuckhtml
  1090. ->getElementsByClassName(
  1091. $this->getstyle([
  1092. "margin-top" => "12px"
  1093. ]),
  1094. "div"
  1095. );
  1096. $author = null;
  1097. $date = null;
  1098. if(count($metadiv) !== 0){
  1099. $metadiv =
  1100. explode(
  1101. "·",
  1102. $this->fuckhtml
  1103. ->getTextContent(
  1104. $metadiv[0]
  1105. )
  1106. );
  1107. if(count($metadiv) === 3){
  1108. $author = trim($metadiv[1]);
  1109. $date = strtotime(trim($metadiv[2]));
  1110. }elseif(count($metadiv) === 2){
  1111. $author = trim($metadiv[0]);
  1112. $date = strtotime(trim($metadiv[1]));
  1113. }
  1114. }
  1115. $thumb = [
  1116. "url" => null,
  1117. "ratio" => null
  1118. ];
  1119. $image =
  1120. $this->fuckhtml
  1121. ->getElementsByTagName(
  1122. "img"
  1123. );
  1124. $duration = null;
  1125. if(
  1126. count($image) !== 0 &&
  1127. isset($image[0]["attributes"]["id"])
  1128. ){
  1129. $thumb = [
  1130. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1131. "ratio" => "16:9"
  1132. ];
  1133. // get duration
  1134. $duration =
  1135. $this->fuckhtml
  1136. ->getElementsByClassName(
  1137. $this->getstyle([
  1138. "background-color" => "rgba(0,0,0,0.6)",
  1139. "color" => "#fff",
  1140. "fill" => "#fff"
  1141. ])
  1142. );
  1143. if(count($duration) !== 0){
  1144. $duration =
  1145. $this->hms2int(
  1146. $this->fuckhtml
  1147. ->getTextContent(
  1148. $duration[0]
  1149. ));
  1150. }else{
  1151. $duration = null;
  1152. }
  1153. }
  1154. $out["video"][] = [
  1155. "title" =>
  1156. $this->titledots(
  1157. $this->fuckhtml
  1158. ->getTextContent(
  1159. $title[0]
  1160. )
  1161. ),
  1162. "description" => $description,
  1163. "author" => [
  1164. "name" => $author,
  1165. "url" => null,
  1166. "avatar" => null
  1167. ],
  1168. "date" => $date,
  1169. "duration" => $duration,
  1170. "views" => null,
  1171. "thumb" => $thumb,
  1172. "url" =>
  1173. $this->fuckhtml
  1174. ->getTextContent(
  1175. $url[0]["attributes"]["href"]
  1176. )
  1177. ];
  1178. }
  1179. return $out;
  1180. }
  1181. public function news($get){
  1182. throw new Exception("Broke for now, fuck off lol");
  1183. if($get["npt"]){
  1184. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1185. /*parse_str(
  1186. parse_url($req, PHP_URL_QUERY),
  1187. $search
  1188. );*/
  1189. try{
  1190. $html =
  1191. $this->get(
  1192. $proxy,
  1193. "https://www.google.com" . $req,
  1194. []
  1195. );
  1196. }catch(Exception $error){
  1197. throw new Exception("Failed to get HTML");
  1198. }
  1199. }else{
  1200. $search = $get["s"];
  1201. $country = $get["country"];
  1202. $nsfw = $get["nsfw"];
  1203. $older = $get["older"];
  1204. $newer = $get["newer"];
  1205. $sort = $get["sort"];
  1206. $proxy = $this->backend->get_ip();
  1207. $params = [
  1208. "q" => $search,
  1209. "tbm" => "nws",
  1210. "hl" => "en",
  1211. "num" => "20"
  1212. ];
  1213. // country
  1214. if($country != "any"){
  1215. $params["gl"] = $country;
  1216. }
  1217. // nsfw
  1218. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1219. $tbs = [];
  1220. // get date
  1221. $older = $older === false ? null : date("m/d/Y", $older);
  1222. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1223. if(
  1224. $older !== null ||
  1225. $newer !== null
  1226. ){
  1227. $tbs["cdr"] = "1";
  1228. $tbs["cd_min"] = $newer;
  1229. $tbs["cd_max"] = $older;
  1230. }
  1231. // relevance
  1232. if($sort == "date"){
  1233. $tbs["sbd"] = "1";
  1234. }
  1235. // append tbs
  1236. if(count($tbs) !== 0){
  1237. $params["tbs"] = "";
  1238. foreach($tbs as $key => $value){
  1239. $params["tbs"] .= $key . ":" . $value . ",";
  1240. }
  1241. $params["tbs"] = rtrim($params["tbs"], ",");
  1242. }
  1243. //$html = file_get_contents("scraper/google-news.html");
  1244. $html =
  1245. $this->get(
  1246. $proxy,
  1247. "https://www.google.com/search",
  1248. $params
  1249. );
  1250. }
  1251. $out = [
  1252. "status" => "ok",
  1253. "npt" => null,
  1254. "news" => []
  1255. ];
  1256. $this->fuckhtml->load($html);
  1257. $this->detect_sorry();
  1258. // get images
  1259. $this->scrape_dimg($html);
  1260. // parse styles
  1261. $this->parsestyles();
  1262. $center_col =
  1263. $this->fuckhtml
  1264. ->getElementById(
  1265. "center_col",
  1266. "div"
  1267. );
  1268. if($center_col === null){
  1269. throw new Exception("Could not grep result div");
  1270. }
  1271. $this->fuckhtml->load($center_col);
  1272. // get next page
  1273. $npt =
  1274. $this->fuckhtml
  1275. ->getElementById(
  1276. "pnnext",
  1277. "a"
  1278. );
  1279. if($npt !== false){
  1280. $out["npt"] =
  1281. $this->backend->store(
  1282. $this->fuckhtml
  1283. ->getTextContent(
  1284. $npt["attributes"]
  1285. ["href"]
  1286. ),
  1287. "news",
  1288. $proxy
  1289. );
  1290. }
  1291. $as =
  1292. $this->fuckhtml
  1293. ->getElementsByAttributeName(
  1294. "jsname",
  1295. "a"
  1296. );
  1297. foreach($as as $a){
  1298. $this->fuckhtml->load($a);
  1299. // get title
  1300. $title =
  1301. $this->fuckhtml
  1302. ->getElementsByAttributeValue(
  1303. "role",
  1304. "heading",
  1305. "div"
  1306. );
  1307. if(count($title) === 0){
  1308. continue;
  1309. }
  1310. $title =
  1311. $this->titledots(
  1312. $this->fuckhtml
  1313. ->getTextContent(
  1314. $title[0]
  1315. )
  1316. );
  1317. // get thumbnail
  1318. $image =
  1319. $this->fuckhtml
  1320. ->getElementsByAttributeName(
  1321. "id",
  1322. "img"
  1323. );
  1324. // check for padded title node, if found, we're inside a carousel
  1325. $probe =
  1326. $this->fuckhtml
  1327. ->getElementsByClassName(
  1328. $this->getstyle(
  1329. [
  1330. "padding" => "16px 16px 40px 16px"
  1331. ]
  1332. ),
  1333. "div"
  1334. );
  1335. if(count($probe) !== 0){
  1336. $probe = true;
  1337. }else{
  1338. $probe = false;
  1339. }
  1340. if(
  1341. count($image) !== 0 &&
  1342. !isset($image[0]["attributes"]["width"])
  1343. ){
  1344. $thumb = [
  1345. "url" =>
  1346. $this->getdimg(
  1347. $image[0]["attributes"]["id"]
  1348. ),
  1349. "ratio" => $probe === true ? "16:9" : "1:1"
  1350. ];
  1351. }else{
  1352. $thumb = [
  1353. "url" => null,
  1354. "ratio" => null
  1355. ];
  1356. }
  1357. $description = null;
  1358. if($probe === false){
  1359. $desc_divs =
  1360. $this->fuckhtml
  1361. ->getElementsByAttributeName(
  1362. "style",
  1363. "div"
  1364. );
  1365. foreach($desc_divs as $desc){
  1366. if(
  1367. strpos(
  1368. $desc["attributes"]["style"],
  1369. "margin-top:"
  1370. ) !== false
  1371. ){
  1372. $description =
  1373. $this->titledots(
  1374. $this->fuckhtml
  1375. ->getTextContent(
  1376. $desc
  1377. )
  1378. );
  1379. break;
  1380. }
  1381. }
  1382. }
  1383. // get author
  1384. $author =
  1385. $this->fuckhtml
  1386. ->getElementsByClassName(
  1387. $this->getstyle(
  1388. [
  1389. "overflow" => "hidden",
  1390. "text-align" => "left",
  1391. "text-overflow" => "ellipsis",
  1392. "white-space" => "nowrap",
  1393. "margin-bottom" => "8px"
  1394. ]
  1395. ),
  1396. "div"
  1397. );
  1398. if(count($author) !== 0){
  1399. $author =
  1400. $this->fuckhtml
  1401. ->getTextContent(
  1402. $author[0]
  1403. );
  1404. }else{
  1405. $author = null;
  1406. }
  1407. // get date
  1408. $date = null;
  1409. $date_div =
  1410. $this->fuckhtml
  1411. ->getElementsByAttributeName(
  1412. "style",
  1413. "div"
  1414. );
  1415. foreach($date_div as $d){
  1416. $this->fuckhtml->load($d);
  1417. $span =
  1418. $this->fuckhtml
  1419. ->getElementsByTagName(
  1420. "span"
  1421. );
  1422. if(
  1423. strpos(
  1424. $d["attributes"]["style"],
  1425. "bottom:"
  1426. ) !== false
  1427. ){
  1428. $date =
  1429. strtotime(
  1430. $this->fuckhtml
  1431. ->getTextContent(
  1432. $span[count($span) - 1]
  1433. )
  1434. );
  1435. break;
  1436. }
  1437. }
  1438. $out["news"][] = [
  1439. "title" => $title,
  1440. "author" => $author,
  1441. "description" => $description,
  1442. "date" => $date,
  1443. "thumb" => $thumb,
  1444. "url" =>
  1445. $this->unshiturl(
  1446. $a["attributes"]
  1447. ["href"]
  1448. )
  1449. ];
  1450. }
  1451. return $out;
  1452. }
  1453. public function image($get){
  1454. // generate parameters
  1455. if($get["npt"]){
  1456. [$params, $proxy] =
  1457. $this->backend->get(
  1458. $get["npt"],
  1459. "images"
  1460. );
  1461. $params = json_decode($params, true);
  1462. }else{
  1463. $search = $get["s"];
  1464. if(strlen($search) === 0){
  1465. throw new Exception("Search term is empty!");
  1466. }
  1467. $proxy = $this->backend->get_ip();
  1468. $country = $get["country"];
  1469. $nsfw = $get["nsfw"];
  1470. $time = $get["time"];
  1471. $size = $get["size"];
  1472. $ratio = $get["ratio"];
  1473. $color = $get["color"];
  1474. $type = $get["type"];
  1475. $format = $get["format"];
  1476. $rights = $get["rights"];
  1477. $params = [
  1478. "q" => $search,
  1479. "udm" => "2" // get images
  1480. ];
  1481. // country (image search uses cr instead of gl)
  1482. if($country != "any"){
  1483. $params["cr"] = "country" . strtoupper($country);
  1484. }
  1485. // nsfw
  1486. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1487. // generate tbs
  1488. $tbs = [];
  1489. // time
  1490. if($time != "any"){
  1491. $tbs["qdr"] = $time;
  1492. }
  1493. // size
  1494. if($size != "any"){
  1495. $params["imgsz"] = $size;
  1496. }
  1497. // ratio
  1498. if($ratio != "any"){
  1499. $params["imgar"] = $ratio;
  1500. }
  1501. // color
  1502. if($color != "any"){
  1503. if(
  1504. $color == "color" ||
  1505. $color == "trans"
  1506. ){
  1507. $params["imgc"] = $color;
  1508. }elseif($color == "bnw"){
  1509. $params["imgc"] = "gray";
  1510. }else{
  1511. $tbs["ic"] = "specific";
  1512. $tbs["isc"] = $color;
  1513. }
  1514. }
  1515. // type
  1516. if($type != "any"){
  1517. $tbs["itp"] = $type;
  1518. }
  1519. // format
  1520. if($format != "any"){
  1521. $params["as_filetype"] = $format;
  1522. }
  1523. // rights (tbs)
  1524. if($rights != "any"){
  1525. $tbs["sur"] = $rights;
  1526. }
  1527. // append tbs
  1528. if(count($tbs) !== 0){
  1529. $params["tbs"] = "";
  1530. foreach($tbs as $key => $value){
  1531. $params["tbs"] .= $key . ":" . $value . ",";
  1532. }
  1533. $params["tbs"] = rtrim($params["tbs"], ",");
  1534. }
  1535. }
  1536. /*
  1537. $handle = fopen("scraper/page.html", "r");
  1538. $html = fread($handle, filesize("scraper/page.html"));
  1539. fclose($handle);*/
  1540. try{
  1541. $html =
  1542. $this->get(
  1543. $proxy,
  1544. "https://www.google.com/search",
  1545. $params
  1546. );
  1547. }catch(Exception $error){
  1548. throw new Exception("Failed to get search page");
  1549. }
  1550. $this->fuckhtml->load($html);
  1551. $this->detect_sorry();
  1552. // get javascript images
  1553. $this->scrape_imagearr($html);
  1554. $out = [
  1555. "status" => "ok",
  1556. "npt" => null,
  1557. "image" => []
  1558. ];
  1559. $images =
  1560. $this->fuckhtml
  1561. ->getElementsByClassName(
  1562. "ivg-i",
  1563. "div"
  1564. );
  1565. foreach($images as $div){
  1566. $this->fuckhtml->load($div);
  1567. $image =
  1568. $this->fuckhtml
  1569. ->getElementsByTagName("img")[0];
  1570. // make sure we dont attempt to show an image we dont have data for
  1571. if(
  1572. isset($div["attributes"]["data-docid"]) &&
  1573. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1574. ){
  1575. $source =
  1576. $this->image_arr[
  1577. $div["attributes"]["data-docid"]
  1578. ];
  1579. }else{
  1580. continue;
  1581. }
  1582. $out["image"][] = [
  1583. "title" =>
  1584. $this->titledots(
  1585. $this->fuckhtml
  1586. ->getTextContent(
  1587. $image["attributes"]["alt"]
  1588. )
  1589. ),
  1590. "source" => $source,
  1591. "url" =>
  1592. $this->fuckhtml
  1593. ->getTextContent(
  1594. $div["attributes"]["data-lpage"]
  1595. )
  1596. ];
  1597. }
  1598. // as usual, no way to check if there is a next page reliably
  1599. if(count($out["image"]) > 50){
  1600. if(!isset($params["start"])){
  1601. $params["start"] = 10;
  1602. }else{
  1603. $params["start"] += 10;
  1604. }
  1605. $out["npt"] =
  1606. $this->backend
  1607. ->store(
  1608. json_encode($params),
  1609. "image",
  1610. $proxy
  1611. );
  1612. }
  1613. return $out;
  1614. }
  1615. private function scrape_dimg($html){
  1616. // get images loaded through javascript
  1617. $this->dimg = [];
  1618. preg_match_all(
  1619. '/function\(\){google\.ldi=({.*?});/',
  1620. $html,
  1621. $dimg
  1622. );
  1623. if(isset($dimg[1])){
  1624. foreach($dimg[1] as $i){
  1625. $tmp = json_decode($i, true);
  1626. foreach($tmp as $key => $value){
  1627. $this->dimg[$key] =
  1628. $this->unshit_thumb(
  1629. $value
  1630. );
  1631. }
  1632. }
  1633. }
  1634. // get additional javascript base64 images
  1635. preg_match_all(
  1636. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1637. $html,
  1638. $dimg
  1639. );
  1640. if(isset($dimg[1])){
  1641. for($i=0; $i<count($dimg[1]); $i++){
  1642. $delims = explode(",", $dimg[2][$i]);
  1643. $string =
  1644. $this->fuckhtml
  1645. ->parseJsString(
  1646. $dimg[1][$i]
  1647. );
  1648. foreach($delims as $delim){
  1649. $this->dimg[trim($delim, "'")] = $string;
  1650. }
  1651. }
  1652. }
  1653. }
  1654. private function scrape_imagearr($html){
  1655. // get image links arrays
  1656. preg_match_all(
  1657. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1658. $html,
  1659. $image_arr
  1660. );
  1661. $this->image_arr = [];
  1662. if(isset($image_arr[1])){
  1663. for($i=0; $i<count($image_arr[1]); $i++){
  1664. $original =
  1665. $this->fuckhtml
  1666. ->parseJsString(
  1667. $image_arr[5][$i]
  1668. );
  1669. if(
  1670. preg_match(
  1671. '/^x-raw-image/',
  1672. $original
  1673. )
  1674. ){
  1675. // only add thumbnail, google doesnt have OG resolution
  1676. $this->image_arr[$image_arr[1][$i]] = [
  1677. [
  1678. "url" =>
  1679. $this->unshit_thumb(
  1680. $this->fuckhtml
  1681. ->parseJsString(
  1682. $image_arr[2][$i]
  1683. )
  1684. ),
  1685. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1686. "height" => (int)$image_arr[6][$i]
  1687. ]
  1688. ];
  1689. continue;
  1690. }
  1691. $this->image_arr[$image_arr[1][$i]] =
  1692. [
  1693. [
  1694. "url" => $original,
  1695. "width" => (int)$image_arr[7][$i],
  1696. "height" => (int)$image_arr[6][$i]
  1697. ],
  1698. [
  1699. "url" =>
  1700. $this->unshit_thumb(
  1701. $this->fuckhtml
  1702. ->parseJsString(
  1703. $image_arr[2][$i]
  1704. )
  1705. ),
  1706. "width" => (int)$image_arr[4][$i],
  1707. "height" => (int)$image_arr[3][$i]
  1708. ]
  1709. ];
  1710. }
  1711. }
  1712. }
  1713. private function getdimg($dimg){
  1714. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1715. }
  1716. private function unshit_thumb($url, $get_bigger_res = false){
  1717. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1718. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1719. $parts = parse_url($url);
  1720. if(
  1721. isset($parts["host"]) &&
  1722. preg_match(
  1723. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1724. $parts["host"]
  1725. )
  1726. ){
  1727. parse_str($parts["query"], $params);
  1728. if(isset($params["q"])){
  1729. if($get_bigger_res){
  1730. // this method doesnt always work, but does work for wiki thumbnails
  1731. return
  1732. "https://" . $parts["host"] . "/images?q=tbn:" .
  1733. $this->base64url_encode(
  1734. substr(
  1735. $this->base64url_decode(
  1736. explode(
  1737. ":",
  1738. $params["q"])[1]
  1739. ),
  1740. 0,
  1741. 29
  1742. )
  1743. );
  1744. }else{
  1745. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1746. }
  1747. }
  1748. }
  1749. return $url;
  1750. }
  1751. private function parsestyles(){
  1752. $styles = [];
  1753. $style_div =
  1754. $this->fuckhtml
  1755. ->getElementsByTagName(
  1756. "style"
  1757. );
  1758. $raw_styles = "";
  1759. foreach($style_div as $style){
  1760. $raw_styles .= $style["innerHTML"];
  1761. }
  1762. // filter out media/keyframe queries
  1763. $raw_styles =
  1764. preg_replace(
  1765. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1766. "",
  1767. $raw_styles
  1768. );
  1769. // get styles
  1770. preg_match_all(
  1771. '/(.+?){([\S\s]*?)}/',
  1772. $raw_styles,
  1773. $matches
  1774. );
  1775. for($i=0; $i<count($matches[1]); $i++){
  1776. // get style values
  1777. preg_match_all(
  1778. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1779. $matches[2][$i],
  1780. $values_regex
  1781. );
  1782. $values = [];
  1783. for($k=0; $k<count($values_regex[1]); $k++){
  1784. $values[trim($values_regex[1][$k])] =
  1785. strtolower(trim($values_regex[2][$k]));
  1786. }
  1787. $names = explode(",", $matches[1][$i]);
  1788. // h1,h2,h3 will each get their own array index
  1789. foreach($names as $name){
  1790. $name = trim($name, "}\t\n\r\0\x0B");
  1791. foreach($values as $key => $value){
  1792. $styles[$name][$key] = $value;
  1793. }
  1794. }
  1795. }
  1796. foreach($styles as $key => $values){
  1797. $styles[$key]["_c"] = count($values);
  1798. }
  1799. $this->styles = $styles;
  1800. // get CSS colors
  1801. $this->css_colors = [];
  1802. if(isset($this->styles[":root"])){
  1803. foreach($this->styles[":root"] as $key => $value){
  1804. $this->css_colors[$value] = strtolower($key);
  1805. }
  1806. }
  1807. }
  1808. private function getstyle($styles){
  1809. $styles["_c"] = count($styles);
  1810. foreach($this->styles as $style_key => $style_values){
  1811. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1812. $style_key =
  1813. explode(" ", $style_key);
  1814. $style_key = $style_key[count($style_key) - 1];
  1815. return
  1816. ltrim(
  1817. str_replace(
  1818. [".", "#"],
  1819. " ",
  1820. $style_key
  1821. )
  1822. );
  1823. }
  1824. }
  1825. return false;
  1826. }
  1827. private function getcolorvar($color){
  1828. if(isset($this->css_colors[$color])){
  1829. return $this->css_colors[$color];
  1830. }
  1831. return null;
  1832. }
  1833. private function unshiturl($url, $return_size = false){
  1834. // decode
  1835. $url =
  1836. $this->fuckhtml
  1837. ->getTextContent(
  1838. $url
  1839. );
  1840. $url_parts = parse_url($url);
  1841. if(isset($url_parts["query"])){
  1842. parse_str($url_parts["query"], $query);
  1843. }else{
  1844. $query = [];
  1845. }
  1846. if(
  1847. !isset(
  1848. $url_parts["host"]
  1849. ) ||
  1850. stripos($url_parts["host"], "google.") !== false
  1851. ){
  1852. // no host, we have a tracking url
  1853. if(isset($query["imgurl"])){
  1854. $url = $query["imgurl"];
  1855. }
  1856. elseif(isset($query["q"])){
  1857. $url = $query["q"];
  1858. }
  1859. }
  1860. // rewrite URLs to remove extra tracking parameters
  1861. $domain = parse_url($url, PHP_URL_HOST);
  1862. if(
  1863. preg_match(
  1864. '/wikipedia.org$/',
  1865. $domain
  1866. )
  1867. ){
  1868. // rewrite wikipedia mobile URLs to desktop
  1869. $url =
  1870. $this->replacedomain(
  1871. $url,
  1872. preg_replace(
  1873. '/([a-z0-9]+)(\.m\.)/',
  1874. '$1.',
  1875. $domain
  1876. )
  1877. );
  1878. }
  1879. elseif(
  1880. preg_match(
  1881. '/imdb\.com$|youtube\.[^.]+$/',
  1882. $domain
  1883. )
  1884. ){
  1885. // rewrite imdb and youtube mobile URLs too
  1886. $url =
  1887. $this->replacedomain(
  1888. $url,
  1889. preg_replace(
  1890. '/^m\./',
  1891. "",
  1892. $domain
  1893. )
  1894. );
  1895. }
  1896. elseif(
  1897. preg_match(
  1898. '/play\.google\.[^.]+$/',
  1899. $domain
  1900. )
  1901. ){
  1902. // remove referrers from play.google.com
  1903. $u_query = parse_url($url, PHP_URL_QUERY);
  1904. if($u_query !== null){
  1905. parse_str($u_query, $u_query);
  1906. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  1907. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  1908. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  1909. $query = http_build_query($query);
  1910. $url =
  1911. str_replace(
  1912. $u_query,
  1913. $u_query,
  1914. $url
  1915. );
  1916. }
  1917. }
  1918. elseif(
  1919. preg_match(
  1920. '/twitter\.com$/',
  1921. $domain
  1922. )
  1923. ){
  1924. // remove more referrers from twitter.com
  1925. $u_query = parse_url($url, PHP_URL_QUERY);
  1926. if($u_query !== null){
  1927. parse_str($u_query, $u_query);
  1928. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  1929. $u_query = http_build_query($u_query);
  1930. $url =
  1931. str_replace(
  1932. $oldquery,
  1933. $u_query,
  1934. $url
  1935. );
  1936. }
  1937. }
  1938. elseif(
  1939. preg_match(
  1940. '/maps\.google\.[^.]+/',
  1941. $domain
  1942. )
  1943. ){
  1944. if(stripos($url, "maps?") !== false){
  1945. $u_query = parse_url($url, PHP_URL_QUERY);
  1946. if($u_query !== null){
  1947. parse_str($u_query, $u_query);
  1948. if(isset($u_query["daddr"])){
  1949. $url =
  1950. "https://maps.google.com/maps?daddr=" .
  1951. urlencode($u_query["daddr"]);
  1952. }
  1953. }
  1954. }
  1955. }
  1956. if($return_size){
  1957. return [
  1958. "url" => $url,
  1959. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  1960. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  1961. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  1962. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  1963. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  1964. ];
  1965. }
  1966. return $url;
  1967. }
  1968. private function replacedomain($url, $domain){
  1969. return
  1970. preg_replace(
  1971. '/(https?:\/\/)([^\/]+)/',
  1972. '$1' . $domain,
  1973. $url
  1974. );
  1975. }
  1976. private function titledots($title){
  1977. return trim($title, " .\t\n\r\0\x0B…");
  1978. }
  1979. private function hms2int($time){
  1980. $parts = explode(":", $time, 3);
  1981. $time = 0;
  1982. if(count($parts) === 3){
  1983. // hours
  1984. $time = $time + ((int)$parts[0] * 3600);
  1985. array_shift($parts);
  1986. }
  1987. if(count($parts) === 2){
  1988. // minutes
  1989. $time = $time + ((int)$parts[0] * 60);
  1990. array_shift($parts);
  1991. }
  1992. // seconds
  1993. $time = $time + (int)$parts[0];
  1994. return $time;
  1995. }
  1996. function base64url_decode($data){
  1997. $b64 = strtr($data, "-_", "+/");
  1998. $pad = strlen($b64) % 4;
  1999. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  2000. return base64_decode($b64);
  2001. }
  2002. function base64url_encode($data){
  2003. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  2004. }
  2005. private function detect_sorry(){
  2006. $captcha_form =
  2007. $this->fuckhtml
  2008. ->getElementById(
  2009. "captcha-form",
  2010. "form"
  2011. );
  2012. if($captcha_form !== false){
  2013. throw new Exception("Google returned a captcha");
  2014. }
  2015. }
  2016. }