1
0

google.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771
  1. <?php
  2. class google{
  3. public function __construct(){
  4. include "lib/fuckhtml.php";
  5. $this->fuckhtml = new fuckhtml();
  6. include "lib/backend.php";
  7. $this->backend = new backend("google");
  8. }
  9. public function getfilters($page){
  10. $base = [
  11. "country" => [ // gl=<country> (image: cr=countryAF)
  12. "display" => "Country",
  13. "option" => [
  14. "any" => "Instance's country",
  15. "af" => "Afghanistan",
  16. "al" => "Albania",
  17. "dz" => "Algeria",
  18. "as" => "American Samoa",
  19. "ad" => "Andorra",
  20. "ao" => "Angola",
  21. "ai" => "Anguilla",
  22. "aq" => "Antarctica",
  23. "ag" => "Antigua and Barbuda",
  24. "ar" => "Argentina",
  25. "am" => "Armenia",
  26. "aw" => "Aruba",
  27. "au" => "Australia",
  28. "at" => "Austria",
  29. "az" => "Azerbaijan",
  30. "bs" => "Bahamas",
  31. "bh" => "Bahrain",
  32. "bd" => "Bangladesh",
  33. "bb" => "Barbados",
  34. "by" => "Belarus",
  35. "be" => "Belgium",
  36. "bz" => "Belize",
  37. "bj" => "Benin",
  38. "bm" => "Bermuda",
  39. "bt" => "Bhutan",
  40. "bo" => "Bolivia",
  41. "ba" => "Bosnia and Herzegovina",
  42. "bw" => "Botswana",
  43. "bv" => "Bouvet Island",
  44. "br" => "Brazil",
  45. "io" => "British Indian Ocean Territory",
  46. "bn" => "Brunei Darussalam",
  47. "bg" => "Bulgaria",
  48. "bf" => "Burkina Faso",
  49. "bi" => "Burundi",
  50. "kh" => "Cambodia",
  51. "cm" => "Cameroon",
  52. "ca" => "Canada",
  53. "cv" => "Cape Verde",
  54. "ky" => "Cayman Islands",
  55. "cf" => "Central African Republic",
  56. "td" => "Chad",
  57. "cl" => "Chile",
  58. "cn" => "China",
  59. "cx" => "Christmas Island",
  60. "cc" => "Cocos (Keeling) Islands",
  61. "co" => "Colombia",
  62. "km" => "Comoros",
  63. "cg" => "Congo",
  64. "cd" => "Congo, the Democratic Republic",
  65. "ck" => "Cook Islands",
  66. "cr" => "Costa Rica",
  67. "ci" => "Cote D'ivoire",
  68. "hr" => "Croatia",
  69. "cu" => "Cuba",
  70. "cy" => "Cyprus",
  71. "cz" => "Czech Republic",
  72. "dk" => "Denmark",
  73. "dj" => "Djibouti",
  74. "dm" => "Dominica",
  75. "do" => "Dominican Republic",
  76. "ec" => "Ecuador",
  77. "eg" => "Egypt",
  78. "sv" => "El Salvador",
  79. "gq" => "Equatorial Guinea",
  80. "er" => "Eritrea",
  81. "ee" => "Estonia",
  82. "et" => "Ethiopia",
  83. "fk" => "Falkland Islands (Malvinas)",
  84. "fo" => "Faroe Islands",
  85. "fj" => "Fiji",
  86. "fi" => "Finland",
  87. "fr" => "France",
  88. "gf" => "French Guiana",
  89. "pf" => "French Polynesia",
  90. "tf" => "French Southern Territories",
  91. "ga" => "Gabon",
  92. "gm" => "Gambia",
  93. "ge" => "Georgia",
  94. "de" => "Germany",
  95. "gh" => "Ghana",
  96. "gi" => "Gibraltar",
  97. "gr" => "Greece",
  98. "gl" => "Greenland",
  99. "gd" => "Grenada",
  100. "gp" => "Guadeloupe",
  101. "gu" => "Guam",
  102. "gt" => "Guatemala",
  103. "gn" => "Guinea",
  104. "gw" => "Guinea-Bissau",
  105. "gy" => "Guyana",
  106. "ht" => "Haiti",
  107. "hm" => "Heard Island and Mcdonald Islands",
  108. "va" => "Holy See (Vatican City State)",
  109. "hn" => "Honduras",
  110. "hk" => "Hong Kong",
  111. "hu" => "Hungary",
  112. "is" => "Iceland",
  113. "in" => "India",
  114. "id" => "Indonesia",
  115. "ir" => "Iran, Islamic Republic",
  116. "iq" => "Iraq",
  117. "ie" => "Ireland",
  118. "il" => "Israel",
  119. "it" => "Italy",
  120. "jm" => "Jamaica",
  121. "jp" => "Japan",
  122. "jo" => "Jordan",
  123. "kz" => "Kazakhstan",
  124. "ke" => "Kenya",
  125. "ki" => "Kiribati",
  126. "kp" => "Korea, Democratic People's Republic",
  127. "kr" => "Korea, Republic",
  128. "kw" => "Kuwait",
  129. "kg" => "Kyrgyzstan",
  130. "la" => "Lao People's Democratic Republic",
  131. "lv" => "Latvia",
  132. "lb" => "Lebanon",
  133. "ls" => "Lesotho",
  134. "lr" => "Liberia",
  135. "ly" => "Libyan Arab Jamahiriya",
  136. "li" => "Liechtenstein",
  137. "lt" => "Lithuania",
  138. "lu" => "Luxembourg",
  139. "mo" => "Macao",
  140. "mk" => "Macedonia, the Former Yugosalv Republic",
  141. "mg" => "Madagascar",
  142. "mw" => "Malawi",
  143. "my" => "Malaysia",
  144. "mv" => "Maldives",
  145. "ml" => "Mali",
  146. "mt" => "Malta",
  147. "mh" => "Marshall Islands",
  148. "mq" => "Martinique",
  149. "mr" => "Mauritania",
  150. "mu" => "Mauritius",
  151. "yt" => "Mayotte",
  152. "mx" => "Mexico",
  153. "fm" => "Micronesia, Federated States",
  154. "md" => "Moldova, Republic",
  155. "mc" => "Monaco",
  156. "mn" => "Mongolia",
  157. "ms" => "Montserrat",
  158. "ma" => "Morocco",
  159. "mz" => "Mozambique",
  160. "mm" => "Myanmar",
  161. "na" => "Namibia",
  162. "nr" => "Nauru",
  163. "np" => "Nepal",
  164. "nl" => "Netherlands",
  165. "an" => "Netherlands Antilles",
  166. "nc" => "New Caledonia",
  167. "nz" => "New Zealand",
  168. "ni" => "Nicaragua",
  169. "ne" => "Niger",
  170. "ng" => "Nigeria",
  171. "nu" => "Niue",
  172. "nf" => "Norfolk Island",
  173. "mp" => "Northern Mariana Islands",
  174. "no" => "Norway",
  175. "om" => "Oman",
  176. "pk" => "Pakistan",
  177. "pw" => "Palau",
  178. "ps" => "Palestinian Territory, Occupied",
  179. "pa" => "Panama",
  180. "pg" => "Papua New Guinea",
  181. "py" => "Paraguay",
  182. "pe" => "Peru",
  183. "ph" => "Philippines",
  184. "pn" => "Pitcairn",
  185. "pl" => "Poland",
  186. "pt" => "Portugal",
  187. "pr" => "Puerto Rico",
  188. "qa" => "Qatar",
  189. "re" => "Reunion",
  190. "ro" => "Romania",
  191. "ru" => "Russian Federation",
  192. "rw" => "Rwanda",
  193. "sh" => "Saint Helena",
  194. "kn" => "Saint Kitts and Nevis",
  195. "lc" => "Saint Lucia",
  196. "pm" => "Saint Pierre and Miquelon",
  197. "vc" => "Saint Vincent and the Grenadines",
  198. "ws" => "Samoa",
  199. "sm" => "San Marino",
  200. "st" => "Sao Tome and Principe",
  201. "sa" => "Saudi Arabia",
  202. "sn" => "Senegal",
  203. "cs" => "Serbia and Montenegro",
  204. "sc" => "Seychelles",
  205. "sl" => "Sierra Leone",
  206. "sg" => "Singapore",
  207. "sk" => "Slovakia",
  208. "si" => "Slovenia",
  209. "sb" => "Solomon Islands",
  210. "so" => "Somalia",
  211. "za" => "South Africa",
  212. "gs" => "South Georgia and the South Sandwich Islands",
  213. "es" => "Spain",
  214. "lk" => "Sri Lanka",
  215. "sd" => "Sudan",
  216. "sr" => "Suriname",
  217. "sj" => "Svalbard and Jan Mayen",
  218. "sz" => "Swaziland",
  219. "se" => "Sweden",
  220. "ch" => "Switzerland",
  221. "sy" => "Syrian Arab Republic",
  222. "tw" => "Taiwan, Province of China",
  223. "tj" => "Tajikistan",
  224. "tz" => "Tanzania, United Republic",
  225. "th" => "Thailand",
  226. "tl" => "Timor-Leste",
  227. "tg" => "Togo",
  228. "tk" => "Tokelau",
  229. "to" => "Tonga",
  230. "tt" => "Trinidad and Tobago",
  231. "tn" => "Tunisia",
  232. "tr" => "Turkey",
  233. "tm" => "Turkmenistan",
  234. "tc" => "Turks and Caicos Islands",
  235. "tv" => "Tuvalu",
  236. "ug" => "Uganda",
  237. "ua" => "Ukraine",
  238. "ae" => "United Arab Emirates",
  239. "uk" => "United Kingdom",
  240. "us" => "United States",
  241. "um" => "United States Minor Outlying Islands",
  242. "uy" => "Uruguay",
  243. "uz" => "Uzbekistan",
  244. "vu" => "Vanuatu",
  245. "ve" => "Venezuela",
  246. "vn" => "Viet Nam",
  247. "vg" => "Virgin Islands, British",
  248. "vi" => "Virgin Islands, U.S.",
  249. "wf" => "Wallis and Futuna",
  250. "eh" => "Western Sahara",
  251. "ye" => "Yemen",
  252. "zm" => "Zambia",
  253. "zw" => "Zimbabwe"
  254. ]
  255. ],
  256. "nsfw" => [
  257. "display" => "NSFW",
  258. "option" => [
  259. "yes" => "Yes", // safe=active
  260. "no" => "No" // safe=off
  261. ]
  262. ]
  263. ];
  264. switch($page){
  265. case "web":
  266. return array_merge(
  267. $base,
  268. [
  269. "lang" => [ // lr=<lang> (prefix lang with "lang_")
  270. "display" => "Language",
  271. "option" => [
  272. "any" => "Any language",
  273. "ar" => "Arabic",
  274. "bg" => "Bulgarian",
  275. "ca" => "Catalan",
  276. "cs" => "Czech",
  277. "da" => "Danish",
  278. "de" => "German",
  279. "el" => "Greek",
  280. "en" => "English",
  281. "es" => "Spanish",
  282. "et" => "Estonian",
  283. "fi" => "Finnish",
  284. "fr" => "French",
  285. "hr" => "Croatian",
  286. "hu" => "Hungarian",
  287. "id" => "Indonesian",
  288. "is" => "Icelandic",
  289. "it" => "Italian",
  290. "iw" => "Hebrew",
  291. "ja" => "Japanese",
  292. "ko" => "Korean",
  293. "lt" => "Lithuanian",
  294. "lv" => "Latvian",
  295. "nl" => "Dutch",
  296. "no" => "Norwegian",
  297. "pl" => "Polish",
  298. "pt" => "Portuguese",
  299. "ro" => "Romanian",
  300. "ru" => "Russian",
  301. "sk" => "Slovak",
  302. "sl" => "Slovenian",
  303. "sr" => "Serbian",
  304. "sv" => "Swedish",
  305. "tr" => "Turkish",
  306. "zh-CN" => "Chinese (Simplified)",
  307. "zh-TW" => "Chinese (Traditional)"
  308. ]
  309. ],
  310. "newer" => [ // tbs
  311. "display" => "Newer than",
  312. "option" => "_DATE"
  313. ],
  314. "older" => [
  315. "display" => "Older than",
  316. "option" => "_DATE"
  317. ],
  318. "spellcheck" => [
  319. "display" => "Spellcheck",
  320. "option" => [
  321. "yes" => "Yes",
  322. "no" => "No"
  323. ]
  324. ]
  325. ]
  326. );
  327. break;
  328. case "images":
  329. return array_merge(
  330. $base,
  331. [
  332. "time" => [ // tbs=qdr:<time>
  333. "display" => "Time posted",
  334. "option" => [
  335. "any" => "Any time",
  336. "d" => "Past 24 hours",
  337. "w" => "Past week",
  338. "m" => "Past month",
  339. "y" => "Past year"
  340. ]
  341. ],
  342. "size" => [ // imgsz
  343. "display" => "Size",
  344. "option" => [
  345. "any" => "Any size",
  346. "l" => "Large",
  347. "m" => "Medium",
  348. "i" => "Icon",
  349. "qsvga" => "Larger than 400x300",
  350. "vga" => "Larger than 640x480",
  351. "svga" => "Larger than 800x600",
  352. "xga" => "Larger than 1024x768",
  353. "2mp" => "Larger than 2MP",
  354. "4mp" => "Larger than 4MP",
  355. "6mp" => "Larger than 6MP",
  356. "8mp" => "Larger than 8MP",
  357. "10mp" => "Larger than 10MP",
  358. "12mp" => "Larger than 12MP",
  359. "15mp" => "Larger than 15MP",
  360. "20mp" => "Larger than 20MP",
  361. "40mp" => "Larger than 40MP",
  362. "70mp" => "Larger than 70MP"
  363. ]
  364. ],
  365. "ratio" => [ // imgar
  366. "display" => "Aspect ratio",
  367. "option" => [
  368. "any" => "Any ratio",
  369. "t|xt" => "Tall",
  370. "s" => "Square",
  371. "w" => "Wide",
  372. "xw" => "Panoramic"
  373. ]
  374. ],
  375. "color" => [ // imgc
  376. "display" => "Color",
  377. "option" => [
  378. "any" => "Any color",
  379. "color" => "Full color",
  380. "bnw" => "Black & white",
  381. "trans" => "Transparent",
  382. // from here, imgcolor
  383. "red" => "Red",
  384. "orange" => "Orange",
  385. "yellow" => "Yellow",
  386. "green" => "Green",
  387. "teal" => "Teal",
  388. "blue" => "Blue",
  389. "purple" => "Purple",
  390. "pink" => "Pink",
  391. "white" => "White",
  392. "gray" => "Gray",
  393. "black" => "Black",
  394. "brown" => "Brown"
  395. ]
  396. ],
  397. "type" => [ // tbs=itp:<type>
  398. "display" => "Type",
  399. "option" => [
  400. "any" => "Any type",
  401. "clipart" => "Clip Art",
  402. "lineart" => "Line Drawing",
  403. "animated" => "Animated"
  404. ]
  405. ],
  406. "format" => [ // as_filetype
  407. "display" => "Format",
  408. "option" => [
  409. "any" => "Any format",
  410. "jpg" => "JPG",
  411. "gif" => "GIF",
  412. "png" => "PNG",
  413. "bmp" => "BMP",
  414. "svg" => "SVG",
  415. "webp" => "WEBP",
  416. "ico" => "ICO",
  417. "craw" => "RAW"
  418. ]
  419. ],
  420. "rights" => [ // tbs=sur:<rights>
  421. "display" => "Usage rights",
  422. "option" => [
  423. "any" => "Any license",
  424. "cl" => "Creative Commons licenses",
  425. "ol" => "Commercial & other licenses"
  426. ]
  427. ]
  428. ]
  429. );
  430. break;
  431. case "videos":
  432. return array_merge(
  433. $base,
  434. [
  435. "newer" => [ // tbs
  436. "display" => "Newer than",
  437. "option" => "_DATE"
  438. ],
  439. "older" => [
  440. "display" => "Older than",
  441. "option" => "_DATE"
  442. ],
  443. "duration" => [
  444. "display" => "Duration",
  445. "option" => [
  446. "any" => "Any duration",
  447. "s" => "Short (0-4min)", // tbs=dur:s
  448. "m" => "Medium (4-20min)", // tbs=dur:m
  449. "l" => "Long (20+ min)" // tbs=dur:l
  450. ]
  451. ],
  452. "quality" => [
  453. "display" => "Quality",
  454. "option" => [
  455. "any" => "Any quality",
  456. "h" => "High quality" // tbs=hq:h
  457. ]
  458. ],
  459. "captions" => [
  460. "display" => "Captions",
  461. "option" => [
  462. "any" => "No preference",
  463. "yes" => "Closed captioned" // tbs=cc:1
  464. ]
  465. ]
  466. ]
  467. );
  468. break;
  469. case "news":
  470. return array_merge(
  471. $base,
  472. [
  473. "newer" => [ // tbs
  474. "display" => "Newer than",
  475. "option" => "_DATE"
  476. ],
  477. "older" => [
  478. "display" => "Older than",
  479. "option" => "_DATE"
  480. ],
  481. "sort" => [
  482. "display" => "Sort",
  483. "option" => [
  484. "relevance" => "Relevance",
  485. "date" => "Date" // sbd:1
  486. ]
  487. ]
  488. ]
  489. );
  490. break;
  491. }
  492. }
  493. private function get($proxy, $url, $get = [], $container = null){
  494. if(config::FPLAY_EXTERNAL_ENDPOINT === null){
  495. throw new Exception("This scraper requires 4play, and the instance maintainer did not configure it. Please refer to the documentation for more information");
  496. }
  497. try{
  498. $curlproc = curl_init();
  499. if($get !== []){
  500. $get = http_build_query($get);
  501. $url .= "?" . $get;
  502. }
  503. if($container === null){
  504. $url =
  505. config::FPLAY_EXTERNAL_ENDPOINT .
  506. "?url=" . urlencode($url) .
  507. "&pw=" . urlencode(config::FPLAY_PASSWORD) .
  508. "&container=" . urlencode($container) .
  509. "&proxy=" . urlencode($proxy) .
  510. "&url_match=" . urlencode("&sei=") .
  511. "&fail_url_match=" . urlencode("\/sorry\/");
  512. }else{
  513. $url =
  514. config::FPLAY_EXTERNAL_ENDPOINT .
  515. "?url=" . urlencode($url) .
  516. "&pw=" . urlencode(config::FPLAY_PASSWORD) .
  517. "&container=" . urlencode($container) .
  518. "&proxy=" . urlencode($proxy);
  519. }
  520. $header_out = [];
  521. curl_setopt($curlproc, CURLOPT_HEADERFUNCTION, function($curlproc, $header) use (&$header_out){
  522. $length = strlen($header);
  523. $header_out[] = trim($header);
  524. return $length;
  525. });
  526. curl_setopt($curlproc, CURLOPT_URL, $url);
  527. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  528. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  529. curl_setopt($curlproc, CURLOPT_HTTPHEADER, [
  530. "User-Agent: " . config::USER_AGENT_FRIENDLY,
  531. "Accept: text/plain"
  532. ]);
  533. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  534. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  535. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  536. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  537. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  538. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  539. //$this->backend->assign_proxy($curlproc, $proxy);
  540. $data = curl_exec($curlproc);
  541. if(curl_errno($curlproc)){
  542. throw new Exception(curl_error($curlproc));
  543. }
  544. curl_close($curlproc);
  545. }catch(Exception $error){
  546. throw new Exception("4play API is currently unreachable. Render server offline (probably) woopsies");
  547. }
  548. // check for failstate
  549. if(
  550. str_contains(
  551. $header_out[0],
  552. "400"
  553. )
  554. ){
  555. throw new Exception("Page renderer returned an error: " . $data);
  556. }
  557. // parse headers
  558. $container = null;
  559. foreach($header_out as $header){
  560. $header = explode(":", $header, 2);
  561. if(strtolower($header[0]) == "x-container-id"){
  562. $container = trim($header[1]);
  563. break;
  564. }
  565. }
  566. if($container === null){
  567. throw new Exception("The 4play API did not return a valid container ID. Make sure your configuration forwards X-Container-ID headers properly");
  568. }
  569. return [
  570. "data" => $data,
  571. "container" => $container
  572. ];
  573. }
  574. public function web($get){
  575. if($get["npt"]){
  576. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  577. $get = json_decode($get, true);
  578. $container = $get["c"];
  579. $get = $get["u"];
  580. $data =
  581. $this->get(
  582. $proxy,
  583. "https://www.google.com" . $get,
  584. [],
  585. $container
  586. );
  587. $html = &$data["data"];
  588. $container = &$data["container"];
  589. }else{
  590. $search = $get["s"];
  591. if(strlen($search) === 0){
  592. throw new Exception("Search term is empty!");
  593. }
  594. /*
  595. $session = $this->backend->session_get();
  596. if(config::FPLAY_ENABLE_API === false){
  597. throw new Exception("This scraper requires the 4play session generator. Check the documentation for more information");
  598. }
  599. if($session === false){
  600. throw new Exception("Session database is empty");
  601. }
  602. $proxy = &$session["proxy"];
  603. $headers = &$session["headers"];
  604. */
  605. $proxy = $this->backend->get_ip();
  606. //
  607. // Generate params
  608. //
  609. $params = [
  610. "q" => $get["s"],
  611. "hl" => "en",
  612. "udm" => 14
  613. ];
  614. // country
  615. if($get["country"] != "any"){
  616. $params["gl"] = $country;
  617. }
  618. // nsfw
  619. $params["safe"] = $get["nsfw"] == "yes" ? "off" : "active";
  620. // language
  621. if($get["lang"] != "any"){
  622. $params["lr"] = "lang_" . $lang;
  623. }
  624. // generate tbs
  625. $tbs = [];
  626. // get date
  627. $get["older"] = $get["older"] === false ? null : date("m/d/Y", $older);
  628. $get["newer"] = $get["newer"] === false ? null : date("m/d/Y", $newer);
  629. if(
  630. $get["older"] !== null ||
  631. $get["newer"] !== null
  632. ){
  633. $tbs["cdr"] = "1";
  634. $tbs["cd_min"] = $get["newer"];
  635. $tbs["cd_max"] = $get["older"];
  636. }
  637. // spellcheck filter
  638. if($get["spellcheck"] == "no"){
  639. $params["nfpr"] = "1";
  640. }
  641. if(count($tbs) !== 0){
  642. $params["tbs"] = "";
  643. foreach($tbs as $key => $value){
  644. $params["tbs"] .= $key . ":" . $value . ",";
  645. }
  646. $params["tbs"] = rtrim($params["tbs"], ",");
  647. }
  648. /*
  649. $data = $this->get(
  650. $proxy,
  651. "https://www.google.com/search",
  652. $params,
  653. null
  654. );*/
  655. $html = &$data["data"];
  656. $container = &$data["container"];
  657. //file_put_contents("scraper/google-jp.html", $html);
  658. $html = file_get_contents("scraper/google-softcaptcha.html");
  659. }
  660. $out = [
  661. "status" => "ok",
  662. "spelling" => [
  663. "type" => "no_correction",
  664. "using" => null,
  665. "correction" => null
  666. ],
  667. "npt" => null,
  668. "answer" => [],
  669. "web" => [],
  670. "image" => [],
  671. "video" => [],
  672. "news" => [],
  673. "related" => []
  674. ];
  675. $this->fuckhtml->load($html);
  676. // init
  677. $this->parsestyles();
  678. // solve softcaptcha
  679. $softcaptcha = $this->detect_sorry();
  680. if($softcaptcha){
  681. $params["sei"] = $softcaptcha["sei"];
  682. $data = $this->get(
  683. $proxy,
  684. "https://www.google.com/search",
  685. $params,
  686. $container
  687. );
  688. }
  689. $this->scrape_dimg($html);
  690. $this->scrape_imagearr($html);
  691. $content =
  692. $this->fuckhtml
  693. ->getElementById(
  694. "rso"
  695. );
  696. if($content === null){
  697. throw new Exception("Failed to grep search div");
  698. }
  699. // get encoded links
  700. $encoded_links =
  701. explode(
  702. "function(){var m={",
  703. $html,
  704. 2
  705. );
  706. if(count($encoded_links) === 2){
  707. $encoded_links =
  708. json_decode(
  709. $this->fuckhtml
  710. ->extract_json(
  711. "{" . $encoded_links[1]
  712. ),
  713. true
  714. );
  715. }else{
  716. $encoded_links = [];
  717. }
  718. // get the "did you mean" bullshit
  719. $taw =
  720. $this->fuckhtml
  721. ->getElementById("taw");
  722. if($taw !== null){
  723. $this->fuckhtml->load($taw);
  724. $as =
  725. $this->fuckhtml
  726. ->getElementsByTagName("a");
  727. $links = [];
  728. foreach($as as $a){
  729. $href =
  730. $this->fuckhtml
  731. ->getTextContent(
  732. $a["attributes"]["href"]
  733. );
  734. if(preg_match('/^\/search\?/', $href)){
  735. $links[] =
  736. $this->fuckhtml
  737. ->getTextContent(
  738. $a
  739. );
  740. }
  741. }
  742. switch(count($links)){
  743. case 1:
  744. $out["spelling"] = [
  745. "type" => "including",
  746. "using" => $params["q"],
  747. "correction" => $links[0]
  748. ];
  749. break;
  750. case 2:
  751. $out["spelling"] = [
  752. "type" => "including",
  753. "using" => $links[0],
  754. "correction" => $links[1]
  755. ];
  756. break;
  757. }
  758. // reset
  759. $this->fuckhtml->load($html);
  760. }
  761. $bottom =
  762. $this->fuckhtml
  763. ->getElementById("botstuff");
  764. if($bottom !== null){
  765. $this->fuckhtml->load($bottom);
  766. // get next page thingy
  767. $npt =
  768. $this->fuckhtml
  769. ->getElementById(
  770. "pnnext"
  771. );
  772. if($npt !== false){
  773. $out["npt"] =
  774. $this->backend->store(
  775. json_encode([
  776. "u" =>
  777. $this->fuckhtml
  778. ->getTextContent(
  779. $npt["attributes"]["href"]
  780. ),
  781. "c" => $container
  782. ]),
  783. "web",
  784. $proxy
  785. );
  786. }
  787. // get lumendatabase notices
  788. $notices = [];
  789. $as =
  790. $this->fuckhtml
  791. ->getElementsByTagName("a");
  792. foreach($as as $a){
  793. if(!isset($a["attributes"]["href"])){ continue; }
  794. $link =
  795. $this->fuckhtml
  796. ->getTextContent(
  797. $a["attributes"]["href"]
  798. );
  799. if(
  800. preg_match(
  801. '/^https?:\/\/lumendatabase\.org/',
  802. $link
  803. )
  804. ){
  805. $notices[] = $link;
  806. }
  807. }
  808. // push notices to dom
  809. if(count($notices) !== 0){
  810. $notices_dom = [];
  811. $i = 0;
  812. $c = count($notices);
  813. $notices_dom = [
  814. [
  815. "type" => "text",
  816. "value" => "Google removed results from this page, but you can get them anyways by consulting these takedown notices: "
  817. ]
  818. ];
  819. foreach($notices as $n){
  820. $i++;
  821. $notices_dom[] = [
  822. "type" => "link",
  823. "value" => "Notice #{$i}",
  824. "url" => $n
  825. ];
  826. if($i !== count($notices)){
  827. $notices_dom[] = [
  828. "type" => "text",
  829. "value" => ", "
  830. ];
  831. }
  832. }
  833. $out["answer"][] = [
  834. "title" => "LumenDB notices",
  835. "description" => $notices_dom,
  836. "url" => null,
  837. "thumb" => null,
  838. "table" => [],
  839. "sublink" => []
  840. ];
  841. }
  842. }
  843. $this->fuckhtml->load($content);
  844. // get search results
  845. $results =
  846. $this->fuckhtml
  847. ->getElementsByAttributeName(
  848. "data-rpos",
  849. "div"
  850. );
  851. foreach($results as $result){
  852. $this->fuckhtml->load($result);
  853. // get title
  854. $title =
  855. $this->fuckhtml
  856. ->getElementsByTagName("h3");
  857. if(count($title) === 0){
  858. // should not happen
  859. continue;
  860. }
  861. $title =
  862. $this->titledots(
  863. $this->fuckhtml
  864. ->getTextContent(
  865. $title[0]
  866. )
  867. );
  868. $url =
  869. $this->fuckhtml
  870. ->getElementsByTagName(
  871. "a"
  872. );
  873. if(
  874. count($url) === 0 ||
  875. !isset($url[0]["attributes"]["href"])
  876. ){
  877. continue;
  878. }
  879. $url =
  880. $this->fuckhtml
  881. ->getTextContent(
  882. $url[0]["attributes"]["href"]
  883. );
  884. if(
  885. preg_match(
  886. '/^\/goto\?url/',
  887. $url
  888. )
  889. ){
  890. // encrypted url detected
  891. // get reference div
  892. $refdivs =
  893. $this->fuckhtml
  894. ->getElementsByAttributeName(
  895. "id",
  896. "div"
  897. );
  898. foreach($refdivs as $r){
  899. if(
  900. preg_match(
  901. '/^atritem-/',
  902. $r["attributes"]["id"]
  903. ) &&
  904. isset($r["attributes"]["jsdata"])
  905. ){
  906. $ref = explode(";", $r["attributes"]["jsdata"]);
  907. $ref = $ref[count($ref) - 1];
  908. if(isset($encoded_links[$ref][32][3][0])){
  909. $url = $encoded_links[$ref][32][3][0];
  910. }
  911. break;
  912. }
  913. }
  914. }
  915. // get citation text. contains table elements
  916. $table = [];
  917. $date = null;
  918. $cite =
  919. $this->fuckhtml
  920. ->getElementsByTagName("cite");
  921. if(isset($cite[0])){
  922. $texts =
  923. explode(
  924. " · ",
  925. $this->fuckhtml
  926. ->getTextContent($cite[0])
  927. );
  928. foreach($texts as $text){
  929. $text = trim($text);
  930. preg_match(
  931. '/([0-9+kmb,.]+) +(likes?|views?|comments?|followers?|subscribers?|(?:hours?|days?|months?|years?) +ago)/i',
  932. $text,
  933. $probe
  934. );
  935. if(isset($probe[1])){
  936. // is it date?
  937. if(
  938. preg_match(
  939. '/ago$/i',
  940. $text
  941. )
  942. ){
  943. $time = strtotime($text);
  944. if($time !== false){ $date = $time; }
  945. continue;
  946. }
  947. // anything else?
  948. $table[ucfirst($probe[2])] = trim($probe[1]);
  949. }
  950. }
  951. }
  952. // get description/image wrapper
  953. $desc_img_wrappers =
  954. $this->fuckhtml
  955. ->getElementsByAttributeName(
  956. "data-sncf",
  957. "div"
  958. );
  959. $description = null;
  960. $thumb = [
  961. "url" => null,
  962. "ratio" => null
  963. ];
  964. foreach($desc_img_wrappers as $wrapper){
  965. $this->fuckhtml->load($wrapper);
  966. // probe for image
  967. $image =
  968. $this->fuckhtml
  969. ->getElementsByTagName(
  970. "img"
  971. );
  972. if(count($image) !== 0){
  973. // handle thumbnails
  974. $i = null;
  975. if(isset($image[0]["attributes"]["id"])){
  976. $i = $this->getdimg($image[0]["attributes"]["id"]);
  977. }
  978. if($i !== null){
  979. $thumb = [
  980. "ratio" => "1:1",
  981. "url" => $i
  982. ];
  983. }
  984. }else{
  985. if($description === null){
  986. // parse it as description
  987. $description =
  988. $this->titledots(
  989. $this->fuckhtml
  990. ->getTextContent(
  991. $wrapper
  992. )
  993. );
  994. }else{
  995. // parse table elements
  996. $ts =
  997. explode(
  998. " · ",
  999. $this->fuckhtml
  1000. ->getTextContent(
  1001. $wrapper
  1002. )
  1003. );
  1004. foreach($ts as $t){
  1005. $t = trim(html_entity_decode($t));
  1006. if(preg_match("/\p{Sc}/u", $t)){
  1007. $table["Price"] = $t;
  1008. continue;
  1009. }
  1010. if(str_contains($t, "stock")){
  1011. $table["Stock"] = $t;
  1012. continue;
  1013. }
  1014. if(str_contains($t, "return")){
  1015. $table["Returns"] = $t;
  1016. continue;
  1017. }
  1018. if(str_contains($t, "delivery")){
  1019. $table["Delivery"] = $t;
  1020. continue;
  1021. }
  1022. // 5.0(590,004)
  1023. if(preg_match('/^([0-9kmb\/%.,]+) *(\([0-9kmb,.]+\))?/', $t, $match)){
  1024. if(isset($match[2])){
  1025. $table["Ratings"] = "{$match[1]} {$match[2]}";
  1026. continue;
  1027. }
  1028. }
  1029. if(str_contains($t, "rating")){
  1030. $table["Ratings"] = $t;
  1031. continue;
  1032. }
  1033. }
  1034. }
  1035. }
  1036. }
  1037. // extract date from description
  1038. // Nov 6, 2017 — Keira Hagai from Jak and Daxter. I took off her belt/suspenders/goggles. Originally posted by DemonBoy on Models Resource, and edited and posted to Sketchfab
  1039. if($date === null){
  1040. $date_probe =
  1041. explode(
  1042. " — ",
  1043. $description,
  1044. 2
  1045. );
  1046. if(count($date_probe) === 2){
  1047. $time = strtotime($date_probe[0]);
  1048. if($time !== false){
  1049. $description = $date_probe[1];
  1050. $date = $time;
  1051. }
  1052. }
  1053. }
  1054. $sublinks = [];
  1055. $out["web"][] = [
  1056. "title" => $title,
  1057. "description" => $description,
  1058. "url" => $url,
  1059. "date" => $date,
  1060. "type" => "web",
  1061. "thumb" => $thumb,
  1062. "sublink" => $sublinks,
  1063. "table" => $table
  1064. ];
  1065. }
  1066. return $out;
  1067. }
  1068. public function image($get){
  1069. // generate parameters
  1070. if($get["npt"]){
  1071. [$params, $proxy] =
  1072. $this->backend->get(
  1073. $get["npt"],
  1074. "images"
  1075. );
  1076. $params = json_decode($params, true);
  1077. $container = $params["c"];
  1078. unset($params["c"]);
  1079. }else{
  1080. $search = $get["s"];
  1081. if(strlen($search) === 0){
  1082. throw new Exception("Search term is empty!");
  1083. }
  1084. $proxy = $this->backend->get_ip();
  1085. $country = $get["country"];
  1086. $nsfw = $get["nsfw"];
  1087. $time = $get["time"];
  1088. $size = $get["size"];
  1089. $ratio = $get["ratio"];
  1090. $color = $get["color"];
  1091. $type = $get["type"];
  1092. $format = $get["format"];
  1093. $rights = $get["rights"];
  1094. $params = [
  1095. "q" => $search,
  1096. "udm" => "2", // get images
  1097. "start" => 0
  1098. ];
  1099. // country (image search uses cr instead of gl)
  1100. if($country != "any"){
  1101. $params["cr"] = "country" . strtoupper($country);
  1102. }
  1103. // nsfw
  1104. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1105. // generate tbs
  1106. $tbs = [];
  1107. // time
  1108. if($time != "any"){
  1109. $tbs["qdr"] = $time;
  1110. }
  1111. // size
  1112. if($size != "any"){
  1113. $params["imgsz"] = $size;
  1114. }
  1115. // ratio
  1116. if($ratio != "any"){
  1117. $params["imgar"] = $ratio;
  1118. }
  1119. // color
  1120. if($color != "any"){
  1121. if(
  1122. $color == "color" ||
  1123. $color == "trans"
  1124. ){
  1125. $params["imgc"] = $color;
  1126. }elseif($color == "bnw"){
  1127. $params["imgc"] = "gray";
  1128. }else{
  1129. $tbs["ic"] = "specific";
  1130. $tbs["isc"] = $color;
  1131. }
  1132. }
  1133. // type
  1134. if($type != "any"){
  1135. $tbs["itp"] = $type;
  1136. }
  1137. // format
  1138. if($format != "any"){
  1139. $params["as_filetype"] = $format;
  1140. }
  1141. // rights (tbs)
  1142. if($rights != "any"){
  1143. $tbs["sur"] = $rights;
  1144. }
  1145. // append tbs
  1146. if(count($tbs) !== 0){
  1147. $params["tbs"] = "";
  1148. foreach($tbs as $key => $value){
  1149. $params["tbs"] .= $key . ":" . $value . ",";
  1150. }
  1151. $params["tbs"] = rtrim($params["tbs"], ",");
  1152. }
  1153. }
  1154. //$html = file_get_contents("scraper/google.html");
  1155. $data = $this->get(
  1156. $proxy,
  1157. "https://www.google.com/search",
  1158. $params
  1159. );
  1160. $html = &$data["data"];
  1161. $container = &$data["container"];
  1162. $this->fuckhtml->load($html);
  1163. // parse all <style> tags
  1164. $this->parsestyles();
  1165. $softcaptcha = $this->detect_sorry();
  1166. if($softcaptcha){
  1167. $params["sei"] = $softcaptcha["sei"];
  1168. $data = $this->get(
  1169. $proxy,
  1170. "https://www.google.com/search",
  1171. $params,
  1172. $container
  1173. );
  1174. }
  1175. // get javascript images
  1176. $this->scrape_imagearr($html);
  1177. $out = [
  1178. "status" => "ok",
  1179. "npt" => null,
  1180. "image" => []
  1181. ];
  1182. $images =
  1183. $this->fuckhtml
  1184. ->getElementsByClassName(
  1185. "ivg-i",
  1186. "div"
  1187. );
  1188. foreach($images as $div){
  1189. $this->fuckhtml->load($div);
  1190. $image =
  1191. $this->fuckhtml
  1192. ->getElementsByTagName("img")[0];
  1193. // make sure we dont attempt to show an image we dont have data for
  1194. if(
  1195. isset($div["attributes"]["data-docid"]) &&
  1196. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1197. ){
  1198. $source =
  1199. $this->image_arr[
  1200. $div["attributes"]["data-docid"]
  1201. ];
  1202. }else{
  1203. continue;
  1204. }
  1205. $out["image"][] = [
  1206. "title" =>
  1207. $this->titledots(
  1208. $this->fuckhtml
  1209. ->getTextContent(
  1210. $image["attributes"]["alt"]
  1211. )
  1212. ),
  1213. "source" => $source,
  1214. "url" =>
  1215. $this->fuckhtml
  1216. ->getTextContent(
  1217. $div["attributes"]["data-lpage"]
  1218. )
  1219. ];
  1220. }
  1221. // as usual, no way to check if there is a next page reliably
  1222. if(count($out["image"]) > 50){
  1223. if(!isset($params["start"])){
  1224. $params["start"] = 10;
  1225. }else{
  1226. $params["start"] += 10;
  1227. }
  1228. $out["npt"] =
  1229. $this->backend
  1230. ->store(
  1231. json_encode($params),
  1232. "image",
  1233. $proxy
  1234. );
  1235. }
  1236. return $out;
  1237. }
  1238. public function video($get){
  1239. if($get["npt"]){
  1240. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1241. $params = json_decode($params, true);
  1242. $container = $params["c"];
  1243. unset($params["c"]);
  1244. }else{
  1245. $search = $get["s"];
  1246. $country = $get["country"];
  1247. $nsfw = $get["nsfw"];
  1248. $older = $get["older"];
  1249. $newer = $get["newer"];
  1250. $duration = $get["duration"];
  1251. $quality = $get["quality"];
  1252. $captions = $get["captions"];
  1253. $container = null;
  1254. $proxy = $this->backend->get_ip();
  1255. $params = [
  1256. "q" => $search,
  1257. "udm" => "7",
  1258. "hl" => "en"
  1259. ];
  1260. // country
  1261. if($country != "any"){
  1262. $params["gl"] = $country;
  1263. }
  1264. // nsfw
  1265. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1266. $tbs = [];
  1267. // get date
  1268. $older = $older === false ? null : date("m/d/Y", $older);
  1269. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1270. if(
  1271. $older !== null ||
  1272. $newer !== null
  1273. ){
  1274. $tbs["cdr"] = "1";
  1275. $tbs["cd_min"] = $newer;
  1276. $tbs["cd_max"] = $older;
  1277. }
  1278. // duration
  1279. if($duration != "any"){
  1280. $tbs[] = "dur:" . $duration;
  1281. }
  1282. // quality
  1283. if($quality != "any"){
  1284. $tbs[] = "hq:" . $quality;
  1285. }
  1286. // captions
  1287. if($captions != "any"){
  1288. $tbs[] = "cc:" . $captions;
  1289. }
  1290. // append tbs
  1291. if(count($tbs) !== 0){
  1292. $params["tbs"] =
  1293. implode(",", $tbs);
  1294. }
  1295. }
  1296. //$html = file_get_contents("scraper/google.html");
  1297. $data = $this->get(
  1298. $proxy,
  1299. "https://www.google.com/search",
  1300. $params,
  1301. $container
  1302. );
  1303. $html = &$data["data"];
  1304. $container = &$data["container"];
  1305. if(!isset($params["start"])){
  1306. $params["start"] = 0;
  1307. }
  1308. $params["start"] += 20;
  1309. $this->fuckhtml->load($html);
  1310. // parse all <style> tags
  1311. $this->parsestyles();
  1312. //
  1313. // Parse web video page
  1314. //
  1315. $softcaptcha = $this->detect_sorry();
  1316. if($softcaptcha){
  1317. $params["sei"] = $softcaptcha["sei"];
  1318. $data = $this->get(
  1319. $proxy,
  1320. "https://www.google.com/search",
  1321. $params,
  1322. $container
  1323. );
  1324. }
  1325. // get javascript images
  1326. $this->scrape_dimg($html);
  1327. $this->scrape_imagearr($html);
  1328. $params["c"] = $container;
  1329. $out = [
  1330. "status" => "ok",
  1331. "npt" =>
  1332. $this->backend->store(
  1333. json_encode($params),
  1334. "videos",
  1335. $proxy
  1336. ),
  1337. "video" => [],
  1338. "author" => [],
  1339. "livestream" => [],
  1340. "playlist" => [],
  1341. "reel" => []
  1342. ];
  1343. $search_div =
  1344. $this->fuckhtml
  1345. ->getElementById(
  1346. "center_col"
  1347. );
  1348. if($search_div === false){
  1349. throw new Exception("Failed to grep search div");
  1350. }
  1351. $this->fuckhtml->load($search_div);
  1352. $results =
  1353. $this->fuckhtml
  1354. ->getElementsByClassName(
  1355. $this->getstyle([
  1356. "margin" => "0px 0px 30px"
  1357. ]),
  1358. "div"
  1359. );
  1360. foreach($results as $result){
  1361. $this->fuckhtml->load($result);
  1362. $url =
  1363. $this->fuckhtml
  1364. ->getElementsByTagName(
  1365. "a"
  1366. );
  1367. if(count($url) === 0){
  1368. // no url, weird, continue
  1369. continue;
  1370. }
  1371. $title =
  1372. $this->fuckhtml
  1373. ->getElementsByTagName(
  1374. "h3"
  1375. );
  1376. if(count($title) === 0){
  1377. // no title, weird, continue
  1378. continue;
  1379. }
  1380. // get description
  1381. $description =
  1382. $this->fuckhtml
  1383. ->getElementsByClassName(
  1384. $this->getstyle([
  1385. "-webkit-box-orient" => "vertical",
  1386. "display" => "-webkit-box",
  1387. "-webkit-line-clamp" => "2",
  1388. "overflow" => "hidden",
  1389. "word-break" => "break-word"
  1390. ]),
  1391. "div"
  1392. );
  1393. if(count($description) === 0){
  1394. $description = null;
  1395. }else{
  1396. $description =
  1397. html_entity_decode(
  1398. $this->titledots(
  1399. $this->fuckhtml
  1400. ->getTextContent(
  1401. $description[0]
  1402. )
  1403. )
  1404. );
  1405. }
  1406. // get author + date posted
  1407. $metadiv =
  1408. $this->fuckhtml
  1409. ->getElementsByClassName(
  1410. $this->getstyle([
  1411. "margin-top" => "12px"
  1412. ]),
  1413. "div"
  1414. );
  1415. $author = null;
  1416. $date = null;
  1417. if(count($metadiv) !== 0){
  1418. $metadiv =
  1419. explode(
  1420. "·",
  1421. $this->fuckhtml
  1422. ->getTextContent(
  1423. $metadiv[0]
  1424. )
  1425. );
  1426. if(count($metadiv) === 3){
  1427. $author = trim($metadiv[1]);
  1428. $date = strtotime(trim($metadiv[2]));
  1429. }elseif(count($metadiv) === 2){
  1430. $author = trim($metadiv[0]);
  1431. $date = strtotime(trim($metadiv[1]));
  1432. }
  1433. }
  1434. $thumb = [
  1435. "url" => null,
  1436. "ratio" => null
  1437. ];
  1438. $image =
  1439. $this->fuckhtml
  1440. ->getElementsByTagName(
  1441. "img"
  1442. );
  1443. $duration = null;
  1444. if(
  1445. count($image) !== 0 &&
  1446. isset($image[0]["attributes"]["id"])
  1447. ){
  1448. $thumb = [
  1449. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1450. "ratio" => "16:9"
  1451. ];
  1452. // get duration
  1453. $duration =
  1454. $this->fuckhtml
  1455. ->getElementsByClassName(
  1456. $this->getstyle([
  1457. "background-color" => "rgba(0,0,0,0.6)",
  1458. "color" => "#fff",
  1459. "fill" => "#fff"
  1460. ])
  1461. );
  1462. if(count($duration) !== 0){
  1463. $duration =
  1464. $this->hms2int(
  1465. $this->fuckhtml
  1466. ->getTextContent(
  1467. $duration[0]
  1468. ));
  1469. }else{
  1470. $duration = null;
  1471. }
  1472. }
  1473. $out["video"][] = [
  1474. "title" =>
  1475. $this->titledots(
  1476. $this->fuckhtml
  1477. ->getTextContent(
  1478. $title[0]
  1479. )
  1480. ),
  1481. "description" => $description,
  1482. "author" => [
  1483. "name" => $author,
  1484. "url" => null,
  1485. "avatar" => null
  1486. ],
  1487. "date" => $date,
  1488. "duration" => $duration,
  1489. "views" => null,
  1490. "thumb" => $thumb,
  1491. "url" =>
  1492. $this->fuckhtml
  1493. ->getTextContent(
  1494. $url[0]["attributes"]["href"]
  1495. )
  1496. ];
  1497. }
  1498. return $out;
  1499. }
  1500. public function news($get){
  1501. if($get["npt"]){
  1502. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1503. $req = json_decode($req, true);
  1504. $container = $req["c"];
  1505. $req = $req["u"];
  1506. $data = $this->get(
  1507. $proxy,
  1508. "https://www.google.com" . $req,
  1509. [],
  1510. $container
  1511. );
  1512. $html = &$data["data"];
  1513. $container = &$data["container"];
  1514. }else{
  1515. $search = $get["s"];
  1516. $country = $get["country"];
  1517. $nsfw = $get["nsfw"];
  1518. $older = $get["older"];
  1519. $newer = $get["newer"];
  1520. $sort = $get["sort"];
  1521. $container = null;
  1522. $proxy = $this->backend->get_ip();
  1523. $params = [
  1524. "q" => $search,
  1525. "tbm" => "nws",
  1526. "hl" => "en"
  1527. ];
  1528. // country
  1529. if($country != "any"){
  1530. $params["gl"] = $country;
  1531. }
  1532. // nsfw
  1533. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1534. $tbs = [];
  1535. // get date
  1536. $older = $older === false ? null : date("m/d/Y", $older);
  1537. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1538. if(
  1539. $older !== null ||
  1540. $newer !== null
  1541. ){
  1542. $tbs["cdr"] = "1";
  1543. $tbs["cd_min"] = $newer;
  1544. $tbs["cd_max"] = $older;
  1545. }
  1546. // relevance
  1547. if($sort == "date"){
  1548. $tbs["sbd"] = "1";
  1549. }
  1550. // append tbs
  1551. if(count($tbs) !== 0){
  1552. $params["tbs"] = "";
  1553. foreach($tbs as $key => $value){
  1554. $params["tbs"] .= $key . ":" . $value . ",";
  1555. }
  1556. $params["tbs"] = rtrim($params["tbs"], ",");
  1557. }
  1558. //$html = file_get_contents("scraper/google.html");
  1559. $data = $this->get(
  1560. $proxy,
  1561. "https://www.google.com/search",
  1562. $params
  1563. );
  1564. $html = &$data["data"];
  1565. $container = &$data["container"];
  1566. }
  1567. $out = [
  1568. "status" => "ok",
  1569. "npt" => null,
  1570. "news" => []
  1571. ];
  1572. $this->fuckhtml->load($html);
  1573. // parse styles
  1574. $this->parsestyles();
  1575. $softcaptcha = $this->detect_sorry();
  1576. if($softcaptcha){
  1577. $params["sei"] = $softcaptcha["sei"];
  1578. $data = $this->get(
  1579. $proxy,
  1580. "https://www.google.com/search",
  1581. $params,
  1582. $container
  1583. );
  1584. }
  1585. // get images
  1586. $this->scrape_dimg($html);
  1587. $center_col =
  1588. $this->fuckhtml
  1589. ->getElementById(
  1590. "center_col",
  1591. "div"
  1592. );
  1593. if($center_col === null){
  1594. throw new Exception("Could not grep result div");
  1595. }
  1596. $this->fuckhtml->load($center_col);
  1597. // get next page
  1598. $npt =
  1599. $this->fuckhtml
  1600. ->getElementById(
  1601. "pnnext",
  1602. "a"
  1603. );
  1604. if($npt !== false){
  1605. $out["npt"] =
  1606. $this->backend->store(
  1607. json_encode([
  1608. "u" =>
  1609. $this->fuckhtml
  1610. ->getTextContent(
  1611. $npt["attributes"]
  1612. ["href"]
  1613. ),
  1614. "c" => $container
  1615. ]),
  1616. "news",
  1617. $proxy
  1618. );
  1619. }
  1620. $as =
  1621. $this->fuckhtml
  1622. ->getElementsByAttributeName(
  1623. "jsname",
  1624. "a"
  1625. );
  1626. foreach($as as $a){
  1627. $this->fuckhtml->load($a);
  1628. // get title
  1629. $title =
  1630. $this->fuckhtml
  1631. ->getElementsByAttributeValue(
  1632. "role",
  1633. "heading",
  1634. "div"
  1635. );
  1636. if(count($title) === 0){
  1637. continue;
  1638. }
  1639. $title =
  1640. $this->titledots(
  1641. $this->fuckhtml
  1642. ->getTextContent(
  1643. $title[0]
  1644. )
  1645. );
  1646. // get thumbnail
  1647. $image =
  1648. $this->fuckhtml
  1649. ->getElementsByAttributeName(
  1650. "id",
  1651. "img"
  1652. );
  1653. // check for padded title node, if found, we're inside a carousel
  1654. $probe =
  1655. $this->fuckhtml
  1656. ->getElementsByClassName(
  1657. $this->getstyle(
  1658. [
  1659. "padding" => "16px 16px 40px 16px"
  1660. ]
  1661. ),
  1662. "div"
  1663. );
  1664. if(count($probe) !== 0){
  1665. $probe = true;
  1666. }else{
  1667. $probe = false;
  1668. }
  1669. if(
  1670. count($image) !== 0 &&
  1671. !isset($image[0]["attributes"]["width"])
  1672. ){
  1673. $thumb = [
  1674. "url" =>
  1675. $this->getdimg(
  1676. $image[0]["attributes"]["id"]
  1677. ),
  1678. "ratio" => $probe === true ? "16:9" : "1:1"
  1679. ];
  1680. }else{
  1681. $thumb = [
  1682. "url" => null,
  1683. "ratio" => null
  1684. ];
  1685. }
  1686. $description = null;
  1687. if($probe === false){
  1688. $desc_divs =
  1689. $this->fuckhtml
  1690. ->getElementsByAttributeName(
  1691. "style",
  1692. "div"
  1693. );
  1694. foreach($desc_divs as $desc){
  1695. if(
  1696. strpos(
  1697. $desc["attributes"]["style"],
  1698. "margin-top:"
  1699. ) !== false
  1700. ){
  1701. $description =
  1702. $this->titledots(
  1703. $this->fuckhtml
  1704. ->getTextContent(
  1705. $desc
  1706. )
  1707. );
  1708. break;
  1709. }
  1710. }
  1711. }
  1712. // get author
  1713. $author =
  1714. $this->fuckhtml
  1715. ->getElementsByClassName(
  1716. $this->getstyle(
  1717. [
  1718. "overflow" => "hidden",
  1719. "text-align" => "left",
  1720. "text-overflow" => "ellipsis",
  1721. "white-space" => "nowrap",
  1722. "margin-bottom" => "8px"
  1723. ]
  1724. ),
  1725. "div"
  1726. );
  1727. if(count($author) !== 0){
  1728. $author =
  1729. $this->fuckhtml
  1730. ->getTextContent(
  1731. $author[0]
  1732. );
  1733. }else{
  1734. $author = null;
  1735. }
  1736. // get date
  1737. $date = null;
  1738. $date_div =
  1739. $this->fuckhtml
  1740. ->getElementsByAttributeName(
  1741. "style",
  1742. "div"
  1743. );
  1744. foreach($date_div as $d){
  1745. $this->fuckhtml->load($d);
  1746. $span =
  1747. $this->fuckhtml
  1748. ->getElementsByTagName(
  1749. "span"
  1750. );
  1751. if(
  1752. strpos(
  1753. $d["attributes"]["style"],
  1754. "bottom:"
  1755. ) !== false
  1756. ){
  1757. $date =
  1758. strtotime(
  1759. $this->fuckhtml
  1760. ->getTextContent(
  1761. $span[count($span) - 1]
  1762. )
  1763. );
  1764. break;
  1765. }
  1766. }
  1767. $out["news"][] = [
  1768. "title" => $title,
  1769. "author" => $author,
  1770. "description" => $description,
  1771. "date" => $date,
  1772. "thumb" => $thumb,
  1773. "url" =>
  1774. $this->unshiturl(
  1775. $a["attributes"]
  1776. ["href"]
  1777. )
  1778. ];
  1779. }
  1780. return $out;
  1781. }
  1782. private function scrape_dimg($html){
  1783. // get images loaded through javascript
  1784. $this->dimg = [];
  1785. preg_match_all(
  1786. '/function\(\){google\.ldi=({.*?});/',
  1787. $html,
  1788. $dimg
  1789. );
  1790. if(isset($dimg[1])){
  1791. foreach($dimg[1] as $i){
  1792. $tmp = json_decode($i, true);
  1793. foreach($tmp as $key => $value){
  1794. $this->dimg[$key] =
  1795. $this->unshit_thumb(
  1796. $value
  1797. );
  1798. }
  1799. }
  1800. }
  1801. // get additional javascript base64 images
  1802. preg_match_all(
  1803. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1804. $html,
  1805. $dimg
  1806. );
  1807. if(isset($dimg[1])){
  1808. for($i=0; $i<count($dimg[1]); $i++){
  1809. $delims = explode(",", $dimg[2][$i]);
  1810. $string =
  1811. $this->fuckhtml
  1812. ->parseJsString(
  1813. $dimg[1][$i]
  1814. );
  1815. foreach($delims as $delim){
  1816. $this->dimg[trim($delim, "'")] = $string;
  1817. }
  1818. }
  1819. }
  1820. }
  1821. private function scrape_imagearr($html){
  1822. // get image links arrays
  1823. preg_match_all(
  1824. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1825. $html,
  1826. $image_arr
  1827. );
  1828. $this->image_arr = [];
  1829. if(isset($image_arr[1])){
  1830. for($i=0; $i<count($image_arr[1]); $i++){
  1831. $original =
  1832. $this->fuckhtml
  1833. ->parseJsString(
  1834. $image_arr[5][$i]
  1835. );
  1836. if(
  1837. preg_match(
  1838. '/^x-raw-image/',
  1839. $original
  1840. )
  1841. ){
  1842. // only add thumbnail, google doesnt have OG resolution
  1843. $this->image_arr[$image_arr[1][$i]] = [
  1844. [
  1845. "url" =>
  1846. $this->unshit_thumb(
  1847. $this->fuckhtml
  1848. ->parseJsString(
  1849. $image_arr[2][$i]
  1850. )
  1851. ),
  1852. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1853. "height" => (int)$image_arr[6][$i]
  1854. ]
  1855. ];
  1856. continue;
  1857. }
  1858. $this->image_arr[$image_arr[1][$i]] =
  1859. [
  1860. [
  1861. "url" => $original,
  1862. "width" => (int)$image_arr[7][$i],
  1863. "height" => (int)$image_arr[6][$i]
  1864. ],
  1865. [
  1866. "url" =>
  1867. $this->unshit_thumb(
  1868. $this->fuckhtml
  1869. ->parseJsString(
  1870. $image_arr[2][$i]
  1871. )
  1872. ),
  1873. "width" => (int)$image_arr[4][$i],
  1874. "height" => (int)$image_arr[3][$i]
  1875. ]
  1876. ];
  1877. }
  1878. }
  1879. }
  1880. private function getdimg($dimg){
  1881. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1882. }
  1883. private function unshit_thumb($url, $get_bigger_res = false){
  1884. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1885. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1886. $parts = parse_url($url);
  1887. if(
  1888. isset($parts["host"]) &&
  1889. preg_match(
  1890. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1891. $parts["host"]
  1892. )
  1893. ){
  1894. parse_str($parts["query"], $params);
  1895. if(isset($params["q"])){
  1896. if($get_bigger_res){
  1897. // this method doesnt always work, but does work for wiki thumbnails
  1898. return
  1899. "https://" . $parts["host"] . "/images?q=tbn:" .
  1900. $this->base64url_encode(
  1901. substr(
  1902. $this->base64url_decode(
  1903. explode(
  1904. ":",
  1905. $params["q"])[1]
  1906. ),
  1907. 0,
  1908. 29
  1909. )
  1910. );
  1911. }else{
  1912. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1913. }
  1914. }
  1915. }
  1916. return $url;
  1917. }
  1918. private function parsestyles(){
  1919. $styles = [];
  1920. $style_div =
  1921. $this->fuckhtml
  1922. ->getElementsByTagName(
  1923. "style"
  1924. );
  1925. $raw_styles = "";
  1926. foreach($style_div as $style){
  1927. $raw_styles .= $style["innerHTML"];
  1928. }
  1929. // filter out media/keyframe queries
  1930. $raw_styles =
  1931. preg_replace(
  1932. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1933. "",
  1934. $raw_styles
  1935. );
  1936. // get styles
  1937. preg_match_all(
  1938. '/(.+?){([\S\s]*?)}/',
  1939. $raw_styles,
  1940. $matches
  1941. );
  1942. for($i=0; $i<count($matches[1]); $i++){
  1943. // get style values
  1944. preg_match_all(
  1945. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1946. $matches[2][$i],
  1947. $values_regex
  1948. );
  1949. $values = [];
  1950. for($k=0; $k<count($values_regex[1]); $k++){
  1951. $values[trim($values_regex[1][$k])] =
  1952. strtolower(trim($values_regex[2][$k]));
  1953. }
  1954. $names = explode(",", $matches[1][$i]);
  1955. // h1,h2,h3 will each get their own array index
  1956. foreach($names as $name){
  1957. $name = trim($name, "}\t\n\r\0\x0B");
  1958. foreach($values as $key => $value){
  1959. $styles[$name][$key] = $value;
  1960. }
  1961. }
  1962. }
  1963. foreach($styles as $key => $values){
  1964. $styles[$key]["_c"] = count($values);
  1965. }
  1966. $this->styles = $styles;
  1967. // get CSS colors
  1968. $this->css_colors = [];
  1969. if(isset($this->styles[":root"])){
  1970. foreach($this->styles[":root"] as $key => $value){
  1971. $this->css_colors[$value] = strtolower($key);
  1972. }
  1973. }
  1974. }
  1975. private function getstyle($styles){
  1976. $styles["_c"] = count($styles);
  1977. foreach($this->styles as $style_key => $style_values){
  1978. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1979. $style_key =
  1980. explode(" ", $style_key);
  1981. $style_key = $style_key[count($style_key) - 1];
  1982. return
  1983. ltrim(
  1984. str_replace(
  1985. [".", "#"],
  1986. " ",
  1987. $style_key
  1988. )
  1989. );
  1990. }
  1991. }
  1992. return false;
  1993. }
  1994. private function getcolorvar($color){
  1995. if(isset($this->css_colors[$color])){
  1996. return $this->css_colors[$color];
  1997. }
  1998. return null;
  1999. }
  2000. private function unshiturl($url, $return_size = false){
  2001. // decode
  2002. $url =
  2003. $this->fuckhtml
  2004. ->getTextContent(
  2005. $url
  2006. );
  2007. $url_parts = parse_url($url);
  2008. if(isset($url_parts["query"])){
  2009. parse_str($url_parts["query"], $query);
  2010. }else{
  2011. $query = [];
  2012. }
  2013. if(
  2014. !isset(
  2015. $url_parts["host"]
  2016. ) ||
  2017. stripos($url_parts["host"], "google.") !== false
  2018. ){
  2019. // no host, we have a tracking url
  2020. if(isset($query["imgurl"])){
  2021. $url = $query["imgurl"];
  2022. }
  2023. elseif(isset($query["q"])){
  2024. $url = $query["q"];
  2025. }
  2026. }
  2027. // rewrite URLs to remove extra tracking parameters
  2028. $domain = parse_url($url, PHP_URL_HOST);
  2029. if(
  2030. preg_match(
  2031. '/wikipedia.org$/',
  2032. $domain
  2033. )
  2034. ){
  2035. // rewrite wikipedia mobile URLs to desktop
  2036. $url =
  2037. $this->replacedomain(
  2038. $url,
  2039. preg_replace(
  2040. '/([a-z0-9]+)(\.m\.)/',
  2041. '$1.',
  2042. $domain
  2043. )
  2044. );
  2045. }
  2046. elseif(
  2047. preg_match(
  2048. '/imdb\.com$|youtube\.[^.]+$/',
  2049. $domain
  2050. )
  2051. ){
  2052. // rewrite imdb and youtube mobile URLs too
  2053. $url =
  2054. $this->replacedomain(
  2055. $url,
  2056. preg_replace(
  2057. '/^m\./',
  2058. "",
  2059. $domain
  2060. )
  2061. );
  2062. }
  2063. elseif(
  2064. preg_match(
  2065. '/play\.google\.[^.]+$/',
  2066. $domain
  2067. )
  2068. ){
  2069. // remove referrers from play.google.com
  2070. $u_query = parse_url($url, PHP_URL_QUERY);
  2071. if($u_query !== null){
  2072. parse_str($u_query, $u_query);
  2073. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2074. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2075. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2076. $query = http_build_query($query);
  2077. $url =
  2078. str_replace(
  2079. $u_query,
  2080. $u_query,
  2081. $url
  2082. );
  2083. }
  2084. }
  2085. elseif(
  2086. preg_match(
  2087. '/twitter\.com$/',
  2088. $domain
  2089. )
  2090. ){
  2091. // remove more referrers from twitter.com
  2092. $u_query = parse_url($url, PHP_URL_QUERY);
  2093. if($u_query !== null){
  2094. parse_str($u_query, $u_query);
  2095. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  2096. $u_query = http_build_query($u_query);
  2097. $url =
  2098. str_replace(
  2099. $oldquery,
  2100. $u_query,
  2101. $url
  2102. );
  2103. }
  2104. }
  2105. elseif(
  2106. preg_match(
  2107. '/maps\.google\.[^.]+/',
  2108. $domain
  2109. )
  2110. ){
  2111. if(stripos($url, "maps?") !== false){
  2112. $u_query = parse_url($url, PHP_URL_QUERY);
  2113. if($u_query !== null){
  2114. parse_str($u_query, $u_query);
  2115. if(isset($u_query["daddr"])){
  2116. $url =
  2117. "https://maps.google.com/maps?daddr=" .
  2118. urlencode($u_query["daddr"]);
  2119. }
  2120. }
  2121. }
  2122. }
  2123. if($return_size){
  2124. return [
  2125. "url" => $url,
  2126. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2127. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2128. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2129. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2130. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2131. ];
  2132. }
  2133. return $url;
  2134. }
  2135. private function replacedomain($url, $domain){
  2136. return
  2137. preg_replace(
  2138. '/(https?:\/\/)([^\/]+)/',
  2139. '$1' . $domain,
  2140. $url
  2141. );
  2142. }
  2143. private function titledots($title){
  2144. return trim($title, " .\t\n\r\0\x0B");
  2145. }
  2146. private function hms2int($time){
  2147. $parts = explode(":", $time, 3);
  2148. $time = 0;
  2149. if(count($parts) === 3){
  2150. // hours
  2151. $time = $time + ((int)$parts[0] * 3600);
  2152. array_shift($parts);
  2153. }
  2154. if(count($parts) === 2){
  2155. // minutes
  2156. $time = $time + ((int)$parts[0] * 60);
  2157. array_shift($parts);
  2158. }
  2159. // seconds
  2160. $time = $time + (int)$parts[0];
  2161. return $time;
  2162. }
  2163. function base64url_decode($data){
  2164. $b64 = strtr($data, "-_", "+/");
  2165. $pad = strlen($b64) % 4;
  2166. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  2167. return base64_decode($b64);
  2168. }
  2169. function base64url_encode($data){
  2170. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  2171. }
  2172. private function detect_sorry(){
  2173. // detect recaptcha
  2174. $captcha_form =
  2175. $this->fuckhtml
  2176. ->getElementById(
  2177. "captcha-form",
  2178. "form"
  2179. );
  2180. if($captcha_form !== false){
  2181. throw new Exception("Google returned a captcha");
  2182. }
  2183. // detect JS challenge
  2184. $title =
  2185. $this->fuckhtml
  2186. ->getElementsByTagName(
  2187. "title"
  2188. );
  2189. if(
  2190. count($title) !== 0 &&
  2191. $this->fuckhtml
  2192. ->getTextContent(
  2193. $title[0]
  2194. ) == "Google Search"
  2195. ){
  2196. throw new Exception("Google returned a JS challenge");
  2197. }
  2198. // bypass soft captcha
  2199. // "Verifying your request
  2200. // You'll be able to continue in a few seconds. Don't refresh this page."
  2201. $scripts =
  2202. $this->fuckhtml
  2203. ->getElementsByTagName(
  2204. "script"
  2205. );
  2206. foreach($scripts as $sc){
  2207. if(
  2208. preg_match(
  2209. '/var eid ?= ?\'(.*)\'[\S\s]*, ?([0-9]+)\);/U',
  2210. $sc["innerHTML"],
  2211. $matches
  2212. )
  2213. ){
  2214. sleep(10); // gotta sleep to not trigger captcha. blame google, not me
  2215. return [
  2216. "sei" => $matches[1],
  2217. "timeout" => (int)$matches[2] // set to 10000 usually
  2218. ];
  2219. }
  2220. }
  2221. return false; // no need to bypass
  2222. }
  2223. }