1
0

google.php 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448
  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 parsepage($html, $pagetype, $search, $proxy, $params){
  549. $out = [
  550. "status" => "ok",
  551. "spelling" => [
  552. "type" => "no_correction",
  553. "using" => null,
  554. "correction" => null
  555. ],
  556. "npt" => null,
  557. "answer" => [],
  558. "web" => [],
  559. "image" => [],
  560. "video" => [],
  561. "news" => [],
  562. "related" => []
  563. ];
  564. $this->fuckhtml->load($html);
  565. $this->detect_sorry();
  566. // parse all <style> tags
  567. $this->parsestyles();
  568. // get javascript images
  569. $this->scrape_dimg($html);
  570. // get html blobs
  571. preg_match_all(
  572. '/function\(\){window\.jsl\.dh\(\'([^\']+?)\',\'(.+?[^\'])\'\);/',
  573. $html,
  574. $blobs
  575. );
  576. $this->blobs = [];
  577. if(isset($blobs[1])){
  578. for($i=0; $i<count($blobs[1]); $i++){
  579. $this->blobs[$blobs[1][$i]] =
  580. $this->fuckhtml
  581. ->parseJsString(
  582. $blobs[2][$i]
  583. );
  584. }
  585. }
  586. $this->scrape_imagearr($html);
  587. //
  588. // load result column
  589. //
  590. $result_div =
  591. $this->fuckhtml
  592. ->getElementById(
  593. "center_col",
  594. "div"
  595. );
  596. if($result_div === false){
  597. throw new Exception("Failed to grep result div");
  598. }
  599. $this->fuckhtml->load($result_div);
  600. // important for later
  601. $last_page = false;
  602. //
  603. // Get text results
  604. //
  605. $results =
  606. $this->fuckhtml
  607. ->getElementsByClassName(
  608. "g",
  609. "div"
  610. );
  611. $this->skip_next = false;
  612. foreach($results as $result){
  613. if($this->skip_next){
  614. $this->skip_next = false;
  615. continue;
  616. }
  617. $this->fuckhtml->load($result);
  618. $web = [
  619. "title" => null,
  620. "description" => null,
  621. "url" => null,
  622. "date" => null,
  623. "type" => "web",
  624. "thumb" => [
  625. "url" => null,
  626. "ratio" => null
  627. ],
  628. "sublink" => [],
  629. "table" => []
  630. ];
  631. // Detect presence of sublinks
  632. $g =
  633. $this->fuckhtml
  634. ->getElementsByClassName(
  635. "g",
  636. "div"
  637. );
  638. if(count($g) > 0){
  639. // skip on next iteration
  640. $this->skip_next = true;
  641. }
  642. // get title
  643. $h3 =
  644. $this->fuckhtml
  645. ->getElementsByTagName(
  646. "h3"
  647. );
  648. if(count($h3) === 0){
  649. continue;
  650. }
  651. $web["title"] =
  652. $this->titledots(
  653. $this->fuckhtml
  654. ->getTextContent(
  655. $h3[0]
  656. )
  657. );
  658. // get url
  659. $as =
  660. $this->fuckhtml
  661. ->getElementsByTagName(
  662. "a"
  663. );
  664. $web["url"] =
  665. $this->unshiturl(
  666. $as[0]
  667. ["attributes"]
  668. ["href"]
  669. );
  670. if(
  671. !preg_match(
  672. '/^http/',
  673. $web["url"]
  674. )
  675. ){
  676. // skip if invalid url is found
  677. continue;
  678. }
  679. //
  680. // get viewcount, time posted and follower count from <cite> tag
  681. //
  682. $cite =
  683. $this->fuckhtml
  684. ->getElementsByTagName(
  685. "cite"
  686. );
  687. if(count($cite) !== 0){
  688. $this->fuckhtml->load($cite[0]);
  689. $spans =
  690. $this->fuckhtml
  691. ->getElementsByTagName("span");
  692. if(count($spans) === 0){
  693. $cites =
  694. explode(
  695. "·",
  696. $this->fuckhtml
  697. ->getTextContent(
  698. $cite[0]
  699. )
  700. );
  701. foreach($cites as $cite){
  702. $cite = trim($cite);
  703. if(
  704. preg_match(
  705. '/(.+) (views|followers|likes)$/',
  706. $cite,
  707. $match
  708. )
  709. ){
  710. $web["table"][ucfirst($match[2])] =
  711. $match[1];
  712. }elseif(
  713. preg_match(
  714. '/ago$/',
  715. $cite
  716. )
  717. ){
  718. $web["date"] =
  719. strtotime($cite);
  720. }
  721. }
  722. }
  723. // reset
  724. $this->fuckhtml->load($result);
  725. }
  726. //
  727. // attempt to fetch description cleanly
  728. //
  729. $description =
  730. $this->fuckhtml
  731. ->getElementsByAttributeValue(
  732. "style",
  733. "-webkit-line-clamp:2"
  734. );
  735. if(count($description) !== 0){
  736. $web["description"] =
  737. $this->titledots(
  738. $this->fuckhtml
  739. ->getTextContent(
  740. $description[0]
  741. )
  742. );
  743. }else{
  744. // use ANOTHER method where the description is a header of the result
  745. $description =
  746. $this->fuckhtml
  747. ->getElementsByAttributeValue(
  748. "data-attrid",
  749. "wa:/description"
  750. );
  751. if(count($description) !== 0){
  752. // get date off that shit
  753. $date =
  754. $this->fuckhtml
  755. ->getElementsByClassName(
  756. $this->getstyle(
  757. [
  758. "font-size" => "12px",
  759. "line-height" => "1.34",
  760. "display" => "inline-block",
  761. "font-family" => "google sans,arial,sans-serif",
  762. "padding-right" => "0",
  763. "white-space" => "nowrap"
  764. ]
  765. ),
  766. "span"
  767. );
  768. if(count($date) !== 0){
  769. $description[0]["innerHTML"] =
  770. str_replace(
  771. $date[0]["outerHTML"],
  772. "",
  773. $description[0]["innerHTML"]
  774. );
  775. $web["date"] =
  776. strtotime(
  777. $this->fuckhtml
  778. ->getTextContent(
  779. $date[0]
  780. )
  781. );
  782. }
  783. $web["description"] =
  784. $this->fuckhtml
  785. ->getTextContent(
  786. $description[0]
  787. );
  788. }else{
  789. // Yes.. You guessed it, use ANOTHER method to get descriptions
  790. // off youtube containers
  791. $description =
  792. $this->fuckhtml
  793. ->getElementsByClassName(
  794. $this->getstyle(
  795. [
  796. "-webkit-box-orient" => "vertical",
  797. "display" => "-webkit-box",
  798. "font-size" => "14px",
  799. "-webkit-line-clamp" => "2",
  800. "line-height" => "22px",
  801. "overflow" => "hidden",
  802. "word-break" => "break-word",
  803. "color" => "#4d5156"
  804. ]
  805. ),
  806. "div"
  807. );
  808. if(count($description) !== 0){
  809. // check for video duration
  810. $duration =
  811. $this->fuckhtml
  812. ->getElementsByClassName(
  813. $this->getstyle(
  814. [
  815. "background-color" => "rgba(0,0,0,0.6)",
  816. "color" => "#fff",
  817. "fill" => "#fff"
  818. ]
  819. ),
  820. "div"
  821. );
  822. if(count($duration) !== 0){
  823. $web["table"]["Duration"] =
  824. $this->fuckhtml
  825. ->getTextContent(
  826. $duration[0]
  827. );
  828. }
  829. $web["description"] =
  830. $this->titledots(
  831. html_entity_decode(
  832. $this->fuckhtml
  833. ->getTextContent(
  834. $description[0]
  835. )
  836. )
  837. );
  838. // get author + time posted
  839. $info =
  840. $this->fuckhtml
  841. ->getElementsByClassName(
  842. $this->getstyle(
  843. [
  844. "color" => "var(" . $this->getcolorvar("#70757a") . ")",
  845. "font-size" => "14px",
  846. "line-height" => "20px",
  847. "margin-top" => "12px"
  848. ]
  849. ),
  850. "div"
  851. );
  852. if(count($info) !== 0){
  853. $info =
  854. explode(
  855. "·",
  856. $this->fuckhtml
  857. ->getTextContent(
  858. $info[0]
  859. )
  860. );
  861. switch(count($info)){
  862. case 3:
  863. $web["table"]["Author"] = trim($info[1]);
  864. $web["date"] = strtotime(trim($info[2]));
  865. break;
  866. case 2:
  867. $web["date"] = strtotime(trim($info[1]));
  868. break;
  869. }
  870. }
  871. }
  872. }
  873. }
  874. //
  875. // get categories of content within the search result
  876. //
  877. $cats =
  878. $this->fuckhtml
  879. ->getElementsByAttributeName(
  880. "data-sncf",
  881. "div"
  882. );
  883. foreach($cats as $cat){
  884. $this->fuckhtml->load($cat);
  885. // detect image category
  886. $images =
  887. $this->fuckhtml
  888. ->getElementsByTagName(
  889. "img"
  890. );
  891. if(count($images) !== 0){
  892. foreach($images as $image){
  893. if(isset($image["attributes"]["id"])){
  894. // we found an image
  895. if(isset($image["attributes"]["width"])){
  896. $width = (int)$image["attributes"]["width"];
  897. if($width == 110){
  898. $ratio = "1:1";
  899. }elseif($width > 110){
  900. $ratio = "16:9";
  901. }else{
  902. $ratio = "9:16";
  903. }
  904. }else{
  905. $ratio = "1:1";
  906. }
  907. $web["thumb"] = [
  908. "url" => $this->getdimg($image["attributes"]["id"]),
  909. "ratio" => $ratio
  910. ];
  911. continue 2;
  912. }
  913. }
  914. }
  915. // Detect rating
  916. $spans_unfiltered =
  917. $this->fuckhtml
  918. ->getElementsByTagName(
  919. "span"
  920. );
  921. $spans =
  922. $this->fuckhtml
  923. ->getElementsByAttributeName(
  924. "aria-label",
  925. $spans_unfiltered
  926. );
  927. foreach($spans as $span){
  928. if(
  929. preg_match(
  930. '/^Rated/',
  931. $span["attributes"]["aria-label"]
  932. )
  933. ){
  934. // found rating
  935. // scrape rating
  936. preg_match(
  937. '/([0-9.]+).*([0-9.]+)/',
  938. $span["attributes"]["aria-label"],
  939. $rating
  940. );
  941. if(isset($rating[1])){
  942. $web["table"]["Rating"] =
  943. $rating[1] . "/" . $rating[2];
  944. }
  945. $has_seen_reviews = 0;
  946. foreach($spans_unfiltered as $span_unfiltered){
  947. if(
  948. preg_match(
  949. '/([0-9,.]+) +([A-z]+)$/',
  950. $this->fuckhtml
  951. ->getTextContent(
  952. $span_unfiltered
  953. ),
  954. $votes
  955. )
  956. ){
  957. $has_seen_reviews++;
  958. $web["table"][ucfirst($votes[2])] = $votes[1];
  959. continue;
  960. }
  961. $text =
  962. $this->fuckhtml
  963. ->getTextContent(
  964. $span_unfiltered
  965. );
  966. if(
  967. $text == "&nbsp;&nbsp;&nbsp;" ||
  968. $text == ""
  969. ){
  970. break;
  971. }
  972. switch($has_seen_reviews){
  973. case 1:
  974. // scrape price
  975. $web["table"]["Price"] = $text;
  976. $has_seen_reviews++;
  977. break;
  978. case 2:
  979. // scrape platform
  980. $web["table"]["Platform"] = $text;
  981. $has_seen_reviews++;
  982. break;
  983. case 3:
  984. // Scrape type
  985. $web["table"]["Medium"] = $text;
  986. break;
  987. }
  988. }
  989. continue 2;
  990. }
  991. }
  992. // check if its an answer header
  993. $answer_header =
  994. $this->fuckhtml
  995. ->getElementsByClassName(
  996. $this->getstyle(
  997. [
  998. "overflow" => "hidden",
  999. "text-overflow" => "ellipsis"
  1000. ]
  1001. ),
  1002. "span"
  1003. );
  1004. if(count($answer_header) !== 0){
  1005. $link =
  1006. $this->fuckhtml
  1007. ->getElementsByTagName(
  1008. "a"
  1009. );
  1010. $cat["innerHTML"] =
  1011. str_replace(
  1012. $link[0]["outerHTML"],
  1013. "",
  1014. $cat["innerHTML"]
  1015. );
  1016. continue;
  1017. }
  1018. // we probed everything, assume this is the description
  1019. // if we didn't find one cleanly previously
  1020. if($web["description"] === null){
  1021. $web["description"] =
  1022. $this->titledots(
  1023. $this->fuckhtml
  1024. ->getTextContent(
  1025. $cat
  1026. )
  1027. );
  1028. }
  1029. }
  1030. // check if description contains date
  1031. $description = explode("—", $web["description"], 2);
  1032. if(
  1033. count($description) === 2 &&
  1034. strlen($description[0]) <= 20
  1035. ){
  1036. $date = strtotime($description[0]);
  1037. if($date !== false){
  1038. $web["date"] = $date;
  1039. $web["description"] = ltrim($description[1]);
  1040. }
  1041. }
  1042. // fetch youtube thumbnail
  1043. $thumbnail =
  1044. $this->fuckhtml
  1045. ->getElementsByClassName(
  1046. $this->getstyle(
  1047. [
  1048. "border-radius" => "8px",
  1049. "height" => "fit-content",
  1050. "justify-content" => "center",
  1051. "margin-right" => "20px",
  1052. "margin-top" => "4px",
  1053. "position" => "relative",
  1054. "width" => "fit-content"
  1055. ]
  1056. ),
  1057. "div"
  1058. );
  1059. if(count($thumbnail) !== 0){
  1060. // load thumbnail container
  1061. $this->fuckhtml->load($thumbnail[0]);
  1062. $image =
  1063. $this->fuckhtml
  1064. ->getElementsByTagName(
  1065. "img"
  1066. );
  1067. if(
  1068. count($image) !== 0 &&
  1069. isset($image[0]["attributes"]["id"])
  1070. ){
  1071. $web["thumb"] = [
  1072. "url" =>
  1073. $this->unshit_thumb(
  1074. $this->getdimg(
  1075. $image[0]["attributes"]["id"]
  1076. )
  1077. ),
  1078. "ratio" => "16:9"
  1079. ];
  1080. }
  1081. // reset
  1082. $this->fuckhtml->load($result);
  1083. }
  1084. $out["web"][] = $web;
  1085. }
  1086. // reset
  1087. $this->fuckhtml->load($result_div);
  1088. //
  1089. // craft $npt token
  1090. //
  1091. if(
  1092. $last_page === false &&
  1093. count($out["web"]) !== 0
  1094. ){
  1095. if(!isset($params["start"])){
  1096. $params["start"] = 20;
  1097. }else{
  1098. $params["start"] += 20;
  1099. }
  1100. $out["npt"] =
  1101. $this->backend
  1102. ->store(
  1103. json_encode($params),
  1104. $pagetype,
  1105. $proxy
  1106. );
  1107. }
  1108. return $out;
  1109. }
  1110. private function scrape_dimg($html){
  1111. // get images loaded through javascript
  1112. $this->dimg = [];
  1113. preg_match_all(
  1114. '/function\(\){google\.ldi=({.*?});/',
  1115. $html,
  1116. $dimg
  1117. );
  1118. if(isset($dimg[1])){
  1119. foreach($dimg[1] as $i){
  1120. $tmp = json_decode($i, true);
  1121. foreach($tmp as $key => $value){
  1122. $this->dimg[$key] =
  1123. $this->unshit_thumb(
  1124. $value
  1125. );
  1126. }
  1127. }
  1128. }
  1129. // get additional javascript base64 images
  1130. preg_match_all(
  1131. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1132. $html,
  1133. $dimg
  1134. );
  1135. if(isset($dimg[1])){
  1136. for($i=0; $i<count($dimg[1]); $i++){
  1137. $delims = explode(",", $dimg[2][$i]);
  1138. $string =
  1139. $this->fuckhtml
  1140. ->parseJsString(
  1141. $dimg[1][$i]
  1142. );
  1143. foreach($delims as $delim){
  1144. $this->dimg[trim($delim, "'")] = $string;
  1145. }
  1146. }
  1147. }
  1148. }
  1149. private function scrape_imagearr($html){
  1150. // get image links arrays
  1151. preg_match_all(
  1152. '/\[0,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1153. $html,
  1154. $image_arr
  1155. );
  1156. $this->image_arr = [];
  1157. if(isset($image_arr[1])){
  1158. for($i=0; $i<count($image_arr[1]); $i++){
  1159. $this->image_arr[$image_arr[1][$i]] =
  1160. [
  1161. [
  1162. "url" =>
  1163. $this->fuckhtml
  1164. ->parseJsString(
  1165. $image_arr[5][$i]
  1166. ),
  1167. "width" => (int)$image_arr[7][$i],
  1168. "height" => (int)$image_arr[6][$i]
  1169. ],
  1170. [
  1171. "url" =>
  1172. $this->unshit_thumb(
  1173. $this->fuckhtml
  1174. ->parseJsString(
  1175. $image_arr[2][$i]
  1176. )
  1177. ),
  1178. "width" => (int)$image_arr[4][$i],
  1179. "height" => (int)$image_arr[3][$i]
  1180. ]
  1181. ];
  1182. }
  1183. }
  1184. }
  1185. private function getdimg($dimg){
  1186. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1187. }
  1188. private function unshit_thumb($url){
  1189. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1190. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1191. $parts = parse_url($url);
  1192. if(
  1193. isset($parts["host"]) &&
  1194. preg_match(
  1195. '/tbn.*\.gstatic\.com/',
  1196. $parts["host"]
  1197. )
  1198. ){
  1199. parse_str($parts["query"], $params);
  1200. if(isset($params["q"])){
  1201. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1202. }
  1203. }
  1204. return $url;
  1205. }
  1206. private function parsestyles(){
  1207. $styles = [];
  1208. $style_div =
  1209. $this->fuckhtml
  1210. ->getElementsByTagName(
  1211. "style"
  1212. );
  1213. $raw_styles = "";
  1214. foreach($style_div as $style){
  1215. $raw_styles .= $style["innerHTML"];
  1216. }
  1217. // filter out media/keyframe queries
  1218. $raw_styles =
  1219. preg_replace(
  1220. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1221. "",
  1222. $raw_styles
  1223. );
  1224. // get styles
  1225. preg_match_all(
  1226. '/(.+?){([\S\s]*?)}/',
  1227. $raw_styles,
  1228. $matches
  1229. );
  1230. for($i=0; $i<count($matches[1]); $i++){
  1231. // get style values
  1232. preg_match_all(
  1233. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1234. $matches[2][$i],
  1235. $values_regex
  1236. );
  1237. $values = [];
  1238. for($k=0; $k<count($values_regex[1]); $k++){
  1239. $values[trim($values_regex[1][$k])] =
  1240. strtolower(trim($values_regex[2][$k]));
  1241. }
  1242. $names = explode(",", $matches[1][$i]);
  1243. // h1,h2,h3 will each get their own array index
  1244. foreach($names as $name){
  1245. $name = trim($name, "}\t\n\r\0\x0B");
  1246. foreach($values as $key => $value){
  1247. $styles[$name][$key] = $value;
  1248. }
  1249. }
  1250. }
  1251. foreach($styles as $key => $values){
  1252. $styles[$key]["_c"] = count($values);
  1253. }
  1254. $this->styles = $styles;
  1255. // get CSS colors
  1256. $this->css_colors = [];
  1257. if(isset($this->styles[":root"])){
  1258. foreach($this->styles[":root"] as $key => $value){
  1259. $this->css_colors[$value] = strtolower($key);
  1260. }
  1261. }
  1262. }
  1263. private function getstyle($styles){
  1264. $styles["_c"] = count($styles);
  1265. foreach($this->styles as $style_key => $style_values){
  1266. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1267. $style_key =
  1268. explode(" ", $style_key);
  1269. $style_key = $style_key[count($style_key) - 1];
  1270. return
  1271. ltrim(
  1272. str_replace(
  1273. [".", "#"],
  1274. " ",
  1275. $style_key
  1276. )
  1277. );
  1278. }
  1279. }
  1280. return false;
  1281. }
  1282. private function getcolorvar($color){
  1283. if(isset($this->css_colors[$color])){
  1284. return $this->css_colors[$color];
  1285. }
  1286. return null;
  1287. }
  1288. public function web($get){
  1289. if($get["npt"]){
  1290. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  1291. try{
  1292. $html =
  1293. $this->get(
  1294. $proxy,
  1295. "https://www.google.com" . $get,
  1296. [],
  1297. true
  1298. );
  1299. }catch(Exception $error){
  1300. throw new Exception("Failed to get HTML");
  1301. }
  1302. }else{
  1303. $search = $get["s"];
  1304. $country = $get["country"];
  1305. $nsfw = $get["nsfw"];
  1306. $lang = $get["lang"];
  1307. $older = $get["older"];
  1308. $newer = $get["newer"];
  1309. $spellcheck = $get["spellcheck"];
  1310. $proxy = $this->backend->get_ip();
  1311. $offset = 0;
  1312. $params = [
  1313. "q" => $search,
  1314. "hl" => "en",
  1315. "num" => 20
  1316. ];
  1317. // country
  1318. if($country != "any"){
  1319. $params["gl"] = $country;
  1320. }
  1321. // nsfw
  1322. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1323. // language
  1324. if($lang != "any"){
  1325. $params["lr"] = "lang_" . $lang;
  1326. }
  1327. // generate tbs
  1328. $tbs = [];
  1329. // get date
  1330. $older = $older === false ? null : date("m/d/Y", $older);
  1331. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1332. if(
  1333. $older !== null ||
  1334. $newer !== null
  1335. ){
  1336. $tbs["cdr"] = "1";
  1337. $tbs["cd_min"] = $newer;
  1338. $tbs["cd_max"] = $older;
  1339. }
  1340. // spellcheck filter
  1341. if($spellcheck == "no"){
  1342. $params["nfpr"] = "1";
  1343. }
  1344. if(count($tbs) !== 0){
  1345. $params["tbs"] = "";
  1346. foreach($tbs as $key => $value){
  1347. $params["tbs"] .= $key . ":" . $value . ",";
  1348. }
  1349. $params["tbs"] = rtrim($params["tbs"], ",");
  1350. }
  1351. try{
  1352. $html =
  1353. $this->get(
  1354. $proxy,
  1355. "https://www.google.com/search",
  1356. $params,
  1357. true
  1358. );
  1359. }catch(Exception $error){
  1360. throw new Exception("Failed to get HTML");
  1361. }
  1362. //$html = file_get_contents("scraper/google.html");
  1363. }
  1364. $out = [
  1365. "status" => "ok",
  1366. "spelling" => [
  1367. "type" => "no_correction",
  1368. "using" => null,
  1369. "correction" => null
  1370. ],
  1371. "npt" => null,
  1372. "answer" => [],
  1373. "web" => [],
  1374. "image" => [],
  1375. "video" => [],
  1376. "news" => [],
  1377. "related" => []
  1378. ];
  1379. $this->fuckhtml->load($html);
  1380. $this->parsestyles();
  1381. $boxes =
  1382. $this->fuckhtml
  1383. ->getElementsByClassName(
  1384. $this->getstyle([
  1385. "border" => "thin solid #dadce0",
  1386. "padding" => "12px 16px 12px 16px",
  1387. "margin-bottom" => "10px",
  1388. "font-family" => "sans-serif"
  1389. ]),
  1390. "div"
  1391. );
  1392. $skip_next = false;
  1393. // get next page token
  1394. $npt =
  1395. $this->fuckhtml
  1396. ->getElementsByClassName(
  1397. $this->getstyle([
  1398. "border" => "thin solid #dadce0",
  1399. "color" => "#70757a",
  1400. "font-size" => "14px",
  1401. "text-align" => "center",
  1402. "table-layout" => "fixed",
  1403. "width" => "100%"
  1404. ]),
  1405. "table"
  1406. );
  1407. if(count($npt) !== 0){
  1408. $this->fuckhtml->load($npt[0]);
  1409. $as =
  1410. $this->fuckhtml
  1411. ->getElementsByTagName(
  1412. "a"
  1413. );
  1414. foreach($as as $a){
  1415. $text =
  1416. $this->fuckhtml
  1417. ->getTextContent(
  1418. $a
  1419. );
  1420. if(
  1421. $text == "Next&nbsp;>" ||
  1422. $text == ">"
  1423. ){
  1424. $out["npt"] =
  1425. $this->backend->store(
  1426. $this->fuckhtml
  1427. ->getTextContent(
  1428. $a["attributes"]["href"]
  1429. ),
  1430. "web",
  1431. $proxy
  1432. );
  1433. }
  1434. }
  1435. $this->fuckhtml->load($html);
  1436. }
  1437. $first_box = true;
  1438. foreach($boxes as $box){
  1439. $this->fuckhtml->load($box);
  1440. if($first_box){
  1441. //
  1442. // Probe for word correction
  1443. //
  1444. $first_box = false;
  1445. $txt =
  1446. $this->fuckhtml
  1447. ->getTextContent($box);
  1448. if(
  1449. preg_match(
  1450. '/^Showing results for /',
  1451. $txt
  1452. )
  1453. ){
  1454. $as =
  1455. $this->fuckhtml
  1456. ->getElementsByTagName(
  1457. "a"
  1458. );
  1459. if(count($as) === 2){
  1460. $out["spelling"] = [
  1461. "type" => "including",
  1462. "using" =>
  1463. $this->fuckhtml
  1464. ->getTextContent(
  1465. $as[0]
  1466. ),
  1467. "correction" =>
  1468. $this->fuckhtml
  1469. ->getTextContent(
  1470. $as[1]
  1471. )
  1472. ];
  1473. }
  1474. continue;
  1475. }
  1476. }
  1477. // probe for custom container
  1478. $container_title =
  1479. $this->fuckhtml
  1480. ->getElementsByClassName(
  1481. $this->getstyle([
  1482. "font-weight" => "bold"
  1483. ])
  1484. );
  1485. if(count($container_title) !== 0){
  1486. $container_title =
  1487. strtolower(
  1488. $this->fuckhtml
  1489. ->getTextContent(
  1490. $container_title[0]
  1491. )
  1492. );
  1493. if($container_title == "images"){
  1494. //
  1495. // Parse image carousel
  1496. //
  1497. $images =
  1498. $this->fuckhtml
  1499. ->getElementsByClassName(
  1500. $this->getstyle([
  1501. "display" => "inline-block",
  1502. "padding" => "2px",
  1503. "padding-bottom" => "4px"
  1504. ]),
  1505. "a"
  1506. );
  1507. foreach($images as $image){
  1508. $this->fuckhtml->load($image);
  1509. $image_data =
  1510. $this->unshiturl(
  1511. $image["attributes"]["href"],
  1512. true
  1513. );
  1514. $img =
  1515. $this->fuckhtml
  1516. ->getElementsByTagName(
  1517. "img"
  1518. )[0];
  1519. $out["image"][] = [
  1520. "title" =>
  1521. $this->titledots(
  1522. $this->fuckhtml
  1523. ->getTextContent(
  1524. $img["attributes"]["alt"]
  1525. )
  1526. ),
  1527. "source" => [
  1528. [
  1529. "url" => $image_data["url"],
  1530. "width" => $image_data["image_width"],
  1531. "height" => $image_data["image_height"]
  1532. ],
  1533. [
  1534. "url" =>
  1535. $this->fuckhtml
  1536. ->getTextContent(
  1537. $img["attributes"]["src"]
  1538. ),
  1539. "width" => $image_data["thumb_width"],
  1540. "height" => $image_data["thumb_height"]
  1541. ]
  1542. ],
  1543. "url" => $image_data["ref"]
  1544. ];
  1545. }
  1546. continue;
  1547. }
  1548. if(
  1549. $container_title == "related searches" ||
  1550. $container_title == "people also search for"
  1551. ){
  1552. $as =
  1553. $this->fuckhtml
  1554. ->getElementsByClassName(
  1555. $this->getstyle([
  1556. "color" => "#202124",
  1557. "font-size" => "13px",
  1558. "line-height" => "20px"
  1559. ]),
  1560. "span"
  1561. );
  1562. foreach($as as $a){
  1563. $out["related"][] =
  1564. $this->fuckhtml
  1565. ->getTextContent(
  1566. $a
  1567. );
  1568. }
  1569. continue;
  1570. }
  1571. }
  1572. // probe for website link
  1573. $link =
  1574. $this->fuckhtml
  1575. ->getElementsByClassName(
  1576. $this->getstyle([
  1577. "color" => "#1967d2",
  1578. "font-size" => "18px",
  1579. "line-height" => "24px"
  1580. ]),
  1581. "a"
  1582. );
  1583. if(count($link) !== 0){
  1584. //
  1585. // Parse search result
  1586. //
  1587. $this->fuckhtml->load($link[0]);
  1588. $title =
  1589. $this->fuckhtml
  1590. ->getElementsByClassName(
  1591. $this->getstyle([
  1592. "color" => "#1967d2",
  1593. "font-size" => "18px",
  1594. "line-height" => "24px"
  1595. ]),
  1596. "span"
  1597. );
  1598. if(count($title) === 0){
  1599. continue;
  1600. }
  1601. $this->fuckhtml->load($box);
  1602. $sublinks = [];
  1603. $table = [];
  1604. $categories =
  1605. $this->fuckhtml
  1606. ->getElementsByClassName(
  1607. $this->getstyle([
  1608. "color" => "#202124",
  1609. "font-size" => "13px",
  1610. "line-height" => "20px"
  1611. ]),
  1612. "span"
  1613. );
  1614. $i = 0;
  1615. foreach($categories as $category){
  1616. $this->fuckhtml->load($category);
  1617. // probe for sublinks
  1618. $subs =
  1619. $this->fuckhtml
  1620. ->getElementsByClassName(
  1621. $this->getstyle([
  1622. "color" => "#1967d2"
  1623. ]),
  1624. "a"
  1625. );
  1626. if(count($subs) !== 0){
  1627. foreach($subs as $sub){
  1628. $url =
  1629. $this->unshiturl(
  1630. $this->fuckhtml
  1631. ->getTextContent(
  1632. $sub["attributes"]["href"]
  1633. )
  1634. );
  1635. if(
  1636. preg_match(
  1637. '/^https?:\/\//',
  1638. $url
  1639. )
  1640. ){
  1641. $sublinks[] = [
  1642. "title" =>
  1643. $this->titledots(
  1644. $this->fuckhtml
  1645. ->getTextContent(
  1646. $sub
  1647. )
  1648. ),
  1649. "description" => null,
  1650. "url" =>
  1651. $this->unshiturl(
  1652. $this->fuckhtml
  1653. ->getTextContent(
  1654. $sub["attributes"]["href"]
  1655. )
  1656. ),
  1657. "date" => null
  1658. ];
  1659. }
  1660. }
  1661. unset($categories[$i]);
  1662. }
  1663. $i++;
  1664. }
  1665. // get description & date
  1666. $date = null;
  1667. $categories = array_values($categories);
  1668. //print_r($categories);
  1669. $c = count($categories) - 1;
  1670. $description =
  1671. $this->fuckhtml
  1672. ->getTextContent(
  1673. $categories[$c]
  1674. );
  1675. // remove last category since we're done with it
  1676. unset($categories[$c]);
  1677. // probe for date
  1678. $description_tmp = explode("·", $description, 2);
  1679. $date_tmp = strtotime(trim($description_tmp[0]));
  1680. if(
  1681. count($description_tmp) === 2 &&
  1682. strlen($description_tmp[0]) <= 20 &&
  1683. $date_tmp !== false
  1684. ){
  1685. $description =
  1686. ltrim(
  1687. $this->titledots(
  1688. $description_tmp[1]
  1689. )
  1690. );
  1691. $date = $date_tmp;
  1692. }else{
  1693. $description =
  1694. $this->titledots(
  1695. $description
  1696. );
  1697. }
  1698. // remaining categories should all be greytext
  1699. if(count($categories) !== 0){
  1700. $texts =
  1701. explode(
  1702. "·",
  1703. preg_replace(
  1704. '/\s+/',
  1705. " ",
  1706. $this->fuckhtml
  1707. ->getTextContent(
  1708. $categories[0]
  1709. )
  1710. )
  1711. );
  1712. foreach($texts as $text){
  1713. $text = trim($text);
  1714. if(
  1715. preg_match(
  1716. '/^Rating ([0-9.]+)(?: \(([0-9,]+)\))?/',
  1717. $text,
  1718. $rating
  1719. )
  1720. ){
  1721. $table["Rating"] = $rating[1];
  1722. if(isset($rating[2])){
  1723. $table["Rating"] .= " (" . $rating[2] . " votes)";
  1724. }
  1725. continue;
  1726. }
  1727. if(stripos($text, "stock") !== false){
  1728. $table["Stock"] = $text;
  1729. continue;
  1730. }
  1731. }
  1732. }
  1733. $out["web"][] = [
  1734. "title" =>
  1735. $this->titledots(
  1736. $this->fuckhtml
  1737. ->getTextContent(
  1738. $title[0]
  1739. )
  1740. ),
  1741. "description" => $description,
  1742. "url" =>
  1743. $this->unshiturl(
  1744. $link[0]["attributes"]["href"]
  1745. ),
  1746. "date" => $date,
  1747. "type" => "web",
  1748. "thumb" => [
  1749. "url" => null,
  1750. "ratio" => null
  1751. ],
  1752. "sublink" => $sublinks,
  1753. "table" => $table
  1754. ];
  1755. continue;
  1756. }
  1757. // parse wikipedia heads
  1758. $wiki_title =
  1759. $this->fuckhtml
  1760. ->getElementsByClassName(
  1761. $this->getstyle([
  1762. "color" => "#202124",
  1763. "font-size" => "18px",
  1764. "line-height" => "24px"
  1765. ]),
  1766. "span"
  1767. );
  1768. if(count($wiki_title) !== 0){
  1769. $wiki_title =
  1770. $this->fuckhtml
  1771. ->getTextContent(
  1772. $wiki_title[0]
  1773. );
  1774. if($wiki_title == "See results about"){
  1775. // ignore
  1776. continue;
  1777. }
  1778. if($wiki_title == "Top stories"){
  1779. //
  1780. // Parse news
  1781. //
  1782. $tds =
  1783. $this->fuckhtml
  1784. ->getElementsByTagName(
  1785. "td"
  1786. );
  1787. foreach($tds as $td){
  1788. $this->fuckhtml->load($td);
  1789. $a =
  1790. $this->fuckhtml
  1791. ->getElementsByTagName(
  1792. "a"
  1793. );
  1794. if(count($a) === 0){
  1795. continue;
  1796. }
  1797. $title =
  1798. $this->fuckhtml
  1799. ->getElementsByClassName(
  1800. $this->getstyle([
  1801. "color" => "#1967d2"
  1802. ]),
  1803. "span"
  1804. );
  1805. if(count($title) === 0){
  1806. continue;
  1807. }
  1808. $date = null;
  1809. $meta_div =
  1810. $this->fuckhtml
  1811. ->getElementsByClassName(
  1812. $this->getstyle([
  1813. "color" => "#70757a",
  1814. "font-size" => "13px",
  1815. "line-height" => "20px"
  1816. ]),
  1817. "span"
  1818. );
  1819. $meta_div =
  1820. explode(
  1821. "·",
  1822. $this->fuckhtml
  1823. ->getTextContent(
  1824. $meta_div[count($meta_div) - 1]
  1825. ),
  1826. 2
  1827. );
  1828. if(count($meta_div) === 2){
  1829. $date = strtotime($meta_div[count($meta_div) - 1]);
  1830. if($date === false){
  1831. $date = null;
  1832. }
  1833. }
  1834. $out["news"][] = [
  1835. "title" =>
  1836. $this->titledots(
  1837. $this->fuckhtml
  1838. ->getTextContent(
  1839. $title[0]
  1840. )
  1841. ),
  1842. "description" => null,
  1843. "date" => $date,
  1844. "thumb" => [
  1845. "url" => null,
  1846. "ratio" => null
  1847. ],
  1848. "url" =>
  1849. $this->unshiturl(
  1850. $a[0]["attributes"]["href"]
  1851. )
  1852. ];
  1853. }
  1854. continue;
  1855. }
  1856. //
  1857. // Parse wikipedia heads
  1858. //
  1859. $table_div =
  1860. $this->fuckhtml
  1861. ->getElementsByTagName(
  1862. "table"
  1863. );
  1864. if(count($table_div) === 0){
  1865. continue;
  1866. }
  1867. $this->fuckhtml->load($table_div[0]);
  1868. // remove table from box
  1869. $box["innerHTML"] =
  1870. str_replace(
  1871. $table_div[0]["outerHTML"],
  1872. "",
  1873. $box["innerHTML"]
  1874. );
  1875. // find wiki image
  1876. $thumb = null;
  1877. $img =
  1878. $this->fuckhtml
  1879. ->getElementsByTagName(
  1880. "img"
  1881. );
  1882. if(count($img) !== 0){
  1883. $thumb =
  1884. $this->fuckhtml
  1885. ->getTextContent(
  1886. $img[0]["attributes"]["src"]
  1887. );
  1888. }
  1889. $tds =
  1890. $this->fuckhtml
  1891. ->getElementsByTagName(
  1892. "td"
  1893. );
  1894. $description = [];
  1895. foreach($tds as $td){
  1896. // probe for subtitle
  1897. $this->fuckhtml->load($td);
  1898. $subtext =
  1899. $this->fuckhtml
  1900. ->getElementsByClassName(
  1901. $this->getstyle([
  1902. "color" => "#70757a",
  1903. "font-size" => "13px",
  1904. "line-height" => "20px"
  1905. ])
  1906. );
  1907. if(count($subtext) !== 0){
  1908. $description[] = [
  1909. "type" => "quote",
  1910. "value" =>
  1911. $this->fuckhtml
  1912. ->getTextContent(
  1913. $subtext[0]
  1914. )
  1915. ];
  1916. break;
  1917. }
  1918. }
  1919. $this->fuckhtml->load($box);
  1920. // probe for word definition
  1921. $lists =
  1922. $this->fuckhtml
  1923. ->getElementsByTagName(
  1924. "ol"
  1925. );
  1926. if(count($lists) !== 0){
  1927. $description = [];
  1928. foreach($lists as $list){
  1929. $box["innerHTML"] =
  1930. explode(
  1931. $list["outerHTML"],
  1932. $box["innerHTML"],
  1933. 2
  1934. );
  1935. if(
  1936. count($box["innerHTML"]) === 1 ||
  1937. trim($box["innerHTML"][0]) == ""
  1938. ){
  1939. break;
  1940. }
  1941. $description[] = [
  1942. "type" => "title",
  1943. "value" =>
  1944. $this->fuckhtml
  1945. ->getTextContent(
  1946. $box["innerHTML"][0]
  1947. )
  1948. ];
  1949. $this->fuckhtml->load($list);
  1950. $lis =
  1951. $this->fuckhtml
  1952. ->getElementsByTagName(
  1953. "li"
  1954. );
  1955. $increment = 1;
  1956. foreach($lis as $li){
  1957. $this->fuckhtml->load($li);
  1958. $list_items =
  1959. $this->fuckhtml
  1960. ->getElementsByClassName(
  1961. $this->getstyle([
  1962. "color" => "#202124",
  1963. "font-size" => "13px",
  1964. "line-height" => "20px"
  1965. ])
  1966. );
  1967. $first_item = true;
  1968. foreach($list_items as $it){
  1969. if($first_item){
  1970. $first_item = false;
  1971. $c = count($description);
  1972. if(
  1973. $c !== 0 &&
  1974. $description[$c - 1]["type"] == "text"
  1975. ){
  1976. $description[$c - 1]["value"] .=
  1977. "\n\n" .
  1978. $increment . ". " . $this->fuckhtml
  1979. ->getTextContent(
  1980. $it
  1981. );
  1982. }else{
  1983. $description[] = [
  1984. "type" => "text",
  1985. "value" =>
  1986. $increment . ". " . $this->fuckhtml
  1987. ->getTextContent(
  1988. $it
  1989. )
  1990. ];
  1991. }
  1992. }else{
  1993. $description[] = [
  1994. "type" => "quote",
  1995. "value" =>
  1996. $this->fuckhtml
  1997. ->getTextContent(
  1998. $it
  1999. )
  2000. ];
  2001. }
  2002. $increment++;
  2003. }
  2004. }
  2005. $box["innerHTML"] = $box["innerHTML"][1];
  2006. }
  2007. $out["answer"][] = [
  2008. "title" => $wiki_title,
  2009. "description" => $description,
  2010. "url" => null,
  2011. "thumb" => null,
  2012. "table" => [],
  2013. "sublink" => []
  2014. ];
  2015. continue;
  2016. }
  2017. // get separator between description and facts
  2018. $separator =
  2019. $this->fuckhtml
  2020. ->getElementsByClassName(
  2021. $this->getstyle([
  2022. "height" => "4px"
  2023. ]),
  2024. "div"
  2025. );
  2026. $box_html = [];
  2027. $table = [];
  2028. if(count($separator) !== 0){
  2029. $box_html =
  2030. explode(
  2031. $separator[0]["outerHTML"],
  2032. $box["innerHTML"],
  2033. 2
  2034. );
  2035. if(count($box_html) === 2){
  2036. $box["innerHTML"] = $box_html[0];
  2037. }
  2038. $this->fuckhtml->load($box_html[1]);
  2039. // get all facts
  2040. $facts =
  2041. $this->fuckhtml
  2042. ->getElementsByTagName(
  2043. "div"
  2044. );
  2045. foreach($facts as $fact){
  2046. if($fact["level"] !== 1){ continue; }
  2047. $fact =
  2048. explode(
  2049. ":",
  2050. $this->fuckhtml
  2051. ->getTextContent(
  2052. $fact
  2053. )
  2054. );
  2055. $table[trim(preg_replace('/\s+/', " ", $fact[0]))] =
  2056. trim(preg_replace('/\s+/', " ", $fact[1]));
  2057. }
  2058. $this->fuckhtml->load($box);
  2059. }
  2060. // remove wikipedia link
  2061. $wiki_link =
  2062. $this->fuckhtml
  2063. ->getElementsByClassName(
  2064. $this->getstyle([
  2065. "color" => "#1967d2"
  2066. ]),
  2067. "a"
  2068. );
  2069. $url = null;
  2070. if(count($wiki_link) !== 0){
  2071. foreach($wiki_link as $link){
  2072. if(
  2073. strtolower(
  2074. $this->fuckhtml
  2075. ->getTextContent(
  2076. $link
  2077. )
  2078. ) == "wikipedia"
  2079. ){
  2080. $box["innerHTML"] =
  2081. str_replace(
  2082. $link["outerHTML"],
  2083. "",
  2084. $box["innerHTML"]
  2085. );
  2086. $url =
  2087. $this->unshiturl(
  2088. $link["attributes"]["href"]
  2089. );
  2090. $this->fuckhtml->load($box);
  2091. break;
  2092. }
  2093. }
  2094. }
  2095. // remains of box should be description
  2096. $description[] = [
  2097. "type" => "text",
  2098. "value" =>
  2099. $this->titledots(
  2100. $this->fuckhtml
  2101. ->getTextContent(
  2102. $box
  2103. )
  2104. )
  2105. ];
  2106. $out["answer"][] = [
  2107. "title" => $wiki_title,
  2108. "description" => $description,
  2109. "url" => $url,
  2110. "thumb" => $thumb,
  2111. "table" => $table,
  2112. "sublink" => []
  2113. ];
  2114. }
  2115. }
  2116. return $out;
  2117. }
  2118. public function video($get){
  2119. if($get["npt"]){
  2120. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  2121. $params = json_decode($params, true);
  2122. $search = $params["q"];
  2123. }else{
  2124. $search = $get["s"];
  2125. $country = $get["country"];
  2126. $nsfw = $get["nsfw"];
  2127. $older = $get["older"];
  2128. $newer = $get["newer"];
  2129. $duration = $get["duration"];
  2130. $quality = $get["quality"];
  2131. $captions = $get["captions"];
  2132. $proxy = $this->backend->get_ip();
  2133. $params = [
  2134. "q" => $search,
  2135. "tbm" => "vid",
  2136. "hl" => "en",
  2137. "num" => "20"
  2138. ];
  2139. // country
  2140. if($country != "any"){
  2141. $params["gl"] = $country;
  2142. }
  2143. // nsfw
  2144. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  2145. $tbs = [];
  2146. // get date
  2147. $older = $older === false ? null : date("m/d/Y", $older);
  2148. $newer = $newer === false ? null : date("m/d/Y", $newer);
  2149. if(
  2150. $older !== null ||
  2151. $newer !== null
  2152. ){
  2153. $tbs["cdr"] = "1";
  2154. $tbs["cd_min"] = $newer;
  2155. $tbs["cd_max"] = $older;
  2156. }
  2157. // duration
  2158. if($duration != "any"){
  2159. $tbs[] = "dur:" . $duration;
  2160. }
  2161. // quality
  2162. if($quality != "any"){
  2163. $tbs[] = "hq:" . $quality;
  2164. }
  2165. // captions
  2166. if($captions != "any"){
  2167. $tbs[] = "cc:" . $captions;
  2168. }
  2169. // append tbs
  2170. if(count($tbs) !== 0){
  2171. $params["tbs"] =
  2172. implode(",", $tbs);
  2173. }
  2174. }
  2175. try{
  2176. $html =
  2177. $this->get(
  2178. $proxy,
  2179. "https://www.google.com/search",
  2180. $params
  2181. );
  2182. }catch(Exception $error){
  2183. throw new Exception("Failed to get HTML");
  2184. }
  2185. //$html = file_get_contents("scraper/google.html");
  2186. $response = $this->parsepage($html, "videos", $search, $proxy, $params);
  2187. $out = [
  2188. "status" => "ok",
  2189. "npt" => $response["npt"],
  2190. "video" => [],
  2191. "author" => [],
  2192. "livestream" => [],
  2193. "playlist" => [],
  2194. "reel" => []
  2195. ];
  2196. foreach($response["web"] as $result){
  2197. $out["video"][] = [
  2198. "title" => $result["title"],
  2199. "description" => $result["description"],
  2200. "author" => [
  2201. "name" => isset($result["table"]["Author"]) ? $result["table"]["Author"] : null,
  2202. "url" => null,
  2203. "avatar" => null
  2204. ],
  2205. "date" => $result["date"],
  2206. "duration" => isset($result["table"]["Duration"]) ? $this->hms2int($result["table"]["Duration"]) : null,
  2207. "views" => null,
  2208. "thumb" => $result["thumb"],
  2209. "url" => $result["url"]
  2210. ];
  2211. }
  2212. return $out;
  2213. }
  2214. public function news($get){
  2215. if($get["npt"]){
  2216. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  2217. /*parse_str(
  2218. parse_url($req, PHP_URL_QUERY),
  2219. $search
  2220. );*/
  2221. try{
  2222. $html =
  2223. $this->get(
  2224. $proxy,
  2225. "https://www.google.com" . $req,
  2226. []
  2227. );
  2228. }catch(Exception $error){
  2229. throw new Exception("Failed to get HTML");
  2230. }
  2231. }else{
  2232. $search = $get["s"];
  2233. $country = $get["country"];
  2234. $nsfw = $get["nsfw"];
  2235. $older = $get["older"];
  2236. $newer = $get["newer"];
  2237. $sort = $get["sort"];
  2238. $proxy = $this->backend->get_ip();
  2239. $params = [
  2240. "q" => $search,
  2241. "tbm" => "nws",
  2242. "hl" => "en",
  2243. "num" => "20"
  2244. ];
  2245. // country
  2246. if($country != "any"){
  2247. $params["gl"] = $country;
  2248. }
  2249. // nsfw
  2250. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  2251. $tbs = [];
  2252. // get date
  2253. $older = $older === false ? null : date("m/d/Y", $older);
  2254. $newer = $newer === false ? null : date("m/d/Y", $newer);
  2255. if(
  2256. $older !== null ||
  2257. $newer !== null
  2258. ){
  2259. $tbs["cdr"] = "1";
  2260. $tbs["cd_min"] = $newer;
  2261. $tbs["cd_max"] = $older;
  2262. }
  2263. // relevance
  2264. if($sort == "date"){
  2265. $tbs["sbd"] = "1";
  2266. }
  2267. // append tbs
  2268. if(count($tbs) !== 0){
  2269. $params["tbs"] = "";
  2270. foreach($tbs as $key => $value){
  2271. $params["tbs"] .= $key . ":" . $value . ",";
  2272. }
  2273. $params["tbs"] = rtrim($params["tbs"], ",");
  2274. }
  2275. //$html = file_get_contents("scraper/google-news.html");
  2276. $html =
  2277. $this->get(
  2278. $proxy,
  2279. "https://www.google.com/search",
  2280. $params
  2281. );
  2282. }
  2283. $out = [
  2284. "status" => "ok",
  2285. "npt" => null,
  2286. "news" => []
  2287. ];
  2288. $this->fuckhtml->load($html);
  2289. $this->detect_sorry();
  2290. // get images
  2291. $this->scrape_dimg($html);
  2292. // parse styles
  2293. $this->parsestyles();
  2294. $center_col =
  2295. $this->fuckhtml
  2296. ->getElementById(
  2297. "center_col",
  2298. "div"
  2299. );
  2300. if($center_col === null){
  2301. throw new Exception("Could not grep result div");
  2302. }
  2303. $this->fuckhtml->load($center_col);
  2304. // get next page
  2305. $npt =
  2306. $this->fuckhtml
  2307. ->getElementById(
  2308. "pnnext",
  2309. "a"
  2310. );
  2311. if($npt !== false){
  2312. $out["npt"] =
  2313. $this->backend->store(
  2314. $this->fuckhtml
  2315. ->getTextContent(
  2316. $npt["attributes"]
  2317. ["href"]
  2318. ),
  2319. "news",
  2320. $proxy
  2321. );
  2322. }
  2323. $as =
  2324. $this->fuckhtml
  2325. ->getElementsByAttributeName(
  2326. "jsname",
  2327. "a"
  2328. );
  2329. foreach($as as $a){
  2330. $this->fuckhtml->load($a);
  2331. // get title
  2332. $title =
  2333. $this->fuckhtml
  2334. ->getElementsByAttributeValue(
  2335. "role",
  2336. "heading",
  2337. "div"
  2338. );
  2339. if(count($title) === 0){
  2340. continue;
  2341. }
  2342. $title =
  2343. $this->titledots(
  2344. $this->fuckhtml
  2345. ->getTextContent(
  2346. $title[0]
  2347. )
  2348. );
  2349. // get thumbnail
  2350. $image =
  2351. $this->fuckhtml
  2352. ->getElementsByAttributeName(
  2353. "id",
  2354. "img"
  2355. );
  2356. // check for padded title node, if found, we're inside a carousel
  2357. $probe =
  2358. $this->fuckhtml
  2359. ->getElementsByClassName(
  2360. $this->getstyle(
  2361. [
  2362. "padding" => "16px 16px 40px 16px"
  2363. ]
  2364. ),
  2365. "div"
  2366. );
  2367. if(count($probe) !== 0){
  2368. $probe = true;
  2369. }else{
  2370. $probe = false;
  2371. }
  2372. if(
  2373. count($image) !== 0 &&
  2374. !isset($image[0]["attributes"]["width"])
  2375. ){
  2376. $thumb = [
  2377. "url" =>
  2378. $this->getdimg(
  2379. $image[0]["attributes"]["id"]
  2380. ),
  2381. "ratio" => $probe === true ? "16:9" : "1:1"
  2382. ];
  2383. }else{
  2384. $thumb = [
  2385. "url" => null,
  2386. "ratio" => null
  2387. ];
  2388. }
  2389. $description = null;
  2390. if($probe === false){
  2391. $desc_divs =
  2392. $this->fuckhtml
  2393. ->getElementsByAttributeName(
  2394. "style",
  2395. "div"
  2396. );
  2397. foreach($desc_divs as $desc){
  2398. if(
  2399. strpos(
  2400. $desc["attributes"]["style"],
  2401. "margin-top:"
  2402. ) !== false
  2403. ){
  2404. $description =
  2405. $this->titledots(
  2406. $this->fuckhtml
  2407. ->getTextContent(
  2408. $desc
  2409. )
  2410. );
  2411. break;
  2412. }
  2413. }
  2414. }
  2415. // get author
  2416. $author =
  2417. $this->fuckhtml
  2418. ->getElementsByClassName(
  2419. $this->getstyle(
  2420. [
  2421. "overflow" => "hidden",
  2422. "text-align" => "left",
  2423. "text-overflow" => "ellipsis",
  2424. "white-space" => "nowrap",
  2425. "margin-bottom" => "8px"
  2426. ]
  2427. ),
  2428. "div"
  2429. );
  2430. if(count($author) !== 0){
  2431. $author =
  2432. $this->fuckhtml
  2433. ->getTextContent(
  2434. $author[0]
  2435. );
  2436. }else{
  2437. $author = null;
  2438. }
  2439. // get date
  2440. $date = null;
  2441. $date_div =
  2442. $this->fuckhtml
  2443. ->getElementsByAttributeName(
  2444. "style",
  2445. "div"
  2446. );
  2447. foreach($date_div as $d){
  2448. $this->fuckhtml->load($d);
  2449. $span =
  2450. $this->fuckhtml
  2451. ->getElementsByTagName(
  2452. "span"
  2453. );
  2454. if(
  2455. strpos(
  2456. $d["attributes"]["style"],
  2457. "bottom:"
  2458. ) !== false
  2459. ){
  2460. $date =
  2461. strtotime(
  2462. $this->fuckhtml
  2463. ->getTextContent(
  2464. $span[count($span) - 1]
  2465. )
  2466. );
  2467. break;
  2468. }
  2469. }
  2470. $out["news"][] = [
  2471. "title" => $title,
  2472. "author" => $author,
  2473. "description" => $description,
  2474. "date" => $date,
  2475. "thumb" => $thumb,
  2476. "url" =>
  2477. $this->unshiturl(
  2478. $a["attributes"]
  2479. ["href"]
  2480. )
  2481. ];
  2482. }
  2483. return $out;
  2484. }
  2485. public function image($get){
  2486. // generate parameters
  2487. if($get["npt"]){
  2488. [$params, $proxy] =
  2489. $this->backend->get(
  2490. $get["npt"],
  2491. "images"
  2492. );
  2493. $params = json_decode($params, true);
  2494. }else{
  2495. $search = $get["s"];
  2496. if(strlen($search) === 0){
  2497. throw new Exception("Search term is empty!");
  2498. }
  2499. $proxy = $this->backend->get_ip();
  2500. $country = $get["country"];
  2501. $nsfw = $get["nsfw"];
  2502. $time = $get["time"];
  2503. $size = $get["size"];
  2504. $ratio = $get["ratio"];
  2505. $color = $get["color"];
  2506. $type = $get["type"];
  2507. $format = $get["format"];
  2508. $rights = $get["rights"];
  2509. $params = [
  2510. "q" => $search,
  2511. "udm" => "2" // get images
  2512. ];
  2513. // country (image search uses cr instead of gl)
  2514. if($country != "any"){
  2515. $params["cr"] = "country" . strtoupper($country);
  2516. }
  2517. // nsfw
  2518. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  2519. // generate tbs
  2520. $tbs = [];
  2521. // time
  2522. if($time != "any"){
  2523. $tbs["qdr"] = $time;
  2524. }
  2525. // size
  2526. if($size != "any"){
  2527. $params["imgsz"] = $size;
  2528. }
  2529. // ratio
  2530. if($ratio != "any"){
  2531. $params["imgar"] = $ratio;
  2532. }
  2533. // color
  2534. if($color != "any"){
  2535. if(
  2536. $color == "color" ||
  2537. $color == "trans"
  2538. ){
  2539. $params["imgc"] = $color;
  2540. }elseif($color == "bnw"){
  2541. $params["imgc"] = "gray";
  2542. }else{
  2543. $tbs["ic"] = "specific";
  2544. $tbs["isc"] = $color;
  2545. }
  2546. }
  2547. // type
  2548. if($type != "any"){
  2549. $tbs["itp"] = $type;
  2550. }
  2551. // format
  2552. if($format != "any"){
  2553. $params["as_filetype"] = $format;
  2554. }
  2555. // rights (tbs)
  2556. if($rights != "any"){
  2557. $tbs["sur"] = $rights;
  2558. }
  2559. // append tbs
  2560. if(count($tbs) !== 0){
  2561. $params["tbs"] = "";
  2562. foreach($tbs as $key => $value){
  2563. $params["tbs"] .= $key . ":" . $value . ",";
  2564. }
  2565. $params["tbs"] = rtrim($params["tbs"], ",");
  2566. }
  2567. }
  2568. /*
  2569. $handle = fopen("scraper/google-img.html", "r");
  2570. $html = fread($handle, filesize("scraper/google-img.html"));
  2571. fclose($handle);*/
  2572. try{
  2573. $html =
  2574. $this->get(
  2575. $proxy,
  2576. "https://www.google.com/search",
  2577. $params
  2578. );
  2579. }catch(Exception $error){
  2580. throw new Exception("Failed to get search page");
  2581. }
  2582. $this->fuckhtml->load($html);
  2583. $this->detect_sorry();
  2584. // get javascript images
  2585. $this->scrape_imagearr($html);
  2586. $out = [
  2587. "status" => "ok",
  2588. "npt" => null,
  2589. "image" => []
  2590. ];
  2591. $images =
  2592. $this->fuckhtml
  2593. ->getElementsByClassName(
  2594. "ivg-i",
  2595. "div"
  2596. );
  2597. foreach($images as $div){
  2598. $this->fuckhtml->load($div);
  2599. $image =
  2600. $this->fuckhtml
  2601. ->getElementsByTagName("img")[0];
  2602. $out["image"][] = [
  2603. "title" =>
  2604. $this->titledots(
  2605. $this->fuckhtml
  2606. ->getTextContent(
  2607. $image["attributes"]["alt"]
  2608. )
  2609. ),
  2610. "source" =>
  2611. $this->image_arr[
  2612. $div["attributes"]["data-docid"]
  2613. ],
  2614. "url" =>
  2615. $this->fuckhtml
  2616. ->getTextContent(
  2617. $div["attributes"]["data-lpage"]
  2618. )
  2619. ];
  2620. }
  2621. // as usual, no way to check if there is a next page reliably
  2622. if(count($out["image"]) > 50){
  2623. if(!isset($params["start"])){
  2624. $params["start"] = 10;
  2625. }else{
  2626. $params["start"] += 10;
  2627. }
  2628. $out["npt"] =
  2629. $this->backend
  2630. ->store(
  2631. json_encode($params),
  2632. "image",
  2633. $proxy
  2634. );
  2635. }
  2636. return $out;
  2637. }
  2638. private function unshiturl($url, $return_size = false){
  2639. // decode
  2640. $url =
  2641. $this->fuckhtml
  2642. ->getTextContent(
  2643. $url
  2644. );
  2645. $url_parts = parse_url($url);
  2646. if(isset($url_parts["query"])){
  2647. parse_str($url_parts["query"], $query);
  2648. }else{
  2649. $query = [];
  2650. }
  2651. if(
  2652. !isset(
  2653. $url_parts["host"]
  2654. ) ||
  2655. stripos($url_parts["host"], "google.") !== false
  2656. ){
  2657. // no host, we have a tracking url
  2658. if(isset($query["imgurl"])){
  2659. $url = $query["imgurl"];
  2660. }
  2661. elseif(isset($query["q"])){
  2662. $url = $query["q"];
  2663. }
  2664. }
  2665. // rewrite URLs to remove extra tracking parameters
  2666. $domain = parse_url($url, PHP_URL_HOST);
  2667. if(
  2668. preg_match(
  2669. '/wikipedia.org$/',
  2670. $domain
  2671. )
  2672. ){
  2673. // rewrite wikipedia mobile URLs to desktop
  2674. $url =
  2675. $this->replacedomain(
  2676. $url,
  2677. preg_replace(
  2678. '/([a-z0-9]+)(\.m\.)/',
  2679. '$1.',
  2680. $domain
  2681. )
  2682. );
  2683. }
  2684. elseif(
  2685. preg_match(
  2686. '/imdb\.com$|youtube\.[^.]+$/',
  2687. $domain
  2688. )
  2689. ){
  2690. // rewrite imdb and youtube mobile URLs too
  2691. $url =
  2692. $this->replacedomain(
  2693. $url,
  2694. preg_replace(
  2695. '/^m\./',
  2696. "",
  2697. $domain
  2698. )
  2699. );
  2700. }
  2701. elseif(
  2702. preg_match(
  2703. '/play\.google\.[^.]+$/',
  2704. $domain
  2705. )
  2706. ){
  2707. // remove referrers from play.google.com
  2708. $u_query = parse_url($url, PHP_URL_QUERY);
  2709. if($u_query !== null){
  2710. parse_str($u_query, $u_query);
  2711. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2712. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2713. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2714. $query = http_build_query($query);
  2715. $url =
  2716. str_replace(
  2717. $u_query,
  2718. $u_query,
  2719. $url
  2720. );
  2721. }
  2722. }
  2723. elseif(
  2724. preg_match(
  2725. '/twitter\.com$/',
  2726. $domain
  2727. )
  2728. ){
  2729. // remove more referrers from twitter.com
  2730. $u_query = parse_url($url, PHP_URL_QUERY);
  2731. if($u_query !== null){
  2732. parse_str($u_query, $u_query);
  2733. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  2734. $u_query = http_build_query($u_query);
  2735. $url =
  2736. str_replace(
  2737. $oldquery,
  2738. $u_query,
  2739. $url
  2740. );
  2741. }
  2742. }
  2743. elseif(
  2744. preg_match(
  2745. '/maps\.google\.[^.]+/',
  2746. $domain
  2747. )
  2748. ){
  2749. if(stripos($url, "maps?") !== false){
  2750. $u_query = parse_url($url, PHP_URL_QUERY);
  2751. if($u_query !== null){
  2752. parse_str($u_query, $u_query);
  2753. if(isset($u_query["daddr"])){
  2754. $url =
  2755. "https://maps.google.com/maps?daddr=" .
  2756. urlencode($u_query["daddr"]);
  2757. }
  2758. }
  2759. }
  2760. }
  2761. if($return_size){
  2762. return [
  2763. "url" => $url,
  2764. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2765. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2766. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2767. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2768. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2769. ];
  2770. }
  2771. return $url;
  2772. }
  2773. private function replacedomain($url, $domain){
  2774. return
  2775. preg_replace(
  2776. '/(https?:\/\/)([^\/]+)/',
  2777. '$1' . $domain,
  2778. $url
  2779. );
  2780. }
  2781. private function titledots($title){
  2782. return trim($title, " .\t\n\r\0\x0B…");
  2783. }
  2784. private function hms2int($time){
  2785. $parts = explode(":", $time, 3);
  2786. $time = 0;
  2787. if(count($parts) === 3){
  2788. // hours
  2789. $time = $time + ((int)$parts[0] * 3600);
  2790. array_shift($parts);
  2791. }
  2792. if(count($parts) === 2){
  2793. // minutes
  2794. $time = $time + ((int)$parts[0] * 60);
  2795. array_shift($parts);
  2796. }
  2797. // seconds
  2798. $time = $time + (int)$parts[0];
  2799. return $time;
  2800. }
  2801. private function detect_sorry(){
  2802. $recaptcha =
  2803. $this->fuckhtml
  2804. ->getElementById(
  2805. "recaptcha",
  2806. "div"
  2807. );
  2808. if($recaptcha !== false){
  2809. throw new Exception("Google returned a captcha");
  2810. }
  2811. }
  2812. }