1
0

google.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029
  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, $get_bigger_res = false){
  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. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  657. $parts["host"]
  658. )
  659. ){
  660. parse_str($parts["query"], $params);
  661. if(isset($params["q"])){
  662. if($get_bigger_res){
  663. // this method doesnt always work, but does work for wiki thumbnails
  664. return
  665. "https://" . $parts["host"] . "/images?q=tbn:" .
  666. $this->base64url_encode(
  667. substr(
  668. $this->base64url_decode(
  669. explode(
  670. ":",
  671. $params["q"])[1]
  672. ),
  673. 0,
  674. 29
  675. )
  676. );
  677. }else{
  678. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  679. }
  680. }
  681. }
  682. return $url;
  683. }
  684. private function parsestyles(){
  685. $styles = [];
  686. $style_div =
  687. $this->fuckhtml
  688. ->getElementsByTagName(
  689. "style"
  690. );
  691. $raw_styles = "";
  692. foreach($style_div as $style){
  693. $raw_styles .= $style["innerHTML"];
  694. }
  695. // filter out media/keyframe queries
  696. $raw_styles =
  697. preg_replace(
  698. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  699. "",
  700. $raw_styles
  701. );
  702. // get styles
  703. preg_match_all(
  704. '/(.+?){([\S\s]*?)}/',
  705. $raw_styles,
  706. $matches
  707. );
  708. for($i=0; $i<count($matches[1]); $i++){
  709. // get style values
  710. preg_match_all(
  711. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  712. $matches[2][$i],
  713. $values_regex
  714. );
  715. $values = [];
  716. for($k=0; $k<count($values_regex[1]); $k++){
  717. $values[trim($values_regex[1][$k])] =
  718. strtolower(trim($values_regex[2][$k]));
  719. }
  720. $names = explode(",", $matches[1][$i]);
  721. // h1,h2,h3 will each get their own array index
  722. foreach($names as $name){
  723. $name = trim($name, "}\t\n\r\0\x0B");
  724. foreach($values as $key => $value){
  725. $styles[$name][$key] = $value;
  726. }
  727. }
  728. }
  729. foreach($styles as $key => $values){
  730. $styles[$key]["_c"] = count($values);
  731. }
  732. $this->styles = $styles;
  733. // get CSS colors
  734. $this->css_colors = [];
  735. if(isset($this->styles[":root"])){
  736. foreach($this->styles[":root"] as $key => $value){
  737. $this->css_colors[$value] = strtolower($key);
  738. }
  739. }
  740. }
  741. private function getstyle($styles){
  742. $styles["_c"] = count($styles);
  743. foreach($this->styles as $style_key => $style_values){
  744. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  745. $style_key =
  746. explode(" ", $style_key);
  747. $style_key = $style_key[count($style_key) - 1];
  748. return
  749. ltrim(
  750. str_replace(
  751. [".", "#"],
  752. " ",
  753. $style_key
  754. )
  755. );
  756. }
  757. }
  758. return false;
  759. }
  760. private function getcolorvar($color){
  761. if(isset($this->css_colors[$color])){
  762. return $this->css_colors[$color];
  763. }
  764. return null;
  765. }
  766. public function web($get){
  767. if($get["npt"]){
  768. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  769. try{
  770. $html =
  771. $this->get(
  772. $proxy,
  773. "https://www.google.com" . $get,
  774. [],
  775. true
  776. );
  777. }catch(Exception $error){
  778. throw new Exception("Failed to get HTML");
  779. }
  780. }else{
  781. $search = $get["s"];
  782. $country = $get["country"];
  783. $nsfw = $get["nsfw"];
  784. $lang = $get["lang"];
  785. $older = $get["older"];
  786. $newer = $get["newer"];
  787. $spellcheck = $get["spellcheck"];
  788. $proxy = $this->backend->get_ip();
  789. $offset = 0;
  790. $params = [
  791. "q" => $search,
  792. "hl" => "en",
  793. "num" => 20
  794. ];
  795. // country
  796. if($country != "any"){
  797. $params["gl"] = $country;
  798. }
  799. // nsfw
  800. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  801. // language
  802. if($lang != "any"){
  803. $params["lr"] = "lang_" . $lang;
  804. }
  805. // generate tbs
  806. $tbs = [];
  807. // get date
  808. $older = $older === false ? null : date("m/d/Y", $older);
  809. $newer = $newer === false ? null : date("m/d/Y", $newer);
  810. if(
  811. $older !== null ||
  812. $newer !== null
  813. ){
  814. $tbs["cdr"] = "1";
  815. $tbs["cd_min"] = $newer;
  816. $tbs["cd_max"] = $older;
  817. }
  818. // spellcheck filter
  819. if($spellcheck == "no"){
  820. $params["nfpr"] = "1";
  821. }
  822. if(count($tbs) !== 0){
  823. $params["tbs"] = "";
  824. foreach($tbs as $key => $value){
  825. $params["tbs"] .= $key . ":" . $value . ",";
  826. }
  827. $params["tbs"] = rtrim($params["tbs"], ",");
  828. }
  829. try{
  830. $html =
  831. $this->get(
  832. $proxy,
  833. "https://www.google.com/search",
  834. $params,
  835. true
  836. );
  837. }catch(Exception $error){
  838. throw new Exception("Failed to get HTML");
  839. }
  840. //$html = file_get_contents("scraper/google.html");
  841. }
  842. $out = [
  843. "status" => "ok",
  844. "spelling" => [
  845. "type" => "no_correction",
  846. "using" => null,
  847. "correction" => null
  848. ],
  849. "npt" => null,
  850. "answer" => [],
  851. "web" => [],
  852. "image" => [],
  853. "video" => [],
  854. "news" => [],
  855. "related" => []
  856. ];
  857. $this->fuckhtml->load($html);
  858. $this->detect_sorry();
  859. $this->parsestyles();
  860. $boxes =
  861. $this->fuckhtml
  862. ->getElementsByClassName(
  863. $this->getstyle([
  864. "border" => "thin solid #dadce0",
  865. "padding" => "12px 16px 12px 16px",
  866. "margin-bottom" => "10px",
  867. "font-family" => "sans-serif"
  868. ]),
  869. "div"
  870. );
  871. $skip_next = false;
  872. // get next page token
  873. $npt =
  874. $this->fuckhtml
  875. ->getElementsByClassName(
  876. $this->getstyle([
  877. "border" => "thin solid #dadce0",
  878. "color" => "#70757a",
  879. "font-size" => "14px",
  880. "text-align" => "center",
  881. "table-layout" => "fixed",
  882. "width" => "100%"
  883. ]),
  884. "table"
  885. );
  886. if(count($npt) !== 0){
  887. $this->fuckhtml->load($npt[0]);
  888. $as =
  889. $this->fuckhtml
  890. ->getElementsByTagName(
  891. "a"
  892. );
  893. foreach($as as $a){
  894. $text =
  895. $this->fuckhtml
  896. ->getTextContent(
  897. $a
  898. );
  899. if(
  900. $text == "Next&nbsp;>" ||
  901. $text == ">"
  902. ){
  903. $out["npt"] =
  904. $this->backend->store(
  905. $this->fuckhtml
  906. ->getTextContent(
  907. $a["attributes"]["href"]
  908. ),
  909. "web",
  910. $proxy
  911. );
  912. }
  913. }
  914. $this->fuckhtml->load($html);
  915. }
  916. $first_box = true;
  917. foreach($boxes as $box){
  918. $this->fuckhtml->load($box);
  919. if($first_box){
  920. //
  921. // Probe for word correction
  922. //
  923. $first_box = false;
  924. $txt =
  925. $this->fuckhtml
  926. ->getTextContent($box);
  927. if(
  928. preg_match(
  929. '/^Showing results for /',
  930. $txt
  931. )
  932. ){
  933. $as =
  934. $this->fuckhtml
  935. ->getElementsByTagName(
  936. "a"
  937. );
  938. if(count($as) === 2){
  939. $out["spelling"] = [
  940. "type" => "including",
  941. "using" =>
  942. $this->fuckhtml
  943. ->getTextContent(
  944. $as[0]
  945. ),
  946. "correction" =>
  947. $this->fuckhtml
  948. ->getTextContent(
  949. $as[1]
  950. )
  951. ];
  952. }
  953. continue;
  954. }
  955. }
  956. // probe for custom container
  957. $container_title =
  958. $this->fuckhtml
  959. ->getElementsByClassName(
  960. $this->getstyle([
  961. "font-weight" => "bold"
  962. ])
  963. );
  964. if(count($container_title) !== 0){
  965. $container_title =
  966. strtolower(
  967. $this->fuckhtml
  968. ->getTextContent(
  969. $container_title[0]
  970. )
  971. );
  972. if($container_title == "images"){
  973. //
  974. // Parse image carousel
  975. //
  976. $images =
  977. $this->fuckhtml
  978. ->getElementsByClassName(
  979. $this->getstyle([
  980. "display" => "inline-block",
  981. "padding" => "2px",
  982. "padding-bottom" => "4px"
  983. ]),
  984. "a"
  985. );
  986. foreach($images as $image){
  987. $this->fuckhtml->load($image);
  988. $image_data =
  989. $this->unshiturl(
  990. $image["attributes"]["href"],
  991. true
  992. );
  993. $img =
  994. $this->fuckhtml
  995. ->getElementsByTagName(
  996. "img"
  997. )[0];
  998. $out["image"][] = [
  999. "title" =>
  1000. $this->titledots(
  1001. $this->fuckhtml
  1002. ->getTextContent(
  1003. $img["attributes"]["alt"]
  1004. )
  1005. ),
  1006. "source" => [
  1007. [
  1008. "url" => $image_data["url"],
  1009. "width" => $image_data["image_width"],
  1010. "height" => $image_data["image_height"]
  1011. ],
  1012. [
  1013. "url" =>
  1014. $this->fuckhtml
  1015. ->getTextContent(
  1016. $img["attributes"]["src"]
  1017. ),
  1018. "width" => $image_data["thumb_width"],
  1019. "height" => $image_data["thumb_height"]
  1020. ]
  1021. ],
  1022. "url" => $image_data["ref"]
  1023. ];
  1024. }
  1025. continue;
  1026. }
  1027. if(
  1028. $container_title == "related searches" ||
  1029. $container_title == "people also search for"
  1030. ){
  1031. $as =
  1032. $this->fuckhtml
  1033. ->getElementsByClassName(
  1034. $this->getstyle([
  1035. "color" => "#202124",
  1036. "font-size" => "13px",
  1037. "line-height" => "20px"
  1038. ]),
  1039. "span"
  1040. );
  1041. foreach($as as $a){
  1042. $out["related"][] =
  1043. $this->fuckhtml
  1044. ->getTextContent(
  1045. $a
  1046. );
  1047. }
  1048. continue;
  1049. }
  1050. }
  1051. // probe for website link
  1052. $link =
  1053. $this->fuckhtml
  1054. ->getElementsByClassName(
  1055. $this->getstyle([
  1056. "color" => "#1967d2",
  1057. "font-size" => "18px",
  1058. "line-height" => "24px"
  1059. ]),
  1060. "a"
  1061. );
  1062. if(count($link) !== 0){
  1063. //
  1064. // Parse search result
  1065. //
  1066. $this->fuckhtml->load($link[0]);
  1067. $title =
  1068. $this->fuckhtml
  1069. ->getElementsByClassName(
  1070. $this->getstyle([
  1071. "color" => "#1967d2",
  1072. "font-size" => "18px",
  1073. "line-height" => "24px"
  1074. ]),
  1075. "span"
  1076. );
  1077. if(count($title) === 0){
  1078. continue;
  1079. }
  1080. $this->fuckhtml->load($box);
  1081. $sublinks = [];
  1082. $table = [];
  1083. $categories =
  1084. $this->fuckhtml
  1085. ->getElementsByClassName(
  1086. $this->getstyle([
  1087. "color" => "#202124",
  1088. "font-size" => "13px",
  1089. "line-height" => "20px"
  1090. ]),
  1091. "span"
  1092. );
  1093. $i = 0;
  1094. foreach($categories as $category){
  1095. $this->fuckhtml->load($category);
  1096. // probe for sublinks
  1097. $subs =
  1098. $this->fuckhtml
  1099. ->getElementsByClassName(
  1100. $this->getstyle([
  1101. "color" => "#1967d2"
  1102. ]),
  1103. "a"
  1104. );
  1105. if(count($subs) !== 0){
  1106. foreach($subs as $sub){
  1107. $url =
  1108. $this->unshiturl(
  1109. $this->fuckhtml
  1110. ->getTextContent(
  1111. $sub["attributes"]["href"]
  1112. )
  1113. );
  1114. if(
  1115. preg_match(
  1116. '/^https?:\/\//',
  1117. $url
  1118. )
  1119. ){
  1120. $sublinks[] = [
  1121. "title" =>
  1122. $this->titledots(
  1123. $this->fuckhtml
  1124. ->getTextContent(
  1125. $sub
  1126. )
  1127. ),
  1128. "description" => null,
  1129. "url" =>
  1130. $this->unshiturl(
  1131. $this->fuckhtml
  1132. ->getTextContent(
  1133. $sub["attributes"]["href"]
  1134. )
  1135. ),
  1136. "date" => null
  1137. ];
  1138. }
  1139. }
  1140. unset($categories[$i]);
  1141. }
  1142. $i++;
  1143. }
  1144. // get description & date
  1145. $date = null;
  1146. $categories = array_values($categories);
  1147. //print_r($categories);
  1148. $c = count($categories) - 1;
  1149. $description =
  1150. $this->fuckhtml
  1151. ->getTextContent(
  1152. $categories[$c]
  1153. );
  1154. // remove last category since we're done with it
  1155. unset($categories[$c]);
  1156. // probe for date
  1157. $description_tmp = explode("·", $description, 2);
  1158. $date_tmp = strtotime(trim($description_tmp[0]));
  1159. if(
  1160. count($description_tmp) === 2 &&
  1161. strlen($description_tmp[0]) <= 20 &&
  1162. $date_tmp !== false
  1163. ){
  1164. $description =
  1165. ltrim(
  1166. $this->titledots(
  1167. $description_tmp[1]
  1168. )
  1169. );
  1170. $date = $date_tmp;
  1171. }else{
  1172. $description =
  1173. $this->titledots(
  1174. $description
  1175. );
  1176. }
  1177. // remaining categories should all be greytext
  1178. if(count($categories) !== 0){
  1179. $texts =
  1180. explode(
  1181. "·",
  1182. preg_replace(
  1183. '/\s+/',
  1184. " ",
  1185. $this->fuckhtml
  1186. ->getTextContent(
  1187. $categories[0]
  1188. )
  1189. )
  1190. );
  1191. foreach($texts as $text){
  1192. $text = trim($text);
  1193. if(
  1194. preg_match(
  1195. '/^Rating ([0-9.]+)(?: \(([0-9,]+)\))?/',
  1196. $text,
  1197. $rating
  1198. )
  1199. ){
  1200. $table["Rating"] = $rating[1];
  1201. if(isset($rating[2])){
  1202. $table["Rating"] .= " (" . $rating[2] . " votes)";
  1203. }
  1204. continue;
  1205. }
  1206. if(stripos($text, "stock") !== false){
  1207. $table["Stock"] = $text;
  1208. continue;
  1209. }
  1210. }
  1211. }
  1212. $out["web"][] = [
  1213. "title" =>
  1214. $this->titledots(
  1215. $this->fuckhtml
  1216. ->getTextContent(
  1217. $title[0]
  1218. )
  1219. ),
  1220. "description" => $description,
  1221. "url" =>
  1222. $this->unshiturl(
  1223. $link[0]["attributes"]["href"]
  1224. ),
  1225. "date" => $date,
  1226. "type" => "web",
  1227. "thumb" => [
  1228. "url" => null,
  1229. "ratio" => null
  1230. ],
  1231. "sublink" => $sublinks,
  1232. "table" => $table
  1233. ];
  1234. continue;
  1235. }
  1236. // parse wikipedia heads
  1237. $wiki_title =
  1238. $this->fuckhtml
  1239. ->getElementsByClassName(
  1240. $this->getstyle([
  1241. "color" => "#202124",
  1242. "font-size" => "18px",
  1243. "line-height" => "24px"
  1244. ]),
  1245. "span"
  1246. );
  1247. if(count($wiki_title) !== 0){
  1248. $wiki_title =
  1249. $this->fuckhtml
  1250. ->getTextContent(
  1251. $wiki_title[0]
  1252. );
  1253. if($wiki_title == "See results about"){
  1254. // ignore
  1255. continue;
  1256. }
  1257. if($wiki_title == "Top stories"){
  1258. //
  1259. // Parse news
  1260. //
  1261. $tds =
  1262. $this->fuckhtml
  1263. ->getElementsByTagName(
  1264. "td"
  1265. );
  1266. foreach($tds as $td){
  1267. $this->fuckhtml->load($td);
  1268. $a =
  1269. $this->fuckhtml
  1270. ->getElementsByTagName(
  1271. "a"
  1272. );
  1273. if(count($a) === 0){
  1274. continue;
  1275. }
  1276. $title =
  1277. $this->fuckhtml
  1278. ->getElementsByClassName(
  1279. $this->getstyle([
  1280. "color" => "#1967d2"
  1281. ]),
  1282. "span"
  1283. );
  1284. if(count($title) === 0){
  1285. continue;
  1286. }
  1287. $date = null;
  1288. $meta_div =
  1289. $this->fuckhtml
  1290. ->getElementsByClassName(
  1291. $this->getstyle([
  1292. "color" => "#70757a",
  1293. "font-size" => "13px",
  1294. "line-height" => "20px"
  1295. ]),
  1296. "span"
  1297. );
  1298. $meta_div =
  1299. explode(
  1300. "·",
  1301. $this->fuckhtml
  1302. ->getTextContent(
  1303. $meta_div[count($meta_div) - 1]
  1304. ),
  1305. 2
  1306. );
  1307. if(count($meta_div) === 2){
  1308. $date = strtotime($meta_div[count($meta_div) - 1]);
  1309. if($date === false){
  1310. $date = null;
  1311. }
  1312. }
  1313. $out["news"][] = [
  1314. "title" =>
  1315. $this->titledots(
  1316. $this->fuckhtml
  1317. ->getTextContent(
  1318. $title[0]
  1319. )
  1320. ),
  1321. "description" => null,
  1322. "date" => $date,
  1323. "thumb" => [
  1324. "url" => null,
  1325. "ratio" => null
  1326. ],
  1327. "url" =>
  1328. $this->unshiturl(
  1329. $a[0]["attributes"]["href"]
  1330. )
  1331. ];
  1332. }
  1333. continue;
  1334. }
  1335. //
  1336. // Parse wikipedia heads
  1337. //
  1338. $table_div =
  1339. $this->fuckhtml
  1340. ->getElementsByTagName(
  1341. "table"
  1342. );
  1343. if(count($table_div) === 0){
  1344. continue;
  1345. }
  1346. $this->fuckhtml->load($table_div[0]);
  1347. // remove table from box
  1348. $box["innerHTML"] =
  1349. str_replace(
  1350. $table_div[0]["outerHTML"],
  1351. "",
  1352. $box["innerHTML"]
  1353. );
  1354. // find wiki image
  1355. $thumb = null;
  1356. $img =
  1357. $this->fuckhtml
  1358. ->getElementsByTagName(
  1359. "img"
  1360. );
  1361. if(count($img) !== 0){
  1362. $thumb =
  1363. $this->unshit_thumb(
  1364. $this->fuckhtml
  1365. ->getTextContent(
  1366. $img[0]["attributes"]["src"]
  1367. ),
  1368. true
  1369. );
  1370. }
  1371. $tds =
  1372. $this->fuckhtml
  1373. ->getElementsByTagName(
  1374. "td"
  1375. );
  1376. $description = [];
  1377. foreach($tds as $td){
  1378. // probe for subtitle
  1379. $this->fuckhtml->load($td);
  1380. $subtext =
  1381. $this->fuckhtml
  1382. ->getElementsByClassName(
  1383. $this->getstyle([
  1384. "color" => "#70757a",
  1385. "font-size" => "13px",
  1386. "line-height" => "20px"
  1387. ])
  1388. );
  1389. if(count($subtext) !== 0){
  1390. $description[] = [
  1391. "type" => "quote",
  1392. "value" =>
  1393. $this->fuckhtml
  1394. ->getTextContent(
  1395. $subtext[0]
  1396. )
  1397. ];
  1398. break;
  1399. }
  1400. }
  1401. $this->fuckhtml->load($box);
  1402. // probe for word definition
  1403. $lists =
  1404. $this->fuckhtml
  1405. ->getElementsByTagName(
  1406. "ol"
  1407. );
  1408. if(count($lists) !== 0){
  1409. $description = [];
  1410. foreach($lists as $list){
  1411. $box["innerHTML"] =
  1412. explode(
  1413. $list["outerHTML"],
  1414. $box["innerHTML"],
  1415. 2
  1416. );
  1417. if(
  1418. count($box["innerHTML"]) === 1 ||
  1419. trim($box["innerHTML"][0]) == ""
  1420. ){
  1421. break;
  1422. }
  1423. $description[] = [
  1424. "type" => "title",
  1425. "value" =>
  1426. $this->fuckhtml
  1427. ->getTextContent(
  1428. $box["innerHTML"][0]
  1429. )
  1430. ];
  1431. $this->fuckhtml->load($list);
  1432. $lis =
  1433. $this->fuckhtml
  1434. ->getElementsByTagName(
  1435. "li"
  1436. );
  1437. $increment = 1;
  1438. foreach($lis as $li){
  1439. $this->fuckhtml->load($li);
  1440. $list_items =
  1441. $this->fuckhtml
  1442. ->getElementsByClassName(
  1443. $this->getstyle([
  1444. "color" => "#202124",
  1445. "font-size" => "13px",
  1446. "line-height" => "20px"
  1447. ])
  1448. );
  1449. $first_item = true;
  1450. foreach($list_items as $it){
  1451. if($first_item){
  1452. $first_item = false;
  1453. $c = count($description);
  1454. if(
  1455. $c !== 0 &&
  1456. $description[$c - 1]["type"] == "text"
  1457. ){
  1458. $description[$c - 1]["value"] .=
  1459. "\n\n" .
  1460. $increment . ". " . $this->fuckhtml
  1461. ->getTextContent(
  1462. $it
  1463. );
  1464. }else{
  1465. $description[] = [
  1466. "type" => "text",
  1467. "value" =>
  1468. $increment . ". " . $this->fuckhtml
  1469. ->getTextContent(
  1470. $it
  1471. )
  1472. ];
  1473. }
  1474. }else{
  1475. $description[] = [
  1476. "type" => "quote",
  1477. "value" =>
  1478. $this->fuckhtml
  1479. ->getTextContent(
  1480. $it
  1481. )
  1482. ];
  1483. }
  1484. $increment++;
  1485. }
  1486. }
  1487. $box["innerHTML"] = $box["innerHTML"][1];
  1488. }
  1489. $out["answer"][] = [
  1490. "title" => $wiki_title,
  1491. "description" => $description,
  1492. "url" => null,
  1493. "thumb" => null,
  1494. "table" => [],
  1495. "sublink" => []
  1496. ];
  1497. continue;
  1498. }
  1499. // get separator between description and facts
  1500. $separator =
  1501. $this->fuckhtml
  1502. ->getElementsByClassName(
  1503. $this->getstyle([
  1504. "height" => "4px"
  1505. ]),
  1506. "div"
  1507. );
  1508. $box_html = [];
  1509. $table = [];
  1510. if(count($separator) !== 0){
  1511. $box_html =
  1512. explode(
  1513. $separator[0]["outerHTML"],
  1514. $box["innerHTML"],
  1515. 2
  1516. );
  1517. if(count($box_html) === 2){
  1518. $box["innerHTML"] = $box_html[0];
  1519. }
  1520. $this->fuckhtml->load($box_html[1]);
  1521. // get all facts
  1522. $facts =
  1523. $this->fuckhtml
  1524. ->getElementsByTagName(
  1525. "div"
  1526. );
  1527. foreach($facts as $fact){
  1528. if($fact["level"] !== 1){ continue; }
  1529. $fact =
  1530. explode(
  1531. ":",
  1532. $this->fuckhtml
  1533. ->getTextContent(
  1534. $fact
  1535. ),
  1536. 2
  1537. );
  1538. if(count($fact) === 2){
  1539. $table[trim(preg_replace('/\s+/', " ", $fact[0]))] =
  1540. trim(preg_replace('/\s+/', " ", $fact[1]));
  1541. }
  1542. }
  1543. $this->fuckhtml->load($box);
  1544. }
  1545. // remove wikipedia link
  1546. $wiki_link =
  1547. $this->fuckhtml
  1548. ->getElementsByClassName(
  1549. $this->getstyle([
  1550. "color" => "#1967d2"
  1551. ]),
  1552. "a"
  1553. );
  1554. $url = null;
  1555. if(count($wiki_link) !== 0){
  1556. foreach($wiki_link as $link){
  1557. if(
  1558. strtolower(
  1559. $this->fuckhtml
  1560. ->getTextContent(
  1561. $link
  1562. )
  1563. ) == "wikipedia"
  1564. ){
  1565. $box["innerHTML"] =
  1566. str_replace(
  1567. $link["outerHTML"],
  1568. "",
  1569. $box["innerHTML"]
  1570. );
  1571. $url =
  1572. $this->unshiturl(
  1573. $link["attributes"]["href"]
  1574. );
  1575. $this->fuckhtml->load($box);
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. // remains of box should be description
  1581. $description[] = [
  1582. "type" => "text",
  1583. "value" =>
  1584. $this->titledots(
  1585. $this->fuckhtml
  1586. ->getTextContent(
  1587. $box
  1588. )
  1589. )
  1590. ];
  1591. $out["answer"][] = [
  1592. "title" => $wiki_title,
  1593. "description" => $description,
  1594. "url" => $url,
  1595. "thumb" => $thumb,
  1596. "table" => $table,
  1597. "sublink" => []
  1598. ];
  1599. }
  1600. }
  1601. return $out;
  1602. }
  1603. public function video($get){
  1604. if($get["npt"]){
  1605. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1606. $params = json_decode($params, true);
  1607. }else{
  1608. $search = $get["s"];
  1609. $country = $get["country"];
  1610. $nsfw = $get["nsfw"];
  1611. $older = $get["older"];
  1612. $newer = $get["newer"];
  1613. $duration = $get["duration"];
  1614. $quality = $get["quality"];
  1615. $captions = $get["captions"];
  1616. $proxy = $this->backend->get_ip();
  1617. $params = [
  1618. "q" => $search,
  1619. "udm" => "7",
  1620. "hl" => "en",
  1621. "num" => 20
  1622. ];
  1623. // country
  1624. if($country != "any"){
  1625. $params["gl"] = $country;
  1626. }
  1627. // nsfw
  1628. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1629. $tbs = [];
  1630. // get date
  1631. $older = $older === false ? null : date("m/d/Y", $older);
  1632. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1633. if(
  1634. $older !== null ||
  1635. $newer !== null
  1636. ){
  1637. $tbs["cdr"] = "1";
  1638. $tbs["cd_min"] = $newer;
  1639. $tbs["cd_max"] = $older;
  1640. }
  1641. // duration
  1642. if($duration != "any"){
  1643. $tbs[] = "dur:" . $duration;
  1644. }
  1645. // quality
  1646. if($quality != "any"){
  1647. $tbs[] = "hq:" . $quality;
  1648. }
  1649. // captions
  1650. if($captions != "any"){
  1651. $tbs[] = "cc:" . $captions;
  1652. }
  1653. // append tbs
  1654. if(count($tbs) !== 0){
  1655. $params["tbs"] =
  1656. implode(",", $tbs);
  1657. }
  1658. }
  1659. try{
  1660. $html =
  1661. $this->get(
  1662. $proxy,
  1663. "https://www.google.com/search",
  1664. $params
  1665. );
  1666. }catch(Exception $error){
  1667. throw new Exception("Failed to get HTML");
  1668. }
  1669. if(!isset($params["start"])){
  1670. $params["start"] = 0;
  1671. }
  1672. $params["start"] += 20;
  1673. $this->fuckhtml->load($html);
  1674. //
  1675. // Parse web video page
  1676. //
  1677. $this->detect_sorry();
  1678. // parse all <style> tags
  1679. $this->parsestyles();
  1680. // get javascript images
  1681. $this->scrape_dimg($html);
  1682. $this->scrape_imagearr($html);
  1683. $out = [
  1684. "status" => "ok",
  1685. "npt" =>
  1686. $this->backend->store(
  1687. json_encode($params),
  1688. "videos",
  1689. $proxy
  1690. ),
  1691. "video" => [],
  1692. "author" => [],
  1693. "livestream" => [],
  1694. "playlist" => [],
  1695. "reel" => []
  1696. ];
  1697. $search_div =
  1698. $this->fuckhtml
  1699. ->getElementById(
  1700. "center_col"
  1701. );
  1702. if($search_div === false){
  1703. throw new Exception("Failed to grep search div");
  1704. }
  1705. $this->fuckhtml->load($search_div);
  1706. $results =
  1707. $this->fuckhtml
  1708. ->getElementsByClassName(
  1709. $this->getstyle([
  1710. "margin" => "0px 0px 30px"
  1711. ]),
  1712. "div"
  1713. );
  1714. foreach($results as $result){
  1715. $this->fuckhtml->load($result);
  1716. $url =
  1717. $this->fuckhtml
  1718. ->getElementsByTagName(
  1719. "a"
  1720. );
  1721. if(count($url) === 0){
  1722. // no url, weird, continue
  1723. continue;
  1724. }
  1725. $title =
  1726. $this->fuckhtml
  1727. ->getElementsByTagName(
  1728. "h3"
  1729. );
  1730. if(count($title) === 0){
  1731. // no title, weird, continue
  1732. continue;
  1733. }
  1734. // get description
  1735. $description =
  1736. $this->fuckhtml
  1737. ->getElementsByClassName(
  1738. $this->getstyle([
  1739. "-webkit-box-orient" => "vertical",
  1740. "display" => "-webkit-box",
  1741. "-webkit-line-clamp" => "2",
  1742. "overflow" => "hidden",
  1743. "word-break" => "break-word"
  1744. ]),
  1745. "div"
  1746. );
  1747. if(count($description) === 0){
  1748. $description = null;
  1749. }else{
  1750. $description =
  1751. html_entity_decode(
  1752. $this->titledots(
  1753. $this->fuckhtml
  1754. ->getTextContent(
  1755. $description[0]
  1756. )
  1757. )
  1758. );
  1759. }
  1760. // get author + date posted
  1761. $metadiv =
  1762. $this->fuckhtml
  1763. ->getElementsByClassName(
  1764. $this->getstyle([
  1765. "margin-top" => "12px"
  1766. ]),
  1767. "div"
  1768. );
  1769. $author = null;
  1770. $date = null;
  1771. if(count($metadiv) !== 0){
  1772. $metadiv =
  1773. explode(
  1774. "·",
  1775. $this->fuckhtml
  1776. ->getTextContent(
  1777. $metadiv[0]
  1778. )
  1779. );
  1780. if(count($metadiv) === 3){
  1781. $author = trim($metadiv[1]);
  1782. $date = strtotime(trim($metadiv[2]));
  1783. }elseif(count($metadiv) === 2){
  1784. $author = trim($metadiv[0]);
  1785. $date = strtotime(trim($metadiv[1]));
  1786. }
  1787. }
  1788. $thumb = [
  1789. "url" => null,
  1790. "ratio" => null
  1791. ];
  1792. $image =
  1793. $this->fuckhtml
  1794. ->getElementsByTagName(
  1795. "img"
  1796. );
  1797. $duration = null;
  1798. if(
  1799. count($image) !== 0 &&
  1800. isset($image[0]["attributes"]["id"])
  1801. ){
  1802. $thumb = [
  1803. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1804. "ratio" => "16:9"
  1805. ];
  1806. // get duration
  1807. $duration =
  1808. $this->fuckhtml
  1809. ->getElementsByClassName(
  1810. $this->getstyle([
  1811. "background-color" => "rgba(0,0,0,0.6)",
  1812. "color" => "#fff",
  1813. "fill" => "#fff"
  1814. ])
  1815. );
  1816. if(count($duration) !== 0){
  1817. $duration =
  1818. $this->hms2int(
  1819. $this->fuckhtml
  1820. ->getTextContent(
  1821. $duration[0]
  1822. ));
  1823. }else{
  1824. $duration = null;
  1825. }
  1826. }
  1827. $out["video"][] = [
  1828. "title" =>
  1829. $this->titledots(
  1830. $this->fuckhtml
  1831. ->getTextContent(
  1832. $title[0]
  1833. )
  1834. ),
  1835. "description" => $description,
  1836. "author" => [
  1837. "name" => $author,
  1838. "url" => null,
  1839. "avatar" => null
  1840. ],
  1841. "date" => $date,
  1842. "duration" => $duration,
  1843. "views" => null,
  1844. "thumb" => $thumb,
  1845. "url" =>
  1846. $this->fuckhtml
  1847. ->getTextContent(
  1848. $url[0]["attributes"]["href"]
  1849. )
  1850. ];
  1851. }
  1852. return $out;
  1853. }
  1854. public function news($get){
  1855. if($get["npt"]){
  1856. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1857. /*parse_str(
  1858. parse_url($req, PHP_URL_QUERY),
  1859. $search
  1860. );*/
  1861. try{
  1862. $html =
  1863. $this->get(
  1864. $proxy,
  1865. "https://www.google.com" . $req,
  1866. []
  1867. );
  1868. }catch(Exception $error){
  1869. throw new Exception("Failed to get HTML");
  1870. }
  1871. }else{
  1872. $search = $get["s"];
  1873. $country = $get["country"];
  1874. $nsfw = $get["nsfw"];
  1875. $older = $get["older"];
  1876. $newer = $get["newer"];
  1877. $sort = $get["sort"];
  1878. $proxy = $this->backend->get_ip();
  1879. $params = [
  1880. "q" => $search,
  1881. "tbm" => "nws",
  1882. "hl" => "en",
  1883. "num" => "20"
  1884. ];
  1885. // country
  1886. if($country != "any"){
  1887. $params["gl"] = $country;
  1888. }
  1889. // nsfw
  1890. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1891. $tbs = [];
  1892. // get date
  1893. $older = $older === false ? null : date("m/d/Y", $older);
  1894. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1895. if(
  1896. $older !== null ||
  1897. $newer !== null
  1898. ){
  1899. $tbs["cdr"] = "1";
  1900. $tbs["cd_min"] = $newer;
  1901. $tbs["cd_max"] = $older;
  1902. }
  1903. // relevance
  1904. if($sort == "date"){
  1905. $tbs["sbd"] = "1";
  1906. }
  1907. // append tbs
  1908. if(count($tbs) !== 0){
  1909. $params["tbs"] = "";
  1910. foreach($tbs as $key => $value){
  1911. $params["tbs"] .= $key . ":" . $value . ",";
  1912. }
  1913. $params["tbs"] = rtrim($params["tbs"], ",");
  1914. }
  1915. //$html = file_get_contents("scraper/google-news.html");
  1916. $html =
  1917. $this->get(
  1918. $proxy,
  1919. "https://www.google.com/search",
  1920. $params
  1921. );
  1922. }
  1923. $out = [
  1924. "status" => "ok",
  1925. "npt" => null,
  1926. "news" => []
  1927. ];
  1928. $this->fuckhtml->load($html);
  1929. $this->detect_sorry();
  1930. // get images
  1931. $this->scrape_dimg($html);
  1932. // parse styles
  1933. $this->parsestyles();
  1934. $center_col =
  1935. $this->fuckhtml
  1936. ->getElementById(
  1937. "center_col",
  1938. "div"
  1939. );
  1940. if($center_col === null){
  1941. throw new Exception("Could not grep result div");
  1942. }
  1943. $this->fuckhtml->load($center_col);
  1944. // get next page
  1945. $npt =
  1946. $this->fuckhtml
  1947. ->getElementById(
  1948. "pnnext",
  1949. "a"
  1950. );
  1951. if($npt !== false){
  1952. $out["npt"] =
  1953. $this->backend->store(
  1954. $this->fuckhtml
  1955. ->getTextContent(
  1956. $npt["attributes"]
  1957. ["href"]
  1958. ),
  1959. "news",
  1960. $proxy
  1961. );
  1962. }
  1963. $as =
  1964. $this->fuckhtml
  1965. ->getElementsByAttributeName(
  1966. "jsname",
  1967. "a"
  1968. );
  1969. foreach($as as $a){
  1970. $this->fuckhtml->load($a);
  1971. // get title
  1972. $title =
  1973. $this->fuckhtml
  1974. ->getElementsByAttributeValue(
  1975. "role",
  1976. "heading",
  1977. "div"
  1978. );
  1979. if(count($title) === 0){
  1980. continue;
  1981. }
  1982. $title =
  1983. $this->titledots(
  1984. $this->fuckhtml
  1985. ->getTextContent(
  1986. $title[0]
  1987. )
  1988. );
  1989. // get thumbnail
  1990. $image =
  1991. $this->fuckhtml
  1992. ->getElementsByAttributeName(
  1993. "id",
  1994. "img"
  1995. );
  1996. // check for padded title node, if found, we're inside a carousel
  1997. $probe =
  1998. $this->fuckhtml
  1999. ->getElementsByClassName(
  2000. $this->getstyle(
  2001. [
  2002. "padding" => "16px 16px 40px 16px"
  2003. ]
  2004. ),
  2005. "div"
  2006. );
  2007. if(count($probe) !== 0){
  2008. $probe = true;
  2009. }else{
  2010. $probe = false;
  2011. }
  2012. if(
  2013. count($image) !== 0 &&
  2014. !isset($image[0]["attributes"]["width"])
  2015. ){
  2016. $thumb = [
  2017. "url" =>
  2018. $this->getdimg(
  2019. $image[0]["attributes"]["id"]
  2020. ),
  2021. "ratio" => $probe === true ? "16:9" : "1:1"
  2022. ];
  2023. }else{
  2024. $thumb = [
  2025. "url" => null,
  2026. "ratio" => null
  2027. ];
  2028. }
  2029. $description = null;
  2030. if($probe === false){
  2031. $desc_divs =
  2032. $this->fuckhtml
  2033. ->getElementsByAttributeName(
  2034. "style",
  2035. "div"
  2036. );
  2037. foreach($desc_divs as $desc){
  2038. if(
  2039. strpos(
  2040. $desc["attributes"]["style"],
  2041. "margin-top:"
  2042. ) !== false
  2043. ){
  2044. $description =
  2045. $this->titledots(
  2046. $this->fuckhtml
  2047. ->getTextContent(
  2048. $desc
  2049. )
  2050. );
  2051. break;
  2052. }
  2053. }
  2054. }
  2055. // get author
  2056. $author =
  2057. $this->fuckhtml
  2058. ->getElementsByClassName(
  2059. $this->getstyle(
  2060. [
  2061. "overflow" => "hidden",
  2062. "text-align" => "left",
  2063. "text-overflow" => "ellipsis",
  2064. "white-space" => "nowrap",
  2065. "margin-bottom" => "8px"
  2066. ]
  2067. ),
  2068. "div"
  2069. );
  2070. if(count($author) !== 0){
  2071. $author =
  2072. $this->fuckhtml
  2073. ->getTextContent(
  2074. $author[0]
  2075. );
  2076. }else{
  2077. $author = null;
  2078. }
  2079. // get date
  2080. $date = null;
  2081. $date_div =
  2082. $this->fuckhtml
  2083. ->getElementsByAttributeName(
  2084. "style",
  2085. "div"
  2086. );
  2087. foreach($date_div as $d){
  2088. $this->fuckhtml->load($d);
  2089. $span =
  2090. $this->fuckhtml
  2091. ->getElementsByTagName(
  2092. "span"
  2093. );
  2094. if(
  2095. strpos(
  2096. $d["attributes"]["style"],
  2097. "bottom:"
  2098. ) !== false
  2099. ){
  2100. $date =
  2101. strtotime(
  2102. $this->fuckhtml
  2103. ->getTextContent(
  2104. $span[count($span) - 1]
  2105. )
  2106. );
  2107. break;
  2108. }
  2109. }
  2110. $out["news"][] = [
  2111. "title" => $title,
  2112. "author" => $author,
  2113. "description" => $description,
  2114. "date" => $date,
  2115. "thumb" => $thumb,
  2116. "url" =>
  2117. $this->unshiturl(
  2118. $a["attributes"]
  2119. ["href"]
  2120. )
  2121. ];
  2122. }
  2123. return $out;
  2124. }
  2125. public function image($get){
  2126. // generate parameters
  2127. if($get["npt"]){
  2128. [$params, $proxy] =
  2129. $this->backend->get(
  2130. $get["npt"],
  2131. "images"
  2132. );
  2133. $params = json_decode($params, true);
  2134. }else{
  2135. $search = $get["s"];
  2136. if(strlen($search) === 0){
  2137. throw new Exception("Search term is empty!");
  2138. }
  2139. $proxy = $this->backend->get_ip();
  2140. $country = $get["country"];
  2141. $nsfw = $get["nsfw"];
  2142. $time = $get["time"];
  2143. $size = $get["size"];
  2144. $ratio = $get["ratio"];
  2145. $color = $get["color"];
  2146. $type = $get["type"];
  2147. $format = $get["format"];
  2148. $rights = $get["rights"];
  2149. $params = [
  2150. "q" => $search,
  2151. "udm" => "2" // get images
  2152. ];
  2153. // country (image search uses cr instead of gl)
  2154. if($country != "any"){
  2155. $params["cr"] = "country" . strtoupper($country);
  2156. }
  2157. // nsfw
  2158. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  2159. // generate tbs
  2160. $tbs = [];
  2161. // time
  2162. if($time != "any"){
  2163. $tbs["qdr"] = $time;
  2164. }
  2165. // size
  2166. if($size != "any"){
  2167. $params["imgsz"] = $size;
  2168. }
  2169. // ratio
  2170. if($ratio != "any"){
  2171. $params["imgar"] = $ratio;
  2172. }
  2173. // color
  2174. if($color != "any"){
  2175. if(
  2176. $color == "color" ||
  2177. $color == "trans"
  2178. ){
  2179. $params["imgc"] = $color;
  2180. }elseif($color == "bnw"){
  2181. $params["imgc"] = "gray";
  2182. }else{
  2183. $tbs["ic"] = "specific";
  2184. $tbs["isc"] = $color;
  2185. }
  2186. }
  2187. // type
  2188. if($type != "any"){
  2189. $tbs["itp"] = $type;
  2190. }
  2191. // format
  2192. if($format != "any"){
  2193. $params["as_filetype"] = $format;
  2194. }
  2195. // rights (tbs)
  2196. if($rights != "any"){
  2197. $tbs["sur"] = $rights;
  2198. }
  2199. // append tbs
  2200. if(count($tbs) !== 0){
  2201. $params["tbs"] = "";
  2202. foreach($tbs as $key => $value){
  2203. $params["tbs"] .= $key . ":" . $value . ",";
  2204. }
  2205. $params["tbs"] = rtrim($params["tbs"], ",");
  2206. }
  2207. }
  2208. /*
  2209. $handle = fopen("scraper/page.html", "r");
  2210. $html = fread($handle, filesize("scraper/page.html"));
  2211. fclose($handle);*/
  2212. try{
  2213. $html =
  2214. $this->get(
  2215. $proxy,
  2216. "https://www.google.com/search",
  2217. $params
  2218. );
  2219. }catch(Exception $error){
  2220. throw new Exception("Failed to get search page");
  2221. }
  2222. $this->fuckhtml->load($html);
  2223. $this->detect_sorry();
  2224. // get javascript images
  2225. $this->scrape_imagearr($html);
  2226. $out = [
  2227. "status" => "ok",
  2228. "npt" => null,
  2229. "image" => []
  2230. ];
  2231. $images =
  2232. $this->fuckhtml
  2233. ->getElementsByClassName(
  2234. "ivg-i",
  2235. "div"
  2236. );
  2237. foreach($images as $div){
  2238. $this->fuckhtml->load($div);
  2239. $image =
  2240. $this->fuckhtml
  2241. ->getElementsByTagName("img")[0];
  2242. // make sure we dont attempt to show an image we dont have data for
  2243. if(
  2244. isset($div["attributes"]["data-docid"]) &&
  2245. isset($this->image_arr[$div["attributes"]["data-docid"]])
  2246. ){
  2247. $source =
  2248. $this->image_arr[
  2249. $div["attributes"]["data-docid"]
  2250. ];
  2251. }else{
  2252. continue;
  2253. }
  2254. $out["image"][] = [
  2255. "title" =>
  2256. $this->titledots(
  2257. $this->fuckhtml
  2258. ->getTextContent(
  2259. $image["attributes"]["alt"]
  2260. )
  2261. ),
  2262. "source" => $source,
  2263. "url" =>
  2264. $this->fuckhtml
  2265. ->getTextContent(
  2266. $div["attributes"]["data-lpage"]
  2267. )
  2268. ];
  2269. }
  2270. // as usual, no way to check if there is a next page reliably
  2271. if(count($out["image"]) > 50){
  2272. if(!isset($params["start"])){
  2273. $params["start"] = 10;
  2274. }else{
  2275. $params["start"] += 10;
  2276. }
  2277. $out["npt"] =
  2278. $this->backend
  2279. ->store(
  2280. json_encode($params),
  2281. "image",
  2282. $proxy
  2283. );
  2284. }
  2285. return $out;
  2286. }
  2287. private function unshiturl($url, $return_size = false){
  2288. // decode
  2289. $url =
  2290. $this->fuckhtml
  2291. ->getTextContent(
  2292. $url
  2293. );
  2294. $url_parts = parse_url($url);
  2295. if(isset($url_parts["query"])){
  2296. parse_str($url_parts["query"], $query);
  2297. }else{
  2298. $query = [];
  2299. }
  2300. if(
  2301. !isset(
  2302. $url_parts["host"]
  2303. ) ||
  2304. stripos($url_parts["host"], "google.") !== false
  2305. ){
  2306. // no host, we have a tracking url
  2307. if(isset($query["imgurl"])){
  2308. $url = $query["imgurl"];
  2309. }
  2310. elseif(isset($query["q"])){
  2311. $url = $query["q"];
  2312. }
  2313. }
  2314. // rewrite URLs to remove extra tracking parameters
  2315. $domain = parse_url($url, PHP_URL_HOST);
  2316. if(
  2317. preg_match(
  2318. '/wikipedia.org$/',
  2319. $domain
  2320. )
  2321. ){
  2322. // rewrite wikipedia mobile URLs to desktop
  2323. $url =
  2324. $this->replacedomain(
  2325. $url,
  2326. preg_replace(
  2327. '/([a-z0-9]+)(\.m\.)/',
  2328. '$1.',
  2329. $domain
  2330. )
  2331. );
  2332. }
  2333. elseif(
  2334. preg_match(
  2335. '/imdb\.com$|youtube\.[^.]+$/',
  2336. $domain
  2337. )
  2338. ){
  2339. // rewrite imdb and youtube mobile URLs too
  2340. $url =
  2341. $this->replacedomain(
  2342. $url,
  2343. preg_replace(
  2344. '/^m\./',
  2345. "",
  2346. $domain
  2347. )
  2348. );
  2349. }
  2350. elseif(
  2351. preg_match(
  2352. '/play\.google\.[^.]+$/',
  2353. $domain
  2354. )
  2355. ){
  2356. // remove referrers from play.google.com
  2357. $u_query = parse_url($url, PHP_URL_QUERY);
  2358. if($u_query !== null){
  2359. parse_str($u_query, $u_query);
  2360. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2361. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2362. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2363. $query = http_build_query($query);
  2364. $url =
  2365. str_replace(
  2366. $u_query,
  2367. $u_query,
  2368. $url
  2369. );
  2370. }
  2371. }
  2372. elseif(
  2373. preg_match(
  2374. '/twitter\.com$/',
  2375. $domain
  2376. )
  2377. ){
  2378. // remove more referrers from twitter.com
  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["ref_src"])){ unset($u_query["ref_src"]); }
  2383. $u_query = http_build_query($u_query);
  2384. $url =
  2385. str_replace(
  2386. $oldquery,
  2387. $u_query,
  2388. $url
  2389. );
  2390. }
  2391. }
  2392. elseif(
  2393. preg_match(
  2394. '/maps\.google\.[^.]+/',
  2395. $domain
  2396. )
  2397. ){
  2398. if(stripos($url, "maps?") !== false){
  2399. $u_query = parse_url($url, PHP_URL_QUERY);
  2400. if($u_query !== null){
  2401. parse_str($u_query, $u_query);
  2402. if(isset($u_query["daddr"])){
  2403. $url =
  2404. "https://maps.google.com/maps?daddr=" .
  2405. urlencode($u_query["daddr"]);
  2406. }
  2407. }
  2408. }
  2409. }
  2410. if($return_size){
  2411. return [
  2412. "url" => $url,
  2413. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2414. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2415. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2416. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2417. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2418. ];
  2419. }
  2420. return $url;
  2421. }
  2422. private function replacedomain($url, $domain){
  2423. return
  2424. preg_replace(
  2425. '/(https?:\/\/)([^\/]+)/',
  2426. '$1' . $domain,
  2427. $url
  2428. );
  2429. }
  2430. private function titledots($title){
  2431. return trim($title, " .\t\n\r\0\x0B…");
  2432. }
  2433. private function hms2int($time){
  2434. $parts = explode(":", $time, 3);
  2435. $time = 0;
  2436. if(count($parts) === 3){
  2437. // hours
  2438. $time = $time + ((int)$parts[0] * 3600);
  2439. array_shift($parts);
  2440. }
  2441. if(count($parts) === 2){
  2442. // minutes
  2443. $time = $time + ((int)$parts[0] * 60);
  2444. array_shift($parts);
  2445. }
  2446. // seconds
  2447. $time = $time + (int)$parts[0];
  2448. return $time;
  2449. }
  2450. function base64url_decode($data){
  2451. $b64 = strtr($data, "-_", "+/");
  2452. $pad = strlen($b64) % 4;
  2453. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  2454. return base64_decode($b64);
  2455. }
  2456. function base64url_encode($data){
  2457. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  2458. }
  2459. private function detect_sorry(){
  2460. $captcha_form =
  2461. $this->fuckhtml
  2462. ->getElementById(
  2463. "captcha-form",
  2464. "form"
  2465. );
  2466. if($captcha_form !== false){
  2467. throw new Exception("Google returned a captcha");
  2468. }
  2469. }
  2470. }