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. $data = $this->get(
  649. $proxy,
  650. "https://www.google.com/search",
  651. $params,
  652. null
  653. );
  654. $html = &$data["data"];
  655. $container = &$data["container"];
  656. //file_put_contents("scraper/google-jp.html", $html);
  657. //$html = file_get_contents("scraper/google-softcaptcha.html");
  658. }
  659. $out = [
  660. "status" => "ok",
  661. "spelling" => [
  662. "type" => "no_correction",
  663. "using" => null,
  664. "correction" => null
  665. ],
  666. "npt" => null,
  667. "answer" => [],
  668. "web" => [],
  669. "image" => [],
  670. "video" => [],
  671. "news" => [],
  672. "related" => []
  673. ];
  674. $this->fuckhtml->load($html);
  675. // init
  676. $this->parsestyles();
  677. // solve softcaptcha
  678. $softcaptcha = $this->detect_sorry();
  679. if($softcaptcha){
  680. $params["sei"] = $softcaptcha["sei"];
  681. $data = $this->get(
  682. $proxy,
  683. "https://www.google.com/search",
  684. $params,
  685. $container
  686. );
  687. }
  688. $this->scrape_dimg($html);
  689. $this->scrape_imagearr($html);
  690. $content =
  691. $this->fuckhtml
  692. ->getElementById(
  693. "rso"
  694. );
  695. if($content === null){
  696. throw new Exception("Failed to grep search div");
  697. }
  698. // get encoded links
  699. $encoded_links =
  700. explode(
  701. "function(){var m={",
  702. $html,
  703. 2
  704. );
  705. if(count($encoded_links) === 2){
  706. $encoded_links =
  707. json_decode(
  708. $this->fuckhtml
  709. ->extract_json(
  710. "{" . $encoded_links[1]
  711. ),
  712. true
  713. );
  714. }else{
  715. $encoded_links = [];
  716. }
  717. // get the "did you mean" bullshit
  718. $taw =
  719. $this->fuckhtml
  720. ->getElementById("taw");
  721. if($taw !== null){
  722. $this->fuckhtml->load($taw);
  723. $as =
  724. $this->fuckhtml
  725. ->getElementsByTagName("a");
  726. $links = [];
  727. foreach($as as $a){
  728. $href =
  729. $this->fuckhtml
  730. ->getTextContent(
  731. $a["attributes"]["href"]
  732. );
  733. if(preg_match('/^\/search\?/', $href)){
  734. $links[] =
  735. $this->fuckhtml
  736. ->getTextContent(
  737. $a
  738. );
  739. }
  740. }
  741. switch(count($links)){
  742. case 1:
  743. $out["spelling"] = [
  744. "type" => "including",
  745. "using" => $params["q"],
  746. "correction" => $links[0]
  747. ];
  748. break;
  749. case 2:
  750. $out["spelling"] = [
  751. "type" => "including",
  752. "using" => $links[0],
  753. "correction" => $links[1]
  754. ];
  755. break;
  756. }
  757. // reset
  758. $this->fuckhtml->load($html);
  759. }
  760. $bottom =
  761. $this->fuckhtml
  762. ->getElementById("botstuff");
  763. if($bottom !== null){
  764. $this->fuckhtml->load($bottom);
  765. // get next page thingy
  766. $npt =
  767. $this->fuckhtml
  768. ->getElementById(
  769. "pnnext"
  770. );
  771. if($npt !== false){
  772. $out["npt"] =
  773. $this->backend->store(
  774. json_encode([
  775. "u" =>
  776. $this->fuckhtml
  777. ->getTextContent(
  778. $npt["attributes"]["href"]
  779. ),
  780. "c" => $container
  781. ]),
  782. "web",
  783. $proxy
  784. );
  785. }
  786. // get lumendatabase notices
  787. $notices = [];
  788. $as =
  789. $this->fuckhtml
  790. ->getElementsByTagName("a");
  791. foreach($as as $a){
  792. if(!isset($a["attributes"]["href"])){ continue; }
  793. $link =
  794. $this->fuckhtml
  795. ->getTextContent(
  796. $a["attributes"]["href"]
  797. );
  798. if(
  799. preg_match(
  800. '/^https?:\/\/lumendatabase\.org/',
  801. $link
  802. )
  803. ){
  804. $notices[] = $link;
  805. }
  806. }
  807. // push notices to dom
  808. if(count($notices) !== 0){
  809. $notices_dom = [];
  810. $i = 0;
  811. $c = count($notices);
  812. $notices_dom = [
  813. [
  814. "type" => "text",
  815. "value" => "Google removed results from this page, but you can get them anyways by consulting these takedown notices: "
  816. ]
  817. ];
  818. foreach($notices as $n){
  819. $i++;
  820. $notices_dom[] = [
  821. "type" => "link",
  822. "value" => "Notice #{$i}",
  823. "url" => $n
  824. ];
  825. if($i !== count($notices)){
  826. $notices_dom[] = [
  827. "type" => "text",
  828. "value" => ", "
  829. ];
  830. }
  831. }
  832. $out["answer"][] = [
  833. "title" => "LumenDB notices",
  834. "description" => $notices_dom,
  835. "url" => null,
  836. "thumb" => null,
  837. "table" => [],
  838. "sublink" => []
  839. ];
  840. }
  841. }
  842. $this->fuckhtml->load($content);
  843. // get search results
  844. $results =
  845. $this->fuckhtml
  846. ->getElementsByAttributeName(
  847. "data-rpos",
  848. "div"
  849. );
  850. foreach($results as $result){
  851. $this->fuckhtml->load($result);
  852. // get title
  853. $title =
  854. $this->fuckhtml
  855. ->getElementsByTagName("h3");
  856. if(count($title) === 0){
  857. // should not happen
  858. continue;
  859. }
  860. $title =
  861. $this->titledots(
  862. $this->fuckhtml
  863. ->getTextContent(
  864. $title[0]
  865. )
  866. );
  867. $url =
  868. $this->fuckhtml
  869. ->getElementsByTagName(
  870. "a"
  871. );
  872. if(
  873. count($url) === 0 ||
  874. !isset($url[0]["attributes"]["href"])
  875. ){
  876. continue;
  877. }
  878. $url =
  879. $this->fuckhtml
  880. ->getTextContent(
  881. $url[0]["attributes"]["href"]
  882. );
  883. if(
  884. preg_match(
  885. '/^\/goto\?url/',
  886. $url
  887. )
  888. ){
  889. // encrypted url detected
  890. // get reference div
  891. $refdivs =
  892. $this->fuckhtml
  893. ->getElementsByAttributeName(
  894. "id",
  895. "div"
  896. );
  897. foreach($refdivs as $r){
  898. if(
  899. preg_match(
  900. '/^atritem-/',
  901. $r["attributes"]["id"]
  902. ) &&
  903. isset($r["attributes"]["jsdata"])
  904. ){
  905. $ref = explode(";", $r["attributes"]["jsdata"]);
  906. $ref = $ref[count($ref) - 1];
  907. if(isset($encoded_links[$ref][32][3][0])){
  908. $url = $encoded_links[$ref][32][3][0];
  909. }
  910. break;
  911. }
  912. }
  913. }
  914. // get citation text. contains table elements
  915. $table = [];
  916. $date = null;
  917. $cite =
  918. $this->fuckhtml
  919. ->getElementsByTagName("cite");
  920. if(isset($cite[0])){
  921. $texts =
  922. explode(
  923. " · ",
  924. $this->fuckhtml
  925. ->getTextContent($cite[0])
  926. );
  927. foreach($texts as $text){
  928. $text = trim($text);
  929. preg_match(
  930. '/([0-9+kmb,.]+) +(likes?|views?|comments?|followers?|subscribers?|(?:hours?|days?|months?|years?) +ago)/i',
  931. $text,
  932. $probe
  933. );
  934. if(isset($probe[1])){
  935. // is it date?
  936. if(
  937. preg_match(
  938. '/ago$/i',
  939. $text
  940. )
  941. ){
  942. $time = strtotime($text);
  943. if($time !== false){ $date = $time; }
  944. continue;
  945. }
  946. // anything else?
  947. $table[ucfirst($probe[2])] = trim($probe[1]);
  948. }
  949. }
  950. }
  951. // get description/image wrapper
  952. $desc_img_wrappers =
  953. $this->fuckhtml
  954. ->getElementsByAttributeName(
  955. "data-sncf",
  956. "div"
  957. );
  958. $description = null;
  959. $thumb = [
  960. "url" => null,
  961. "ratio" => null
  962. ];
  963. foreach($desc_img_wrappers as $wrapper){
  964. $this->fuckhtml->load($wrapper);
  965. // probe for image
  966. $image =
  967. $this->fuckhtml
  968. ->getElementsByTagName(
  969. "img"
  970. );
  971. if(count($image) !== 0){
  972. // handle thumbnails
  973. $i = null;
  974. if(isset($image[0]["attributes"]["id"])){
  975. $i = $this->getdimg($image[0]["attributes"]["id"]);
  976. }
  977. if($i !== null){
  978. $thumb = [
  979. "ratio" => "1:1",
  980. "url" => $i
  981. ];
  982. }
  983. }else{
  984. if($description === null){
  985. // parse it as description
  986. $description =
  987. $this->titledots(
  988. $this->fuckhtml
  989. ->getTextContent(
  990. $wrapper
  991. )
  992. );
  993. }else{
  994. // parse table elements
  995. $ts =
  996. explode(
  997. " · ",
  998. $this->fuckhtml
  999. ->getTextContent(
  1000. $wrapper
  1001. )
  1002. );
  1003. foreach($ts as $t){
  1004. $t = trim(html_entity_decode($t));
  1005. if(preg_match("/\p{Sc}/u", $t)){
  1006. $table["Price"] = $t;
  1007. continue;
  1008. }
  1009. if(str_contains($t, "stock")){
  1010. $table["Stock"] = $t;
  1011. continue;
  1012. }
  1013. if(str_contains($t, "return")){
  1014. $table["Returns"] = $t;
  1015. continue;
  1016. }
  1017. if(str_contains($t, "delivery")){
  1018. $table["Delivery"] = $t;
  1019. continue;
  1020. }
  1021. // 5.0(590,004)
  1022. if(preg_match('/^([0-9kmb\/%.,]+) *(\([0-9kmb,.]+\))?/', $t, $match)){
  1023. if(isset($match[2])){
  1024. $table["Ratings"] = "{$match[1]} {$match[2]}";
  1025. continue;
  1026. }
  1027. }
  1028. if(str_contains($t, "rating")){
  1029. $table["Ratings"] = $t;
  1030. continue;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. // extract date from description
  1037. // 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
  1038. if($date === null){
  1039. $date_probe =
  1040. explode(
  1041. " — ",
  1042. $description,
  1043. 2
  1044. );
  1045. if(count($date_probe) === 2){
  1046. $time = strtotime($date_probe[0]);
  1047. if($time !== false){
  1048. $description = $date_probe[1];
  1049. $date = $time;
  1050. }
  1051. }
  1052. }
  1053. $sublinks = [];
  1054. $out["web"][] = [
  1055. "title" => $title,
  1056. "description" => $description,
  1057. "url" => $url,
  1058. "date" => $date,
  1059. "type" => "web",
  1060. "thumb" => $thumb,
  1061. "sublink" => $sublinks,
  1062. "table" => $table
  1063. ];
  1064. }
  1065. return $out;
  1066. }
  1067. public function image($get){
  1068. // generate parameters
  1069. if($get["npt"]){
  1070. [$params, $proxy] =
  1071. $this->backend->get(
  1072. $get["npt"],
  1073. "images"
  1074. );
  1075. $params = json_decode($params, true);
  1076. $container = $params["c"];
  1077. unset($params["c"]);
  1078. }else{
  1079. $search = $get["s"];
  1080. if(strlen($search) === 0){
  1081. throw new Exception("Search term is empty!");
  1082. }
  1083. $proxy = $this->backend->get_ip();
  1084. $country = $get["country"];
  1085. $nsfw = $get["nsfw"];
  1086. $time = $get["time"];
  1087. $size = $get["size"];
  1088. $ratio = $get["ratio"];
  1089. $color = $get["color"];
  1090. $type = $get["type"];
  1091. $format = $get["format"];
  1092. $rights = $get["rights"];
  1093. $params = [
  1094. "q" => $search,
  1095. "udm" => "2", // get images
  1096. "start" => 0
  1097. ];
  1098. // country (image search uses cr instead of gl)
  1099. if($country != "any"){
  1100. $params["cr"] = "country" . strtoupper($country);
  1101. }
  1102. // nsfw
  1103. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1104. // generate tbs
  1105. $tbs = [];
  1106. // time
  1107. if($time != "any"){
  1108. $tbs["qdr"] = $time;
  1109. }
  1110. // size
  1111. if($size != "any"){
  1112. $params["imgsz"] = $size;
  1113. }
  1114. // ratio
  1115. if($ratio != "any"){
  1116. $params["imgar"] = $ratio;
  1117. }
  1118. // color
  1119. if($color != "any"){
  1120. if(
  1121. $color == "color" ||
  1122. $color == "trans"
  1123. ){
  1124. $params["imgc"] = $color;
  1125. }elseif($color == "bnw"){
  1126. $params["imgc"] = "gray";
  1127. }else{
  1128. $tbs["ic"] = "specific";
  1129. $tbs["isc"] = $color;
  1130. }
  1131. }
  1132. // type
  1133. if($type != "any"){
  1134. $tbs["itp"] = $type;
  1135. }
  1136. // format
  1137. if($format != "any"){
  1138. $params["as_filetype"] = $format;
  1139. }
  1140. // rights (tbs)
  1141. if($rights != "any"){
  1142. $tbs["sur"] = $rights;
  1143. }
  1144. // append tbs
  1145. if(count($tbs) !== 0){
  1146. $params["tbs"] = "";
  1147. foreach($tbs as $key => $value){
  1148. $params["tbs"] .= $key . ":" . $value . ",";
  1149. }
  1150. $params["tbs"] = rtrim($params["tbs"], ",");
  1151. }
  1152. }
  1153. //$html = file_get_contents("scraper/google.html");
  1154. $data = $this->get(
  1155. $proxy,
  1156. "https://www.google.com/search",
  1157. $params
  1158. );
  1159. $html = &$data["data"];
  1160. $container = &$data["container"];
  1161. $this->fuckhtml->load($html);
  1162. // parse all <style> tags
  1163. $this->parsestyles();
  1164. $softcaptcha = $this->detect_sorry();
  1165. if($softcaptcha){
  1166. $params["sei"] = $softcaptcha["sei"];
  1167. $data = $this->get(
  1168. $proxy,
  1169. "https://www.google.com/search",
  1170. $params,
  1171. $container
  1172. );
  1173. }
  1174. // get javascript images
  1175. $this->scrape_imagearr($html);
  1176. $out = [
  1177. "status" => "ok",
  1178. "npt" => null,
  1179. "image" => []
  1180. ];
  1181. $images =
  1182. $this->fuckhtml
  1183. ->getElementsByClassName(
  1184. "ivg-i",
  1185. "div"
  1186. );
  1187. foreach($images as $div){
  1188. $this->fuckhtml->load($div);
  1189. $image =
  1190. $this->fuckhtml
  1191. ->getElementsByTagName("img")[0];
  1192. // make sure we dont attempt to show an image we dont have data for
  1193. if(
  1194. isset($div["attributes"]["data-docid"]) &&
  1195. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1196. ){
  1197. $source =
  1198. $this->image_arr[
  1199. $div["attributes"]["data-docid"]
  1200. ];
  1201. }else{
  1202. continue;
  1203. }
  1204. $out["image"][] = [
  1205. "title" =>
  1206. $this->titledots(
  1207. $this->fuckhtml
  1208. ->getTextContent(
  1209. $image["attributes"]["alt"]
  1210. )
  1211. ),
  1212. "source" => $source,
  1213. "url" =>
  1214. $this->fuckhtml
  1215. ->getTextContent(
  1216. $div["attributes"]["data-lpage"]
  1217. )
  1218. ];
  1219. }
  1220. // as usual, no way to check if there is a next page reliably
  1221. if(count($out["image"]) > 50){
  1222. if(!isset($params["start"])){
  1223. $params["start"] = 10;
  1224. }else{
  1225. $params["start"] += 10;
  1226. }
  1227. $out["npt"] =
  1228. $this->backend
  1229. ->store(
  1230. json_encode($params),
  1231. "image",
  1232. $proxy
  1233. );
  1234. }
  1235. return $out;
  1236. }
  1237. public function video($get){
  1238. if($get["npt"]){
  1239. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1240. $params = json_decode($params, true);
  1241. $container = $params["c"];
  1242. unset($params["c"]);
  1243. }else{
  1244. $search = $get["s"];
  1245. $country = $get["country"];
  1246. $nsfw = $get["nsfw"];
  1247. $older = $get["older"];
  1248. $newer = $get["newer"];
  1249. $duration = $get["duration"];
  1250. $quality = $get["quality"];
  1251. $captions = $get["captions"];
  1252. $container = null;
  1253. $proxy = $this->backend->get_ip();
  1254. $params = [
  1255. "q" => $search,
  1256. "udm" => "7",
  1257. "hl" => "en"
  1258. ];
  1259. // country
  1260. if($country != "any"){
  1261. $params["gl"] = $country;
  1262. }
  1263. // nsfw
  1264. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1265. $tbs = [];
  1266. // get date
  1267. $older = $older === false ? null : date("m/d/Y", $older);
  1268. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1269. if(
  1270. $older !== null ||
  1271. $newer !== null
  1272. ){
  1273. $tbs["cdr"] = "1";
  1274. $tbs["cd_min"] = $newer;
  1275. $tbs["cd_max"] = $older;
  1276. }
  1277. // duration
  1278. if($duration != "any"){
  1279. $tbs[] = "dur:" . $duration;
  1280. }
  1281. // quality
  1282. if($quality != "any"){
  1283. $tbs[] = "hq:" . $quality;
  1284. }
  1285. // captions
  1286. if($captions != "any"){
  1287. $tbs[] = "cc:" . $captions;
  1288. }
  1289. // append tbs
  1290. if(count($tbs) !== 0){
  1291. $params["tbs"] =
  1292. implode(",", $tbs);
  1293. }
  1294. }
  1295. //$html = file_get_contents("scraper/google.html");
  1296. $data = $this->get(
  1297. $proxy,
  1298. "https://www.google.com/search",
  1299. $params,
  1300. $container
  1301. );
  1302. $html = &$data["data"];
  1303. $container = &$data["container"];
  1304. if(!isset($params["start"])){
  1305. $params["start"] = 0;
  1306. }
  1307. $params["start"] += 20;
  1308. $this->fuckhtml->load($html);
  1309. // parse all <style> tags
  1310. $this->parsestyles();
  1311. //
  1312. // Parse web video page
  1313. //
  1314. $softcaptcha = $this->detect_sorry();
  1315. if($softcaptcha){
  1316. $params["sei"] = $softcaptcha["sei"];
  1317. $data = $this->get(
  1318. $proxy,
  1319. "https://www.google.com/search",
  1320. $params,
  1321. $container
  1322. );
  1323. }
  1324. // get javascript images
  1325. $this->scrape_dimg($html);
  1326. $this->scrape_imagearr($html);
  1327. $params["c"] = $container;
  1328. $out = [
  1329. "status" => "ok",
  1330. "npt" =>
  1331. $this->backend->store(
  1332. json_encode($params),
  1333. "videos",
  1334. $proxy
  1335. ),
  1336. "video" => [],
  1337. "author" => [],
  1338. "livestream" => [],
  1339. "playlist" => [],
  1340. "reel" => []
  1341. ];
  1342. $search_div =
  1343. $this->fuckhtml
  1344. ->getElementById(
  1345. "center_col"
  1346. );
  1347. if($search_div === false){
  1348. throw new Exception("Failed to grep search div");
  1349. }
  1350. $this->fuckhtml->load($search_div);
  1351. $results =
  1352. $this->fuckhtml
  1353. ->getElementsByClassName(
  1354. $this->getstyle([
  1355. "margin" => "0px 0px 30px"
  1356. ]),
  1357. "div"
  1358. );
  1359. foreach($results as $result){
  1360. $this->fuckhtml->load($result);
  1361. $url =
  1362. $this->fuckhtml
  1363. ->getElementsByTagName(
  1364. "a"
  1365. );
  1366. if(count($url) === 0){
  1367. // no url, weird, continue
  1368. continue;
  1369. }
  1370. $title =
  1371. $this->fuckhtml
  1372. ->getElementsByTagName(
  1373. "h3"
  1374. );
  1375. if(count($title) === 0){
  1376. // no title, weird, continue
  1377. continue;
  1378. }
  1379. // get description
  1380. $description =
  1381. $this->fuckhtml
  1382. ->getElementsByClassName(
  1383. $this->getstyle([
  1384. "-webkit-box-orient" => "vertical",
  1385. "display" => "-webkit-box",
  1386. "-webkit-line-clamp" => "2",
  1387. "overflow" => "hidden",
  1388. "word-break" => "break-word"
  1389. ]),
  1390. "div"
  1391. );
  1392. if(count($description) === 0){
  1393. $description = null;
  1394. }else{
  1395. $description =
  1396. html_entity_decode(
  1397. $this->titledots(
  1398. $this->fuckhtml
  1399. ->getTextContent(
  1400. $description[0]
  1401. )
  1402. )
  1403. );
  1404. }
  1405. // get author + date posted
  1406. $metadiv =
  1407. $this->fuckhtml
  1408. ->getElementsByClassName(
  1409. $this->getstyle([
  1410. "margin-top" => "12px"
  1411. ]),
  1412. "div"
  1413. );
  1414. $author = null;
  1415. $date = null;
  1416. if(count($metadiv) !== 0){
  1417. $metadiv =
  1418. explode(
  1419. "·",
  1420. $this->fuckhtml
  1421. ->getTextContent(
  1422. $metadiv[0]
  1423. )
  1424. );
  1425. if(count($metadiv) === 3){
  1426. $author = trim($metadiv[1]);
  1427. $date = strtotime(trim($metadiv[2]));
  1428. }elseif(count($metadiv) === 2){
  1429. $author = trim($metadiv[0]);
  1430. $date = strtotime(trim($metadiv[1]));
  1431. }
  1432. }
  1433. $thumb = [
  1434. "url" => null,
  1435. "ratio" => null
  1436. ];
  1437. $image =
  1438. $this->fuckhtml
  1439. ->getElementsByTagName(
  1440. "img"
  1441. );
  1442. $duration = null;
  1443. if(
  1444. count($image) !== 0 &&
  1445. isset($image[0]["attributes"]["id"])
  1446. ){
  1447. $thumb = [
  1448. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1449. "ratio" => "16:9"
  1450. ];
  1451. // get duration
  1452. $duration =
  1453. $this->fuckhtml
  1454. ->getElementsByClassName(
  1455. $this->getstyle([
  1456. "background-color" => "rgba(0,0,0,0.6)",
  1457. "color" => "#fff",
  1458. "fill" => "#fff"
  1459. ])
  1460. );
  1461. if(count($duration) !== 0){
  1462. $duration =
  1463. $this->hms2int(
  1464. $this->fuckhtml
  1465. ->getTextContent(
  1466. $duration[0]
  1467. ));
  1468. }else{
  1469. $duration = null;
  1470. }
  1471. }
  1472. $out["video"][] = [
  1473. "title" =>
  1474. $this->titledots(
  1475. $this->fuckhtml
  1476. ->getTextContent(
  1477. $title[0]
  1478. )
  1479. ),
  1480. "description" => $description,
  1481. "author" => [
  1482. "name" => $author,
  1483. "url" => null,
  1484. "avatar" => null
  1485. ],
  1486. "date" => $date,
  1487. "duration" => $duration,
  1488. "views" => null,
  1489. "thumb" => $thumb,
  1490. "url" =>
  1491. $this->fuckhtml
  1492. ->getTextContent(
  1493. $url[0]["attributes"]["href"]
  1494. )
  1495. ];
  1496. }
  1497. return $out;
  1498. }
  1499. public function news($get){
  1500. if($get["npt"]){
  1501. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1502. $req = json_decode($req, true);
  1503. $container = $req["c"];
  1504. $req = $req["u"];
  1505. $data = $this->get(
  1506. $proxy,
  1507. "https://www.google.com" . $req,
  1508. [],
  1509. $container
  1510. );
  1511. $html = &$data["data"];
  1512. $container = &$data["container"];
  1513. }else{
  1514. $search = $get["s"];
  1515. $country = $get["country"];
  1516. $nsfw = $get["nsfw"];
  1517. $older = $get["older"];
  1518. $newer = $get["newer"];
  1519. $sort = $get["sort"];
  1520. $container = null;
  1521. $proxy = $this->backend->get_ip();
  1522. $params = [
  1523. "q" => $search,
  1524. "tbm" => "nws",
  1525. "hl" => "en"
  1526. ];
  1527. // country
  1528. if($country != "any"){
  1529. $params["gl"] = $country;
  1530. }
  1531. // nsfw
  1532. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1533. $tbs = [];
  1534. // get date
  1535. $older = $older === false ? null : date("m/d/Y", $older);
  1536. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1537. if(
  1538. $older !== null ||
  1539. $newer !== null
  1540. ){
  1541. $tbs["cdr"] = "1";
  1542. $tbs["cd_min"] = $newer;
  1543. $tbs["cd_max"] = $older;
  1544. }
  1545. // relevance
  1546. if($sort == "date"){
  1547. $tbs["sbd"] = "1";
  1548. }
  1549. // append tbs
  1550. if(count($tbs) !== 0){
  1551. $params["tbs"] = "";
  1552. foreach($tbs as $key => $value){
  1553. $params["tbs"] .= $key . ":" . $value . ",";
  1554. }
  1555. $params["tbs"] = rtrim($params["tbs"], ",");
  1556. }
  1557. //$html = file_get_contents("scraper/google.html");
  1558. $data = $this->get(
  1559. $proxy,
  1560. "https://www.google.com/search",
  1561. $params
  1562. );
  1563. $html = &$data["data"];
  1564. $container = &$data["container"];
  1565. }
  1566. $out = [
  1567. "status" => "ok",
  1568. "npt" => null,
  1569. "news" => []
  1570. ];
  1571. $this->fuckhtml->load($html);
  1572. // parse styles
  1573. $this->parsestyles();
  1574. $softcaptcha = $this->detect_sorry();
  1575. if($softcaptcha){
  1576. $params["sei"] = $softcaptcha["sei"];
  1577. $data = $this->get(
  1578. $proxy,
  1579. "https://www.google.com/search",
  1580. $params,
  1581. $container
  1582. );
  1583. }
  1584. // get images
  1585. $this->scrape_dimg($html);
  1586. $center_col =
  1587. $this->fuckhtml
  1588. ->getElementById(
  1589. "center_col",
  1590. "div"
  1591. );
  1592. if($center_col === null){
  1593. throw new Exception("Could not grep result div");
  1594. }
  1595. $this->fuckhtml->load($center_col);
  1596. // get next page
  1597. $npt =
  1598. $this->fuckhtml
  1599. ->getElementById(
  1600. "pnnext",
  1601. "a"
  1602. );
  1603. if($npt !== false){
  1604. $out["npt"] =
  1605. $this->backend->store(
  1606. json_encode([
  1607. "u" =>
  1608. $this->fuckhtml
  1609. ->getTextContent(
  1610. $npt["attributes"]
  1611. ["href"]
  1612. ),
  1613. "c" => $container
  1614. ]),
  1615. "news",
  1616. $proxy
  1617. );
  1618. }
  1619. $as =
  1620. $this->fuckhtml
  1621. ->getElementsByAttributeName(
  1622. "jsname",
  1623. "a"
  1624. );
  1625. foreach($as as $a){
  1626. $this->fuckhtml->load($a);
  1627. // get title
  1628. $title =
  1629. $this->fuckhtml
  1630. ->getElementsByAttributeValue(
  1631. "role",
  1632. "heading",
  1633. "div"
  1634. );
  1635. if(count($title) === 0){
  1636. continue;
  1637. }
  1638. $title =
  1639. $this->titledots(
  1640. $this->fuckhtml
  1641. ->getTextContent(
  1642. $title[0]
  1643. )
  1644. );
  1645. // get thumbnail
  1646. $image =
  1647. $this->fuckhtml
  1648. ->getElementsByAttributeName(
  1649. "id",
  1650. "img"
  1651. );
  1652. // check for padded title node, if found, we're inside a carousel
  1653. $probe =
  1654. $this->fuckhtml
  1655. ->getElementsByClassName(
  1656. $this->getstyle(
  1657. [
  1658. "padding" => "16px 16px 40px 16px"
  1659. ]
  1660. ),
  1661. "div"
  1662. );
  1663. if(count($probe) !== 0){
  1664. $probe = true;
  1665. }else{
  1666. $probe = false;
  1667. }
  1668. if(
  1669. count($image) !== 0 &&
  1670. !isset($image[0]["attributes"]["width"])
  1671. ){
  1672. $thumb = [
  1673. "url" =>
  1674. $this->getdimg(
  1675. $image[0]["attributes"]["id"]
  1676. ),
  1677. "ratio" => $probe === true ? "16:9" : "1:1"
  1678. ];
  1679. }else{
  1680. $thumb = [
  1681. "url" => null,
  1682. "ratio" => null
  1683. ];
  1684. }
  1685. $description = null;
  1686. if($probe === false){
  1687. $desc_divs =
  1688. $this->fuckhtml
  1689. ->getElementsByAttributeName(
  1690. "style",
  1691. "div"
  1692. );
  1693. foreach($desc_divs as $desc){
  1694. if(
  1695. strpos(
  1696. $desc["attributes"]["style"],
  1697. "margin-top:"
  1698. ) !== false
  1699. ){
  1700. $description =
  1701. $this->titledots(
  1702. $this->fuckhtml
  1703. ->getTextContent(
  1704. $desc
  1705. )
  1706. );
  1707. break;
  1708. }
  1709. }
  1710. }
  1711. // get author
  1712. $author =
  1713. $this->fuckhtml
  1714. ->getElementsByClassName(
  1715. $this->getstyle(
  1716. [
  1717. "overflow" => "hidden",
  1718. "text-align" => "left",
  1719. "text-overflow" => "ellipsis",
  1720. "white-space" => "nowrap",
  1721. "margin-bottom" => "8px"
  1722. ]
  1723. ),
  1724. "div"
  1725. );
  1726. if(count($author) !== 0){
  1727. $author =
  1728. $this->fuckhtml
  1729. ->getTextContent(
  1730. $author[0]
  1731. );
  1732. }else{
  1733. $author = null;
  1734. }
  1735. // get date
  1736. $date = null;
  1737. $date_div =
  1738. $this->fuckhtml
  1739. ->getElementsByAttributeName(
  1740. "style",
  1741. "div"
  1742. );
  1743. foreach($date_div as $d){
  1744. $this->fuckhtml->load($d);
  1745. $span =
  1746. $this->fuckhtml
  1747. ->getElementsByTagName(
  1748. "span"
  1749. );
  1750. if(
  1751. strpos(
  1752. $d["attributes"]["style"],
  1753. "bottom:"
  1754. ) !== false
  1755. ){
  1756. $date =
  1757. strtotime(
  1758. $this->fuckhtml
  1759. ->getTextContent(
  1760. $span[count($span) - 1]
  1761. )
  1762. );
  1763. break;
  1764. }
  1765. }
  1766. $out["news"][] = [
  1767. "title" => $title,
  1768. "author" => $author,
  1769. "description" => $description,
  1770. "date" => $date,
  1771. "thumb" => $thumb,
  1772. "url" =>
  1773. $this->unshiturl(
  1774. $a["attributes"]
  1775. ["href"]
  1776. )
  1777. ];
  1778. }
  1779. return $out;
  1780. }
  1781. private function scrape_dimg($html){
  1782. // get images loaded through javascript
  1783. $this->dimg = [];
  1784. preg_match_all(
  1785. '/function\(\){google\.ldi=({.*?});/',
  1786. $html,
  1787. $dimg
  1788. );
  1789. if(isset($dimg[1])){
  1790. foreach($dimg[1] as $i){
  1791. $tmp = json_decode($i, true);
  1792. foreach($tmp as $key => $value){
  1793. $this->dimg[$key] =
  1794. $this->unshit_thumb(
  1795. $value
  1796. );
  1797. }
  1798. }
  1799. }
  1800. // get additional javascript base64 images
  1801. preg_match_all(
  1802. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1803. $html,
  1804. $dimg
  1805. );
  1806. if(isset($dimg[1])){
  1807. for($i=0; $i<count($dimg[1]); $i++){
  1808. $delims = explode(",", $dimg[2][$i]);
  1809. $string =
  1810. $this->fuckhtml
  1811. ->parseJsString(
  1812. $dimg[1][$i]
  1813. );
  1814. foreach($delims as $delim){
  1815. $this->dimg[trim($delim, "'")] = $string;
  1816. }
  1817. }
  1818. }
  1819. }
  1820. private function scrape_imagearr($html){
  1821. // get image links arrays
  1822. preg_match_all(
  1823. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1824. $html,
  1825. $image_arr
  1826. );
  1827. $this->image_arr = [];
  1828. if(isset($image_arr[1])){
  1829. for($i=0; $i<count($image_arr[1]); $i++){
  1830. $original =
  1831. $this->fuckhtml
  1832. ->parseJsString(
  1833. $image_arr[5][$i]
  1834. );
  1835. if(
  1836. preg_match(
  1837. '/^x-raw-image/',
  1838. $original
  1839. )
  1840. ){
  1841. // only add thumbnail, google doesnt have OG resolution
  1842. $this->image_arr[$image_arr[1][$i]] = [
  1843. [
  1844. "url" =>
  1845. $this->unshit_thumb(
  1846. $this->fuckhtml
  1847. ->parseJsString(
  1848. $image_arr[2][$i]
  1849. )
  1850. ),
  1851. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1852. "height" => (int)$image_arr[6][$i]
  1853. ]
  1854. ];
  1855. continue;
  1856. }
  1857. $this->image_arr[$image_arr[1][$i]] =
  1858. [
  1859. [
  1860. "url" => $original,
  1861. "width" => (int)$image_arr[7][$i],
  1862. "height" => (int)$image_arr[6][$i]
  1863. ],
  1864. [
  1865. "url" =>
  1866. $this->unshit_thumb(
  1867. $this->fuckhtml
  1868. ->parseJsString(
  1869. $image_arr[2][$i]
  1870. )
  1871. ),
  1872. "width" => (int)$image_arr[4][$i],
  1873. "height" => (int)$image_arr[3][$i]
  1874. ]
  1875. ];
  1876. }
  1877. }
  1878. }
  1879. private function getdimg($dimg){
  1880. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1881. }
  1882. private function unshit_thumb($url, $get_bigger_res = false){
  1883. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1884. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1885. $parts = parse_url($url);
  1886. if(
  1887. isset($parts["host"]) &&
  1888. preg_match(
  1889. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1890. $parts["host"]
  1891. )
  1892. ){
  1893. parse_str($parts["query"], $params);
  1894. if(isset($params["q"])){
  1895. if($get_bigger_res){
  1896. // this method doesnt always work, but does work for wiki thumbnails
  1897. return
  1898. "https://" . $parts["host"] . "/images?q=tbn:" .
  1899. $this->base64url_encode(
  1900. substr(
  1901. $this->base64url_decode(
  1902. explode(
  1903. ":",
  1904. $params["q"])[1]
  1905. ),
  1906. 0,
  1907. 29
  1908. )
  1909. );
  1910. }else{
  1911. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1912. }
  1913. }
  1914. }
  1915. return $url;
  1916. }
  1917. private function parsestyles(){
  1918. $styles = [];
  1919. $style_div =
  1920. $this->fuckhtml
  1921. ->getElementsByTagName(
  1922. "style"
  1923. );
  1924. $raw_styles = "";
  1925. foreach($style_div as $style){
  1926. $raw_styles .= $style["innerHTML"];
  1927. }
  1928. // filter out media/keyframe queries
  1929. $raw_styles =
  1930. preg_replace(
  1931. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1932. "",
  1933. $raw_styles
  1934. );
  1935. // get styles
  1936. preg_match_all(
  1937. '/(.+?){([\S\s]*?)}/',
  1938. $raw_styles,
  1939. $matches
  1940. );
  1941. for($i=0; $i<count($matches[1]); $i++){
  1942. // get style values
  1943. preg_match_all(
  1944. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1945. $matches[2][$i],
  1946. $values_regex
  1947. );
  1948. $values = [];
  1949. for($k=0; $k<count($values_regex[1]); $k++){
  1950. $values[trim($values_regex[1][$k])] =
  1951. strtolower(trim($values_regex[2][$k]));
  1952. }
  1953. $names = explode(",", $matches[1][$i]);
  1954. // h1,h2,h3 will each get their own array index
  1955. foreach($names as $name){
  1956. $name = trim($name, "}\t\n\r\0\x0B");
  1957. foreach($values as $key => $value){
  1958. $styles[$name][$key] = $value;
  1959. }
  1960. }
  1961. }
  1962. foreach($styles as $key => $values){
  1963. $styles[$key]["_c"] = count($values);
  1964. }
  1965. $this->styles = $styles;
  1966. // get CSS colors
  1967. $this->css_colors = [];
  1968. if(isset($this->styles[":root"])){
  1969. foreach($this->styles[":root"] as $key => $value){
  1970. $this->css_colors[$value] = strtolower($key);
  1971. }
  1972. }
  1973. }
  1974. private function getstyle($styles){
  1975. $styles["_c"] = count($styles);
  1976. foreach($this->styles as $style_key => $style_values){
  1977. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1978. $style_key =
  1979. explode(" ", $style_key);
  1980. $style_key = $style_key[count($style_key) - 1];
  1981. return
  1982. ltrim(
  1983. str_replace(
  1984. [".", "#"],
  1985. " ",
  1986. $style_key
  1987. )
  1988. );
  1989. }
  1990. }
  1991. return false;
  1992. }
  1993. private function getcolorvar($color){
  1994. if(isset($this->css_colors[$color])){
  1995. return $this->css_colors[$color];
  1996. }
  1997. return null;
  1998. }
  1999. private function unshiturl($url, $return_size = false){
  2000. // decode
  2001. $url =
  2002. $this->fuckhtml
  2003. ->getTextContent(
  2004. $url
  2005. );
  2006. $url_parts = parse_url($url);
  2007. if(isset($url_parts["query"])){
  2008. parse_str($url_parts["query"], $query);
  2009. }else{
  2010. $query = [];
  2011. }
  2012. if(
  2013. !isset(
  2014. $url_parts["host"]
  2015. ) ||
  2016. stripos($url_parts["host"], "google.") !== false
  2017. ){
  2018. // no host, we have a tracking url
  2019. if(isset($query["imgurl"])){
  2020. $url = $query["imgurl"];
  2021. }
  2022. elseif(isset($query["q"])){
  2023. $url = $query["q"];
  2024. }
  2025. }
  2026. // rewrite URLs to remove extra tracking parameters
  2027. $domain = parse_url($url, PHP_URL_HOST);
  2028. if(
  2029. preg_match(
  2030. '/wikipedia.org$/',
  2031. $domain
  2032. )
  2033. ){
  2034. // rewrite wikipedia mobile URLs to desktop
  2035. $url =
  2036. $this->replacedomain(
  2037. $url,
  2038. preg_replace(
  2039. '/([a-z0-9]+)(\.m\.)/',
  2040. '$1.',
  2041. $domain
  2042. )
  2043. );
  2044. }
  2045. elseif(
  2046. preg_match(
  2047. '/imdb\.com$|youtube\.[^.]+$/',
  2048. $domain
  2049. )
  2050. ){
  2051. // rewrite imdb and youtube mobile URLs too
  2052. $url =
  2053. $this->replacedomain(
  2054. $url,
  2055. preg_replace(
  2056. '/^m\./',
  2057. "",
  2058. $domain
  2059. )
  2060. );
  2061. }
  2062. elseif(
  2063. preg_match(
  2064. '/play\.google\.[^.]+$/',
  2065. $domain
  2066. )
  2067. ){
  2068. // remove referrers from play.google.com
  2069. $u_query = parse_url($url, PHP_URL_QUERY);
  2070. if($u_query !== null){
  2071. parse_str($u_query, $u_query);
  2072. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2073. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2074. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2075. $query = http_build_query($query);
  2076. $url =
  2077. str_replace(
  2078. $u_query,
  2079. $u_query,
  2080. $url
  2081. );
  2082. }
  2083. }
  2084. elseif(
  2085. preg_match(
  2086. '/twitter\.com$/',
  2087. $domain
  2088. )
  2089. ){
  2090. // remove more referrers from twitter.com
  2091. $u_query = parse_url($url, PHP_URL_QUERY);
  2092. if($u_query !== null){
  2093. parse_str($u_query, $u_query);
  2094. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  2095. $u_query = http_build_query($u_query);
  2096. $url =
  2097. str_replace(
  2098. $oldquery,
  2099. $u_query,
  2100. $url
  2101. );
  2102. }
  2103. }
  2104. elseif(
  2105. preg_match(
  2106. '/maps\.google\.[^.]+/',
  2107. $domain
  2108. )
  2109. ){
  2110. if(stripos($url, "maps?") !== false){
  2111. $u_query = parse_url($url, PHP_URL_QUERY);
  2112. if($u_query !== null){
  2113. parse_str($u_query, $u_query);
  2114. if(isset($u_query["daddr"])){
  2115. $url =
  2116. "https://maps.google.com/maps?daddr=" .
  2117. urlencode($u_query["daddr"]);
  2118. }
  2119. }
  2120. }
  2121. }
  2122. if($return_size){
  2123. return [
  2124. "url" => $url,
  2125. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2126. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2127. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2128. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2129. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2130. ];
  2131. }
  2132. return $url;
  2133. }
  2134. private function replacedomain($url, $domain){
  2135. return
  2136. preg_replace(
  2137. '/(https?:\/\/)([^\/]+)/',
  2138. '$1' . $domain,
  2139. $url
  2140. );
  2141. }
  2142. private function titledots($title){
  2143. return trim($title, " .\t\n\r\0\x0B");
  2144. }
  2145. private function hms2int($time){
  2146. $parts = explode(":", $time, 3);
  2147. $time = 0;
  2148. if(count($parts) === 3){
  2149. // hours
  2150. $time = $time + ((int)$parts[0] * 3600);
  2151. array_shift($parts);
  2152. }
  2153. if(count($parts) === 2){
  2154. // minutes
  2155. $time = $time + ((int)$parts[0] * 60);
  2156. array_shift($parts);
  2157. }
  2158. // seconds
  2159. $time = $time + (int)$parts[0];
  2160. return $time;
  2161. }
  2162. function base64url_decode($data){
  2163. $b64 = strtr($data, "-_", "+/");
  2164. $pad = strlen($b64) % 4;
  2165. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  2166. return base64_decode($b64);
  2167. }
  2168. function base64url_encode($data){
  2169. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  2170. }
  2171. private function detect_sorry(){
  2172. // detect recaptcha
  2173. $captcha_form =
  2174. $this->fuckhtml
  2175. ->getElementById(
  2176. "captcha-form",
  2177. "form"
  2178. );
  2179. if($captcha_form !== false){
  2180. throw new Exception("Google returned a captcha");
  2181. }
  2182. // detect JS challenge
  2183. $title =
  2184. $this->fuckhtml
  2185. ->getElementsByTagName(
  2186. "title"
  2187. );
  2188. if(
  2189. count($title) !== 0 &&
  2190. $this->fuckhtml
  2191. ->getTextContent(
  2192. $title[0]
  2193. ) == "Google Search"
  2194. ){
  2195. throw new Exception("Google returned a JS challenge");
  2196. }
  2197. // bypass soft captcha
  2198. // "Verifying your request
  2199. // You'll be able to continue in a few seconds. Don't refresh this page."
  2200. $scripts =
  2201. $this->fuckhtml
  2202. ->getElementsByTagName(
  2203. "script"
  2204. );
  2205. foreach($scripts as $sc){
  2206. if(
  2207. preg_match(
  2208. '/var eid ?= ?\'(.*)\'[\S\s]*, ?([0-9]+)\);/U',
  2209. $sc["innerHTML"],
  2210. $matches
  2211. )
  2212. ){
  2213. sleep(10); // gotta sleep to not trigger captcha. blame google, not me
  2214. return [
  2215. "sei" => $matches[1],
  2216. "timeout" => (int)$matches[2] // set to 10000 usually
  2217. ];
  2218. }
  2219. }
  2220. return false; // no need to bypass
  2221. }
  2222. }