google.php 46 KB

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