google.php 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818
  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 = []){
  495. $headers = [
  496. "User-Agent: " . config::USER_AGENT,
  497. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  498. "Accept-Language: en-US,en;q=0.5",
  499. "Accept-Encoding: gzip",
  500. "DNT: 1",
  501. //"Cookie: SOCS=CAESNQgCEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjQwMzE3LjA4X3AwGgJlbiAEGgYIgM7orwY",
  502. "Connection: keep-alive",
  503. "Upgrade-Insecure-Requests: 1",
  504. "Sec-Fetch-Dest: document",
  505. "Sec-Fetch-Mode: navigate",
  506. "Sec-Fetch-Site: none",
  507. "Sec-Fetch-User: ?1",
  508. "Priority: u=1",
  509. "TE: trailers"
  510. ];
  511. $curlproc = curl_init();
  512. if($get !== []){
  513. $get = http_build_query($get);
  514. $url .= "?" . $get;
  515. }
  516. curl_setopt($curlproc, CURLOPT_URL, $url);
  517. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  518. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  519. // use http2
  520. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  521. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  522. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  523. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  524. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  525. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  526. // follow redirects
  527. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  528. $this->backend->assign_proxy($curlproc, $proxy);
  529. $data = curl_exec($curlproc);
  530. if(curl_errno($curlproc)){
  531. throw new Exception(curl_error($curlproc));
  532. }
  533. curl_close($curlproc);
  534. return $data;
  535. }
  536. private function parsepage($html, $pagetype, $search, $proxy, $params){
  537. $out = [
  538. "status" => "ok",
  539. "spelling" => [
  540. "type" => "no_correction",
  541. "using" => null,
  542. "correction" => null
  543. ],
  544. "npt" => null,
  545. "answer" => [],
  546. "web" => [],
  547. "image" => [],
  548. "video" => [],
  549. "news" => [],
  550. "related" => []
  551. ];
  552. $this->fuckhtml->load($html);
  553. $this->detect_sorry();
  554. // parse all <style> tags
  555. $this->parsestyles();
  556. // get javascript images
  557. $this->scrape_dimg($html);
  558. // get html blobs
  559. preg_match_all(
  560. '/function\(\){window\.jsl\.dh\(\'([^\']+?)\',\'(.+?[^\'])\'\);/',
  561. $html,
  562. $blobs
  563. );
  564. $this->blobs = [];
  565. if(isset($blobs[1])){
  566. for($i=0; $i<count($blobs[1]); $i++){
  567. $this->blobs[$blobs[1][$i]] =
  568. $this->fuckhtml
  569. ->parseJsString(
  570. $blobs[2][$i]
  571. );
  572. }
  573. }
  574. $this->scrape_imagearr($html);
  575. //
  576. // load result column
  577. //
  578. $result_div =
  579. $this->fuckhtml
  580. ->getElementById(
  581. "center_col",
  582. "div"
  583. );
  584. if($result_div === false){
  585. throw new Exception("Failed to grep result div");
  586. }
  587. $this->fuckhtml->load($result_div);
  588. //
  589. // Get word corrections
  590. //
  591. $correction =
  592. $this->fuckhtml
  593. ->getElementById(
  594. "fprs",
  595. "p"
  596. );
  597. if($correction){
  598. $this->fuckhtml->load($correction);
  599. $a =
  600. $this->fuckhtml
  601. ->getElementsByTagName(
  602. "a"
  603. );
  604. $using =
  605. $this->fuckhtml
  606. ->getElementById(
  607. "fprsl",
  608. $a
  609. );
  610. if($using){
  611. $using =
  612. $this->fuckhtml
  613. ->getTextContent(
  614. $using
  615. );
  616. $spans =
  617. $this->fuckhtml
  618. ->getElementsByTagName(
  619. "span"
  620. );
  621. $type_span =
  622. $this->fuckhtml
  623. ->getTextContent(
  624. $spans[0]
  625. );
  626. $type = "not_many";
  627. if(
  628. stripos(
  629. $type_span,
  630. "Showing results for"
  631. ) !== false
  632. ){
  633. $type = "including";
  634. }
  635. $correction =
  636. $this->fuckhtml
  637. ->getTextContent(
  638. $a[count($a) - 1]
  639. );
  640. $out["spelling"] = [
  641. "type" => $type,
  642. "using" => $using,
  643. "correction" => $correction
  644. ];
  645. }
  646. // reset
  647. $this->fuckhtml->load($result_div);
  648. }
  649. //
  650. // get notices
  651. //
  652. $botstuff =
  653. $this->fuckhtml
  654. ->getElementById(
  655. "botstuff"
  656. );
  657. // important for later
  658. $last_page = false;
  659. if($botstuff){
  660. $this->fuckhtml->load($botstuff);
  661. $cards =
  662. $this->fuckhtml
  663. ->getElementsByClassName(
  664. $this->getstyle(
  665. [
  666. "line-height" => "normal"
  667. ]
  668. ),
  669. "div"
  670. );
  671. foreach($cards as $card){
  672. $this->fuckhtml->load($card);
  673. $h2 =
  674. $this->fuckhtml
  675. ->getElementsByTagName(
  676. "h2"
  677. );
  678. if(count($h2) !== 0){
  679. $title =
  680. $this->fuckhtml
  681. ->getTextContent(
  682. $h2[0]
  683. );
  684. $card["innerHTML"] =
  685. str_replace(
  686. $h2[0]["outerHTML"],
  687. "",
  688. $card["innerHTML"]
  689. );
  690. }else{
  691. $title = "Notice";
  692. }
  693. $description = [];
  694. $as =
  695. $this->fuckhtml
  696. ->getElementsByTagName(
  697. "a"
  698. );
  699. if(count($as) !== 0){
  700. $first = true;
  701. foreach($as as $a){
  702. $text_link =
  703. $this->fuckhtml
  704. ->getTextContent(
  705. $a
  706. );
  707. if(stripos($text_link, "repeat the search") !== false){
  708. $last_page = true;
  709. break 2;
  710. }
  711. $parts =
  712. explode(
  713. $a["outerHTML"],
  714. $card["innerHTML"],
  715. 2
  716. );
  717. $card["innerHTML"] = $parts[1];
  718. $value =
  719. preg_replace(
  720. '/ +/',
  721. " ",
  722. $this->fuckhtml
  723. ->getTextContent(
  724. $parts[0],
  725. false,
  726. false
  727. )
  728. );
  729. if(strlen(trim($value)) !== 0){
  730. $description[] = [
  731. "type" => "text",
  732. "value" => $value
  733. ];
  734. if($first){
  735. $description[0]["value"] =
  736. ltrim($description[0]["value"]);
  737. }
  738. }
  739. $first = false;
  740. $description[] = [
  741. "type" => "link",
  742. "url" =>
  743. $this->fuckhtml
  744. ->getTextContent(
  745. $a["attributes"]
  746. ["href"]
  747. ),
  748. "value" => $text_link
  749. ];
  750. }
  751. $text =
  752. $this->fuckhtml
  753. ->getTextContent(
  754. $card["innerHTML"],
  755. false,
  756. false
  757. );
  758. if(strlen(trim($text)) !== 0){
  759. $description[] = [
  760. "type" => "text",
  761. "value" =>
  762. rtrim(
  763. $text
  764. )
  765. ];
  766. }
  767. }else{
  768. // @TODO: Check if this ever gets populated without giving me garbage
  769. /*
  770. $text =
  771. $this->fuckhtml
  772. ->getTextContent(
  773. $card
  774. );
  775. if($text != ""){
  776. $description[] = [
  777. "type" => "text",
  778. "value" => $text
  779. ];
  780. }*/
  781. }
  782. if(count($description) !== 0){
  783. $out["answer"][] = [
  784. "title" => $title,
  785. "description" => $description,
  786. "url" => null,
  787. "thumb" => null,
  788. "table" => [],
  789. "sublink" => []
  790. ];
  791. }
  792. }
  793. // reset
  794. $this->fuckhtml->load($html);
  795. }
  796. //
  797. // get "Related Searches" and "People also search for"
  798. //
  799. $relateds =
  800. array_merge(
  801. $this->fuckhtml
  802. ->getElementsByClassName(
  803. $this->getstyle(
  804. [
  805. "align-items" => "center",
  806. "background-color" => "#28292a",
  807. "border-radius" => "100px",
  808. "box-sizing" => "border-box",
  809. "display" => "flex",
  810. "max-height" => "none",
  811. "min-height" => "48px",
  812. "padding-left" => "17px",
  813. "padding-right" => "17px",
  814. "position" => "relative"
  815. ]
  816. ) . " " .
  817. $this->getstyle(
  818. [
  819. "margin-left" => "8px",
  820. "margin-right" => "8px"
  821. ]
  822. ),
  823. "a"
  824. ),
  825. $this->fuckhtml
  826. ->getElementsByClassName(
  827. "wyccme",
  828. "div"
  829. )
  830. );
  831. foreach($relateds as $related){
  832. $text =
  833. $this->fuckhtml
  834. ->getTextContent(
  835. $related
  836. );
  837. if($text == "More results"){ continue; }
  838. $out["related"][] = $text;
  839. }
  840. //
  841. // Get text results
  842. //
  843. $results =
  844. $this->fuckhtml
  845. ->getElementsByClassName(
  846. "g",
  847. "div"
  848. );
  849. $this->skip_next = false;
  850. foreach($results as $result){
  851. if($this->skip_next){
  852. $this->skip_next = false;
  853. continue;
  854. }
  855. $this->fuckhtml->load($result);
  856. $web = [
  857. "title" => null,
  858. "description" => null,
  859. "url" => null,
  860. "date" => null,
  861. "type" => "web",
  862. "thumb" => [
  863. "url" => null,
  864. "ratio" => null
  865. ],
  866. "sublink" => [],
  867. "table" => []
  868. ];
  869. // Detect presence of sublinks
  870. $g =
  871. $this->fuckhtml
  872. ->getElementsByClassName(
  873. "g",
  874. "div"
  875. );
  876. $sublinks = [];
  877. if(count($g) > 0){
  878. $table =
  879. $this->fuckhtml
  880. ->getElementsByTagName(
  881. "table"
  882. );
  883. if(count($table) !== 0){
  884. // found some sublinks!
  885. $this->fuckhtml->load($table[0]);
  886. $tds =
  887. $this->fuckhtml
  888. ->getElementsByTagName(
  889. "td"
  890. );
  891. foreach($tds as $td){
  892. $this->fuckhtml->load($td);
  893. $a =
  894. $this->fuckhtml
  895. ->getElementsByTagName(
  896. "a"
  897. );
  898. if(
  899. count($a) === 0 ||
  900. (
  901. isset($a[0]["attributes"]["class"]) &&
  902. $a[0]["attributes"]["class"] == "fl"
  903. )
  904. ){
  905. continue;
  906. }
  907. $td["innerHTML"] =
  908. str_replace(
  909. $a[0]["outerHTML"],
  910. "",
  911. $td["innerHTML"]
  912. );
  913. $web["sublink"][] = [
  914. "title" =>
  915. $this->titledots(
  916. $this->fuckhtml
  917. ->getTextContent(
  918. $a[0]
  919. )
  920. ),
  921. "description" =>
  922. html_entity_decode(
  923. $this->titledots(
  924. $this->fuckhtml
  925. ->getTextContent(
  926. $td
  927. )
  928. )
  929. ),
  930. "url" =>
  931. $this->unshiturl(
  932. $a[0]
  933. ["attributes"]
  934. ["href"]
  935. ),
  936. "date" => null
  937. ];
  938. }
  939. // reset
  940. $this->fuckhtml->load($result);
  941. }
  942. // skip on next iteration
  943. $this->skip_next = true;
  944. }
  945. // get title
  946. $h3 =
  947. $this->fuckhtml
  948. ->getElementsByTagName(
  949. "h3"
  950. );
  951. if(count($h3) === 0){
  952. continue;
  953. }
  954. $web["title"] =
  955. $this->titledots(
  956. $this->fuckhtml
  957. ->getTextContent(
  958. $h3[0]
  959. )
  960. );
  961. // get url
  962. $as =
  963. $this->fuckhtml
  964. ->getElementsByTagName(
  965. "a"
  966. );
  967. $web["url"] =
  968. $this->unshiturl(
  969. $as[0]
  970. ["attributes"]
  971. ["href"]
  972. );
  973. if(
  974. !preg_match(
  975. '/^http/',
  976. $web["url"]
  977. )
  978. ){
  979. // skip if invalid url is found
  980. continue;
  981. }
  982. //
  983. // probe for twitter carousel
  984. //
  985. $carousel =
  986. $this->fuckhtml
  987. ->getElementsByTagName(
  988. "g-scrolling-carousel"
  989. );
  990. if(count($carousel) !== 0){
  991. $this->fuckhtml->load($carousel[0]);
  992. $items =
  993. $this->fuckhtml
  994. ->getElementsByTagName(
  995. "g-inner-card"
  996. );
  997. $has_thumbnail = false;
  998. foreach($items as $item){
  999. $this->fuckhtml->load($item);
  1000. if($has_thumbnail === false){
  1001. // get thumbnail
  1002. $thumb =
  1003. $this->fuckhtml
  1004. ->getElementsByTagName(
  1005. "img"
  1006. );
  1007. if(
  1008. count($thumb) !== 0 &&
  1009. isset($thumb[0]["attributes"]["id"])
  1010. ){
  1011. $web["thumb"] = [
  1012. "url" =>
  1013. $this->getdimg(
  1014. $thumb[0]["attributes"]["id"]
  1015. ),
  1016. "ratio" => "16:9"
  1017. ];
  1018. $has_thumbnail = true;
  1019. }
  1020. // or else, try getting a thumbnail from next container
  1021. }
  1022. // cache div
  1023. $div =
  1024. $this->fuckhtml
  1025. ->getElementsByTagName(
  1026. "div"
  1027. );
  1028. // get link
  1029. $links =
  1030. $this->fuckhtml
  1031. ->getElementsByTagName(
  1032. "a"
  1033. );
  1034. // get description of carousel sublink
  1035. $description =
  1036. $this->fuckhtml
  1037. ->getElementsByAttributeValue(
  1038. "role",
  1039. "heading",
  1040. $div
  1041. );
  1042. if(count($description) !== 0){
  1043. $description =
  1044. $this->titledots(
  1045. $this->fuckhtml
  1046. ->getTextContent(
  1047. $description[0]
  1048. )
  1049. );
  1050. }else{
  1051. $description = null;
  1052. }
  1053. $bottom =
  1054. $this->fuckhtml
  1055. ->getElementsByAttributeValue(
  1056. "style",
  1057. "z-index:2",
  1058. $div
  1059. );
  1060. $title = null;
  1061. $date = null;
  1062. if(count($bottom) !== 0){
  1063. $this->fuckhtml->load($bottom[0]);
  1064. $spans =
  1065. $this->fuckhtml
  1066. ->getElementsByTagName(
  1067. "span"
  1068. );
  1069. $title =
  1070. $this->fuckhtml
  1071. ->getTextContent(
  1072. $spans[0]
  1073. );
  1074. $date =
  1075. strtotime(
  1076. $this->fuckhtml
  1077. ->getTextContent(
  1078. $spans[count($spans) - 1]
  1079. )
  1080. );
  1081. }
  1082. $web["sublink"][] = [
  1083. "title" => $title,
  1084. "description" => $description,
  1085. "url" =>
  1086. $this->unshiturl(
  1087. $links[0]
  1088. ["attributes"]
  1089. ["href"]
  1090. ),
  1091. "date" => $date
  1092. ];
  1093. }
  1094. $out["web"][] = $web;
  1095. continue;
  1096. }
  1097. //
  1098. // get viewcount, time posted and follower count from <cite> tag
  1099. //
  1100. $cite =
  1101. $this->fuckhtml
  1102. ->getElementsByTagName(
  1103. "cite"
  1104. );
  1105. if(count($cite) !== 0){
  1106. $this->fuckhtml->load($cite[0]);
  1107. $spans =
  1108. $this->fuckhtml
  1109. ->getElementsByTagName("span");
  1110. if(count($spans) === 0){
  1111. $cites =
  1112. explode(
  1113. "·",
  1114. $this->fuckhtml
  1115. ->getTextContent(
  1116. $cite[0]
  1117. )
  1118. );
  1119. foreach($cites as $cite){
  1120. $cite = trim($cite);
  1121. if(
  1122. preg_match(
  1123. '/(.+) (views|followers|likes)$/',
  1124. $cite,
  1125. $match
  1126. )
  1127. ){
  1128. $web["table"][ucfirst($match[2])] =
  1129. $match[1];
  1130. }elseif(
  1131. preg_match(
  1132. '/ago$/',
  1133. $cite
  1134. )
  1135. ){
  1136. $web["date"] =
  1137. strtotime($cite);
  1138. }
  1139. }
  1140. }
  1141. // reset
  1142. $this->fuckhtml->load($result);
  1143. }
  1144. //
  1145. // attempt to fetch description cleanly
  1146. //
  1147. $description =
  1148. $this->fuckhtml
  1149. ->getElementsByAttributeValue(
  1150. "style",
  1151. "-webkit-line-clamp:2"
  1152. );
  1153. if(count($description) !== 0){
  1154. $web["description"] =
  1155. $this->titledots(
  1156. $this->fuckhtml
  1157. ->getTextContent(
  1158. $description[0]
  1159. )
  1160. );
  1161. }else{
  1162. // use ANOTHER method where the description is a header of the result
  1163. $description =
  1164. $this->fuckhtml
  1165. ->getElementsByAttributeValue(
  1166. "data-attrid",
  1167. "wa:/description"
  1168. );
  1169. if(count($description) !== 0){
  1170. // get date off that shit
  1171. $date =
  1172. $this->fuckhtml
  1173. ->getElementsByClassName(
  1174. $this->getstyle(
  1175. [
  1176. "font-size" => "12px",
  1177. "line-height" => "1.34",
  1178. "display" => "inline-block",
  1179. "font-family" => "Google Sans,arial,sans-serif",
  1180. "padding-right" => "0",
  1181. "white-space" => "nowrap"
  1182. ]
  1183. ),
  1184. "span"
  1185. );
  1186. if(count($date) !== 0){
  1187. $description[0]["innerHTML"] =
  1188. str_replace(
  1189. $date[0]["outerHTML"],
  1190. "",
  1191. $description[0]["innerHTML"]
  1192. );
  1193. $web["date"] =
  1194. strtotime(
  1195. $this->fuckhtml
  1196. ->getTextContent(
  1197. $date[0]
  1198. )
  1199. );
  1200. }
  1201. $web["description"] =
  1202. $this->fuckhtml
  1203. ->getTextContent(
  1204. $description[0]
  1205. );
  1206. }else{
  1207. // Yes.. You guessed it, use ANOTHER method to get descriptions
  1208. // off youtube containers
  1209. $description =
  1210. $this->fuckhtml
  1211. ->getElementsByClassName(
  1212. $this->getstyle(
  1213. [
  1214. "-webkit-box-orient" => "vertical",
  1215. "display" => "-webkit-box",
  1216. "font-size" => "14px",
  1217. "-webkit-line-clamp" => "2",
  1218. "line-height" => "22px",
  1219. "overflow" => "hidden",
  1220. "word-break" => "break-word",
  1221. "color" => "#bdc1c6"
  1222. ]
  1223. ),
  1224. "div"
  1225. );
  1226. if(count($description) !== 0){
  1227. // check for video duration
  1228. $duration =
  1229. $this->fuckhtml
  1230. ->getElementsByClassName(
  1231. $this->getstyle(
  1232. [
  1233. "border-radius" => "10px",
  1234. "font-family" => "arial,sans-serif-medium,sans-serif",
  1235. "font-size" => "12px",
  1236. "line-height" => "16px",
  1237. "padding-block" => "2px",
  1238. "padding-inline" => "8px"
  1239. ]
  1240. ),
  1241. "div"
  1242. );
  1243. if(count($duration) !== 0){
  1244. $web["table"]["Duration"] =
  1245. $this->fuckhtml
  1246. ->getTextContent(
  1247. $duration[0]
  1248. );
  1249. // remove duration from description
  1250. $description[0]["innerHTML"] =
  1251. str_replace(
  1252. $duration[0]["outerHTML"],
  1253. "",
  1254. $description[0]["innerHTML"]
  1255. );
  1256. }
  1257. $web["description"] =
  1258. $this->titledots(
  1259. html_entity_decode(
  1260. $this->fuckhtml
  1261. ->getTextContent(
  1262. $description[0]
  1263. )
  1264. )
  1265. );
  1266. // get author + time posted
  1267. $info =
  1268. $this->fuckhtml
  1269. ->getElementsByClassName(
  1270. $this->getstyle(
  1271. [
  1272. "color" => "var(" . $this->getcolorvar("#70757a") . ")",
  1273. "font-size" => "14px",
  1274. "line-height" => "20px",
  1275. "margin-top" => "12px"
  1276. ]
  1277. ),
  1278. "div"
  1279. );
  1280. if(count($info) !== 0){
  1281. $info =
  1282. explode(
  1283. "·",
  1284. $this->fuckhtml
  1285. ->getTextContent(
  1286. $info[0]
  1287. )
  1288. );
  1289. switch(count($info)){
  1290. case 3:
  1291. $web["table"]["Author"] = trim($info[1]);
  1292. $web["date"] = strtotime(trim($info[2]));
  1293. break;
  1294. case 2:
  1295. $web["date"] = strtotime(trim($info[1]));
  1296. break;
  1297. }
  1298. }
  1299. }
  1300. }
  1301. }
  1302. //
  1303. // get categories of content within the search result
  1304. //
  1305. $cats =
  1306. $this->fuckhtml
  1307. ->getElementsByAttributeName(
  1308. "data-sncf",
  1309. "div"
  1310. );
  1311. foreach($cats as $cat){
  1312. $this->fuckhtml->load($cat);
  1313. // detect image category
  1314. $images =
  1315. $this->fuckhtml
  1316. ->getElementsByTagName(
  1317. "img"
  1318. );
  1319. if(count($images) !== 0){
  1320. foreach($images as $image){
  1321. if(isset($image["attributes"]["id"])){
  1322. // we found an image
  1323. if(isset($image["attributes"]["width"])){
  1324. $width = (int)$image["attributes"]["width"];
  1325. if($width == 110){
  1326. $ratio = "1:1";
  1327. }elseif($width > 110){
  1328. $ratio = "16:9";
  1329. }else{
  1330. $ratio = "9:16";
  1331. }
  1332. }else{
  1333. $ratio = "1:1";
  1334. }
  1335. $web["thumb"] = [
  1336. "url" => $this->getdimg($image["attributes"]["id"]),
  1337. "ratio" => $ratio
  1338. ];
  1339. continue 2;
  1340. }
  1341. }
  1342. }
  1343. // Detect rating
  1344. $spans_unfiltered =
  1345. $this->fuckhtml
  1346. ->getElementsByTagName(
  1347. "span"
  1348. );
  1349. $spans =
  1350. $this->fuckhtml
  1351. ->getElementsByAttributeName(
  1352. "aria-label",
  1353. $spans_unfiltered
  1354. );
  1355. foreach($spans as $span){
  1356. if(
  1357. preg_match(
  1358. '/^Rated/',
  1359. $span["attributes"]["aria-label"]
  1360. )
  1361. ){
  1362. // found rating
  1363. // scrape rating
  1364. preg_match(
  1365. '/([0-9.]+).*([0-9.]+)/',
  1366. $span["attributes"]["aria-label"],
  1367. $rating
  1368. );
  1369. if(isset($rating[1])){
  1370. $web["table"]["Rating"] =
  1371. $rating[1] . "/" . $rating[2];
  1372. }
  1373. $has_seen_reviews = 0;
  1374. foreach($spans_unfiltered as $span_unfiltered){
  1375. if(
  1376. preg_match(
  1377. '/([0-9,.]+) +([A-z]+)$/',
  1378. $this->fuckhtml
  1379. ->getTextContent(
  1380. $span_unfiltered
  1381. ),
  1382. $votes
  1383. )
  1384. ){
  1385. $has_seen_reviews++;
  1386. $web["table"][ucfirst($votes[2])] = $votes[1];
  1387. continue;
  1388. }
  1389. $text =
  1390. $this->fuckhtml
  1391. ->getTextContent(
  1392. $span_unfiltered
  1393. );
  1394. if(
  1395. $text == "&nbsp;&nbsp;&nbsp;" ||
  1396. $text == ""
  1397. ){
  1398. break;
  1399. }
  1400. switch($has_seen_reviews){
  1401. case 1:
  1402. // scrape price
  1403. $web["table"]["Price"] = $text;
  1404. $has_seen_reviews++;
  1405. break;
  1406. case 2:
  1407. // scrape platform
  1408. $web["table"]["Platform"] = $text;
  1409. $has_seen_reviews++;
  1410. break;
  1411. case 3:
  1412. // Scrape type
  1413. $web["table"]["Medium"] = $text;
  1414. break;
  1415. }
  1416. }
  1417. continue 2;
  1418. }
  1419. }
  1420. // check if its a table of small sublinks
  1421. $table =
  1422. $this->fuckhtml
  1423. ->getElementsByClassName(
  1424. $this->getstyle(
  1425. [
  1426. "display" => "table",
  1427. "white-space" => "nowrap",
  1428. "margin" => "5px 0",
  1429. "line-height" => "1.58",
  1430. "color" => "var(" . $this->getcolorvar("#70757a") . ")"
  1431. ]
  1432. ),
  1433. "div"
  1434. );
  1435. if(count($table) !== 0){
  1436. $this->fuckhtml->load($table[0]);
  1437. $rows =
  1438. $this->fuckhtml
  1439. ->getElementsByClassName(
  1440. $this->getstyle(
  1441. [
  1442. "display" => "flex",
  1443. "white-space" => "normal"
  1444. ]
  1445. ),
  1446. "div"
  1447. );
  1448. foreach($rows as $row){
  1449. $this->fuckhtml->load($row);
  1450. $sublink = [
  1451. "title" => null,
  1452. "description" => null,
  1453. "url" => null,
  1454. "date" => null
  1455. ];
  1456. $link =
  1457. $this->fuckhtml
  1458. ->getElementsByTagName(
  1459. "a"
  1460. )[0];
  1461. $sublink["title"] =
  1462. $this->titledots(
  1463. $this->fuckhtml
  1464. ->getTextContent(
  1465. $link
  1466. )
  1467. );
  1468. $sublink["url"] =
  1469. $this->unshiturl(
  1470. $link
  1471. ["attributes"]
  1472. ["href"]
  1473. );
  1474. $row["innerHTML"] =
  1475. str_replace(
  1476. $link["outerHTML"],
  1477. "",
  1478. $row["innerHTML"]
  1479. );
  1480. $this->fuckhtml->load($row);
  1481. $spans =
  1482. $this->fuckhtml
  1483. ->getElementsByTagName(
  1484. "span"
  1485. );
  1486. foreach($spans as $span){
  1487. $text =
  1488. $this->fuckhtml
  1489. ->getTextContent(
  1490. $span
  1491. );
  1492. if(
  1493. preg_match(
  1494. '/answers?$/',
  1495. $text
  1496. )
  1497. ){
  1498. $sublink["description"] =
  1499. $text;
  1500. continue;
  1501. }
  1502. $time = strtotime($text);
  1503. if($time !== false){
  1504. $sublink["date"] = $time;
  1505. }
  1506. }
  1507. $web["sublink"][] = $sublink;
  1508. }
  1509. // reset
  1510. $this->fuckhtml->load($cat);
  1511. continue;
  1512. }
  1513. // check if its an answer header
  1514. $answer_header =
  1515. $this->fuckhtml
  1516. ->getElementsByClassName(
  1517. $this->getstyle(
  1518. [
  1519. "overflow" => "hidden",
  1520. "text-overflow" => "ellipsis"
  1521. ]
  1522. ),
  1523. "span"
  1524. );
  1525. if(count($answer_header) !== 0){
  1526. $link =
  1527. $this->fuckhtml
  1528. ->getElementsByTagName(
  1529. "a"
  1530. );
  1531. $cat["innerHTML"] =
  1532. str_replace(
  1533. $link[0]["outerHTML"],
  1534. "",
  1535. $cat["innerHTML"]
  1536. );
  1537. $web["sublink"][] = [
  1538. "title" =>
  1539. $this->fuckhtml
  1540. ->getTextContent(
  1541. $link[0]
  1542. ),
  1543. "description" =>
  1544. $this->titledots(
  1545. trim(
  1546. str_replace(
  1547. "\xc2\xa0",
  1548. " ",
  1549. html_entity_decode(
  1550. $this->fuckhtml
  1551. ->getTextContent(
  1552. $cat
  1553. )
  1554. )
  1555. ),
  1556. " ·"
  1557. )
  1558. ),
  1559. "url" =>
  1560. $this->fuckhtml
  1561. ->getTextContent(
  1562. $link[0]
  1563. ["attributes"]
  1564. ["href"]
  1565. ),
  1566. "date" => null
  1567. ];
  1568. continue;
  1569. }
  1570. // check if its list of small sublinks
  1571. $urls =
  1572. $this->fuckhtml
  1573. ->getElementsByTagName(
  1574. "a"
  1575. );
  1576. if(count($urls) !== 0){
  1577. // found small links
  1578. foreach($urls as $url){
  1579. $target =
  1580. $this->fuckhtml
  1581. ->getTextContent(
  1582. $url
  1583. ["attributes"]
  1584. ["href"]
  1585. );
  1586. if(
  1587. !preg_match(
  1588. '/^http/',
  1589. $target
  1590. )
  1591. ){
  1592. continue;
  1593. }
  1594. $web["sublink"][] = [
  1595. "title" =>
  1596. $this->titledots(
  1597. $this->fuckhtml
  1598. ->getTextContent(
  1599. $url
  1600. )
  1601. ),
  1602. "description" => null,
  1603. "url" => $target,
  1604. "date" => null
  1605. ];
  1606. }
  1607. continue;
  1608. }
  1609. // we probed everything, assume this is the description
  1610. // if we didn't find one cleanly previously
  1611. if($web["description"] === null){
  1612. $web["description"] =
  1613. $this->titledots(
  1614. $this->fuckhtml
  1615. ->getTextContent(
  1616. $cat
  1617. )
  1618. );
  1619. }
  1620. }
  1621. // check if description contains date
  1622. $description = explode("—", $web["description"], 2);
  1623. if(
  1624. count($description) === 2 &&
  1625. strlen($description[0]) <= 20
  1626. ){
  1627. $date = strtotime($description[0]);
  1628. if($date !== false){
  1629. $web["date"] = $date;
  1630. $web["description"] = ltrim($description[1]);
  1631. }
  1632. }
  1633. // fetch youtube thumbnail
  1634. $thumbnail =
  1635. $this->fuckhtml
  1636. ->getElementsByClassName(
  1637. $this->getstyle(
  1638. [
  1639. "border-radius" => "8px",
  1640. "height" => "fit-content",
  1641. "justify-content" => "center",
  1642. "margin-right" => "20px",
  1643. "margin-top" => "4px",
  1644. "position" => "relative",
  1645. "width" => "fit-content"
  1646. ]
  1647. ),
  1648. "div"
  1649. );
  1650. if(count($thumbnail) !== 0){
  1651. // load thumbnail container
  1652. $this->fuckhtml->load($thumbnail[0]);
  1653. $image =
  1654. $this->fuckhtml
  1655. ->getElementsByTagName(
  1656. "img"
  1657. );
  1658. if(
  1659. count($image) !== 0 &&
  1660. isset($image[0]["attributes"]["id"])
  1661. ){
  1662. $web["thumb"] = [
  1663. "url" =>
  1664. $this->unshit_thumb(
  1665. $this->getdimg(
  1666. $image[0]["attributes"]["id"]
  1667. )
  1668. ),
  1669. "ratio" => "16:9"
  1670. ];
  1671. }
  1672. // reset
  1673. $this->fuckhtml->load($result);
  1674. }
  1675. $out["web"][] = $web;
  1676. }
  1677. // reset
  1678. $this->fuckhtml->load($result_div);
  1679. //
  1680. // Get instant answers
  1681. //
  1682. $answer_containers =
  1683. $this->fuckhtml
  1684. ->getElementsByClassName(
  1685. $this->getstyle(
  1686. [
  1687. "padding-left" => "0px",
  1688. "padding-right" => "0px"
  1689. ]
  1690. ),
  1691. "div"
  1692. );
  1693. $date_class =
  1694. $this->getstyle(
  1695. [
  1696. "font-size" => "12px",
  1697. "line-height" => "1.34",
  1698. "display" => "inline-block",
  1699. "font-family" => "Google Sans,arial,sans-serif",
  1700. "padding-right" => "0",
  1701. "white-space" => "nowrap"
  1702. ]
  1703. );
  1704. foreach($answer_containers as $container){
  1705. $this->fuckhtml->load($container);
  1706. $web = [
  1707. "title" => null,
  1708. "description" => null,
  1709. "url" => null,
  1710. "date" => null,
  1711. "type" => "web",
  1712. "thumb" => [
  1713. "url" => null,
  1714. "ratio" => null
  1715. ],
  1716. "sublink" => [],
  1717. "table" => []
  1718. ];
  1719. $answers =
  1720. $this->fuckhtml
  1721. ->getElementsByAttributeName(
  1722. "aria-controls",
  1723. "div"
  1724. );
  1725. $item_insert_pos = 1;
  1726. foreach($answers as $answer){
  1727. $out["related"][] =
  1728. $this->fuckhtml
  1729. ->getTextContent(
  1730. $answer
  1731. );
  1732. if(
  1733. isset(
  1734. $this->blobs[
  1735. $answer
  1736. ["attributes"]
  1737. ["aria-controls"]
  1738. ]
  1739. )
  1740. ){
  1741. $this->fuckhtml->load(
  1742. $this->blobs[
  1743. $answer
  1744. ["attributes"]
  1745. ["aria-controls"]
  1746. ]
  1747. );
  1748. $divs =
  1749. $this->fuckhtml
  1750. ->getElementsByAttributeName(
  1751. "id",
  1752. "div"
  1753. );
  1754. foreach($divs as $div){
  1755. if(
  1756. !isset(
  1757. $this->blobs[
  1758. $div
  1759. ["attributes"]
  1760. ["id"]
  1761. ]
  1762. )
  1763. ){
  1764. continue;
  1765. }
  1766. $this->fuckhtml->load(
  1767. $this->blobs[
  1768. $div
  1769. ["attributes"]
  1770. ["id"]
  1771. ]
  1772. );
  1773. // get url
  1774. $as =
  1775. $this->fuckhtml
  1776. ->getElementsByTagName(
  1777. "a"
  1778. );
  1779. if(count($as) !== 0){
  1780. $web["url"] =
  1781. $this->unshiturl(
  1782. $as[0]["attributes"]["href"]
  1783. );
  1784. // skip entries that redirect to a search
  1785. if(
  1786. !preg_match(
  1787. '/^http/',
  1788. $web["url"]
  1789. )
  1790. ){
  1791. continue 3;
  1792. }
  1793. }
  1794. // get title
  1795. $h3 =
  1796. $this->fuckhtml
  1797. ->getElementsByTagName(
  1798. "h3"
  1799. );
  1800. if(count($h3) !== 0){
  1801. $web["title"] =
  1802. $this->titledots(
  1803. $this->fuckhtml
  1804. ->getTextContent(
  1805. $h3[0]
  1806. )
  1807. );
  1808. }
  1809. $description =
  1810. $this->fuckhtml
  1811. ->getElementsByAttributeValue(
  1812. "data-attrid",
  1813. "wa:/description",
  1814. "div"
  1815. );
  1816. if(count($description) !== 0){
  1817. // check for date
  1818. $this->fuckhtml->load($description[0]);
  1819. $date =
  1820. $this->fuckhtml
  1821. ->getElementsByClassName(
  1822. $date_class,
  1823. "span"
  1824. );
  1825. if(count($date) !== 0){
  1826. $description[0]["innerHTML"] =
  1827. str_replace(
  1828. $date[0]["outerHTML"],
  1829. "",
  1830. $description[0]["innerHTML"]
  1831. );
  1832. $web["date"] =
  1833. strtotime(
  1834. $this->fuckhtml
  1835. ->getTextContent(
  1836. $date[0]
  1837. )
  1838. );
  1839. }
  1840. $web["description"] =
  1841. ltrim(
  1842. $this->fuckhtml
  1843. ->getTextContent(
  1844. $description[0]
  1845. ),
  1846. ": "
  1847. );
  1848. }
  1849. }
  1850. foreach($out["web"] as $item){
  1851. if($item["url"] == $web["url"]){
  1852. continue 2;
  1853. }
  1854. }
  1855. array_splice($out["web"], $item_insert_pos, 0, [$web]);
  1856. $item_insert_pos++;
  1857. }
  1858. }
  1859. }
  1860. // reset
  1861. $this->fuckhtml->load($result_div);
  1862. //
  1863. // Scrape word definition
  1864. //
  1865. $definition_container =
  1866. $this->fuckhtml
  1867. ->getElementsByClassName(
  1868. "lr_container",
  1869. "div"
  1870. );
  1871. if(count($definition_container) !== 0){
  1872. $this->fuckhtml->load($definition_container[0]);
  1873. // get header
  1874. $header =
  1875. $this->fuckhtml
  1876. ->getElementsByAttributeValue(
  1877. "data-attrid",
  1878. "EntryHeader",
  1879. "div"
  1880. );
  1881. if(count($header) !== 0){
  1882. $description = [];
  1883. $this->fuckhtml->load($header[0]);
  1884. $title_div =
  1885. $this->fuckhtml
  1886. ->getElementsByClassName(
  1887. $this->getstyle(
  1888. [
  1889. "font-family" => "Google Sans,arial,sans-serif",
  1890. "font-size" => "28px",
  1891. "line-height" => "36px"
  1892. ]
  1893. )
  1894. );
  1895. if(count($title_div) !== 0){
  1896. $title =
  1897. $this->fuckhtml
  1898. ->getTextContent(
  1899. $title_div[0]
  1900. );
  1901. }else{
  1902. $title = "Word definition";
  1903. }
  1904. $subtext_div =
  1905. $this->fuckhtml
  1906. ->getElementsByClassName(
  1907. $this->getstyle(
  1908. [
  1909. "font-family" => "arial,sans-serif",
  1910. "font-size" => "14px",
  1911. "line-height" => "22px"
  1912. ]
  1913. ),
  1914. "span"
  1915. );
  1916. if(count($subtext_div) !== 0){
  1917. $description[] = [
  1918. "type" => "quote",
  1919. "value" =>
  1920. $this->fuckhtml
  1921. ->getTextContent(
  1922. $subtext_div[0]
  1923. )
  1924. ];
  1925. }
  1926. // get audio
  1927. $audio =
  1928. $this->fuckhtml
  1929. ->getElementsByTagName(
  1930. "audio"
  1931. );
  1932. if(count($audio) !== 0){
  1933. $this->fuckhtml->load($audio[0]);
  1934. $source =
  1935. $this->fuckhtml
  1936. ->getElementsByTagName(
  1937. "source"
  1938. );
  1939. if(count($source) !== 0){
  1940. $description[] = [
  1941. "type" => "audio",
  1942. "url" =>
  1943. preg_replace(
  1944. '/^\/\//',
  1945. "https://",
  1946. $this->fuckhtml
  1947. ->getTextContent(
  1948. $source[0]
  1949. ["attributes"]
  1950. ["src"]
  1951. )
  1952. )
  1953. ];
  1954. }
  1955. }
  1956. // remove header to avoid confusion
  1957. $definition_container[0]["innerHTML"] =
  1958. str_replace(
  1959. $header[0]["outerHTML"],
  1960. "",
  1961. $definition_container[0]["innerHTML"]
  1962. );
  1963. // reset
  1964. $this->fuckhtml->load($definition_container[0]);
  1965. $vmods =
  1966. $this->fuckhtml
  1967. ->getElementsByClassName(
  1968. "vmod",
  1969. "div"
  1970. );
  1971. foreach($vmods as $category){
  1972. if(
  1973. !isset(
  1974. $category
  1975. ["attributes"]
  1976. ["data-topic"]
  1977. ) ||
  1978. $category
  1979. ["attributes"]
  1980. ["class"] != "vmod"
  1981. ){
  1982. continue;
  1983. }
  1984. $this->fuckhtml->load($category);
  1985. // get category type
  1986. $type =
  1987. $this->fuckhtml
  1988. ->getElementsByTagName(
  1989. "i"
  1990. );
  1991. if(count($type) !== 0){
  1992. $description[] = [
  1993. "type" => "title",
  1994. "value" =>
  1995. $this->fuckhtml
  1996. ->getTextContent(
  1997. $type[0]
  1998. )
  1999. ];
  2000. }
  2001. // get heading text
  2002. $headings =
  2003. $this->fuckhtml
  2004. ->getElementsByClassName(
  2005. "xpdxpnd",
  2006. "div"
  2007. );
  2008. foreach($headings as $heading){
  2009. $description[] = [
  2010. "type" => "quote",
  2011. "value" =>
  2012. $this->fuckhtml
  2013. ->getTextContent(
  2014. $heading
  2015. )
  2016. ];
  2017. }
  2018. $definitions =
  2019. $this->fuckhtml
  2020. ->getElementsByAttributeValue(
  2021. "data-attrid",
  2022. "SenseDefinition",
  2023. "div"
  2024. );
  2025. $i = 1;
  2026. $text = [];
  2027. foreach($definitions as $definition){
  2028. $text[] =
  2029. $i . ". " .
  2030. $this->fuckhtml
  2031. ->getTextContent(
  2032. $definition
  2033. );
  2034. $i++;
  2035. }
  2036. if(count($text) !== 0){
  2037. $description[] = [
  2038. "type" => "text",
  2039. "value" =>
  2040. implode("\n", $text)
  2041. ];
  2042. }
  2043. }
  2044. $out["answer"][] = [
  2045. "title" => $title,
  2046. "description" => $description,
  2047. "url" => null,
  2048. "thumb" => null,
  2049. "table" => [],
  2050. "sublink" => []
  2051. ];
  2052. }
  2053. // reset
  2054. $this->fuckhtml->load($result_div);
  2055. }
  2056. //
  2057. // scrape elements with a g-section-with-header
  2058. // includes: images, news carousels
  2059. //
  2060. $g_sections =
  2061. $this->fuckhtml
  2062. ->getElementsByTagName(
  2063. "g-section-with-header"
  2064. );
  2065. if(count($g_sections) !== 0){
  2066. foreach($g_sections as $g_section){
  2067. // parse elements with a g-section-with-header
  2068. $this->fuckhtml->load($g_section);
  2069. $div_title =
  2070. $this->fuckhtml
  2071. ->getElementsByClassName(
  2072. "a-no-hover-decoration",
  2073. "a"
  2074. );
  2075. if(count($div_title) !== 0){
  2076. // title detected, skip
  2077. continue;
  2078. }
  2079. // no title detected: detect news container
  2080. $news =
  2081. $this->fuckhtml
  2082. ->getElementsByClassName(
  2083. $this->getstyle(
  2084. [
  2085. "outline-offset" => "-1px",
  2086. "display" => "flex",
  2087. "flex-direction" => "column",
  2088. "flex-grow" => "1"
  2089. ]
  2090. )
  2091. );
  2092. foreach($news as $new){
  2093. $this->fuckhtml->load($new);
  2094. $image =
  2095. $this->fuckhtml
  2096. ->getElementsByAttributeName(
  2097. "id",
  2098. "img"
  2099. );
  2100. if(
  2101. count($image) !== 0 &&
  2102. !(
  2103. isset($image[0]["attributes"]["style"]) &&
  2104. strpos(
  2105. $image[0]["attributes"]["style"],
  2106. "height:18px"
  2107. ) !== false
  2108. )
  2109. ){
  2110. $thumb = [
  2111. "url" =>
  2112. $this->getdimg(
  2113. $image[0]
  2114. ["attributes"]
  2115. ["id"]
  2116. ),
  2117. "ratio" => "1:1"
  2118. ];
  2119. }
  2120. $title =
  2121. $this->titledots(
  2122. $this->fuckhtml
  2123. ->getTextContent(
  2124. $this->fuckhtml
  2125. ->getElementsByAttributeValue(
  2126. "role",
  2127. "heading",
  2128. "div"
  2129. )[0]
  2130. )
  2131. );
  2132. $date_div =
  2133. $this->fuckhtml
  2134. ->getElementsByAttributeName(
  2135. "style",
  2136. "div"
  2137. );
  2138. if(count($date_div) !== 0){
  2139. foreach($date_div as $div){
  2140. if(
  2141. strpos(
  2142. $div["attributes"]["style"],
  2143. "bottom:"
  2144. ) !== false
  2145. ){
  2146. $date =
  2147. strtotime(
  2148. $this->fuckhtml
  2149. ->getTextContent(
  2150. $div
  2151. )
  2152. );
  2153. break;
  2154. }
  2155. }
  2156. }else{
  2157. $date = null;
  2158. }
  2159. $out["news"][] = [
  2160. "title" => $title,
  2161. "description" => null,
  2162. "date" => $date,
  2163. "thumb" => $thumb,
  2164. "url" =>
  2165. $this->fuckhtml
  2166. ->getTextContent(
  2167. $new
  2168. ["attributes"]
  2169. ["href"]
  2170. )
  2171. ];
  2172. }
  2173. }
  2174. // reset
  2175. $this->fuckhtml->load($result_div);
  2176. }
  2177. //
  2178. // Parse images (carousel, left hand-side)
  2179. //
  2180. $image_carousels =
  2181. $this->fuckhtml
  2182. ->getElementsByAttributeValue(
  2183. "id",
  2184. "media_result_group",
  2185. "div"
  2186. );
  2187. if(count($image_carousels) !== 0){
  2188. foreach($image_carousels as $image_carousel){
  2189. $this->fuckhtml->load($image_carousel);
  2190. // get related searches in image carousel
  2191. $relateds =
  2192. $this->fuckhtml
  2193. ->getElementsByClassName(
  2194. $this->getstyle(
  2195. [
  2196. "display" => "inline-block",
  2197. "margin-right" => "6px",
  2198. "outline" => "none",
  2199. "padding" => "6px 0"
  2200. ],
  2201. "a"
  2202. )
  2203. );
  2204. foreach($relateds as $related){
  2205. $text =
  2206. $this->fuckhtml
  2207. ->getTextContent(
  2208. $related
  2209. );
  2210. if($text != ""){
  2211. $out["related"][] = $text;
  2212. }
  2213. }
  2214. $div =
  2215. $this->fuckhtml
  2216. ->getElementsByTagName(
  2217. "div"
  2218. );
  2219. // get loaded images
  2220. $images =
  2221. $this->fuckhtml
  2222. ->getElementsByClassName(
  2223. "ivg-i",
  2224. $div
  2225. );
  2226. foreach($images as $image){
  2227. $this->fuckhtml->load($image);
  2228. $img_tags =
  2229. $this->fuckhtml
  2230. ->getElementsByTagName(
  2231. "img"
  2232. );
  2233. if(
  2234. !isset($image["attributes"]["data-docid"]) ||
  2235. !isset($this->image_arr[$image["attributes"]["data-docid"]])
  2236. ){
  2237. continue;
  2238. }
  2239. // search for the right image tag
  2240. $image_tag = false;
  2241. foreach($img_tags as $img){
  2242. if(
  2243. isset(
  2244. $img
  2245. ["attributes"]
  2246. ["alt"]
  2247. ) &&
  2248. trim(
  2249. $img
  2250. ["attributes"]
  2251. ["alt"]
  2252. ) != ""
  2253. ){
  2254. $image_tag = $img;
  2255. break;
  2256. }
  2257. }
  2258. if($image_tag === false){
  2259. continue;
  2260. }
  2261. $out["image"][] = [
  2262. "title" =>
  2263. $this->titledots(
  2264. $this->fuckhtml
  2265. ->getTextContent(
  2266. $image_tag
  2267. ["attributes"]
  2268. ["alt"]
  2269. )
  2270. ),
  2271. "source" =>
  2272. $this->image_arr[
  2273. $image
  2274. ["attributes"]
  2275. ["data-docid"]
  2276. ],
  2277. "url" =>
  2278. $this->fuckhtml
  2279. ->getTextContent(
  2280. $image
  2281. ["attributes"]
  2282. ["data-lpage"]
  2283. )
  2284. ];
  2285. }
  2286. // get unloaded javascript images
  2287. $images_js_sel =
  2288. $this->fuckhtml
  2289. ->getElementsByAttributeName(
  2290. "id",
  2291. $div
  2292. );
  2293. $loaded = [];
  2294. foreach($images_js_sel as $sel){
  2295. if(
  2296. !isset($this->blobs[$sel["attributes"]["id"]]) ||
  2297. in_array((string)$sel["attributes"]["id"], $loaded, true)
  2298. ){
  2299. // not an unloaded javascript image
  2300. continue;
  2301. }
  2302. $loaded[] = $sel["attributes"]["id"];
  2303. // get yet another javascript component
  2304. $this->fuckhtml->load($this->blobs[$sel["attributes"]["id"]]);
  2305. // get js node: contains title & url
  2306. $js_node =
  2307. $this->fuckhtml
  2308. ->getElementsByTagName(
  2309. "div"
  2310. )[0];
  2311. if(!isset($this->blobs[$js_node["attributes"]["id"]])){
  2312. // did not find refer id
  2313. continue;
  2314. }
  2315. // load second javascript component
  2316. $this->fuckhtml->load($this->blobs[$js_node["attributes"]["id"]]);
  2317. // get title from image alt text.
  2318. // data-src from this image is cropped, ignore it..
  2319. $img =
  2320. $this->fuckhtml
  2321. ->getElementsByTagName(
  2322. "img"
  2323. )[0];
  2324. $out["image"][] = [
  2325. "title" =>
  2326. $this->fuckhtml
  2327. ->getTextContent(
  2328. $img["attributes"]["alt"]
  2329. ),
  2330. "source" =>
  2331. $this->image_arr[
  2332. $js_node["attributes"]["data-docid"]
  2333. ],
  2334. "url" =>
  2335. $this->fuckhtml
  2336. ->getTextContent(
  2337. $js_node["attributes"]["data-lpage"]
  2338. )
  2339. ];
  2340. }
  2341. }
  2342. // reset
  2343. $this->fuckhtml->load($result_div);
  2344. }
  2345. //
  2346. // Parse videos
  2347. //
  2348. $this->fuckhtml->load($result_div);
  2349. $videos =
  2350. $this->fuckhtml
  2351. ->getElementsByAttributeName(
  2352. "data-vid",
  2353. "div"
  2354. );
  2355. foreach($videos as $video){
  2356. $this->fuckhtml->load($video);
  2357. // get url
  2358. $url =
  2359. $this->fuckhtml
  2360. ->getTextContent(
  2361. $video
  2362. ["attributes"]
  2363. ["data-surl"]
  2364. );
  2365. foreach($out["web"] as $link){
  2366. if($link["url"] == $url){
  2367. // ignore if we already have the video in $out["web"]
  2368. continue 2;
  2369. }
  2370. }
  2371. // get thumbnail
  2372. $image =
  2373. $this->fuckhtml
  2374. ->getElementsByAttributeName(
  2375. "id",
  2376. "img"
  2377. );
  2378. if(count($image) !== 0){
  2379. $thumb = [
  2380. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  2381. "ratio" => "16:9"
  2382. ];
  2383. }else{
  2384. $thumb = [
  2385. "url" => null,
  2386. "ratio" => null
  2387. ];
  2388. }
  2389. // get title
  2390. $title =
  2391. $this->fuckhtml
  2392. ->getElementsByClassName(
  2393. $this->getstyle(
  2394. [
  2395. "font-family" => "arial,sans-serif",
  2396. "font-size" => "16px",
  2397. "font-weight" => "400",
  2398. "line-height" => "24px"
  2399. ]
  2400. ),
  2401. "div"
  2402. );
  2403. if(count($title) === 0){
  2404. // ?? no title
  2405. continue;
  2406. }
  2407. $title =
  2408. $this->titledots(
  2409. $this->fuckhtml
  2410. ->getTextContent(
  2411. $title[0]
  2412. )
  2413. );
  2414. // get duration
  2415. $duration_div =
  2416. $this->fuckhtml
  2417. ->getElementsByClassName(
  2418. $this->getstyle(
  2419. [
  2420. "border-radius" => "10px",
  2421. "font-family" => "arial,sans-serif-medium,sans-serif",
  2422. "font-size" => "12px",
  2423. "line-height" => "16px",
  2424. "padding-block" => "2px",
  2425. "padding-inline" => "8px"
  2426. ]
  2427. ),
  2428. "div"
  2429. );
  2430. if(count($duration_div) !== 0){
  2431. $duration =
  2432. $this->hms2int(
  2433. $this->fuckhtml
  2434. ->getTextContent(
  2435. $duration_div[0]
  2436. )
  2437. );
  2438. }else{
  2439. // check if its a livestream
  2440. $duration =
  2441. $this->fuckhtml
  2442. ->getElementsByClassName(
  2443. $this->getstyle(
  2444. [
  2445. "background-color" => "#d93025",
  2446. "border-radius" => "10px",
  2447. "color" => "#fff",
  2448. "font-family" => "arial,sans-serif-medium,sans-serif",
  2449. "font-size" => "12px",
  2450. "line-height" => "16px",
  2451. "padding-block" => "2px",
  2452. "padding-inline" => "8px"
  2453. ]
  2454. ),
  2455. "span"
  2456. );
  2457. if(count($duration) !== 0){
  2458. $duration = "_LIVE";
  2459. }else{
  2460. $duration = null;
  2461. }
  2462. }
  2463. // get date
  2464. $date_div =
  2465. $this->fuckhtml
  2466. ->getElementsByClassName(
  2467. $this->getstyle(
  2468. [
  2469. "color" => "var(" . $this->getcolorvar("#70757a") . ")",
  2470. "font-size" => "14px"
  2471. ]
  2472. ),
  2473. "div"
  2474. );
  2475. if(count($date_div) !== 0){
  2476. $date = strtotime(
  2477. $this->fuckhtml
  2478. ->getTextContent(
  2479. $date_div[0]
  2480. )
  2481. );
  2482. if($date === false){
  2483. // failed to parse date
  2484. $date = null;
  2485. }
  2486. }else{
  2487. $date = null;
  2488. }
  2489. $out["video"][] = [
  2490. "title" => $title,
  2491. "description" => null,
  2492. "date" => $date,
  2493. "duration" => $duration,
  2494. "views" => null,
  2495. "thumb" => $thumb,
  2496. "url" => $url
  2497. ];
  2498. }
  2499. //
  2500. // Parse featured results (which contain images, fuck the rest desu)
  2501. //
  2502. $this->fuckhtml->load($html);
  2503. $top =
  2504. $this->fuckhtml
  2505. ->getElementsByAttributeValue(
  2506. "aria-label",
  2507. "Featured results",
  2508. "div"
  2509. );
  2510. if(count($top) !== 0){
  2511. $this->fuckhtml->load($top[0]);
  2512. // get images
  2513. $grid =
  2514. $this->fuckhtml
  2515. ->getElementsByClassName(
  2516. $this->getstyle(
  2517. [
  2518. "border-radius" => "20px",
  2519. "display" => "grid",
  2520. "grid-gap" => "2px",
  2521. "grid-template-rows" => "repeat(2,minmax(0,1fr))",
  2522. "overflow" => "hidden",
  2523. "bottom" => "0",
  2524. "left" => "0",
  2525. "right" => "0",
  2526. "top" => "0",
  2527. "position" => "absolute",
  2528. ]
  2529. ),
  2530. "div"
  2531. );
  2532. if(count($grid) !== 0){
  2533. // we found image grid
  2534. $this->fuckhtml->load($grid[0]);
  2535. $images_div =
  2536. $this->fuckhtml
  2537. ->getElementsByAttributeName(
  2538. "data-attrid",
  2539. "div"
  2540. );
  2541. foreach($images_div as $image_div){
  2542. $this->fuckhtml->load($image_div);
  2543. $image =
  2544. $this->fuckhtml
  2545. ->getElementsByTagName(
  2546. "img"
  2547. );
  2548. if(
  2549. count($image) === 0 ||
  2550. !isset($image_div["attributes"]["data-docid"]) ||
  2551. !isset($this->image_arr[$image_div["attributes"]["data-docid"]])
  2552. ){
  2553. // ?? no image, continue
  2554. continue;
  2555. }
  2556. $out["image"][] = [
  2557. "title" =>
  2558. $this->titledots(
  2559. $this->fuckhtml
  2560. ->getTextContent(
  2561. $image[0]["attributes"]["alt"]
  2562. )
  2563. ),
  2564. "source" =>
  2565. $this->image_arr[
  2566. $image_div["attributes"]["data-docid"]
  2567. ],
  2568. "url" =>
  2569. $this->fuckhtml
  2570. ->getTextContent(
  2571. $image_div["attributes"]["data-lpage"]
  2572. )
  2573. ];
  2574. }
  2575. }
  2576. }
  2577. //
  2578. // craft $npt token
  2579. //
  2580. if(
  2581. $last_page === false &&
  2582. count($out["web"]) !== 0
  2583. ){
  2584. if(!isset($params["start"])){
  2585. $params["start"] = 20;
  2586. }else{
  2587. $params["start"] += 20;
  2588. }
  2589. $out["npt"] =
  2590. $this->backend
  2591. ->store(
  2592. json_encode($params),
  2593. $pagetype,
  2594. $proxy
  2595. );
  2596. }
  2597. //
  2598. // Parse right handside
  2599. //
  2600. $this->fuckhtml->load($html);
  2601. $rhs =
  2602. $this->fuckhtml
  2603. ->getElementById(
  2604. "rhs"
  2605. );
  2606. if($rhs === null){
  2607. return $out;
  2608. }
  2609. $this->fuckhtml->load($rhs);
  2610. // get images gallery
  2611. $image_gallery =
  2612. $this->fuckhtml
  2613. ->getElementsByAttributeValue(
  2614. "data-rc",
  2615. "ivg-i",
  2616. "div"
  2617. );
  2618. if(count($image_gallery) !== 0){
  2619. $this->fuckhtml->load($image_gallery[0]);
  2620. // get images
  2621. $images_div =
  2622. $this->fuckhtml
  2623. ->getElementsByClassName(
  2624. "ivg-i",
  2625. "div"
  2626. );
  2627. foreach($images_div as $image_div){
  2628. $this->fuckhtml->load($image_div);
  2629. $image =
  2630. $this->fuckhtml
  2631. ->getElementsByTagName(
  2632. "img"
  2633. );
  2634. if(
  2635. count($image) === 0 ||
  2636. !isset(
  2637. $this->image_arr[
  2638. $image_div
  2639. ["attributes"]
  2640. ["data-docid"]
  2641. ]
  2642. )
  2643. ){
  2644. continue;
  2645. }
  2646. foreach($out["image"] as $existing_image){
  2647. // might already exist
  2648. if(
  2649. $existing_image["source"][1]["url"] ==
  2650. $this->image_arr[
  2651. $image_div
  2652. ["attributes"]
  2653. ["data-docid"]
  2654. ][1]["url"]
  2655. ){
  2656. continue 2;
  2657. }
  2658. }
  2659. $out["image"][] = [
  2660. "title" =>
  2661. $this->titledots(
  2662. $this->fuckhtml
  2663. ->getTextContent(
  2664. $image[0]
  2665. ["attributes"]
  2666. ["alt"]
  2667. )
  2668. ),
  2669. "source" =>
  2670. $this->image_arr[
  2671. $image_div
  2672. ["attributes"]
  2673. ["data-docid"]
  2674. ],
  2675. "url" =>
  2676. $this->fuckhtml
  2677. ->getTextContent(
  2678. $image_div
  2679. ["attributes"]
  2680. ["data-lpage"]
  2681. )
  2682. ];
  2683. }
  2684. // reset
  2685. $this->fuckhtml->load($rhs);
  2686. }
  2687. // get header container
  2688. $header =
  2689. $this->fuckhtml
  2690. ->getElementsByClassName(
  2691. $this->getstyle(
  2692. [
  2693. "padding" => "0 0 16px 20px",
  2694. "display" => "flex"
  2695. ]
  2696. ),
  2697. "div"
  2698. );
  2699. // stop parsing wikipedia heads if there isn't a header
  2700. $description = [];
  2701. $title = "About";
  2702. if(count($header) !== 0){
  2703. $this->fuckhtml->load($header[0]);
  2704. $title_tag =
  2705. $this->fuckhtml
  2706. ->getElementsByAttributeValue(
  2707. "data-attrid",
  2708. "title",
  2709. "div"
  2710. );
  2711. if(count($title_tag) !== 0){
  2712. $title =
  2713. $this->fuckhtml
  2714. ->getTextContent(
  2715. $title_tag[0]
  2716. );
  2717. $header[0]["innerHTML"] =
  2718. str_replace(
  2719. $title_tag[0]["outerHTML"],
  2720. "",
  2721. $header[0]["innerHTML"]
  2722. );
  2723. // if header still contains text, add it as a subtitle in description
  2724. $subtitle =
  2725. $this->fuckhtml
  2726. ->getTextContent(
  2727. $header[0]
  2728. );
  2729. if(strlen($subtitle) !== 0){
  2730. $description[] = [
  2731. "type" => "quote",
  2732. "value" => $subtitle
  2733. ];
  2734. }
  2735. }
  2736. // reset
  2737. $this->fuckhtml->load($rhs);
  2738. }
  2739. // get description elements
  2740. $url = null;
  2741. $text =
  2742. $this->fuckhtml
  2743. ->getElementsByAttributeValue(
  2744. "data-attrid",
  2745. "description",
  2746. "div"
  2747. );
  2748. if(count($text) !== 0){
  2749. $this->fuckhtml->load($text[0]);
  2750. $a =
  2751. $this->fuckhtml
  2752. ->getElementsByTagName(
  2753. "a"
  2754. );
  2755. if(count($a) !== 0){
  2756. // get link and remove it from description
  2757. $a = $a[count($a) - 1];
  2758. $text[0]["innerHTML"] =
  2759. str_replace(
  2760. $a["outerHTML"],
  2761. "",
  2762. $text[0]["innerHTML"]
  2763. );
  2764. $url =
  2765. $this->fuckhtml
  2766. ->getTextContent(
  2767. $a
  2768. ["attributes"]
  2769. ["href"]
  2770. );
  2771. }
  2772. $description[] = [
  2773. "type" => "text",
  2774. "value" =>
  2775. html_entity_decode(
  2776. preg_replace(
  2777. '/^Description/',
  2778. "",
  2779. $this->fuckhtml
  2780. ->getTextContent(
  2781. $text[0]
  2782. )
  2783. )
  2784. )
  2785. ];
  2786. // reset
  2787. $this->fuckhtml->load($rhs);
  2788. }
  2789. // get reviews (google play, steam, etc)
  2790. $review_container =
  2791. $this->fuckhtml
  2792. ->getElementsByClassName(
  2793. $this->getstyle(
  2794. [
  2795. "align-items" => "start",
  2796. "display" => "flex"
  2797. ]
  2798. ),
  2799. "div"
  2800. );
  2801. if(count($review_container) !== 0){
  2802. $this->fuckhtml->load($review_container[0]);
  2803. $as =
  2804. $this->fuckhtml
  2805. ->getElementsByTagName(
  2806. "a"
  2807. );
  2808. if(count($as) !== 0){
  2809. $description[] = [
  2810. "type" => "title",
  2811. "value" => "Ratings"
  2812. ];
  2813. foreach($as as $a){
  2814. $this->fuckhtml->load($a);
  2815. $spans =
  2816. $this->fuckhtml
  2817. ->getElementsByTagName(
  2818. "span"
  2819. );
  2820. if(count($spans) >= 2){
  2821. $value =
  2822. trim(
  2823. $this->fuckhtml
  2824. ->getTextContent(
  2825. $spans[1]
  2826. ),
  2827. "· "
  2828. );
  2829. if(
  2830. $value == "" &&
  2831. isset($spans[2])
  2832. ){
  2833. $value =
  2834. $this->fuckhtml
  2835. ->getTextContent(
  2836. $spans[2]
  2837. );
  2838. }
  2839. $description[] = [
  2840. "type" => "link",
  2841. "url" =>
  2842. $this->fuckhtml
  2843. ->getTextContent(
  2844. $a["attributes"]
  2845. ["href"]
  2846. ),
  2847. "value" => $value
  2848. ];
  2849. $description[] = [
  2850. "type" => "text",
  2851. "value" =>
  2852. ": " .
  2853. $this->fuckhtml
  2854. ->getTextContent(
  2855. $spans[0]
  2856. ) . "\n"
  2857. ];
  2858. }
  2859. }
  2860. }
  2861. // reset
  2862. $this->fuckhtml->load($rhs);
  2863. }
  2864. // abort if we didnt find any description
  2865. if(count($description) === 0){
  2866. return $out;
  2867. }
  2868. // get table elements
  2869. $table = [];
  2870. $table_elems =
  2871. $this->fuckhtml
  2872. ->getElementsByClassName(
  2873. $this->getstyle(
  2874. [
  2875. "margin-top" => "7px"
  2876. ]
  2877. ),
  2878. "div"
  2879. );
  2880. foreach($table_elems as $elem){
  2881. $this->fuckhtml->load($elem);
  2882. $spans =
  2883. $this->fuckhtml
  2884. ->getElementsByTagName(
  2885. "span"
  2886. );
  2887. if(count($spans) === 0){
  2888. // ?? invalid
  2889. continue;
  2890. }
  2891. $elem["innerHTML"] =
  2892. str_replace(
  2893. $spans[0]["outerHTML"],
  2894. "",
  2895. $elem["innerHTML"]
  2896. );
  2897. $key =
  2898. rtrim(
  2899. $this->fuckhtml
  2900. ->getTextContent(
  2901. $spans[0]
  2902. ),
  2903. ": "
  2904. );
  2905. if($key == ""){
  2906. continue;
  2907. }
  2908. $table[$key] =
  2909. preg_replace(
  2910. '/ +/',
  2911. " ",
  2912. $this->fuckhtml
  2913. ->getTextContent(
  2914. $elem
  2915. )
  2916. );
  2917. // reset
  2918. $this->fuckhtml->load($rhs);
  2919. }
  2920. // get sublink elements
  2921. $sublinks = [];
  2922. // get the website div
  2923. $as =
  2924. $this->fuckhtml
  2925. ->getElementsByAttributeValue(
  2926. "data-attrid",
  2927. "visit_official_site",
  2928. "a"
  2929. );
  2930. if(count($as) !== 0){
  2931. $sublinks["Website"] =
  2932. str_replace(
  2933. "http://",
  2934. "https://",
  2935. $this->fuckhtml
  2936. ->getTextContent(
  2937. $as[0]
  2938. ["attributes"]
  2939. ["href"]
  2940. )
  2941. );
  2942. }
  2943. // get social media links
  2944. $as =
  2945. $this->fuckhtml
  2946. ->getElementsByTagName(
  2947. "g-link"
  2948. );
  2949. foreach($as as $a){
  2950. $this->fuckhtml->load($a);
  2951. $link =
  2952. $this->fuckhtml
  2953. ->getElementsByTagName(
  2954. "a"
  2955. );
  2956. if(count($link) === 0){
  2957. continue;
  2958. }
  2959. $sublink_title =
  2960. $this->fuckhtml
  2961. ->getTextContent(
  2962. $a
  2963. );
  2964. if($sublink_title == "X (Twitter)"){
  2965. $sublink_title = "Twitter";
  2966. }
  2967. $sublinks[$sublink_title] =
  2968. $this->fuckhtml
  2969. ->getTextContent(
  2970. $link[0]
  2971. ["attributes"]
  2972. ["href"]
  2973. );
  2974. }
  2975. // reset
  2976. $this->fuckhtml->load($rhs);
  2977. // get those round containers
  2978. $containers =
  2979. $this->fuckhtml
  2980. ->getElementsByClassName(
  2981. "tpa-ci"
  2982. );
  2983. foreach($containers as $container){
  2984. $this->fuckhtml->load($container);
  2985. $as =
  2986. $this->fuckhtml
  2987. ->getElementsByTagName(
  2988. "a"
  2989. );
  2990. if(count($as) === 0){
  2991. continue;
  2992. }
  2993. $sublinks[
  2994. $this->fuckhtml
  2995. ->getTextContent(
  2996. $as[0]
  2997. )
  2998. ] =
  2999. $this->fuckhtml
  3000. ->getTextContent(
  3001. $as[0]
  3002. ["attributes"]
  3003. ["href"]
  3004. );
  3005. }
  3006. $out["answer"][] = [
  3007. "title" => $title,
  3008. "description" => $description,
  3009. "url" => $url,
  3010. "thumb" => null,
  3011. "table" => $table,
  3012. "sublink" => $sublinks
  3013. ];
  3014. return $out;
  3015. }
  3016. private function scrape_dimg($html){
  3017. // get images loaded through javascript
  3018. $this->dimg = [];
  3019. preg_match_all(
  3020. '/function\(\){google\.ldi=({.*?});/',
  3021. $html,
  3022. $dimg
  3023. );
  3024. if(isset($dimg[1])){
  3025. foreach($dimg[1] as $i){
  3026. $tmp = json_decode($i, true);
  3027. foreach($tmp as $key => $value){
  3028. $this->dimg[$key] =
  3029. $this->unshit_thumb(
  3030. $value
  3031. );
  3032. }
  3033. }
  3034. }
  3035. // get additional javascript base64 images
  3036. preg_match_all(
  3037. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  3038. $html,
  3039. $dimg
  3040. );
  3041. if(isset($dimg[1])){
  3042. for($i=0; $i<count($dimg[1]); $i++){
  3043. $delims = explode(",", $dimg[2][$i]);
  3044. $string =
  3045. $this->fuckhtml
  3046. ->parseJsString(
  3047. $dimg[1][$i]
  3048. );
  3049. foreach($delims as $delim){
  3050. $this->dimg[trim($delim, "'")] = $string;
  3051. }
  3052. }
  3053. }
  3054. }
  3055. private function scrape_imagearr($html){
  3056. // get image links arrays
  3057. preg_match_all(
  3058. '/\[0,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  3059. $html,
  3060. $image_arr
  3061. );
  3062. $this->image_arr = [];
  3063. if(isset($image_arr[1])){
  3064. for($i=0; $i<count($image_arr[1]); $i++){
  3065. $this->image_arr[$image_arr[1][$i]] =
  3066. [
  3067. [
  3068. "url" =>
  3069. $this->fuckhtml
  3070. ->parseJsString(
  3071. $image_arr[5][$i]
  3072. ),
  3073. "width" => (int)$image_arr[7][$i],
  3074. "height" => (int)$image_arr[6][$i]
  3075. ],
  3076. [
  3077. "url" =>
  3078. $this->unshit_thumb(
  3079. $this->fuckhtml
  3080. ->parseJsString(
  3081. $image_arr[2][$i]
  3082. )
  3083. ),
  3084. "width" => (int)$image_arr[4][$i],
  3085. "height" => (int)$image_arr[3][$i]
  3086. ]
  3087. ];
  3088. }
  3089. }
  3090. }
  3091. private function getdimg($dimg){
  3092. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  3093. }
  3094. private function unshit_thumb($url){
  3095. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  3096. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  3097. $parts = parse_url($url);
  3098. if(
  3099. isset($parts["host"]) &&
  3100. preg_match(
  3101. '/tbn.*\.gstatic\.com/',
  3102. $parts["host"]
  3103. )
  3104. ){
  3105. parse_str($parts["query"], $params);
  3106. if(isset($params["q"])){
  3107. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  3108. }
  3109. }
  3110. return $url;
  3111. }
  3112. private function parsestyles(){
  3113. $styles = [];
  3114. $style_div =
  3115. $this->fuckhtml
  3116. ->getElementsByTagName(
  3117. "style"
  3118. );
  3119. $raw_styles = "";
  3120. foreach($style_div as $style){
  3121. $raw_styles .= $style["innerHTML"];
  3122. }
  3123. // filter out media/keyframe queries
  3124. $raw_styles =
  3125. preg_replace(
  3126. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  3127. "",
  3128. $raw_styles
  3129. );
  3130. // get styles
  3131. preg_match_all(
  3132. '/(.+?){([\S\s]*?)}/',
  3133. $raw_styles,
  3134. $matches
  3135. );
  3136. for($i=0; $i<count($matches[1]); $i++){
  3137. // get style values
  3138. preg_match_all(
  3139. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  3140. $matches[2][$i],
  3141. $values_regex
  3142. );
  3143. $values = [];
  3144. for($k=0; $k<count($values_regex[1]); $k++){
  3145. $values[trim($values_regex[1][$k])] =
  3146. trim($values_regex[2][$k]);
  3147. }
  3148. $names = explode(",", $matches[1][$i]);
  3149. // h1,h2,h3 will each get their own array index
  3150. foreach($names as $name){
  3151. $name = trim($name, "}\t\n\r\0\x0B");
  3152. foreach($values as $key => $value){
  3153. $styles[$name][$key] = $value;
  3154. }
  3155. }
  3156. }
  3157. foreach($styles as $key => $values){
  3158. $styles[$key]["_c"] = count($values);
  3159. }
  3160. $this->styles = $styles;
  3161. // get CSS colors
  3162. $this->css_colors = [];
  3163. if(isset($this->styles[":root"])){
  3164. foreach($this->styles[":root"] as $key => $value){
  3165. $this->css_colors[$value] = $key;
  3166. }
  3167. }
  3168. }
  3169. private function getstyle($styles){
  3170. $styles["_c"] = count($styles);
  3171. foreach($this->styles as $style_key => $style_values){
  3172. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  3173. $style_key =
  3174. explode(" ", $style_key);
  3175. $style_key = $style_key[count($style_key) - 1];
  3176. return
  3177. ltrim(
  3178. str_replace(
  3179. [".", "#"],
  3180. " ",
  3181. $style_key
  3182. )
  3183. );
  3184. }
  3185. }
  3186. return false;
  3187. }
  3188. private function getcolorvar($color){
  3189. if(isset($this->css_colors[$color])){
  3190. return $this->css_colors[$color];
  3191. }
  3192. return null;
  3193. }
  3194. public function web($get){
  3195. if($get["npt"]){
  3196. [$params, $proxy] = $this->backend->get($get["npt"], "web");
  3197. $params = json_decode($params, true);
  3198. $search = $params["q"];
  3199. }else{
  3200. $search = $get["s"];
  3201. $country = $get["country"];
  3202. $nsfw = $get["nsfw"];
  3203. $lang = $get["lang"];
  3204. $older = $get["older"];
  3205. $newer = $get["newer"];
  3206. $spellcheck = $get["spellcheck"];
  3207. $proxy = $this->backend->get_ip();
  3208. $offset = 0;
  3209. $params = [
  3210. "q" => $search,
  3211. "hl" => "en",
  3212. "num" => 20 // get 20 results
  3213. ];
  3214. // country
  3215. if($country != "any"){
  3216. $params["gl"] = $country;
  3217. }
  3218. // nsfw
  3219. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  3220. // language
  3221. if($lang != "any"){
  3222. $params["lr"] = "lang_" . $lang;
  3223. }
  3224. // generate tbs
  3225. $tbs = [];
  3226. // get date
  3227. $older = $older === false ? null : date("m/d/Y", $older);
  3228. $newer = $newer === false ? null : date("m/d/Y", $newer);
  3229. if(
  3230. $older !== null ||
  3231. $newer !== null
  3232. ){
  3233. $tbs["cdr"] = "1";
  3234. $tbs["cd_min"] = $newer;
  3235. $tbs["cd_max"] = $older;
  3236. }
  3237. // spellcheck filter
  3238. if($spellcheck == "no"){
  3239. $params["nfpr"] = "1";
  3240. }
  3241. if(count($tbs) !== 0){
  3242. $params["tbs"] = "";
  3243. foreach($tbs as $key => $value){
  3244. $params["tbs"] .= $key . ":" . $value . ",";
  3245. }
  3246. $params["tbs"] = rtrim($params["tbs"], ",");
  3247. }
  3248. }
  3249. try{
  3250. $html =
  3251. $this->get(
  3252. $proxy,
  3253. "https://www.google.com/search",
  3254. $params
  3255. );
  3256. }catch(Exception $error){
  3257. throw new Exception("Failed to get HTML");
  3258. }
  3259. //$html = file_get_contents("scraper/google.html");
  3260. return $this->parsepage($html, "web", $search, $proxy, $params);
  3261. }
  3262. public function video($get){
  3263. if($get["npt"]){
  3264. [$params, $proxy] = $this->backend->get($get["npt"], "web");
  3265. $params = json_decode($params, true);
  3266. $search = $params["q"];
  3267. }else{
  3268. $search = $get["s"];
  3269. $country = $get["country"];
  3270. $nsfw = $get["nsfw"];
  3271. $older = $get["older"];
  3272. $newer = $get["newer"];
  3273. $duration = $get["duration"];
  3274. $quality = $get["quality"];
  3275. $captions = $get["captions"];
  3276. $proxy = $this->backend->get_ip();
  3277. $params = [
  3278. "q" => $search,
  3279. "tbm" => "vid",
  3280. "hl" => "en",
  3281. "num" => "20"
  3282. ];
  3283. // country
  3284. if($country != "any"){
  3285. $params["gl"] = $country;
  3286. }
  3287. // nsfw
  3288. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  3289. $tbs = [];
  3290. // get date
  3291. $older = $older === false ? null : date("m/d/Y", $older);
  3292. $newer = $newer === false ? null : date("m/d/Y", $newer);
  3293. if(
  3294. $older !== null ||
  3295. $newer !== null
  3296. ){
  3297. $tbs["cdr"] = "1";
  3298. $tbs["cd_min"] = $newer;
  3299. $tbs["cd_max"] = $older;
  3300. }
  3301. // duration
  3302. if($duration != "any"){
  3303. $tbs[] = "dur:" . $duration;
  3304. }
  3305. // quality
  3306. if($quality != "any"){
  3307. $tbs[] = "hq:" . $quality;
  3308. }
  3309. // captions
  3310. if($captions != "any"){
  3311. $tbs[] = "cc:" . $captions;
  3312. }
  3313. // append tbs
  3314. if(count($tbs) !== 0){
  3315. $params["tbs"] =
  3316. implode(",", $tbs);
  3317. }
  3318. }
  3319. try{
  3320. $html =
  3321. $this->get(
  3322. $proxy,
  3323. "https://www.google.com/search",
  3324. $params
  3325. );
  3326. }catch(Exception $error){
  3327. throw new Exception("Failed to get HTML");
  3328. }
  3329. //$html = file_get_contents("scraper/google-video.html");
  3330. $response = $this->parsepage($html, "videos", $search, $proxy, $params);
  3331. $out = [
  3332. "status" => "ok",
  3333. "npt" => $response["npt"],
  3334. "video" => [],
  3335. "author" => [],
  3336. "livestream" => [],
  3337. "playlist" => [],
  3338. "reel" => []
  3339. ];
  3340. foreach($response["web"] as $result){
  3341. $out["video"][] = [
  3342. "title" => $result["title"],
  3343. "description" => $result["description"],
  3344. "author" => [
  3345. "name" => isset($result["table"]["Author"]) ? $result["table"]["Author"] : null,
  3346. "url" => null,
  3347. "avatar" => null
  3348. ],
  3349. "date" => $result["date"],
  3350. "duration" => isset($result["table"]["Duration"]) ? $this->hms2int($result["table"]["Duration"]) : null,
  3351. "views" => null,
  3352. "thumb" => $result["thumb"],
  3353. "url" => $result["url"]
  3354. ];
  3355. }
  3356. return $out;
  3357. }
  3358. public function news($get){
  3359. if($get["npt"]){
  3360. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  3361. /*parse_str(
  3362. parse_url($req, PHP_URL_QUERY),
  3363. $search
  3364. );*/
  3365. try{
  3366. $html =
  3367. $this->get(
  3368. $proxy,
  3369. "https://www.google.com" . $req,
  3370. []
  3371. );
  3372. }catch(Exception $error){
  3373. throw new Exception("Failed to get HTML");
  3374. }
  3375. }else{
  3376. $search = $get["s"];
  3377. $country = $get["country"];
  3378. $nsfw = $get["nsfw"];
  3379. $older = $get["older"];
  3380. $newer = $get["newer"];
  3381. $sort = $get["sort"];
  3382. $proxy = $this->backend->get_ip();
  3383. $params = [
  3384. "q" => $search,
  3385. "tbm" => "nws",
  3386. "hl" => "en",
  3387. "num" => "20"
  3388. ];
  3389. // country
  3390. if($country != "any"){
  3391. $params["gl"] = $country;
  3392. }
  3393. // nsfw
  3394. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  3395. $tbs = [];
  3396. // get date
  3397. $older = $older === false ? null : date("m/d/Y", $older);
  3398. $newer = $newer === false ? null : date("m/d/Y", $newer);
  3399. if(
  3400. $older !== null ||
  3401. $newer !== null
  3402. ){
  3403. $tbs["cdr"] = "1";
  3404. $tbs["cd_min"] = $newer;
  3405. $tbs["cd_max"] = $older;
  3406. }
  3407. // relevance
  3408. if($sort == "date"){
  3409. $tbs["sbd"] = "1";
  3410. }
  3411. // append tbs
  3412. if(count($tbs) !== 0){
  3413. $params["tbs"] = "";
  3414. foreach($tbs as $key => $value){
  3415. $params["tbs"] .= $key . ":" . $value . ",";
  3416. }
  3417. $params["tbs"] = rtrim($params["tbs"], ",");
  3418. }
  3419. //$html = file_get_contents("scraper/google-news.html");
  3420. $html =
  3421. $this->get(
  3422. $proxy,
  3423. "https://www.google.com/search",
  3424. $params
  3425. );
  3426. }
  3427. $out = [
  3428. "status" => "ok",
  3429. "npt" => null,
  3430. "news" => []
  3431. ];
  3432. $this->fuckhtml->load($html);
  3433. $this->detect_sorry();
  3434. // get images
  3435. $this->scrape_dimg($html);
  3436. // parse styles
  3437. $this->parsestyles();
  3438. $center_col =
  3439. $this->fuckhtml
  3440. ->getElementById(
  3441. "center_col",
  3442. "div"
  3443. );
  3444. if($center_col === null){
  3445. throw new Exception("Could not grep result div");
  3446. }
  3447. $this->fuckhtml->load($center_col);
  3448. // get next page
  3449. $npt =
  3450. $this->fuckhtml
  3451. ->getElementById(
  3452. "pnnext",
  3453. "a"
  3454. );
  3455. if($npt !== false){
  3456. $out["npt"] =
  3457. $this->backend->store(
  3458. $this->fuckhtml
  3459. ->getTextContent(
  3460. $npt["attributes"]
  3461. ["href"]
  3462. ),
  3463. "news",
  3464. $proxy
  3465. );
  3466. }
  3467. $as =
  3468. $this->fuckhtml
  3469. ->getElementsByAttributeName(
  3470. "jsname",
  3471. "a"
  3472. );
  3473. foreach($as as $a){
  3474. $this->fuckhtml->load($a);
  3475. // get title
  3476. $title =
  3477. $this->fuckhtml
  3478. ->getElementsByAttributeValue(
  3479. "role",
  3480. "heading",
  3481. "div"
  3482. );
  3483. if(count($title) === 0){
  3484. continue;
  3485. }
  3486. $title =
  3487. $this->titledots(
  3488. $this->fuckhtml
  3489. ->getTextContent(
  3490. $title[0]
  3491. )
  3492. );
  3493. // get thumbnail
  3494. $image =
  3495. $this->fuckhtml
  3496. ->getElementsByAttributeName(
  3497. "id",
  3498. "img"
  3499. );
  3500. // check for padded title node, if found, we're inside a carousel
  3501. $probe =
  3502. $this->fuckhtml
  3503. ->getElementsByClassName(
  3504. $this->getstyle(
  3505. [
  3506. "padding" => "16px 16px 40px 16px"
  3507. ]
  3508. ),
  3509. "div"
  3510. );
  3511. if(count($probe) !== 0){
  3512. $probe = true;
  3513. }else{
  3514. $probe = false;
  3515. }
  3516. if(
  3517. count($image) !== 0 &&
  3518. !isset($image[0]["attributes"]["width"])
  3519. ){
  3520. $thumb = [
  3521. "url" =>
  3522. $this->getdimg(
  3523. $image[0]["attributes"]["id"]
  3524. ),
  3525. "ratio" => $probe === true ? "16:9" : "1:1"
  3526. ];
  3527. }else{
  3528. $thumb = [
  3529. "url" => null,
  3530. "ratio" => null
  3531. ];
  3532. }
  3533. $description = null;
  3534. if($probe === false){
  3535. $desc_divs =
  3536. $this->fuckhtml
  3537. ->getElementsByAttributeName(
  3538. "style",
  3539. "div"
  3540. );
  3541. foreach($desc_divs as $desc){
  3542. if(
  3543. strpos(
  3544. $desc["attributes"]["style"],
  3545. "margin-top:"
  3546. ) !== false
  3547. ){
  3548. $description =
  3549. $this->titledots(
  3550. $this->fuckhtml
  3551. ->getTextContent(
  3552. $desc
  3553. )
  3554. );
  3555. break;
  3556. }
  3557. }
  3558. }
  3559. // get author
  3560. $author =
  3561. $this->fuckhtml
  3562. ->getElementsByClassName(
  3563. $this->getstyle(
  3564. [
  3565. "overflow" => "hidden",
  3566. "text-align" => "left",
  3567. "text-overflow" => "ellipsis",
  3568. "white-space" => "nowrap",
  3569. "margin-bottom" => "8px"
  3570. ]
  3571. ),
  3572. "div"
  3573. );
  3574. if(count($author) !== 0){
  3575. $author =
  3576. $this->fuckhtml
  3577. ->getTextContent(
  3578. $author[0]
  3579. );
  3580. }else{
  3581. $author = null;
  3582. }
  3583. // get date
  3584. $date = null;
  3585. $date_div =
  3586. $this->fuckhtml
  3587. ->getElementsByAttributeName(
  3588. "style",
  3589. "div"
  3590. );
  3591. foreach($date_div as $d){
  3592. $this->fuckhtml->load($d);
  3593. $span =
  3594. $this->fuckhtml
  3595. ->getElementsByTagName(
  3596. "span"
  3597. );
  3598. if(
  3599. strpos(
  3600. $d["attributes"]["style"],
  3601. "bottom:"
  3602. ) !== false
  3603. ){
  3604. $date =
  3605. strtotime(
  3606. $this->fuckhtml
  3607. ->getTextContent(
  3608. $span[count($span) - 1]
  3609. )
  3610. );
  3611. break;
  3612. }
  3613. }
  3614. $out["news"][] = [
  3615. "title" => $title,
  3616. "author" => $author,
  3617. "description" => $description,
  3618. "date" => $date,
  3619. "thumb" => $thumb,
  3620. "url" =>
  3621. $this->unshiturl(
  3622. $a["attributes"]
  3623. ["href"]
  3624. )
  3625. ];
  3626. }
  3627. return $out;
  3628. }
  3629. public function image($get){
  3630. // generate parameters
  3631. if($get["npt"]){
  3632. [$params, $proxy] =
  3633. $this->backend->get(
  3634. $get["npt"],
  3635. "images"
  3636. );
  3637. $params = json_decode($params, true);
  3638. }else{
  3639. $search = $get["s"];
  3640. if(strlen($search) === 0){
  3641. throw new Exception("Search term is empty!");
  3642. }
  3643. $proxy = $this->backend->get_ip();
  3644. $country = $get["country"];
  3645. $nsfw = $get["nsfw"];
  3646. $time = $get["time"];
  3647. $size = $get["size"];
  3648. $ratio = $get["ratio"];
  3649. $color = $get["color"];
  3650. $type = $get["type"];
  3651. $format = $get["format"];
  3652. $rights = $get["rights"];
  3653. $params = [
  3654. "q" => $search,
  3655. "udm" => "2" // get images
  3656. ];
  3657. // country (image search uses cr instead of gl)
  3658. if($country != "any"){
  3659. $params["cr"] = "country" . strtoupper($country);
  3660. }
  3661. // nsfw
  3662. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  3663. // generate tbs
  3664. $tbs = [];
  3665. // time
  3666. if($time != "any"){
  3667. $tbs["qdr"] = $time;
  3668. }
  3669. // size
  3670. if($size != "any"){
  3671. $params["imgsz"] = $size;
  3672. }
  3673. // ratio
  3674. if($ratio != "any"){
  3675. $params["imgar"] = $ratio;
  3676. }
  3677. // color
  3678. if($color != "any"){
  3679. if(
  3680. $color == "color" ||
  3681. $color == "trans"
  3682. ){
  3683. $params["imgc"] = $color;
  3684. }elseif($color == "bnw"){
  3685. $params["imgc"] = "gray";
  3686. }else{
  3687. $tbs["ic"] = "specific";
  3688. $tbs["isc"] = $color;
  3689. }
  3690. }
  3691. // type
  3692. if($type != "any"){
  3693. $tbs["itp"] = $type;
  3694. }
  3695. // format
  3696. if($format != "any"){
  3697. $params["as_filetype"] = $format;
  3698. }
  3699. // rights (tbs)
  3700. if($rights != "any"){
  3701. $tbs["sur"] = $rights;
  3702. }
  3703. // append tbs
  3704. if(count($tbs) !== 0){
  3705. $params["tbs"] = "";
  3706. foreach($tbs as $key => $value){
  3707. $params["tbs"] .= $key . ":" . $value . ",";
  3708. }
  3709. $params["tbs"] = rtrim($params["tbs"], ",");
  3710. }
  3711. }
  3712. /*
  3713. $handle = fopen("scraper/google-img.html", "r");
  3714. $html = fread($handle, filesize("scraper/google-img.html"));
  3715. fclose($handle);*/
  3716. try{
  3717. $html =
  3718. $this->get(
  3719. $proxy,
  3720. "https://www.google.com/search",
  3721. $params
  3722. );
  3723. }catch(Exception $error){
  3724. throw new Exception("Failed to get search page");
  3725. }
  3726. $this->fuckhtml->load($html);
  3727. $this->detect_sorry();
  3728. // get javascript images
  3729. $this->scrape_imagearr($html);
  3730. $out = [
  3731. "status" => "ok",
  3732. "npt" => null,
  3733. "image" => []
  3734. ];
  3735. $images =
  3736. $this->fuckhtml
  3737. ->getElementsByClassName(
  3738. "ivg-i",
  3739. "div"
  3740. );
  3741. foreach($images as $div){
  3742. $this->fuckhtml->load($div);
  3743. $image =
  3744. $this->fuckhtml
  3745. ->getElementsByTagName("img")[0];
  3746. $out["image"][] = [
  3747. "title" =>
  3748. $this->titledots(
  3749. $this->fuckhtml
  3750. ->getTextContent(
  3751. $image["attributes"]["alt"]
  3752. )
  3753. ),
  3754. "source" =>
  3755. $this->image_arr[
  3756. $div["attributes"]["data-docid"]
  3757. ],
  3758. "url" =>
  3759. $this->fuckhtml
  3760. ->getTextContent(
  3761. $div["attributes"]["data-lpage"]
  3762. )
  3763. ];
  3764. }
  3765. // as usual, no way to check if there is a next page reliably
  3766. if(count($out["image"]) > 50){
  3767. if(!isset($params["start"])){
  3768. $params["start"] = 10;
  3769. }else{
  3770. $params["start"] += 10;
  3771. }
  3772. $out["npt"] =
  3773. $this->backend
  3774. ->store(
  3775. json_encode($params),
  3776. "image",
  3777. $proxy
  3778. );
  3779. }
  3780. return $out;
  3781. }
  3782. private function unshiturl($url, $return_size = false){
  3783. // decode
  3784. $url =
  3785. $this->fuckhtml
  3786. ->getTextContent($url);
  3787. $url_parts = parse_url($url);
  3788. if(
  3789. !isset(
  3790. $url_parts["host"]
  3791. )
  3792. ){
  3793. // no host, we have a tracking url
  3794. parse_str($url_parts["query"], $query);
  3795. if(isset($query["imgurl"])){
  3796. $url = $query["imgurl"];
  3797. }
  3798. elseif(isset($query["q"])){
  3799. $url = $query["q"];
  3800. }
  3801. }
  3802. // rewrite URLs to remove extra tracking parameters
  3803. $domain = parse_url($url, PHP_URL_HOST);
  3804. if(
  3805. preg_match(
  3806. '/wikipedia.org$/',
  3807. $domain
  3808. )
  3809. ){
  3810. // rewrite wikipedia mobile URLs to desktop
  3811. $url =
  3812. $this->replacedomain(
  3813. $url,
  3814. preg_replace(
  3815. '/([a-z0-9]+)(\.m\.)/',
  3816. '$1.',
  3817. $domain
  3818. )
  3819. );
  3820. }
  3821. elseif(
  3822. preg_match(
  3823. '/imdb\.com$|youtube\.[^.]+$/',
  3824. $domain
  3825. )
  3826. ){
  3827. // rewrite imdb and youtube mobile URLs too
  3828. $url =
  3829. $this->replacedomain(
  3830. $url,
  3831. preg_replace(
  3832. '/^m\./',
  3833. "",
  3834. $domain
  3835. )
  3836. );
  3837. }
  3838. elseif(
  3839. preg_match(
  3840. '/play\.google\.[^.]+$/',
  3841. $domain
  3842. )
  3843. ){
  3844. // remove referrers from play.google.com
  3845. $oldquery = parse_url($url, PHP_URL_QUERY);
  3846. if($oldquery !== null){
  3847. parse_str($oldquery, $query);
  3848. if(isset($query["referrer"])){ unset($query["referrer"]); }
  3849. if(isset($query["hl"])){ unset($query["hl"]); }
  3850. if(isset($query["gl"])){ unset($query["gl"]); }
  3851. $query = http_build_query($query);
  3852. $url =
  3853. str_replace(
  3854. $oldquery,
  3855. $query,
  3856. $url
  3857. );
  3858. }
  3859. }
  3860. elseif(
  3861. preg_match(
  3862. '/twitter\.com$/',
  3863. $domain
  3864. )
  3865. ){
  3866. // remove more referrers from twitter.com
  3867. $oldquery = parse_url($url, PHP_URL_QUERY);
  3868. if($oldquery !== null){
  3869. parse_str($oldquery, $query);
  3870. if(isset($query["ref_src"])){ unset($query["ref_src"]); }
  3871. $query = http_build_query($query);
  3872. $url =
  3873. str_replace(
  3874. $oldquery,
  3875. $query,
  3876. $url
  3877. );
  3878. }
  3879. }
  3880. elseif(
  3881. preg_match(
  3882. '/maps\.google\.[^.]+/',
  3883. $domain
  3884. )
  3885. ){
  3886. if(stripos($url, "maps?") !== false){
  3887. //https://maps.google.com/maps?daddr=Johnny,+603+Rue+St+Georges,+Saint-J%C3%A9r%C3%B4me,+Quebec+J7Z+5B7
  3888. $query = parse_url($url, PHP_URL_QUERY);
  3889. if($query !== null){
  3890. parse_str($query, $query);
  3891. if(isset($query["daddr"])){
  3892. $url =
  3893. "https://maps.google.com/maps?daddr=" .
  3894. urlencode($query["daddr"]);
  3895. }
  3896. }
  3897. }
  3898. }
  3899. if($return_size){
  3900. return [
  3901. "url" => $url,
  3902. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  3903. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  3904. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  3905. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  3906. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  3907. ];
  3908. }
  3909. return $url;
  3910. }
  3911. private function replacedomain($url, $domain){
  3912. return
  3913. preg_replace(
  3914. '/(https?:\/\/)([^\/]+)/',
  3915. '$1' . $domain,
  3916. $url
  3917. );
  3918. }
  3919. private function titledots($title){
  3920. return trim($title, " .\t\n\r\0\x0B…");
  3921. }
  3922. private function hms2int($time){
  3923. $parts = explode(":", $time, 3);
  3924. $time = 0;
  3925. if(count($parts) === 3){
  3926. // hours
  3927. $time = $time + ((int)$parts[0] * 3600);
  3928. array_shift($parts);
  3929. }
  3930. if(count($parts) === 2){
  3931. // minutes
  3932. $time = $time + ((int)$parts[0] * 60);
  3933. array_shift($parts);
  3934. }
  3935. // seconds
  3936. $time = $time + (int)$parts[0];
  3937. return $time;
  3938. }
  3939. private function detect_sorry(){
  3940. $recaptcha =
  3941. $this->fuckhtml
  3942. ->getElementById(
  3943. "recaptcha",
  3944. "div"
  3945. );
  3946. if($recaptcha !== false){
  3947. throw new Exception("Google returned a captcha");
  3948. }
  3949. }
  3950. }