google.php 56 KB

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