google.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792
  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. // solve softcaptcha
  676. $softcaptcha = $this->detect_sorry();
  677. if($softcaptcha !== false){
  678. $params["sei"] = $softcaptcha["sei"];
  679. $data2 = $this->get(
  680. $proxy,
  681. "https://www.google.com/search",
  682. $params,
  683. $container
  684. );
  685. $html = &$data2["data"];
  686. $this->fuckhtml->load($html);
  687. $this->parsestyles();
  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. // solve softcaptcha
  1166. $softcaptcha = $this->detect_sorry();
  1167. if($softcaptcha !== false){
  1168. $params["sei"] = $softcaptcha["sei"];
  1169. $data2 = $this->get(
  1170. $proxy,
  1171. "https://www.google.com/search",
  1172. $params,
  1173. $container
  1174. );
  1175. $html = &$data2["data"];
  1176. $this->fuckhtml->load($html);
  1177. $this->parsestyles();
  1178. }
  1179. // get javascript images
  1180. $this->scrape_imagearr($html);
  1181. $out = [
  1182. "status" => "ok",
  1183. "npt" => null,
  1184. "image" => []
  1185. ];
  1186. $images =
  1187. $this->fuckhtml
  1188. ->getElementsByClassName(
  1189. "ivg-i",
  1190. "div"
  1191. );
  1192. foreach($images as $div){
  1193. $this->fuckhtml->load($div);
  1194. $image =
  1195. $this->fuckhtml
  1196. ->getElementsByTagName("img")[0];
  1197. // make sure we dont attempt to show an image we dont have data for
  1198. if(
  1199. isset($div["attributes"]["data-docid"]) &&
  1200. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1201. ){
  1202. $source =
  1203. $this->image_arr[
  1204. $div["attributes"]["data-docid"]
  1205. ];
  1206. }else{
  1207. continue;
  1208. }
  1209. $out["image"][] = [
  1210. "title" =>
  1211. $this->titledots(
  1212. $this->fuckhtml
  1213. ->getTextContent(
  1214. $image["attributes"]["alt"]
  1215. )
  1216. ),
  1217. "source" => $source,
  1218. "url" =>
  1219. $this->fuckhtml
  1220. ->getTextContent(
  1221. $div["attributes"]["data-lpage"]
  1222. )
  1223. ];
  1224. }
  1225. // as usual, no way to check if there is a next page reliably
  1226. if(count($out["image"]) > 50){
  1227. if(!isset($params["start"])){
  1228. $params["start"] = 10;
  1229. }else{
  1230. $params["start"] += 10;
  1231. }
  1232. $out["npt"] =
  1233. $this->backend
  1234. ->store(
  1235. json_encode($params),
  1236. "image",
  1237. $proxy
  1238. );
  1239. }
  1240. return $out;
  1241. }
  1242. public function video($get){
  1243. if($get["npt"]){
  1244. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1245. $params = json_decode($params, true);
  1246. $container = $params["c"];
  1247. unset($params["c"]);
  1248. }else{
  1249. $search = $get["s"];
  1250. $country = $get["country"];
  1251. $nsfw = $get["nsfw"];
  1252. $older = $get["older"];
  1253. $newer = $get["newer"];
  1254. $duration = $get["duration"];
  1255. $quality = $get["quality"];
  1256. $captions = $get["captions"];
  1257. $container = null;
  1258. $proxy = $this->backend->get_ip();
  1259. $params = [
  1260. "q" => $search,
  1261. "udm" => "7",
  1262. "hl" => "en"
  1263. ];
  1264. // country
  1265. if($country != "any"){
  1266. $params["gl"] = $country;
  1267. }
  1268. // nsfw
  1269. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1270. $tbs = [];
  1271. // get date
  1272. $older = $older === false ? null : date("m/d/Y", $older);
  1273. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1274. if(
  1275. $older !== null ||
  1276. $newer !== null
  1277. ){
  1278. $tbs["cdr"] = "1";
  1279. $tbs["cd_min"] = $newer;
  1280. $tbs["cd_max"] = $older;
  1281. }
  1282. // duration
  1283. if($duration != "any"){
  1284. $tbs[] = "dur:" . $duration;
  1285. }
  1286. // quality
  1287. if($quality != "any"){
  1288. $tbs[] = "hq:" . $quality;
  1289. }
  1290. // captions
  1291. if($captions != "any"){
  1292. $tbs[] = "cc:" . $captions;
  1293. }
  1294. // append tbs
  1295. if(count($tbs) !== 0){
  1296. $params["tbs"] =
  1297. implode(",", $tbs);
  1298. }
  1299. }
  1300. //$html = file_get_contents("scraper/google.html");
  1301. $data = $this->get(
  1302. $proxy,
  1303. "https://www.google.com/search",
  1304. $params,
  1305. $container
  1306. );
  1307. $html = &$data["data"];
  1308. $container = &$data["container"];
  1309. if(!isset($params["start"])){
  1310. $params["start"] = 0;
  1311. }
  1312. $params["start"] += 20;
  1313. $this->fuckhtml->load($html);
  1314. // parse all <style> tags
  1315. $this->parsestyles();
  1316. // solve softcaptcha
  1317. $softcaptcha = $this->detect_sorry();
  1318. if($softcaptcha !== false){
  1319. $params["sei"] = $softcaptcha["sei"];
  1320. $data2 = $this->get(
  1321. $proxy,
  1322. "https://www.google.com/search",
  1323. $params,
  1324. $container
  1325. );
  1326. $html = &$data2["data"];
  1327. $this->fuckhtml->load($html);
  1328. $this->parsestyles();
  1329. }
  1330. //
  1331. // Parse web video page
  1332. //
  1333. // get javascript images
  1334. $this->scrape_dimg($html);
  1335. $this->scrape_imagearr($html);
  1336. $params["c"] = $container;
  1337. $out = [
  1338. "status" => "ok",
  1339. "npt" =>
  1340. $this->backend->store(
  1341. json_encode($params),
  1342. "videos",
  1343. $proxy
  1344. ),
  1345. "video" => [],
  1346. "author" => [],
  1347. "livestream" => [],
  1348. "playlist" => [],
  1349. "reel" => []
  1350. ];
  1351. $search_div =
  1352. $this->fuckhtml
  1353. ->getElementById(
  1354. "center_col"
  1355. );
  1356. if($search_div === false){
  1357. throw new Exception("Failed to grep search div");
  1358. }
  1359. $this->fuckhtml->load($search_div);
  1360. $results =
  1361. $this->fuckhtml
  1362. ->getElementsByClassName(
  1363. $this->getstyle([
  1364. "margin" => "0px 0px 30px"
  1365. ]),
  1366. "div"
  1367. );
  1368. foreach($results as $result){
  1369. $this->fuckhtml->load($result);
  1370. $url =
  1371. $this->fuckhtml
  1372. ->getElementsByTagName(
  1373. "a"
  1374. );
  1375. if(count($url) === 0){
  1376. // no url, weird, continue
  1377. continue;
  1378. }
  1379. $title =
  1380. $this->fuckhtml
  1381. ->getElementsByTagName(
  1382. "h3"
  1383. );
  1384. if(count($title) === 0){
  1385. // no title, weird, continue
  1386. continue;
  1387. }
  1388. // get description
  1389. $description =
  1390. $this->fuckhtml
  1391. ->getElementsByClassName(
  1392. $this->getstyle([
  1393. "-webkit-box-orient" => "vertical",
  1394. "display" => "-webkit-box",
  1395. "-webkit-line-clamp" => "2",
  1396. "overflow" => "hidden",
  1397. "word-break" => "break-word"
  1398. ]),
  1399. "div"
  1400. );
  1401. if(count($description) === 0){
  1402. $description = null;
  1403. }else{
  1404. $description =
  1405. html_entity_decode(
  1406. $this->titledots(
  1407. $this->fuckhtml
  1408. ->getTextContent(
  1409. $description[0]
  1410. )
  1411. )
  1412. );
  1413. }
  1414. // get author + date posted
  1415. $metadiv =
  1416. $this->fuckhtml
  1417. ->getElementsByClassName(
  1418. $this->getstyle([
  1419. "margin-top" => "12px"
  1420. ]),
  1421. "div"
  1422. );
  1423. $author = null;
  1424. $date = null;
  1425. if(count($metadiv) !== 0){
  1426. $metadiv =
  1427. explode(
  1428. "·",
  1429. $this->fuckhtml
  1430. ->getTextContent(
  1431. $metadiv[0]
  1432. )
  1433. );
  1434. if(count($metadiv) === 3){
  1435. $author = trim($metadiv[1]);
  1436. $date = strtotime(trim($metadiv[2]));
  1437. }elseif(count($metadiv) === 2){
  1438. $author = trim($metadiv[0]);
  1439. $date = strtotime(trim($metadiv[1]));
  1440. }
  1441. }
  1442. $thumb = [
  1443. "url" => null,
  1444. "ratio" => null
  1445. ];
  1446. $image =
  1447. $this->fuckhtml
  1448. ->getElementsByTagName(
  1449. "img"
  1450. );
  1451. $duration = null;
  1452. if(
  1453. count($image) !== 0 &&
  1454. isset($image[0]["attributes"]["id"])
  1455. ){
  1456. $thumb = [
  1457. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1458. "ratio" => "16:9"
  1459. ];
  1460. // get duration
  1461. $duration =
  1462. $this->fuckhtml
  1463. ->getElementsByClassName(
  1464. $this->getstyle([
  1465. "background-color" => "rgba(0,0,0,0.6)",
  1466. "color" => "#fff",
  1467. "fill" => "#fff"
  1468. ])
  1469. );
  1470. if(count($duration) !== 0){
  1471. $duration =
  1472. $this->hms2int(
  1473. $this->fuckhtml
  1474. ->getTextContent(
  1475. $duration[0]
  1476. ));
  1477. }else{
  1478. $duration = null;
  1479. }
  1480. }
  1481. $out["video"][] = [
  1482. "title" =>
  1483. $this->titledots(
  1484. $this->fuckhtml
  1485. ->getTextContent(
  1486. $title[0]
  1487. )
  1488. ),
  1489. "description" => $description,
  1490. "author" => [
  1491. "name" => $author,
  1492. "url" => null,
  1493. "avatar" => null
  1494. ],
  1495. "date" => $date,
  1496. "duration" => $duration,
  1497. "views" => null,
  1498. "thumb" => $thumb,
  1499. "url" =>
  1500. $this->fuckhtml
  1501. ->getTextContent(
  1502. $url[0]["attributes"]["href"]
  1503. )
  1504. ];
  1505. }
  1506. return $out;
  1507. }
  1508. public function news($get){
  1509. if($get["npt"]){
  1510. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1511. $req = json_decode($req, true);
  1512. $container = $req["c"];
  1513. $req = $req["u"];
  1514. $data = $this->get(
  1515. $proxy,
  1516. "https://www.google.com" . $req,
  1517. [],
  1518. $container
  1519. );
  1520. $html = &$data["data"];
  1521. $container = &$data["container"];
  1522. }else{
  1523. $search = $get["s"];
  1524. $country = $get["country"];
  1525. $nsfw = $get["nsfw"];
  1526. $older = $get["older"];
  1527. $newer = $get["newer"];
  1528. $sort = $get["sort"];
  1529. $container = null;
  1530. $proxy = $this->backend->get_ip();
  1531. $params = [
  1532. "q" => $search,
  1533. "tbm" => "nws",
  1534. "hl" => "en"
  1535. ];
  1536. // country
  1537. if($country != "any"){
  1538. $params["gl"] = $country;
  1539. }
  1540. // nsfw
  1541. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1542. $tbs = [];
  1543. // get date
  1544. $older = $older === false ? null : date("m/d/Y", $older);
  1545. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1546. if(
  1547. $older !== null ||
  1548. $newer !== null
  1549. ){
  1550. $tbs["cdr"] = "1";
  1551. $tbs["cd_min"] = $newer;
  1552. $tbs["cd_max"] = $older;
  1553. }
  1554. // relevance
  1555. if($sort == "date"){
  1556. $tbs["sbd"] = "1";
  1557. }
  1558. // append tbs
  1559. if(count($tbs) !== 0){
  1560. $params["tbs"] = "";
  1561. foreach($tbs as $key => $value){
  1562. $params["tbs"] .= $key . ":" . $value . ",";
  1563. }
  1564. $params["tbs"] = rtrim($params["tbs"], ",");
  1565. }
  1566. //$html = file_get_contents("scraper/google.html");
  1567. $data = $this->get(
  1568. $proxy,
  1569. "https://www.google.com/search",
  1570. $params
  1571. );
  1572. $html = &$data["data"];
  1573. $container = &$data["container"];
  1574. }
  1575. $out = [
  1576. "status" => "ok",
  1577. "npt" => null,
  1578. "news" => []
  1579. ];
  1580. $this->fuckhtml->load($html);
  1581. // parse styles
  1582. $this->parsestyles();
  1583. // solve softcaptcha
  1584. $softcaptcha = $this->detect_sorry();
  1585. if($softcaptcha !== false){
  1586. $params["sei"] = $softcaptcha["sei"];
  1587. $data2 = $this->get(
  1588. $proxy,
  1589. "https://www.google.com/search",
  1590. $params,
  1591. $container
  1592. );
  1593. $html = &$data2["data"];
  1594. $this->fuckhtml->load($html);
  1595. $this->parsestyles();
  1596. }
  1597. // get images
  1598. $this->scrape_dimg($html);
  1599. $center_col =
  1600. $this->fuckhtml
  1601. ->getElementById(
  1602. "center_col",
  1603. "div"
  1604. );
  1605. if($center_col === null){
  1606. throw new Exception("Could not grep result div");
  1607. }
  1608. $this->fuckhtml->load($center_col);
  1609. // get next page
  1610. $npt =
  1611. $this->fuckhtml
  1612. ->getElementById(
  1613. "pnnext",
  1614. "a"
  1615. );
  1616. if($npt !== false){
  1617. $out["npt"] =
  1618. $this->backend->store(
  1619. json_encode([
  1620. "u" =>
  1621. $this->fuckhtml
  1622. ->getTextContent(
  1623. $npt["attributes"]
  1624. ["href"]
  1625. ),
  1626. "c" => $container
  1627. ]),
  1628. "news",
  1629. $proxy
  1630. );
  1631. }
  1632. $as =
  1633. $this->fuckhtml
  1634. ->getElementsByAttributeName(
  1635. "jsname",
  1636. "a"
  1637. );
  1638. foreach($as as $a){
  1639. $this->fuckhtml->load($a);
  1640. // get title
  1641. $title =
  1642. $this->fuckhtml
  1643. ->getElementsByAttributeValue(
  1644. "role",
  1645. "heading",
  1646. "div"
  1647. );
  1648. if(count($title) === 0){
  1649. continue;
  1650. }
  1651. $title =
  1652. $this->titledots(
  1653. $this->fuckhtml
  1654. ->getTextContent(
  1655. $title[0]
  1656. )
  1657. );
  1658. // get thumbnail
  1659. $image =
  1660. $this->fuckhtml
  1661. ->getElementsByAttributeName(
  1662. "id",
  1663. "img"
  1664. );
  1665. // check for padded title node, if found, we're inside a carousel
  1666. $probe =
  1667. $this->fuckhtml
  1668. ->getElementsByClassName(
  1669. $this->getstyle(
  1670. [
  1671. "padding" => "16px 16px 40px 16px"
  1672. ]
  1673. ),
  1674. "div"
  1675. );
  1676. if(count($probe) !== 0){
  1677. $probe = true;
  1678. }else{
  1679. $probe = false;
  1680. }
  1681. if(
  1682. count($image) !== 0 &&
  1683. !isset($image[0]["attributes"]["width"])
  1684. ){
  1685. $thumb = [
  1686. "url" =>
  1687. $this->getdimg(
  1688. $image[0]["attributes"]["id"]
  1689. ),
  1690. "ratio" => $probe === true ? "16:9" : "1:1"
  1691. ];
  1692. }else{
  1693. $thumb = [
  1694. "url" => null,
  1695. "ratio" => null
  1696. ];
  1697. }
  1698. $description = null;
  1699. if($probe === false){
  1700. $desc_divs =
  1701. $this->fuckhtml
  1702. ->getElementsByAttributeName(
  1703. "style",
  1704. "div"
  1705. );
  1706. foreach($desc_divs as $desc){
  1707. if(
  1708. strpos(
  1709. $desc["attributes"]["style"],
  1710. "margin-top:"
  1711. ) !== false
  1712. ){
  1713. $description =
  1714. $this->titledots(
  1715. $this->fuckhtml
  1716. ->getTextContent(
  1717. $desc
  1718. )
  1719. );
  1720. break;
  1721. }
  1722. }
  1723. }
  1724. // get author
  1725. $author =
  1726. $this->fuckhtml
  1727. ->getElementsByClassName(
  1728. $this->getstyle(
  1729. [
  1730. "overflow" => "hidden",
  1731. "text-align" => "left",
  1732. "text-overflow" => "ellipsis",
  1733. "white-space" => "nowrap",
  1734. "margin-bottom" => "8px"
  1735. ]
  1736. ),
  1737. "div"
  1738. );
  1739. if(count($author) !== 0){
  1740. $author =
  1741. $this->fuckhtml
  1742. ->getTextContent(
  1743. $author[0]
  1744. );
  1745. }else{
  1746. $author = null;
  1747. }
  1748. // get date
  1749. $date = null;
  1750. $date_div =
  1751. $this->fuckhtml
  1752. ->getElementsByAttributeName(
  1753. "style",
  1754. "div"
  1755. );
  1756. foreach($date_div as $d){
  1757. $this->fuckhtml->load($d);
  1758. $span =
  1759. $this->fuckhtml
  1760. ->getElementsByTagName(
  1761. "span"
  1762. );
  1763. if(
  1764. strpos(
  1765. $d["attributes"]["style"],
  1766. "bottom:"
  1767. ) !== false
  1768. ){
  1769. $date =
  1770. strtotime(
  1771. $this->fuckhtml
  1772. ->getTextContent(
  1773. $span[count($span) - 1]
  1774. )
  1775. );
  1776. break;
  1777. }
  1778. }
  1779. $out["news"][] = [
  1780. "title" => $title,
  1781. "author" => $author,
  1782. "description" => $description,
  1783. "date" => $date,
  1784. "thumb" => $thumb,
  1785. "url" =>
  1786. $this->unshiturl(
  1787. $a["attributes"]
  1788. ["href"]
  1789. )
  1790. ];
  1791. }
  1792. return $out;
  1793. }
  1794. private function scrape_dimg($html){
  1795. // get images loaded through javascript
  1796. $this->dimg = [];
  1797. preg_match_all(
  1798. '/function\(\){google\.ldi=({.*?});/',
  1799. $html,
  1800. $dimg
  1801. );
  1802. if(isset($dimg[1])){
  1803. foreach($dimg[1] as $i){
  1804. $tmp = json_decode($i, true);
  1805. foreach($tmp as $key => $value){
  1806. $this->dimg[$key] =
  1807. $this->unshit_thumb(
  1808. $value
  1809. );
  1810. }
  1811. }
  1812. }
  1813. // get additional javascript base64 images
  1814. preg_match_all(
  1815. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1816. $html,
  1817. $dimg
  1818. );
  1819. if(isset($dimg[1])){
  1820. for($i=0; $i<count($dimg[1]); $i++){
  1821. $delims = explode(",", $dimg[2][$i]);
  1822. $string =
  1823. $this->fuckhtml
  1824. ->parseJsString(
  1825. $dimg[1][$i]
  1826. );
  1827. foreach($delims as $delim){
  1828. $this->dimg[trim($delim, "'")] = $string;
  1829. }
  1830. }
  1831. }
  1832. }
  1833. private function scrape_imagearr($html){
  1834. // get image links arrays
  1835. preg_match_all(
  1836. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1837. $html,
  1838. $image_arr
  1839. );
  1840. $this->image_arr = [];
  1841. if(isset($image_arr[1])){
  1842. for($i=0; $i<count($image_arr[1]); $i++){
  1843. $original =
  1844. $this->fuckhtml
  1845. ->parseJsString(
  1846. $image_arr[5][$i]
  1847. );
  1848. if(
  1849. preg_match(
  1850. '/^x-raw-image/',
  1851. $original
  1852. )
  1853. ){
  1854. // only add thumbnail, google doesnt have OG resolution
  1855. $this->image_arr[$image_arr[1][$i]] = [
  1856. [
  1857. "url" =>
  1858. $this->unshit_thumb(
  1859. $this->fuckhtml
  1860. ->parseJsString(
  1861. $image_arr[2][$i]
  1862. )
  1863. ),
  1864. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1865. "height" => (int)$image_arr[6][$i]
  1866. ]
  1867. ];
  1868. continue;
  1869. }
  1870. $this->image_arr[$image_arr[1][$i]] =
  1871. [
  1872. [
  1873. "url" => $original,
  1874. "width" => (int)$image_arr[7][$i],
  1875. "height" => (int)$image_arr[6][$i]
  1876. ],
  1877. [
  1878. "url" =>
  1879. $this->unshit_thumb(
  1880. $this->fuckhtml
  1881. ->parseJsString(
  1882. $image_arr[2][$i]
  1883. )
  1884. ),
  1885. "width" => (int)$image_arr[4][$i],
  1886. "height" => (int)$image_arr[3][$i]
  1887. ]
  1888. ];
  1889. }
  1890. }
  1891. }
  1892. private function getdimg($dimg){
  1893. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1894. }
  1895. private function unshit_thumb($url, $get_bigger_res = false){
  1896. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1897. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1898. $parts = parse_url($url);
  1899. if(
  1900. isset($parts["host"]) &&
  1901. preg_match(
  1902. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1903. $parts["host"]
  1904. )
  1905. ){
  1906. parse_str($parts["query"], $params);
  1907. if(isset($params["q"])){
  1908. if($get_bigger_res){
  1909. // this method doesnt always work, but does work for wiki thumbnails
  1910. return
  1911. "https://" . $parts["host"] . "/images?q=tbn:" .
  1912. $this->base64url_encode(
  1913. substr(
  1914. $this->base64url_decode(
  1915. explode(
  1916. ":",
  1917. $params["q"])[1]
  1918. ),
  1919. 0,
  1920. 29
  1921. )
  1922. );
  1923. }else{
  1924. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1925. }
  1926. }
  1927. }
  1928. return $url;
  1929. }
  1930. private function parsestyles(){
  1931. $styles = [];
  1932. $style_div =
  1933. $this->fuckhtml
  1934. ->getElementsByTagName(
  1935. "style"
  1936. );
  1937. $raw_styles = "";
  1938. foreach($style_div as $style){
  1939. $raw_styles .= $style["innerHTML"];
  1940. }
  1941. // filter out media/keyframe queries
  1942. $raw_styles =
  1943. preg_replace(
  1944. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1945. "",
  1946. $raw_styles
  1947. );
  1948. // get styles
  1949. preg_match_all(
  1950. '/(.+?){([\S\s]*?)}/',
  1951. $raw_styles,
  1952. $matches
  1953. );
  1954. for($i=0; $i<count($matches[1]); $i++){
  1955. // get style values
  1956. preg_match_all(
  1957. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1958. $matches[2][$i],
  1959. $values_regex
  1960. );
  1961. $values = [];
  1962. for($k=0; $k<count($values_regex[1]); $k++){
  1963. $values[trim($values_regex[1][$k])] =
  1964. strtolower(trim($values_regex[2][$k]));
  1965. }
  1966. $names = explode(",", $matches[1][$i]);
  1967. // h1,h2,h3 will each get their own array index
  1968. foreach($names as $name){
  1969. $name = trim($name, "}\t\n\r\0\x0B");
  1970. foreach($values as $key => $value){
  1971. $styles[$name][$key] = $value;
  1972. }
  1973. }
  1974. }
  1975. foreach($styles as $key => $values){
  1976. $styles[$key]["_c"] = count($values);
  1977. }
  1978. $this->styles = $styles;
  1979. // get CSS colors
  1980. $this->css_colors = [];
  1981. if(isset($this->styles[":root"])){
  1982. foreach($this->styles[":root"] as $key => $value){
  1983. $this->css_colors[$value] = strtolower($key);
  1984. }
  1985. }
  1986. }
  1987. private function getstyle($styles){
  1988. $styles["_c"] = count($styles);
  1989. foreach($this->styles as $style_key => $style_values){
  1990. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1991. $style_key =
  1992. explode(" ", $style_key);
  1993. $style_key = $style_key[count($style_key) - 1];
  1994. return
  1995. ltrim(
  1996. str_replace(
  1997. [".", "#"],
  1998. " ",
  1999. $style_key
  2000. )
  2001. );
  2002. }
  2003. }
  2004. return false;
  2005. }
  2006. private function getcolorvar($color){
  2007. if(isset($this->css_colors[$color])){
  2008. return $this->css_colors[$color];
  2009. }
  2010. return null;
  2011. }
  2012. private function unshiturl($url, $return_size = false){
  2013. // decode
  2014. $url =
  2015. $this->fuckhtml
  2016. ->getTextContent(
  2017. $url
  2018. );
  2019. $url_parts = parse_url($url);
  2020. if(isset($url_parts["query"])){
  2021. parse_str($url_parts["query"], $query);
  2022. }else{
  2023. $query = [];
  2024. }
  2025. if(
  2026. !isset(
  2027. $url_parts["host"]
  2028. ) ||
  2029. stripos($url_parts["host"], "google.") !== false
  2030. ){
  2031. // no host, we have a tracking url
  2032. if(isset($query["imgurl"])){
  2033. $url = $query["imgurl"];
  2034. }
  2035. elseif(isset($query["q"])){
  2036. $url = $query["q"];
  2037. }
  2038. }
  2039. // rewrite URLs to remove extra tracking parameters
  2040. $domain = parse_url($url, PHP_URL_HOST);
  2041. if(
  2042. preg_match(
  2043. '/wikipedia.org$/',
  2044. $domain
  2045. )
  2046. ){
  2047. // rewrite wikipedia mobile URLs to desktop
  2048. $url =
  2049. $this->replacedomain(
  2050. $url,
  2051. preg_replace(
  2052. '/([a-z0-9]+)(\.m\.)/',
  2053. '$1.',
  2054. $domain
  2055. )
  2056. );
  2057. }
  2058. elseif(
  2059. preg_match(
  2060. '/imdb\.com$|youtube\.[^.]+$/',
  2061. $domain
  2062. )
  2063. ){
  2064. // rewrite imdb and youtube mobile URLs too
  2065. $url =
  2066. $this->replacedomain(
  2067. $url,
  2068. preg_replace(
  2069. '/^m\./',
  2070. "",
  2071. $domain
  2072. )
  2073. );
  2074. }
  2075. elseif(
  2076. preg_match(
  2077. '/play\.google\.[^.]+$/',
  2078. $domain
  2079. )
  2080. ){
  2081. // remove referrers from play.google.com
  2082. $u_query = parse_url($url, PHP_URL_QUERY);
  2083. if($u_query !== null){
  2084. parse_str($u_query, $u_query);
  2085. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2086. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2087. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2088. $query = http_build_query($query);
  2089. $url =
  2090. str_replace(
  2091. $u_query,
  2092. $u_query,
  2093. $url
  2094. );
  2095. }
  2096. }
  2097. elseif(
  2098. preg_match(
  2099. '/twitter\.com$/',
  2100. $domain
  2101. )
  2102. ){
  2103. // remove more referrers from twitter.com
  2104. $u_query = parse_url($url, PHP_URL_QUERY);
  2105. if($u_query !== null){
  2106. parse_str($u_query, $u_query);
  2107. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  2108. $u_query = http_build_query($u_query);
  2109. $url =
  2110. str_replace(
  2111. $oldquery,
  2112. $u_query,
  2113. $url
  2114. );
  2115. }
  2116. }
  2117. elseif(
  2118. preg_match(
  2119. '/maps\.google\.[^.]+/',
  2120. $domain
  2121. )
  2122. ){
  2123. if(stripos($url, "maps?") !== false){
  2124. $u_query = parse_url($url, PHP_URL_QUERY);
  2125. if($u_query !== null){
  2126. parse_str($u_query, $u_query);
  2127. if(isset($u_query["daddr"])){
  2128. $url =
  2129. "https://maps.google.com/maps?daddr=" .
  2130. urlencode($u_query["daddr"]);
  2131. }
  2132. }
  2133. }
  2134. }
  2135. if($return_size){
  2136. return [
  2137. "url" => $url,
  2138. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2139. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2140. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2141. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2142. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2143. ];
  2144. }
  2145. return $url;
  2146. }
  2147. private function replacedomain($url, $domain){
  2148. return
  2149. preg_replace(
  2150. '/(https?:\/\/)([^\/]+)/',
  2151. '$1' . $domain,
  2152. $url
  2153. );
  2154. }
  2155. private function titledots($title){
  2156. return trim($title, " .\t\n\r\0\x0B");
  2157. }
  2158. private function hms2int($time){
  2159. $parts = explode(":", $time, 3);
  2160. $time = 0;
  2161. if(count($parts) === 3){
  2162. // hours
  2163. $time = $time + ((int)$parts[0] * 3600);
  2164. array_shift($parts);
  2165. }
  2166. if(count($parts) === 2){
  2167. // minutes
  2168. $time = $time + ((int)$parts[0] * 60);
  2169. array_shift($parts);
  2170. }
  2171. // seconds
  2172. $time = $time + (int)$parts[0];
  2173. return $time;
  2174. }
  2175. function base64url_decode($data){
  2176. $b64 = strtr($data, "-_", "+/");
  2177. $pad = strlen($b64) % 4;
  2178. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  2179. return base64_decode($b64);
  2180. }
  2181. function base64url_encode($data){
  2182. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  2183. }
  2184. private function detect_sorry(){
  2185. // detect recaptcha
  2186. $captcha_form =
  2187. $this->fuckhtml
  2188. ->getElementById(
  2189. "captcha-form",
  2190. "form"
  2191. );
  2192. if($captcha_form !== false){
  2193. throw new Exception("Google returned a captcha");
  2194. }
  2195. // detect JS challenge
  2196. $title =
  2197. $this->fuckhtml
  2198. ->getElementsByTagName(
  2199. "title"
  2200. );
  2201. if(
  2202. count($title) !== 0 &&
  2203. $this->fuckhtml
  2204. ->getTextContent(
  2205. $title[0]
  2206. ) == "Google Search"
  2207. ){
  2208. throw new Exception("Google returned a JS challenge");
  2209. }
  2210. // bypass soft captcha
  2211. // "Verifying your request
  2212. // You'll be able to continue in a few seconds. Don't refresh this page."
  2213. $scripts =
  2214. $this->fuckhtml
  2215. ->getElementsByTagName(
  2216. "script"
  2217. );
  2218. foreach($scripts as $sc){
  2219. if(
  2220. preg_match(
  2221. '/var eid ?= ?\'(.*)\'[\S\s]*, ?([0-9]+)\);/U',
  2222. $sc["innerHTML"],
  2223. $matches
  2224. )
  2225. ){
  2226. sleep(10); // gotta sleep to not trigger captcha. blame google, not me
  2227. return [
  2228. "sei" => $matches[1],
  2229. "timeout" => (int)$matches[2] // set to 10000 usually
  2230. ];
  2231. }
  2232. }
  2233. return false; // no need to bypass
  2234. }
  2235. }