google.php 90 KB

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