1
0

google.php 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009
  1. <?php
  2. // @TODO check for consent.google.com page, if need be
  3. class google{
  4. public function __construct(){
  5. include "lib/fuckhtml.php";
  6. $this->fuckhtml = new fuckhtml();
  7. include "lib/backend.php";
  8. $this->backend = new backend("google");
  9. }
  10. public function getfilters($page){
  11. $base = [
  12. "country" => [ // gl=<country> (image: cr=countryAF)
  13. "display" => "Country",
  14. "option" => [
  15. "any" => "Instance's country",
  16. "af" => "Afghanistan",
  17. "al" => "Albania",
  18. "dz" => "Algeria",
  19. "as" => "American Samoa",
  20. "ad" => "Andorra",
  21. "ao" => "Angola",
  22. "ai" => "Anguilla",
  23. "aq" => "Antarctica",
  24. "ag" => "Antigua and Barbuda",
  25. "ar" => "Argentina",
  26. "am" => "Armenia",
  27. "aw" => "Aruba",
  28. "au" => "Australia",
  29. "at" => "Austria",
  30. "az" => "Azerbaijan",
  31. "bs" => "Bahamas",
  32. "bh" => "Bahrain",
  33. "bd" => "Bangladesh",
  34. "bb" => "Barbados",
  35. "by" => "Belarus",
  36. "be" => "Belgium",
  37. "bz" => "Belize",
  38. "bj" => "Benin",
  39. "bm" => "Bermuda",
  40. "bt" => "Bhutan",
  41. "bo" => "Bolivia",
  42. "ba" => "Bosnia and Herzegovina",
  43. "bw" => "Botswana",
  44. "bv" => "Bouvet Island",
  45. "br" => "Brazil",
  46. "io" => "British Indian Ocean Territory",
  47. "bn" => "Brunei Darussalam",
  48. "bg" => "Bulgaria",
  49. "bf" => "Burkina Faso",
  50. "bi" => "Burundi",
  51. "kh" => "Cambodia",
  52. "cm" => "Cameroon",
  53. "ca" => "Canada",
  54. "cv" => "Cape Verde",
  55. "ky" => "Cayman Islands",
  56. "cf" => "Central African Republic",
  57. "td" => "Chad",
  58. "cl" => "Chile",
  59. "cn" => "China",
  60. "cx" => "Christmas Island",
  61. "cc" => "Cocos (Keeling) Islands",
  62. "co" => "Colombia",
  63. "km" => "Comoros",
  64. "cg" => "Congo",
  65. "cd" => "Congo, the Democratic Republic",
  66. "ck" => "Cook Islands",
  67. "cr" => "Costa Rica",
  68. "ci" => "Cote D'ivoire",
  69. "hr" => "Croatia",
  70. "cu" => "Cuba",
  71. "cy" => "Cyprus",
  72. "cz" => "Czech Republic",
  73. "dk" => "Denmark",
  74. "dj" => "Djibouti",
  75. "dm" => "Dominica",
  76. "do" => "Dominican Republic",
  77. "ec" => "Ecuador",
  78. "eg" => "Egypt",
  79. "sv" => "El Salvador",
  80. "gq" => "Equatorial Guinea",
  81. "er" => "Eritrea",
  82. "ee" => "Estonia",
  83. "et" => "Ethiopia",
  84. "fk" => "Falkland Islands (Malvinas)",
  85. "fo" => "Faroe Islands",
  86. "fj" => "Fiji",
  87. "fi" => "Finland",
  88. "fr" => "France",
  89. "gf" => "French Guiana",
  90. "pf" => "French Polynesia",
  91. "tf" => "French Southern Territories",
  92. "ga" => "Gabon",
  93. "gm" => "Gambia",
  94. "ge" => "Georgia",
  95. "de" => "Germany",
  96. "gh" => "Ghana",
  97. "gi" => "Gibraltar",
  98. "gr" => "Greece",
  99. "gl" => "Greenland",
  100. "gd" => "Grenada",
  101. "gp" => "Guadeloupe",
  102. "gu" => "Guam",
  103. "gt" => "Guatemala",
  104. "gn" => "Guinea",
  105. "gw" => "Guinea-Bissau",
  106. "gy" => "Guyana",
  107. "ht" => "Haiti",
  108. "hm" => "Heard Island and Mcdonald Islands",
  109. "va" => "Holy See (Vatican City State)",
  110. "hn" => "Honduras",
  111. "hk" => "Hong Kong",
  112. "hu" => "Hungary",
  113. "is" => "Iceland",
  114. "in" => "India",
  115. "id" => "Indonesia",
  116. "ir" => "Iran, Islamic Republic",
  117. "iq" => "Iraq",
  118. "ie" => "Ireland",
  119. "il" => "Israel",
  120. "it" => "Italy",
  121. "jm" => "Jamaica",
  122. "jp" => "Japan",
  123. "jo" => "Jordan",
  124. "kz" => "Kazakhstan",
  125. "ke" => "Kenya",
  126. "ki" => "Kiribati",
  127. "kp" => "Korea, Democratic People's Republic",
  128. "kr" => "Korea, Republic",
  129. "kw" => "Kuwait",
  130. "kg" => "Kyrgyzstan",
  131. "la" => "Lao People's Democratic Republic",
  132. "lv" => "Latvia",
  133. "lb" => "Lebanon",
  134. "ls" => "Lesotho",
  135. "lr" => "Liberia",
  136. "ly" => "Libyan Arab Jamahiriya",
  137. "li" => "Liechtenstein",
  138. "lt" => "Lithuania",
  139. "lu" => "Luxembourg",
  140. "mo" => "Macao",
  141. "mk" => "Macedonia, the Former Yugosalv Republic",
  142. "mg" => "Madagascar",
  143. "mw" => "Malawi",
  144. "my" => "Malaysia",
  145. "mv" => "Maldives",
  146. "ml" => "Mali",
  147. "mt" => "Malta",
  148. "mh" => "Marshall Islands",
  149. "mq" => "Martinique",
  150. "mr" => "Mauritania",
  151. "mu" => "Mauritius",
  152. "yt" => "Mayotte",
  153. "mx" => "Mexico",
  154. "fm" => "Micronesia, Federated States",
  155. "md" => "Moldova, Republic",
  156. "mc" => "Monaco",
  157. "mn" => "Mongolia",
  158. "ms" => "Montserrat",
  159. "ma" => "Morocco",
  160. "mz" => "Mozambique",
  161. "mm" => "Myanmar",
  162. "na" => "Namibia",
  163. "nr" => "Nauru",
  164. "np" => "Nepal",
  165. "nl" => "Netherlands",
  166. "an" => "Netherlands Antilles",
  167. "nc" => "New Caledonia",
  168. "nz" => "New Zealand",
  169. "ni" => "Nicaragua",
  170. "ne" => "Niger",
  171. "ng" => "Nigeria",
  172. "nu" => "Niue",
  173. "nf" => "Norfolk Island",
  174. "mp" => "Northern Mariana Islands",
  175. "no" => "Norway",
  176. "om" => "Oman",
  177. "pk" => "Pakistan",
  178. "pw" => "Palau",
  179. "ps" => "Palestinian Territory, Occupied",
  180. "pa" => "Panama",
  181. "pg" => "Papua New Guinea",
  182. "py" => "Paraguay",
  183. "pe" => "Peru",
  184. "ph" => "Philippines",
  185. "pn" => "Pitcairn",
  186. "pl" => "Poland",
  187. "pt" => "Portugal",
  188. "pr" => "Puerto Rico",
  189. "qa" => "Qatar",
  190. "re" => "Reunion",
  191. "ro" => "Romania",
  192. "ru" => "Russian Federation",
  193. "rw" => "Rwanda",
  194. "sh" => "Saint Helena",
  195. "kn" => "Saint Kitts and Nevis",
  196. "lc" => "Saint Lucia",
  197. "pm" => "Saint Pierre and Miquelon",
  198. "vc" => "Saint Vincent and the Grenadines",
  199. "ws" => "Samoa",
  200. "sm" => "San Marino",
  201. "st" => "Sao Tome and Principe",
  202. "sa" => "Saudi Arabia",
  203. "sn" => "Senegal",
  204. "cs" => "Serbia and Montenegro",
  205. "sc" => "Seychelles",
  206. "sl" => "Sierra Leone",
  207. "sg" => "Singapore",
  208. "sk" => "Slovakia",
  209. "si" => "Slovenia",
  210. "sb" => "Solomon Islands",
  211. "so" => "Somalia",
  212. "za" => "South Africa",
  213. "gs" => "South Georgia and the South Sandwich Islands",
  214. "es" => "Spain",
  215. "lk" => "Sri Lanka",
  216. "sd" => "Sudan",
  217. "sr" => "Suriname",
  218. "sj" => "Svalbard and Jan Mayen",
  219. "sz" => "Swaziland",
  220. "se" => "Sweden",
  221. "ch" => "Switzerland",
  222. "sy" => "Syrian Arab Republic",
  223. "tw" => "Taiwan, Province of China",
  224. "tj" => "Tajikistan",
  225. "tz" => "Tanzania, United Republic",
  226. "th" => "Thailand",
  227. "tl" => "Timor-Leste",
  228. "tg" => "Togo",
  229. "tk" => "Tokelau",
  230. "to" => "Tonga",
  231. "tt" => "Trinidad and Tobago",
  232. "tn" => "Tunisia",
  233. "tr" => "Turkey",
  234. "tm" => "Turkmenistan",
  235. "tc" => "Turks and Caicos Islands",
  236. "tv" => "Tuvalu",
  237. "ug" => "Uganda",
  238. "ua" => "Ukraine",
  239. "ae" => "United Arab Emirates",
  240. "uk" => "United Kingdom",
  241. "us" => "United States",
  242. "um" => "United States Minor Outlying Islands",
  243. "uy" => "Uruguay",
  244. "uz" => "Uzbekistan",
  245. "vu" => "Vanuatu",
  246. "ve" => "Venezuela",
  247. "vn" => "Viet Nam",
  248. "vg" => "Virgin Islands, British",
  249. "vi" => "Virgin Islands, U.S.",
  250. "wf" => "Wallis and Futuna",
  251. "eh" => "Western Sahara",
  252. "ye" => "Yemen",
  253. "zm" => "Zambia",
  254. "zw" => "Zimbabwe"
  255. ]
  256. ],
  257. "nsfw" => [
  258. "display" => "NSFW",
  259. "option" => [
  260. "yes" => "Yes", // safe=active
  261. "no" => "No" // safe=off
  262. ]
  263. ]
  264. ];
  265. switch($page){
  266. case "web":
  267. return array_merge(
  268. $base,
  269. [
  270. "lang" => [ // lr=<lang> (prefix lang with "lang_")
  271. "display" => "Language",
  272. "option" => [
  273. "any" => "Any language",
  274. "ar" => "Arabic",
  275. "bg" => "Bulgarian",
  276. "ca" => "Catalan",
  277. "cs" => "Czech",
  278. "da" => "Danish",
  279. "de" => "German",
  280. "el" => "Greek",
  281. "en" => "English",
  282. "es" => "Spanish",
  283. "et" => "Estonian",
  284. "fi" => "Finnish",
  285. "fr" => "French",
  286. "hr" => "Croatian",
  287. "hu" => "Hungarian",
  288. "id" => "Indonesian",
  289. "is" => "Icelandic",
  290. "it" => "Italian",
  291. "iw" => "Hebrew",
  292. "ja" => "Japanese",
  293. "ko" => "Korean",
  294. "lt" => "Lithuanian",
  295. "lv" => "Latvian",
  296. "nl" => "Dutch",
  297. "no" => "Norwegian",
  298. "pl" => "Polish",
  299. "pt" => "Portuguese",
  300. "ro" => "Romanian",
  301. "ru" => "Russian",
  302. "sk" => "Slovak",
  303. "sl" => "Slovenian",
  304. "sr" => "Serbian",
  305. "sv" => "Swedish",
  306. "tr" => "Turkish",
  307. "zh-CN" => "Chinese (Simplified)",
  308. "zh-TW" => "Chinese (Traditional)"
  309. ]
  310. ],
  311. "newer" => [ // tbs
  312. "display" => "Newer than",
  313. "option" => "_DATE"
  314. ],
  315. "older" => [
  316. "display" => "Older than",
  317. "option" => "_DATE"
  318. ],
  319. "spellcheck" => [
  320. "display" => "Spellcheck",
  321. "option" => [
  322. "yes" => "Yes",
  323. "no" => "No"
  324. ]
  325. ]
  326. ]
  327. );
  328. break;
  329. case "images":
  330. return array_merge(
  331. $base,
  332. [
  333. "time" => [ // tbs=qdr:<time>
  334. "display" => "Time posted",
  335. "option" => [
  336. "any" => "Any time",
  337. "d" => "Past 24 hours",
  338. "w" => "Past week",
  339. "m" => "Past month",
  340. "y" => "Past year"
  341. ]
  342. ],
  343. "size" => [ // imgsz
  344. "display" => "Size",
  345. "option" => [
  346. "any" => "Any size",
  347. "l" => "Large",
  348. "m" => "Medium",
  349. "i" => "Icon",
  350. "qsvga" => "Larger than 400x300",
  351. "vga" => "Larger than 640x480",
  352. "svga" => "Larger than 800x600",
  353. "xga" => "Larger than 1024x768",
  354. "2mp" => "Larger than 2MP",
  355. "4mp" => "Larger than 4MP",
  356. "6mp" => "Larger than 6MP",
  357. "8mp" => "Larger than 8MP",
  358. "10mp" => "Larger than 10MP",
  359. "12mp" => "Larger than 12MP",
  360. "15mp" => "Larger than 15MP",
  361. "20mp" => "Larger than 20MP",
  362. "40mp" => "Larger than 40MP",
  363. "70mp" => "Larger than 70MP"
  364. ]
  365. ],
  366. "ratio" => [ // imgar
  367. "display" => "Aspect ratio",
  368. "option" => [
  369. "any" => "Any ratio",
  370. "t|xt" => "Tall",
  371. "s" => "Square",
  372. "w" => "Wide",
  373. "xw" => "Panoramic"
  374. ]
  375. ],
  376. "color" => [ // imgc
  377. "display" => "Color",
  378. "option" => [
  379. "any" => "Any color",
  380. "color" => "Full color",
  381. "bnw" => "Black & white",
  382. "trans" => "Transparent",
  383. // from here, imgcolor
  384. "red" => "Red",
  385. "orange" => "Orange",
  386. "yellow" => "Yellow",
  387. "green" => "Green",
  388. "teal" => "Teal",
  389. "blue" => "Blue",
  390. "purple" => "Purple",
  391. "pink" => "Pink",
  392. "white" => "White",
  393. "gray" => "Gray",
  394. "black" => "Black",
  395. "brown" => "Brown"
  396. ]
  397. ],
  398. "type" => [ // tbs=itp:<type>
  399. "display" => "Type",
  400. "option" => [
  401. "any" => "Any type",
  402. "clipart" => "Clip Art",
  403. "lineart" => "Line Drawing",
  404. "animated" => "Animated"
  405. ]
  406. ],
  407. "format" => [ // as_filetype
  408. "display" => "Format",
  409. "option" => [
  410. "any" => "Any format",
  411. "jpg" => "JPG",
  412. "gif" => "GIF",
  413. "png" => "PNG",
  414. "bmp" => "BMP",
  415. "svg" => "SVG",
  416. "webp" => "WEBP",
  417. "ico" => "ICO",
  418. "craw" => "RAW"
  419. ]
  420. ],
  421. "rights" => [ // tbs=sur:<rights>
  422. "display" => "Usage rights",
  423. "option" => [
  424. "any" => "Any license",
  425. "cl" => "Creative Commons licenses",
  426. "ol" => "Commercial & other licenses"
  427. ]
  428. ]
  429. ]
  430. );
  431. break;
  432. case "videos":
  433. return array_merge(
  434. $base,
  435. [
  436. "newer" => [ // tbs
  437. "display" => "Newer than",
  438. "option" => "_DATE"
  439. ],
  440. "older" => [
  441. "display" => "Older than",
  442. "option" => "_DATE"
  443. ],
  444. "duration" => [
  445. "display" => "Duration",
  446. "option" => [
  447. "any" => "Any duration",
  448. "s" => "Short (0-4min)", // tbs=dur:s
  449. "m" => "Medium (4-20min)", // tbs=dur:m
  450. "l" => "Long (20+ min)" // tbs=dur:l
  451. ]
  452. ],
  453. "quality" => [
  454. "display" => "Quality",
  455. "option" => [
  456. "any" => "Any quality",
  457. "h" => "High quality" // tbs=hq:h
  458. ]
  459. ],
  460. "captions" => [
  461. "display" => "Captions",
  462. "option" => [
  463. "any" => "No preference",
  464. "yes" => "Closed captioned" // tbs=cc:1
  465. ]
  466. ]
  467. ]
  468. );
  469. break;
  470. case "news":
  471. return array_merge(
  472. $base,
  473. [
  474. "newer" => [ // tbs
  475. "display" => "Newer than",
  476. "option" => "_DATE"
  477. ],
  478. "older" => [
  479. "display" => "Older than",
  480. "option" => "_DATE"
  481. ],
  482. "sort" => [
  483. "display" => "Sort",
  484. "option" => [
  485. "relevance" => "Relevance",
  486. "date" => "Date" // sbd:1
  487. ]
  488. ]
  489. ]
  490. );
  491. break;
  492. }
  493. }
  494. private function get($proxy, $url, $get = []){
  495. $headers = [
  496. "User-Agent: " . config::USER_AGENT,
  497. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  498. "Accept-Language: en-US,en;q=0.5",
  499. "Accept-Encoding: gzip",
  500. "DNT: 1",
  501. //"Cookie: SOCS=CAESNQgCEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjQwMzE3LjA4X3AwGgJlbiAEGgYIgM7orwY",
  502. "Connection: keep-alive",
  503. "Upgrade-Insecure-Requests: 1",
  504. "Sec-Fetch-Dest: document",
  505. "Sec-Fetch-Mode: navigate",
  506. "Sec-Fetch-Site: none",
  507. "Sec-Fetch-User: ?1",
  508. "Priority: u=1",
  509. "TE: trailers"
  510. ];
  511. $curlproc = curl_init();
  512. if($get !== []){
  513. $get = http_build_query($get);
  514. $url .= "?" . $get;
  515. }
  516. curl_setopt($curlproc, CURLOPT_URL, $url);
  517. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  518. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  519. // use http2
  520. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  521. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  522. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  523. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  524. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  525. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  526. // follow redirects
  527. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  528. $this->backend->assign_proxy($curlproc, $proxy);
  529. $data = curl_exec($curlproc);
  530. if(curl_errno($curlproc)){
  531. throw new Exception(curl_error($curlproc));
  532. }
  533. curl_close($curlproc);
  534. return $data;
  535. }
  536. private function parsepage($html, $pagetype, $search, $proxy, $params){
  537. $out = [
  538. "status" => "ok",
  539. "spelling" => [
  540. "type" => "no_correction",
  541. "using" => null,
  542. "correction" => null
  543. ],
  544. "npt" => null,
  545. "answer" => [],
  546. "web" => [],
  547. "image" => [],
  548. "video" => [],
  549. "news" => [],
  550. "related" => []
  551. ];
  552. $this->fuckhtml->load($html);
  553. $this->detect_sorry();
  554. // parse all <style> tags
  555. $this->parsestyles();
  556. // get javascript images
  557. $this->scrape_dimg($html);
  558. // get html blobs
  559. preg_match_all(
  560. '/function\(\){window\.jsl\.dh\(\'([^\']+?)\',\'(.+?[^\'])\'\);/',
  561. $html,
  562. $blobs
  563. );
  564. $this->blobs = [];
  565. if(isset($blobs[1])){
  566. for($i=0; $i<count($blobs[1]); $i++){
  567. $this->blobs[$blobs[1][$i]] =
  568. $this->fuckhtml
  569. ->parseJsString(
  570. $blobs[2][$i]
  571. );
  572. }
  573. }
  574. $this->scrape_imagearr($html);
  575. //
  576. // load result column
  577. //
  578. $result_div =
  579. $this->fuckhtml
  580. ->getElementById(
  581. "center_col",
  582. "div"
  583. );
  584. if($result_div === false){
  585. throw new Exception("Failed to grep result div");
  586. }
  587. $this->fuckhtml->load($result_div);
  588. //
  589. // Get word corrections
  590. //
  591. $correction =
  592. $this->fuckhtml
  593. ->getElementById(
  594. "fprs",
  595. "p"
  596. );
  597. if($correction){
  598. $this->fuckhtml->load($correction);
  599. $a =
  600. $this->fuckhtml
  601. ->getElementsByTagName(
  602. "a"
  603. );
  604. $using =
  605. $this->fuckhtml
  606. ->getElementById(
  607. "fprsl",
  608. $a
  609. );
  610. if($using){
  611. $using =
  612. $this->fuckhtml
  613. ->getTextContent(
  614. $using
  615. );
  616. $spans =
  617. $this->fuckhtml
  618. ->getElementsByTagName(
  619. "span"
  620. );
  621. $type_span =
  622. $this->fuckhtml
  623. ->getTextContent(
  624. $spans[0]
  625. );
  626. $type = "not_many";
  627. if(
  628. stripos(
  629. $type_span,
  630. "Showing results for"
  631. ) !== false
  632. ){
  633. $type = "including";
  634. }
  635. $correction =
  636. $this->fuckhtml
  637. ->getTextContent(
  638. $a[count($a) - 1]
  639. );
  640. $out["spelling"] = [
  641. "type" => $type,
  642. "using" => $using,
  643. "correction" => $correction
  644. ];
  645. }
  646. // reset
  647. $this->fuckhtml->load($result_div);
  648. }
  649. //
  650. // get notices
  651. //
  652. $botstuff =
  653. $this->fuckhtml
  654. ->getElementById(
  655. "botstuff"
  656. );
  657. // important for later
  658. $last_page = false;
  659. if($botstuff){
  660. $this->fuckhtml->load($botstuff);
  661. $cards =
  662. $this->fuckhtml
  663. ->getElementsByClassName(
  664. $this->getstyle(
  665. [
  666. "line-height" => "normal"
  667. ]
  668. ),
  669. "div"
  670. );
  671. foreach($cards as $card){
  672. $this->fuckhtml->load($card);
  673. $h2 =
  674. $this->fuckhtml
  675. ->getElementsByTagName(
  676. "h2"
  677. );
  678. if(count($h2) !== 0){
  679. $title =
  680. $this->fuckhtml
  681. ->getTextContent(
  682. $h2[0]
  683. );
  684. $card["innerHTML"] =
  685. str_replace(
  686. $h2[0]["outerHTML"],
  687. "",
  688. $card["innerHTML"]
  689. );
  690. }else{
  691. $title = "Notice";
  692. }
  693. $description = [];
  694. $as =
  695. $this->fuckhtml
  696. ->getElementsByTagName(
  697. "a"
  698. );
  699. if(count($as) !== 0){
  700. $first = true;
  701. foreach($as as $a){
  702. $text_link =
  703. $this->fuckhtml
  704. ->getTextContent(
  705. $a
  706. );
  707. if(stripos($text_link, "repeat the search") !== false){
  708. $last_page = true;
  709. break 2;
  710. }
  711. $parts =
  712. explode(
  713. $a["outerHTML"],
  714. $card["innerHTML"],
  715. 2
  716. );
  717. $card["innerHTML"] = $parts[1];
  718. $value =
  719. preg_replace(
  720. '/ +/',
  721. " ",
  722. $this->fuckhtml
  723. ->getTextContent(
  724. $parts[0],
  725. false,
  726. false
  727. )
  728. );
  729. if(strlen(trim($value)) !== 0){
  730. $description[] = [
  731. "type" => "text",
  732. "value" => $value
  733. ];
  734. if($first){
  735. $description[0]["value"] =
  736. ltrim($description[0]["value"]);
  737. }
  738. }
  739. $first = false;
  740. $description[] = [
  741. "type" => "link",
  742. "url" =>
  743. $this->fuckhtml
  744. ->getTextContent(
  745. $a["attributes"]
  746. ["href"]
  747. ),
  748. "value" => $text_link
  749. ];
  750. }
  751. $text =
  752. $this->fuckhtml
  753. ->getTextContent(
  754. $card["innerHTML"],
  755. false,
  756. false
  757. );
  758. if(strlen(trim($text)) !== 0){
  759. $description[] = [
  760. "type" => "text",
  761. "value" =>
  762. rtrim(
  763. $text
  764. )
  765. ];
  766. }
  767. }else{
  768. // @TODO: Check if this ever gets populated without giving me garbage
  769. /*
  770. $text =
  771. $this->fuckhtml
  772. ->getTextContent(
  773. $card
  774. );
  775. if($text != ""){
  776. $description[] = [
  777. "type" => "text",
  778. "value" => $text
  779. ];
  780. }*/
  781. }
  782. if(count($description) !== 0){
  783. $out["answer"][] = [
  784. "title" => $title,
  785. "description" => $description,
  786. "url" => null,
  787. "thumb" => null,
  788. "table" => [],
  789. "sublink" => []
  790. ];
  791. }
  792. }
  793. // reset
  794. $this->fuckhtml->load($html);
  795. }
  796. //
  797. // get "Related Searches" and "People also search for"
  798. //
  799. $relateds =
  800. array_merge(
  801. $this->fuckhtml
  802. ->getElementsByClassName(
  803. $this->getstyle(
  804. [
  805. "align-items" => "center",
  806. "background-color" => "#28292a",
  807. "border-radius" => "100px",
  808. "box-sizing" => "border-box",
  809. "display" => "flex",
  810. "max-height" => "none",
  811. "min-height" => "48px",
  812. "padding-left" => "17px",
  813. "padding-right" => "17px",
  814. "position" => "relative"
  815. ]
  816. ) . " " .
  817. $this->getstyle(
  818. [
  819. "margin-left" => "8px",
  820. "margin-right" => "8px"
  821. ]
  822. ),
  823. "a"
  824. ),
  825. $this->fuckhtml
  826. ->getElementsByClassName(
  827. "wyccme",
  828. "div"
  829. )
  830. );
  831. foreach($relateds as $related){
  832. $text =
  833. $this->fuckhtml
  834. ->getTextContent(
  835. $related
  836. );
  837. if($text == "More results"){ continue; }
  838. $out["related"][] = $text;
  839. }
  840. //
  841. // Get text results
  842. //
  843. $results =
  844. $this->fuckhtml
  845. ->getElementsByClassName(
  846. "g",
  847. "div"
  848. );
  849. $this->skip_next = false;
  850. foreach($results as $result){
  851. if($this->skip_next){
  852. $this->skip_next = false;
  853. continue;
  854. }
  855. $this->fuckhtml->load($result);
  856. $web = [
  857. "title" => null,
  858. "description" => null,
  859. "url" => null,
  860. "date" => null,
  861. "type" => "web",
  862. "thumb" => [
  863. "url" => null,
  864. "ratio" => null
  865. ],
  866. "sublink" => [],
  867. "table" => []
  868. ];
  869. // Detect presence of sublinks
  870. $g =
  871. $this->fuckhtml
  872. ->getElementsByClassName(
  873. "g",
  874. "div"
  875. );
  876. $sublinks = [];
  877. if(count($g) > 0){
  878. $table =
  879. $this->fuckhtml
  880. ->getElementsByTagName(
  881. "table"
  882. );
  883. if(count($table) !== 0){
  884. // found some sublinks!
  885. $this->fuckhtml->load($table[0]);
  886. $tds =
  887. $this->fuckhtml
  888. ->getElementsByTagName(
  889. "td"
  890. );
  891. foreach($tds as $td){
  892. $this->fuckhtml->load($td);
  893. $a =
  894. $this->fuckhtml
  895. ->getElementsByTagName(
  896. "a"
  897. );
  898. if(
  899. count($a) === 0 ||
  900. (
  901. isset($a[0]["attributes"]["class"]) &&
  902. $a[0]["attributes"]["class"] == "fl"
  903. )
  904. ){
  905. continue;
  906. }
  907. $td["innerHTML"] =
  908. str_replace(
  909. $a[0]["outerHTML"],
  910. "",
  911. $td["innerHTML"]
  912. );
  913. $web["sublink"][] = [
  914. "title" =>
  915. $this->titledots(
  916. $this->fuckhtml
  917. ->getTextContent(
  918. $a[0]
  919. )
  920. ),
  921. "description" =>
  922. html_entity_decode(
  923. $this->titledots(
  924. $this->fuckhtml
  925. ->getTextContent(
  926. $td
  927. )
  928. )
  929. ),
  930. "url" =>
  931. $this->unshiturl(
  932. $a[0]
  933. ["attributes"]
  934. ["href"]
  935. ),
  936. "date" => null
  937. ];
  938. }
  939. // reset
  940. $this->fuckhtml->load($result);
  941. }
  942. // skip on next iteration
  943. $this->skip_next = true;
  944. }
  945. // get title
  946. $h3 =
  947. $this->fuckhtml
  948. ->getElementsByTagName(
  949. "h3"
  950. );
  951. if(count($h3) === 0){
  952. continue;
  953. }
  954. $web["title"] =
  955. $this->titledots(
  956. $this->fuckhtml
  957. ->getTextContent(
  958. $h3[0]
  959. )
  960. );
  961. // get url
  962. $as =
  963. $this->fuckhtml
  964. ->getElementsByTagName(
  965. "a"
  966. );
  967. $web["url"] =
  968. $this->unshiturl(
  969. $as[0]
  970. ["attributes"]
  971. ["href"]
  972. );
  973. if(
  974. !preg_match(
  975. '/^http/',
  976. $web["url"]
  977. )
  978. ){
  979. // skip if invalid url is found
  980. continue;
  981. }
  982. //
  983. // probe for twitter carousel
  984. //
  985. $carousel =
  986. $this->fuckhtml
  987. ->getElementsByTagName(
  988. "g-scrolling-carousel"
  989. );
  990. if(count($carousel) !== 0){
  991. $this->fuckhtml->load($carousel[0]);
  992. $items =
  993. $this->fuckhtml
  994. ->getElementsByTagName(
  995. "g-inner-card"
  996. );
  997. $has_thumbnail = false;
  998. foreach($items as $item){
  999. $this->fuckhtml->load($item);
  1000. if($has_thumbnail === false){
  1001. // get thumbnail
  1002. $thumb =
  1003. $this->fuckhtml
  1004. ->getElementsByTagName(
  1005. "img"
  1006. );
  1007. if(
  1008. count($thumb) !== 0 &&
  1009. isset($thumb[0]["attributes"]["id"])
  1010. ){
  1011. $web["thumb"] = [
  1012. "url" =>
  1013. $this->getdimg(
  1014. $thumb[0]["attributes"]["id"]
  1015. ),
  1016. "ratio" => "16:9"
  1017. ];
  1018. $has_thumbnail = true;
  1019. }
  1020. // or else, try getting a thumbnail from next container
  1021. }
  1022. // cache div
  1023. $div =
  1024. $this->fuckhtml
  1025. ->getElementsByTagName(
  1026. "div"
  1027. );
  1028. // get link
  1029. $links =
  1030. $this->fuckhtml
  1031. ->getElementsByTagName(
  1032. "a"
  1033. );
  1034. // get description of carousel sublink
  1035. $description =
  1036. $this->fuckhtml
  1037. ->getElementsByAttributeValue(
  1038. "role",
  1039. "heading",
  1040. $div
  1041. );
  1042. if(count($description) !== 0){
  1043. $description =
  1044. $this->titledots(
  1045. $this->fuckhtml
  1046. ->getTextContent(
  1047. $description[0]
  1048. )
  1049. );
  1050. }else{
  1051. $description = null;
  1052. }
  1053. $bottom =
  1054. $this->fuckhtml
  1055. ->getElementsByAttributeValue(
  1056. "style",
  1057. "z-index:2",
  1058. $div
  1059. );
  1060. $title = null;
  1061. $date = null;
  1062. if(count($bottom) !== 0){
  1063. $this->fuckhtml->load($bottom[0]);
  1064. $spans =
  1065. $this->fuckhtml
  1066. ->getElementsByTagName(
  1067. "span"
  1068. );
  1069. $title =
  1070. $this->fuckhtml
  1071. ->getTextContent(
  1072. $spans[0]
  1073. );
  1074. $date =
  1075. strtotime(
  1076. $this->fuckhtml
  1077. ->getTextContent(
  1078. $spans[count($spans) - 1]
  1079. )
  1080. );
  1081. }
  1082. $web["sublink"][] = [
  1083. "title" => $title,
  1084. "description" => $description,
  1085. "url" =>
  1086. $this->unshiturl(
  1087. $links[0]
  1088. ["attributes"]
  1089. ["href"]
  1090. ),
  1091. "date" => $date
  1092. ];
  1093. }
  1094. $out["web"][] = $web;
  1095. continue;
  1096. }
  1097. //
  1098. // get viewcount, time posted and follower count from <cite> tag
  1099. //
  1100. $cite =
  1101. $this->fuckhtml
  1102. ->getElementsByTagName(
  1103. "cite"
  1104. );
  1105. if(count($cite) !== 0){
  1106. $this->fuckhtml->load($cite[0]);
  1107. $spans =
  1108. $this->fuckhtml
  1109. ->getElementsByTagName("span");
  1110. if(count($spans) === 0){
  1111. $cites =
  1112. explode(
  1113. "·",
  1114. $this->fuckhtml
  1115. ->getTextContent(
  1116. $cite[0]
  1117. )
  1118. );
  1119. foreach($cites as $cite){
  1120. $cite = trim($cite);
  1121. if(
  1122. preg_match(
  1123. '/(.+) (views|followers|likes)$/',
  1124. $cite,
  1125. $match
  1126. )
  1127. ){
  1128. $web["table"][ucfirst($match[2])] =
  1129. $match[1];
  1130. }elseif(
  1131. preg_match(
  1132. '/ago$/',
  1133. $cite
  1134. )
  1135. ){
  1136. $web["date"] =
  1137. strtotime($cite);
  1138. }
  1139. }
  1140. }
  1141. // reset
  1142. $this->fuckhtml->load($result);
  1143. }
  1144. //
  1145. // attempt to fetch description cleanly
  1146. //
  1147. $description =
  1148. $this->fuckhtml
  1149. ->getElementsByAttributeValue(
  1150. "style",
  1151. "-webkit-line-clamp:2"
  1152. );
  1153. if(count($description) !== 0){
  1154. $web["description"] =
  1155. $this->titledots(
  1156. $this->fuckhtml
  1157. ->getTextContent(
  1158. $description[0]
  1159. )
  1160. );
  1161. }else{
  1162. // use ANOTHER method where the description is a header of the result
  1163. $description =
  1164. $this->fuckhtml
  1165. ->getElementsByAttributeValue(
  1166. "data-attrid",
  1167. "wa:/description"
  1168. );
  1169. if(count($description) !== 0){
  1170. // get date off that shit
  1171. $date =
  1172. $this->fuckhtml
  1173. ->getElementsByClassName(
  1174. $this->getstyle(
  1175. [
  1176. "font-size" => "12px",
  1177. "line-height" => "1.34",
  1178. "display" => "inline-block",
  1179. "font-family" => "Google Sans,arial,sans-serif",
  1180. "padding-right" => "0",
  1181. "white-space" => "nowrap"
  1182. ]
  1183. ),
  1184. "span"
  1185. );
  1186. if(count($date) !== 0){
  1187. $description[0]["innerHTML"] =
  1188. str_replace(
  1189. $date[0]["outerHTML"],
  1190. "",
  1191. $description[0]["innerHTML"]
  1192. );
  1193. $web["date"] =
  1194. strtotime(
  1195. $this->fuckhtml
  1196. ->getTextContent(
  1197. $date[0]
  1198. )
  1199. );
  1200. }
  1201. $web["description"] =
  1202. $this->fuckhtml
  1203. ->getTextContent(
  1204. $description[0]
  1205. );
  1206. }else{
  1207. // Yes.. You guessed it, use ANOTHER method to get descriptions
  1208. // off youtube containers
  1209. $description =
  1210. $this->fuckhtml
  1211. ->getElementsByClassName(
  1212. $this->getstyle(
  1213. [
  1214. "-webkit-box-orient" => "vertical",
  1215. "display" => "-webkit-box",
  1216. "font-size" => "14px",
  1217. "-webkit-line-clamp" => "2",
  1218. "line-height" => "22px",
  1219. "overflow" => "hidden",
  1220. "word-break" => "break-word",
  1221. "color" => "#bdc1c6"
  1222. ]
  1223. ),
  1224. "div"
  1225. );
  1226. if(count($description) !== 0){
  1227. // check for video duration
  1228. $duration =
  1229. $this->fuckhtml
  1230. ->getElementsByClassName(
  1231. $this->getstyle(
  1232. [
  1233. "border-radius" => "10px",
  1234. "font-family" => "arial,sans-serif-medium,sans-serif",
  1235. "font-size" => "12px",
  1236. "line-height" => "16px",
  1237. "padding-block" => "2px",
  1238. "padding-inline" => "8px"
  1239. ]
  1240. ),
  1241. "div"
  1242. );
  1243. if(count($duration) !== 0){
  1244. $web["table"]["Duration"] =
  1245. $this->fuckhtml
  1246. ->getTextContent(
  1247. $duration[0]
  1248. );
  1249. // remove duration from description
  1250. $description[0]["innerHTML"] =
  1251. str_replace(
  1252. $duration[0]["outerHTML"],
  1253. "",
  1254. $description[0]["innerHTML"]
  1255. );
  1256. }
  1257. $web["description"] =
  1258. $this->titledots(
  1259. html_entity_decode(
  1260. $this->fuckhtml
  1261. ->getTextContent(
  1262. $description[0]
  1263. )
  1264. )
  1265. );
  1266. // get author + time posted
  1267. $info =
  1268. $this->fuckhtml
  1269. ->getElementsByClassName(
  1270. $this->getstyle(
  1271. [
  1272. "color" => "var(" . $this->getcolorvar("#70757a") . ")",
  1273. "font-size" => "14px",
  1274. "line-height" => "20px",
  1275. "margin-top" => "12px"
  1276. ]
  1277. ),
  1278. "div"
  1279. );
  1280. if(count($info) !== 0){
  1281. $info =
  1282. explode(
  1283. "·",
  1284. $this->fuckhtml
  1285. ->getTextContent(
  1286. $info[0]
  1287. )
  1288. );
  1289. switch(count($info)){
  1290. case 3:
  1291. $web["table"]["Author"] = trim($info[1]);
  1292. $web["date"] = strtotime(trim($info[2]));
  1293. break;
  1294. case 2:
  1295. $web["date"] = strtotime(trim($info[1]));
  1296. break;
  1297. }
  1298. }
  1299. }
  1300. }
  1301. }
  1302. //
  1303. // get categories of content within the search result
  1304. //
  1305. $cats =
  1306. $this->fuckhtml
  1307. ->getElementsByAttributeName(
  1308. "data-sncf",
  1309. "div"
  1310. );
  1311. foreach($cats as $cat){
  1312. $this->fuckhtml->load($cat);
  1313. // detect image category
  1314. $images =
  1315. $this->fuckhtml
  1316. ->getElementsByTagName(
  1317. "img"
  1318. );
  1319. if(count($images) !== 0){
  1320. foreach($images as $image){
  1321. if(isset($image["attributes"]["id"])){
  1322. // we found an image
  1323. if(isset($image["attributes"]["width"])){
  1324. $width = (int)$image["attributes"]["width"];
  1325. if($width == 110){
  1326. $ratio = "1:1";
  1327. }elseif($width > 110){
  1328. $ratio = "16:9";
  1329. }else{
  1330. $ratio = "9:16";
  1331. }
  1332. }else{
  1333. $ratio = "1:1";
  1334. }
  1335. $web["thumb"] = [
  1336. "url" => $this->getdimg($image["attributes"]["id"]),
  1337. "ratio" => $ratio
  1338. ];
  1339. continue 2;
  1340. }
  1341. }
  1342. }
  1343. // Detect rating
  1344. $spans_unfiltered =
  1345. $this->fuckhtml
  1346. ->getElementsByTagName(
  1347. "span"
  1348. );
  1349. $spans =
  1350. $this->fuckhtml
  1351. ->getElementsByAttributeName(
  1352. "aria-label",
  1353. $spans_unfiltered
  1354. );
  1355. foreach($spans as $span){
  1356. if(
  1357. preg_match(
  1358. '/^Rated/',
  1359. $span["attributes"]["aria-label"]
  1360. )
  1361. ){
  1362. // found rating
  1363. // scrape rating
  1364. preg_match(
  1365. '/([0-9.]+).*([0-9.]+)/',
  1366. $span["attributes"]["aria-label"],
  1367. $rating
  1368. );
  1369. if(isset($rating[1])){
  1370. $web["table"]["Rating"] =
  1371. $rating[1] . "/" . $rating[2];
  1372. }
  1373. $has_seen_reviews = 0;
  1374. foreach($spans_unfiltered as $span_unfiltered){
  1375. if(
  1376. preg_match(
  1377. '/([0-9,.]+) +([A-z]+)$/',
  1378. $this->fuckhtml
  1379. ->getTextContent(
  1380. $span_unfiltered
  1381. ),
  1382. $votes
  1383. )
  1384. ){
  1385. $has_seen_reviews++;
  1386. $web["table"][ucfirst($votes[2])] = $votes[1];
  1387. continue;
  1388. }
  1389. $text =
  1390. $this->fuckhtml
  1391. ->getTextContent(
  1392. $span_unfiltered
  1393. );
  1394. if(
  1395. $text == "&nbsp;&nbsp;&nbsp;" ||
  1396. $text == ""
  1397. ){
  1398. break;
  1399. }
  1400. switch($has_seen_reviews){
  1401. case 1:
  1402. // scrape price
  1403. $web["table"]["Price"] = $text;
  1404. $has_seen_reviews++;
  1405. break;
  1406. case 2:
  1407. // scrape platform
  1408. $web["table"]["Platform"] = $text;
  1409. $has_seen_reviews++;
  1410. break;
  1411. case 3:
  1412. // Scrape type
  1413. $web["table"]["Medium"] = $text;
  1414. break;
  1415. }
  1416. }
  1417. continue 2;
  1418. }
  1419. }
  1420. // check if its a table of small sublinks
  1421. $table =
  1422. $this->fuckhtml
  1423. ->getElementsByClassName(
  1424. $this->getstyle(
  1425. [
  1426. "display" => "table",
  1427. "white-space" => "nowrap",
  1428. "margin" => "5px 0",
  1429. "line-height" => "1.58",
  1430. "color" => "var(" . $this->getcolorvar("#70757a") . ")"
  1431. ]
  1432. ),
  1433. "div"
  1434. );
  1435. if(count($table) !== 0){
  1436. $this->fuckhtml->load($table[0]);
  1437. $rows =
  1438. $this->fuckhtml
  1439. ->getElementsByClassName(
  1440. $this->getstyle(
  1441. [
  1442. "display" => "flex",
  1443. "white-space" => "normal"
  1444. ]
  1445. ),
  1446. "div"
  1447. );
  1448. foreach($rows as $row){
  1449. $this->fuckhtml->load($row);
  1450. $sublink = [
  1451. "title" => null,
  1452. "description" => null,
  1453. "url" => null,
  1454. "date" => null
  1455. ];
  1456. $link =
  1457. $this->fuckhtml
  1458. ->getElementsByTagName(
  1459. "a"
  1460. )[0];
  1461. $sublink["title"] =
  1462. $this->titledots(
  1463. $this->fuckhtml
  1464. ->getTextContent(
  1465. $link
  1466. )
  1467. );
  1468. $sublink["url"] =
  1469. $this->unshiturl(
  1470. $link
  1471. ["attributes"]
  1472. ["href"]
  1473. );
  1474. $row["innerHTML"] =
  1475. str_replace(
  1476. $link["outerHTML"],
  1477. "",
  1478. $row["innerHTML"]
  1479. );
  1480. $this->fuckhtml->load($row);
  1481. $spans =
  1482. $this->fuckhtml
  1483. ->getElementsByTagName(
  1484. "span"
  1485. );
  1486. foreach($spans as $span){
  1487. $text =
  1488. $this->fuckhtml
  1489. ->getTextContent(
  1490. $span
  1491. );
  1492. if(
  1493. preg_match(
  1494. '/answers?$/',
  1495. $text
  1496. )
  1497. ){
  1498. $sublink["description"] =
  1499. $text;
  1500. continue;
  1501. }
  1502. $time = strtotime($text);
  1503. if($time !== false){
  1504. $sublink["date"] = $time;
  1505. }
  1506. }
  1507. $web["sublink"][] = $sublink;
  1508. }
  1509. // reset
  1510. $this->fuckhtml->load($cat);
  1511. continue;
  1512. }
  1513. // check if its an answer header
  1514. $answer_header =
  1515. $this->fuckhtml
  1516. ->getElementsByClassName(
  1517. $this->getstyle(
  1518. [
  1519. "overflow" => "hidden",
  1520. "text-overflow" => "ellipsis"
  1521. ]
  1522. ),
  1523. "span"
  1524. );
  1525. if(count($answer_header) !== 0){
  1526. $link =
  1527. $this->fuckhtml
  1528. ->getElementsByTagName(
  1529. "a"
  1530. );
  1531. $cat["innerHTML"] =
  1532. str_replace(
  1533. $link[0]["outerHTML"],
  1534. "",
  1535. $cat["innerHTML"]
  1536. );
  1537. $web["sublink"][] = [
  1538. "title" =>
  1539. $this->fuckhtml
  1540. ->getTextContent(
  1541. $link[0]
  1542. ),
  1543. "description" =>
  1544. $this->titledots(
  1545. trim(
  1546. str_replace(
  1547. "\xc2\xa0",
  1548. " ",
  1549. html_entity_decode(
  1550. $this->fuckhtml
  1551. ->getTextContent(
  1552. $cat
  1553. )
  1554. )
  1555. ),
  1556. " ·"
  1557. )
  1558. ),
  1559. "url" =>
  1560. $this->fuckhtml
  1561. ->getTextContent(
  1562. $link[0]
  1563. ["attributes"]
  1564. ["href"]
  1565. ),
  1566. "date" => null
  1567. ];
  1568. continue;
  1569. }
  1570. // check if its list of small sublinks
  1571. $urls =
  1572. $this->fuckhtml
  1573. ->getElementsByTagName(
  1574. "a"
  1575. );
  1576. if(count($urls) !== 0){
  1577. // found small links
  1578. foreach($urls as $url){
  1579. $target =
  1580. $this->fuckhtml
  1581. ->getTextContent(
  1582. $url
  1583. ["attributes"]
  1584. ["href"]
  1585. );
  1586. if(
  1587. !preg_match(
  1588. '/^http/',
  1589. $target
  1590. )
  1591. ){
  1592. continue;
  1593. }
  1594. $web["sublink"][] = [
  1595. "title" =>
  1596. $this->titledots(
  1597. $this->fuckhtml
  1598. ->getTextContent(
  1599. $url
  1600. )
  1601. ),
  1602. "description" => null,
  1603. "url" => $target,
  1604. "date" => null
  1605. ];
  1606. }
  1607. continue;
  1608. }
  1609. // we probed everything, assume this is the description
  1610. // if we didn't find one cleanly previously
  1611. if($web["description"] === null){
  1612. $web["description"] =
  1613. $this->titledots(
  1614. $this->fuckhtml
  1615. ->getTextContent(
  1616. $cat
  1617. )
  1618. );
  1619. }
  1620. }
  1621. // check if description contains date
  1622. $description = explode("—", $web["description"], 2);
  1623. if(
  1624. count($description) === 2 &&
  1625. strlen($description[0]) <= 20
  1626. ){
  1627. $date = strtotime($description[0]);
  1628. if($date !== false){
  1629. $web["date"] = $date;
  1630. $web["description"] = ltrim($description[1]);
  1631. }
  1632. }
  1633. // fetch youtube thumbnail
  1634. $thumbnail =
  1635. $this->fuckhtml
  1636. ->getElementsByClassName(
  1637. $this->getstyle(
  1638. [
  1639. "border-radius" => "8px",
  1640. "height" => "fit-content",
  1641. "justify-content" => "center",
  1642. "margin-right" => "20px",
  1643. "margin-top" => "4px",
  1644. "position" => "relative",
  1645. "width" => "fit-content"
  1646. ]
  1647. ),
  1648. "div"
  1649. );
  1650. if(count($thumbnail) !== 0){
  1651. // load thumbnail container
  1652. $this->fuckhtml->load($thumbnail[0]);
  1653. $image =
  1654. $this->fuckhtml
  1655. ->getElementsByTagName(
  1656. "img"
  1657. );
  1658. if(
  1659. count($image) !== 0 &&
  1660. isset($image[0]["attributes"]["id"])
  1661. ){
  1662. $web["thumb"] = [
  1663. "url" =>
  1664. $this->unshit_thumb(
  1665. $this->getdimg(
  1666. $image[0]["attributes"]["id"]
  1667. )
  1668. ),
  1669. "ratio" => "16:9"
  1670. ];
  1671. }
  1672. // reset
  1673. $this->fuckhtml->load($result);
  1674. }
  1675. $out["web"][] = $web;
  1676. }
  1677. // reset
  1678. $this->fuckhtml->load($result_div);
  1679. //
  1680. // Get instant answers
  1681. //
  1682. $answer_containers =
  1683. $this->fuckhtml
  1684. ->getElementsByClassName(
  1685. $this->getstyle(
  1686. [
  1687. "padding-left" => "0px",
  1688. "padding-right" => "0px"
  1689. ]
  1690. ),
  1691. "div"
  1692. );
  1693. $date_class =
  1694. $this->getstyle(
  1695. [
  1696. "font-size" => "12px",
  1697. "line-height" => "1.34",
  1698. "display" => "inline-block",
  1699. "font-family" => "Google Sans,arial,sans-serif",
  1700. "padding-right" => "0",
  1701. "white-space" => "nowrap"
  1702. ]
  1703. );
  1704. foreach($answer_containers as $container){
  1705. $this->fuckhtml->load($container);
  1706. $web = [
  1707. "title" => null,
  1708. "description" => null,
  1709. "url" => null,
  1710. "date" => null,
  1711. "type" => "web",
  1712. "thumb" => [
  1713. "url" => null,
  1714. "ratio" => null
  1715. ],
  1716. "sublink" => [],
  1717. "table" => []
  1718. ];
  1719. $answers =
  1720. $this->fuckhtml
  1721. ->getElementsByAttributeName(
  1722. "aria-controls",
  1723. "div"
  1724. );
  1725. $item_insert_pos = 1;
  1726. foreach($answers as $answer){
  1727. $out["related"][] =
  1728. $this->fuckhtml
  1729. ->getTextContent(
  1730. $answer
  1731. );
  1732. if(
  1733. isset(
  1734. $this->blobs[
  1735. $answer
  1736. ["attributes"]
  1737. ["aria-controls"]
  1738. ]
  1739. )
  1740. ){
  1741. $this->fuckhtml->load(
  1742. $this->blobs[
  1743. $answer
  1744. ["attributes"]
  1745. ["aria-controls"]
  1746. ]
  1747. );
  1748. $divs =
  1749. $this->fuckhtml
  1750. ->getElementsByAttributeName(
  1751. "id",
  1752. "div"
  1753. );
  1754. foreach($divs as $div){
  1755. if(
  1756. !isset(
  1757. $this->blobs[
  1758. $div
  1759. ["attributes"]
  1760. ["id"]
  1761. ]
  1762. )
  1763. ){
  1764. continue;
  1765. }
  1766. $this->fuckhtml->load(
  1767. $this->blobs[
  1768. $div
  1769. ["attributes"]
  1770. ["id"]
  1771. ]
  1772. );
  1773. // get url
  1774. $as =
  1775. $this->fuckhtml
  1776. ->getElementsByTagName(
  1777. "a"
  1778. );
  1779. if(count($as) !== 0){
  1780. $web["url"] =
  1781. $this->unshiturl(
  1782. $as[0]["attributes"]["href"]
  1783. );
  1784. // skip entries that redirect to a search
  1785. if(
  1786. !preg_match(
  1787. '/^http/',
  1788. $web["url"]
  1789. )
  1790. ){
  1791. continue 3;
  1792. }
  1793. }
  1794. // get title
  1795. $h3 =
  1796. $this->fuckhtml
  1797. ->getElementsByTagName(
  1798. "h3"
  1799. );
  1800. if(count($h3) !== 0){
  1801. $web["title"] =
  1802. $this->titledots(
  1803. $this->fuckhtml
  1804. ->getTextContent(
  1805. $h3[0]
  1806. )
  1807. );
  1808. }
  1809. $description =
  1810. $this->fuckhtml
  1811. ->getElementsByAttributeValue(
  1812. "data-attrid",
  1813. "wa:/description",
  1814. "div"
  1815. );
  1816. if(count($description) !== 0){
  1817. // check for date
  1818. $this->fuckhtml->load($description[0]);
  1819. $date =
  1820. $this->fuckhtml
  1821. ->getElementsByClassName(
  1822. $date_class,
  1823. "span"
  1824. );
  1825. if(count($date) !== 0){
  1826. $description[0]["innerHTML"] =
  1827. str_replace(
  1828. $date[0]["outerHTML"],
  1829. "",
  1830. $description[0]["innerHTML"]
  1831. );
  1832. $web["date"] =
  1833. strtotime(
  1834. $this->fuckhtml
  1835. ->getTextContent(
  1836. $date[0]
  1837. )
  1838. );
  1839. }
  1840. $web["description"] =
  1841. ltrim(
  1842. $this->fuckhtml
  1843. ->getTextContent(
  1844. $description[0]
  1845. ),
  1846. ": "
  1847. );
  1848. }
  1849. }
  1850. foreach($out["web"] as $item){
  1851. if($item["url"] == $web["url"]){
  1852. continue 2;
  1853. }
  1854. }
  1855. array_splice($out["web"], $item_insert_pos, 0, [$web]);
  1856. $item_insert_pos++;
  1857. }
  1858. }
  1859. }
  1860. // reset
  1861. $this->fuckhtml->load($result_div);
  1862. //
  1863. // Scrape word definition
  1864. //
  1865. $definition_container =
  1866. $this->fuckhtml
  1867. ->getElementsByClassName(
  1868. "lr_container",
  1869. "div"
  1870. );
  1871. if(count($definition_container) !== 0){
  1872. $this->fuckhtml->load($definition_container[0]);
  1873. // get header
  1874. $header =
  1875. $this->fuckhtml
  1876. ->getElementsByAttributeValue(
  1877. "data-attrid",
  1878. "EntryHeader",
  1879. "div"
  1880. );
  1881. if(count($header) !== 0){
  1882. $description = [];
  1883. $this->fuckhtml->load($header[0]);
  1884. $title_div =
  1885. $this->fuckhtml
  1886. ->getElementsByClassName(
  1887. $this->getstyle(
  1888. [
  1889. "font-family" => "Google Sans,arial,sans-serif",
  1890. "font-size" => "28px",
  1891. "line-height" => "36px"
  1892. ]
  1893. )
  1894. );
  1895. if(count($title_div) !== 0){
  1896. $title =
  1897. $this->fuckhtml
  1898. ->getTextContent(
  1899. $title_div[0]
  1900. );
  1901. }else{
  1902. $title = "Word definition";
  1903. }
  1904. $subtext_div =
  1905. $this->fuckhtml
  1906. ->getElementsByClassName(
  1907. $this->getstyle(
  1908. [
  1909. "font-family" => "arial,sans-serif",
  1910. "font-size" => "14px",
  1911. "line-height" => "22px"
  1912. ]
  1913. ),
  1914. "span"
  1915. );
  1916. if(count($subtext_div) !== 0){
  1917. $description[] = [
  1918. "type" => "quote",
  1919. "value" =>
  1920. $this->fuckhtml
  1921. ->getTextContent(
  1922. $subtext_div[0]
  1923. )
  1924. ];
  1925. }
  1926. // get audio
  1927. $audio =
  1928. $this->fuckhtml
  1929. ->getElementsByTagName(
  1930. "audio"
  1931. );
  1932. if(count($audio) !== 0){
  1933. $this->fuckhtml->load($audio[0]);
  1934. $source =
  1935. $this->fuckhtml
  1936. ->getElementsByTagName(
  1937. "source"
  1938. );
  1939. if(count($source) !== 0){
  1940. $description[] = [
  1941. "type" => "audio",
  1942. "url" =>
  1943. preg_replace(
  1944. '/^\/\//',
  1945. "https://",
  1946. $this->fuckhtml
  1947. ->getTextContent(
  1948. $source[0]
  1949. ["attributes"]
  1950. ["src"]
  1951. )
  1952. )
  1953. ];
  1954. }
  1955. }
  1956. // remove header to avoid confusion
  1957. $definition_container[0]["innerHTML"] =
  1958. str_replace(
  1959. $header[0]["outerHTML"],
  1960. "",
  1961. $definition_container[0]["innerHTML"]
  1962. );
  1963. // reset
  1964. $this->fuckhtml->load($definition_container[0]);
  1965. $vmods =
  1966. $this->fuckhtml
  1967. ->getElementsByClassName(
  1968. "vmod",
  1969. "div"
  1970. );
  1971. foreach($vmods as $category){
  1972. if(
  1973. !isset(
  1974. $category
  1975. ["attributes"]
  1976. ["data-topic"]
  1977. ) ||
  1978. $category
  1979. ["attributes"]
  1980. ["class"] != "vmod"
  1981. ){
  1982. continue;
  1983. }
  1984. $this->fuckhtml->load($category);
  1985. // get category type
  1986. $type =
  1987. $this->fuckhtml
  1988. ->getElementsByTagName(
  1989. "i"
  1990. );
  1991. if(count($type) !== 0){
  1992. $description[] = [
  1993. "type" => "title",
  1994. "value" =>
  1995. $this->fuckhtml
  1996. ->getTextContent(
  1997. $type[0]
  1998. )
  1999. ];
  2000. }
  2001. // get heading text
  2002. $headings =
  2003. $this->fuckhtml
  2004. ->getElementsByClassName(
  2005. "xpdxpnd",
  2006. "div"
  2007. );
  2008. foreach($headings as $heading){
  2009. $description[] = [
  2010. "type" => "quote",
  2011. "value" =>
  2012. $this->fuckhtml
  2013. ->getTextContent(
  2014. $heading
  2015. )
  2016. ];
  2017. }
  2018. $definitions =
  2019. $this->fuckhtml
  2020. ->getElementsByAttributeValue(
  2021. "data-attrid",
  2022. "SenseDefinition",
  2023. "div"
  2024. );
  2025. $i = 1;
  2026. $text = [];
  2027. foreach($definitions as $definition){
  2028. $text[] =
  2029. $i . ". " .
  2030. $this->fuckhtml
  2031. ->getTextContent(
  2032. $definition
  2033. );
  2034. $i++;
  2035. }
  2036. if(count($text) !== 0){
  2037. $description[] = [
  2038. "type" => "text",
  2039. "value" =>
  2040. implode("\n", $text)
  2041. ];
  2042. }
  2043. }
  2044. $out["answer"][] = [
  2045. "title" => $title,
  2046. "description" => $description,
  2047. "url" => null,
  2048. "thumb" => null,
  2049. "table" => [],
  2050. "sublink" => []
  2051. ];
  2052. }
  2053. // reset
  2054. $this->fuckhtml->load($result_div);
  2055. }
  2056. //
  2057. // scrape elements with a g-section-with-header
  2058. // includes: images, news carousels
  2059. //
  2060. $g_sections =
  2061. $this->fuckhtml
  2062. ->getElementsByTagName(
  2063. "g-section-with-header"
  2064. );
  2065. if(count($g_sections) !== 0){
  2066. foreach($g_sections as $g_section){
  2067. // parse elements with a g-section-with-header
  2068. $this->fuckhtml->load($g_section);
  2069. $div_title =
  2070. $this->fuckhtml
  2071. ->getElementsByClassName(
  2072. "a-no-hover-decoration",
  2073. "a"
  2074. );
  2075. if(count($div_title) !== 0){
  2076. // title detected, skip
  2077. continue;
  2078. }
  2079. // no title detected: detect news container
  2080. $news =
  2081. $this->fuckhtml
  2082. ->getElementsByClassName(
  2083. $this->getstyle(
  2084. [
  2085. "outline-offset" => "-1px",
  2086. "display" => "flex",
  2087. "flex-direction" => "column",
  2088. "flex-grow" => "1"
  2089. ]
  2090. )
  2091. );
  2092. foreach($news as $new){
  2093. $this->fuckhtml->load($new);
  2094. $image =
  2095. $this->fuckhtml
  2096. ->getElementsByAttributeName(
  2097. "id",
  2098. "img"
  2099. );
  2100. if(
  2101. count($image) !== 0 &&
  2102. !(
  2103. isset($image[0]["attributes"]["style"]) &&
  2104. strpos(
  2105. $image[0]["attributes"]["style"],
  2106. "height:18px"
  2107. ) !== false
  2108. )
  2109. ){
  2110. $thumb = [
  2111. "url" =>
  2112. $this->getdimg(
  2113. $image[0]
  2114. ["attributes"]
  2115. ["id"]
  2116. ),
  2117. "ratio" => "1:1"
  2118. ];
  2119. }
  2120. $title =
  2121. $this->titledots(
  2122. $this->fuckhtml
  2123. ->getTextContent(
  2124. $this->fuckhtml
  2125. ->getElementsByAttributeValue(
  2126. "role",
  2127. "heading",
  2128. "div"
  2129. )[0]
  2130. )
  2131. );
  2132. $date_div =
  2133. $this->fuckhtml
  2134. ->getElementsByAttributeName(
  2135. "style",
  2136. "div"
  2137. );
  2138. if(count($date_div) !== 0){
  2139. foreach($date_div as $div){
  2140. if(
  2141. strpos(
  2142. $div["attributes"]["style"],
  2143. "bottom:"
  2144. ) !== false
  2145. ){
  2146. $date =
  2147. strtotime(
  2148. $this->fuckhtml
  2149. ->getTextContent(
  2150. $div
  2151. )
  2152. );
  2153. break;
  2154. }
  2155. }
  2156. }else{
  2157. $date = null;
  2158. }
  2159. $out["news"][] = [
  2160. "title" => $title,
  2161. "description" => null,
  2162. "date" => $date,
  2163. "thumb" => $thumb,
  2164. "url" =>
  2165. $this->fuckhtml
  2166. ->getTextContent(
  2167. $new
  2168. ["attributes"]
  2169. ["href"]
  2170. )
  2171. ];
  2172. }
  2173. }
  2174. // reset
  2175. $this->fuckhtml->load($result_div);
  2176. }
  2177. //
  2178. // Parse images (carousel, left hand-side)
  2179. //
  2180. $image_carousels =
  2181. $this->fuckhtml
  2182. ->getElementsByAttributeValue(
  2183. "id",
  2184. "media_result_group",
  2185. "div"
  2186. );
  2187. if(count($image_carousels) !== 0){
  2188. foreach($image_carousels as $image_carousel){
  2189. $this->fuckhtml->load($image_carousel);
  2190. // get related searches in image carousel
  2191. $relateds =
  2192. $this->fuckhtml
  2193. ->getElementsByClassName(
  2194. $this->getstyle(
  2195. [
  2196. "display" => "inline-block",
  2197. "margin-right" => "6px",
  2198. "outline" => "none",
  2199. "padding" => "6px 0"
  2200. ],
  2201. "a"
  2202. )
  2203. );
  2204. foreach($relateds as $related){
  2205. if(!isset($related["innerHTML"])){
  2206. // found an image
  2207. continue;
  2208. }
  2209. $text =
  2210. $this->fuckhtml
  2211. ->getTextContent(
  2212. $related
  2213. );
  2214. if($text != ""){
  2215. $out["related"][] = $text;
  2216. }
  2217. }
  2218. $div =
  2219. $this->fuckhtml
  2220. ->getElementsByTagName(
  2221. "div"
  2222. );
  2223. // get loaded images
  2224. $images =
  2225. $this->fuckhtml
  2226. ->getElementsByClassName(
  2227. "ivg-i",
  2228. $div
  2229. );
  2230. foreach($images as $image){
  2231. $this->fuckhtml->load($image);
  2232. $img_tags =
  2233. $this->fuckhtml
  2234. ->getElementsByTagName(
  2235. "img"
  2236. );
  2237. if(
  2238. !isset($image["attributes"]["data-docid"]) ||
  2239. !isset($this->image_arr[$image["attributes"]["data-docid"]])
  2240. ){
  2241. continue;
  2242. }
  2243. // search for the right image tag
  2244. $image_tag = false;
  2245. foreach($img_tags as $img){
  2246. if(
  2247. isset(
  2248. $img
  2249. ["attributes"]
  2250. ["alt"]
  2251. ) &&
  2252. trim(
  2253. $img
  2254. ["attributes"]
  2255. ["alt"]
  2256. ) != ""
  2257. ){
  2258. $image_tag = $img;
  2259. break;
  2260. }
  2261. }
  2262. if($image_tag === false){
  2263. continue;
  2264. }
  2265. $out["image"][] = [
  2266. "title" =>
  2267. $this->titledots(
  2268. $this->fuckhtml
  2269. ->getTextContent(
  2270. $image_tag
  2271. ["attributes"]
  2272. ["alt"]
  2273. )
  2274. ),
  2275. "source" =>
  2276. $this->image_arr[
  2277. $image
  2278. ["attributes"]
  2279. ["data-docid"]
  2280. ],
  2281. "url" =>
  2282. $this->fuckhtml
  2283. ->getTextContent(
  2284. $image
  2285. ["attributes"]
  2286. ["data-lpage"]
  2287. )
  2288. ];
  2289. }
  2290. // get unloaded javascript images
  2291. $images_js_sel =
  2292. $this->fuckhtml
  2293. ->getElementsByAttributeName(
  2294. "id",
  2295. $div
  2296. );
  2297. $loaded = [];
  2298. foreach($images_js_sel as $sel){
  2299. if(
  2300. !isset($this->blobs[$sel["attributes"]["id"]]) ||
  2301. in_array((string)$sel["attributes"]["id"], $loaded, true)
  2302. ){
  2303. // not an unloaded javascript image
  2304. continue;
  2305. }
  2306. $loaded[] = $sel["attributes"]["id"];
  2307. // get yet another javascript component
  2308. $this->fuckhtml->load($this->blobs[$sel["attributes"]["id"]]);
  2309. // get js node: contains title & url
  2310. $js_node =
  2311. $this->fuckhtml
  2312. ->getElementsByTagName(
  2313. "div"
  2314. )[0];
  2315. if(!isset($this->blobs[$js_node["attributes"]["id"]])){
  2316. // did not find refer id
  2317. continue;
  2318. }
  2319. // load second javascript component
  2320. $this->fuckhtml->load($this->blobs[$js_node["attributes"]["id"]]);
  2321. // get title from image alt text.
  2322. // data-src from this image is cropped, ignore it..
  2323. $img =
  2324. $this->fuckhtml
  2325. ->getElementsByTagName(
  2326. "img"
  2327. )[0];
  2328. $out["image"][] = [
  2329. "title" =>
  2330. $this->fuckhtml
  2331. ->getTextContent(
  2332. $img["attributes"]["alt"]
  2333. ),
  2334. "source" =>
  2335. $this->image_arr[
  2336. $js_node["attributes"]["data-docid"]
  2337. ],
  2338. "url" =>
  2339. $this->fuckhtml
  2340. ->getTextContent(
  2341. $js_node["attributes"]["data-lpage"]
  2342. )
  2343. ];
  2344. }
  2345. }
  2346. // reset
  2347. $this->fuckhtml->load($result_div);
  2348. }
  2349. //
  2350. // Parse videos
  2351. //
  2352. $this->fuckhtml->load($result_div);
  2353. $videos =
  2354. $this->fuckhtml
  2355. ->getElementsByAttributeName(
  2356. "data-vid",
  2357. "div"
  2358. );
  2359. foreach($videos as $video){
  2360. $this->fuckhtml->load($video);
  2361. // get url
  2362. $url =
  2363. $this->fuckhtml
  2364. ->getTextContent(
  2365. $video
  2366. ["attributes"]
  2367. ["data-surl"]
  2368. );
  2369. foreach($out["web"] as $link){
  2370. if($link["url"] == $url){
  2371. // ignore if we already have the video in $out["web"]
  2372. continue 2;
  2373. }
  2374. }
  2375. // get thumbnail
  2376. $image =
  2377. $this->fuckhtml
  2378. ->getElementsByAttributeName(
  2379. "id",
  2380. "img"
  2381. );
  2382. if(count($image) !== 0){
  2383. $thumb = [
  2384. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  2385. "ratio" => "16:9"
  2386. ];
  2387. }else{
  2388. $thumb = [
  2389. "url" => null,
  2390. "ratio" => null
  2391. ];
  2392. }
  2393. // get title
  2394. $title =
  2395. $this->fuckhtml
  2396. ->getElementsByClassName(
  2397. $this->getstyle(
  2398. [
  2399. "font-family" => "arial,sans-serif",
  2400. "font-size" => "16px",
  2401. "font-weight" => "400",
  2402. "line-height" => "24px"
  2403. ]
  2404. ),
  2405. "div"
  2406. );
  2407. if(count($title) === 0){
  2408. // ?? no title
  2409. continue;
  2410. }
  2411. $title =
  2412. $this->titledots(
  2413. $this->fuckhtml
  2414. ->getTextContent(
  2415. $title[0]
  2416. )
  2417. );
  2418. // get duration
  2419. $duration_div =
  2420. $this->fuckhtml
  2421. ->getElementsByClassName(
  2422. $this->getstyle(
  2423. [
  2424. "border-radius" => "10px",
  2425. "font-family" => "arial,sans-serif-medium,sans-serif",
  2426. "font-size" => "12px",
  2427. "line-height" => "16px",
  2428. "padding-block" => "2px",
  2429. "padding-inline" => "8px"
  2430. ]
  2431. ),
  2432. "div"
  2433. );
  2434. if(count($duration_div) !== 0){
  2435. $duration =
  2436. $this->hms2int(
  2437. $this->fuckhtml
  2438. ->getTextContent(
  2439. $duration_div[0]
  2440. )
  2441. );
  2442. }else{
  2443. // check if its a livestream
  2444. $duration =
  2445. $this->fuckhtml
  2446. ->getElementsByClassName(
  2447. $this->getstyle(
  2448. [
  2449. "background-color" => "#d93025",
  2450. "border-radius" => "10px",
  2451. "color" => "#fff",
  2452. "font-family" => "arial,sans-serif-medium,sans-serif",
  2453. "font-size" => "12px",
  2454. "line-height" => "16px",
  2455. "padding-block" => "2px",
  2456. "padding-inline" => "8px"
  2457. ]
  2458. ),
  2459. "span"
  2460. );
  2461. if(count($duration) !== 0){
  2462. $duration = "_LIVE";
  2463. }else{
  2464. $duration = null;
  2465. }
  2466. }
  2467. // get date
  2468. $date_div =
  2469. $this->fuckhtml
  2470. ->getElementsByClassName(
  2471. $this->getstyle(
  2472. [
  2473. "color" => "var(" . $this->getcolorvar("#70757a") . ")",
  2474. "font-size" => "14px"
  2475. ]
  2476. ),
  2477. "div"
  2478. );
  2479. if(count($date_div) !== 0){
  2480. $date = strtotime(
  2481. $this->fuckhtml
  2482. ->getTextContent(
  2483. $date_div[0]
  2484. )
  2485. );
  2486. if($date === false){
  2487. // failed to parse date
  2488. $date = null;
  2489. }
  2490. }else{
  2491. $date = null;
  2492. }
  2493. $out["video"][] = [
  2494. "title" => $title,
  2495. "description" => null,
  2496. "date" => $date,
  2497. "duration" => $duration,
  2498. "views" => null,
  2499. "thumb" => $thumb,
  2500. "url" => $url
  2501. ];
  2502. }
  2503. //
  2504. // Parse featured results (which contain images, fuck the rest desu)
  2505. //
  2506. $this->fuckhtml->load($html);
  2507. $top =
  2508. $this->fuckhtml
  2509. ->getElementsByAttributeValue(
  2510. "aria-label",
  2511. "Featured results",
  2512. "div"
  2513. );
  2514. if(count($top) !== 0){
  2515. $this->fuckhtml->load($top[0]);
  2516. // get images
  2517. $grid =
  2518. $this->fuckhtml
  2519. ->getElementsByClassName(
  2520. $this->getstyle(
  2521. [
  2522. "border-radius" => "20px",
  2523. "display" => "grid",
  2524. "grid-gap" => "2px",
  2525. "grid-template-rows" => "repeat(2,minmax(0,1fr))",
  2526. "overflow" => "hidden",
  2527. "bottom" => "0",
  2528. "left" => "0",
  2529. "right" => "0",
  2530. "top" => "0",
  2531. "position" => "absolute",
  2532. ]
  2533. ),
  2534. "div"
  2535. );
  2536. if(count($grid) !== 0){
  2537. // we found image grid
  2538. $this->fuckhtml->load($grid[0]);
  2539. $images_div =
  2540. $this->fuckhtml
  2541. ->getElementsByAttributeName(
  2542. "data-attrid",
  2543. "div"
  2544. );
  2545. foreach($images_div as $image_div){
  2546. $this->fuckhtml->load($image_div);
  2547. $image =
  2548. $this->fuckhtml
  2549. ->getElementsByTagName(
  2550. "img"
  2551. );
  2552. if(
  2553. count($image) === 0 ||
  2554. !isset($image_div["attributes"]["data-docid"]) ||
  2555. !isset($this->image_arr[$image_div["attributes"]["data-docid"]])
  2556. ){
  2557. // ?? no image, continue
  2558. continue;
  2559. }
  2560. $out["image"][] = [
  2561. "title" =>
  2562. $this->titledots(
  2563. $this->fuckhtml
  2564. ->getTextContent(
  2565. $image[0]["attributes"]["alt"]
  2566. )
  2567. ),
  2568. "source" =>
  2569. $this->image_arr[
  2570. $image_div["attributes"]["data-docid"]
  2571. ],
  2572. "url" =>
  2573. $this->fuckhtml
  2574. ->getTextContent(
  2575. $image_div["attributes"]["data-lpage"]
  2576. )
  2577. ];
  2578. }
  2579. }
  2580. }
  2581. //
  2582. // craft $npt token
  2583. //
  2584. if(
  2585. $last_page === false &&
  2586. count($out["web"]) !== 0
  2587. ){
  2588. if(!isset($params["start"])){
  2589. $params["start"] = 20;
  2590. }else{
  2591. $params["start"] += 20;
  2592. }
  2593. $out["npt"] =
  2594. $this->backend
  2595. ->store(
  2596. json_encode($params),
  2597. $pagetype,
  2598. $proxy
  2599. );
  2600. }
  2601. //
  2602. // Parse right handside
  2603. //
  2604. $this->fuckhtml->load($html);
  2605. $rhs =
  2606. $this->fuckhtml
  2607. ->getElementById(
  2608. "rhs"
  2609. );
  2610. if($rhs === null){
  2611. return $out;
  2612. }
  2613. $this->fuckhtml->load($rhs);
  2614. // get images gallery
  2615. $image_gallery =
  2616. $this->fuckhtml
  2617. ->getElementsByAttributeValue(
  2618. "data-rc",
  2619. "ivg-i",
  2620. "div"
  2621. );
  2622. if(count($image_gallery) !== 0){
  2623. $this->fuckhtml->load($image_gallery[0]);
  2624. // get images
  2625. $images_div =
  2626. $this->fuckhtml
  2627. ->getElementsByClassName(
  2628. "ivg-i",
  2629. "div"
  2630. );
  2631. foreach($images_div as $image_div){
  2632. $this->fuckhtml->load($image_div);
  2633. $image =
  2634. $this->fuckhtml
  2635. ->getElementsByTagName(
  2636. "img"
  2637. );
  2638. if(
  2639. count($image) === 0 ||
  2640. !isset(
  2641. $this->image_arr[
  2642. $image_div
  2643. ["attributes"]
  2644. ["data-docid"]
  2645. ]
  2646. )
  2647. ){
  2648. continue;
  2649. }
  2650. foreach($out["image"] as $existing_image){
  2651. // might already exist
  2652. if(
  2653. $existing_image["source"][1]["url"] ==
  2654. $this->image_arr[
  2655. $image_div
  2656. ["attributes"]
  2657. ["data-docid"]
  2658. ][1]["url"]
  2659. ){
  2660. continue 2;
  2661. }
  2662. }
  2663. $out["image"][] = [
  2664. "title" =>
  2665. $this->titledots(
  2666. $this->fuckhtml
  2667. ->getTextContent(
  2668. $image[0]
  2669. ["attributes"]
  2670. ["alt"]
  2671. )
  2672. ),
  2673. "source" =>
  2674. $this->image_arr[
  2675. $image_div
  2676. ["attributes"]
  2677. ["data-docid"]
  2678. ],
  2679. "url" =>
  2680. $this->fuckhtml
  2681. ->getTextContent(
  2682. $image_div
  2683. ["attributes"]
  2684. ["data-lpage"]
  2685. )
  2686. ];
  2687. }
  2688. // reset
  2689. $this->fuckhtml->load($rhs);
  2690. }
  2691. // get header container
  2692. $header =
  2693. $this->fuckhtml
  2694. ->getElementsByClassName(
  2695. $this->getstyle(
  2696. [
  2697. "padding" => "0 0 16px 20px",
  2698. "display" => "flex"
  2699. ]
  2700. ),
  2701. "div"
  2702. );
  2703. // stop parsing wikipedia heads if there isn't a header
  2704. $description = [];
  2705. $title = "About";
  2706. if(count($header) !== 0){
  2707. $this->fuckhtml->load($header[0]);
  2708. // g-snackbar-action present: we found a button instead
  2709. if(
  2710. count(
  2711. $this->fuckhtml
  2712. ->getElementsByTagName(
  2713. "g-snackbar-action"
  2714. )
  2715. ) !== 0
  2716. ){
  2717. $title_tag =
  2718. $this->fuckhtml
  2719. ->getElementsByAttributeValue(
  2720. "data-attrid",
  2721. "title",
  2722. "div"
  2723. );
  2724. if(count($title_tag) !== 0){
  2725. $title =
  2726. $this->fuckhtml
  2727. ->getTextContent(
  2728. $title_tag[0]
  2729. );
  2730. $header[0]["innerHTML"] =
  2731. str_replace(
  2732. $title_tag[0]["outerHTML"],
  2733. "",
  2734. $header[0]["innerHTML"]
  2735. );
  2736. // if header still contains text, add it as a subtitle in description
  2737. $subtitle =
  2738. $this->fuckhtml
  2739. ->getTextContent(
  2740. $header[0]
  2741. );
  2742. if(strlen($subtitle) !== 0){
  2743. $description[] = [
  2744. "type" => "quote",
  2745. "value" => $subtitle
  2746. ];
  2747. }
  2748. }
  2749. }
  2750. // reset
  2751. $this->fuckhtml->load($rhs);
  2752. }
  2753. // get description elements
  2754. $url = null;
  2755. $text =
  2756. $this->fuckhtml
  2757. ->getElementsByAttributeValue(
  2758. "data-attrid",
  2759. "description",
  2760. "div"
  2761. );
  2762. if(count($text) !== 0){
  2763. $this->fuckhtml->load($text[0]);
  2764. $a =
  2765. $this->fuckhtml
  2766. ->getElementsByTagName(
  2767. "a"
  2768. );
  2769. if(count($a) !== 0){
  2770. // get link and remove it from description
  2771. $a = $a[count($a) - 1];
  2772. $text[0]["innerHTML"] =
  2773. str_replace(
  2774. $a["outerHTML"],
  2775. "",
  2776. $text[0]["innerHTML"]
  2777. );
  2778. $url =
  2779. $this->fuckhtml
  2780. ->getTextContent(
  2781. $a
  2782. ["attributes"]
  2783. ["href"]
  2784. );
  2785. }
  2786. $description[] = [
  2787. "type" => "text",
  2788. "value" =>
  2789. html_entity_decode(
  2790. preg_replace(
  2791. '/^Description/',
  2792. "",
  2793. $this->fuckhtml
  2794. ->getTextContent(
  2795. $text[0]
  2796. )
  2797. )
  2798. )
  2799. ];
  2800. // reset
  2801. $this->fuckhtml->load($rhs);
  2802. }
  2803. // get reviews (google play, steam, etc)
  2804. $review_container =
  2805. $this->fuckhtml
  2806. ->getElementsByClassName(
  2807. $this->getstyle(
  2808. [
  2809. "align-items" => "start",
  2810. "display" => "flex"
  2811. ]
  2812. ),
  2813. "div"
  2814. );
  2815. if(count($review_container) !== 0){
  2816. $this->fuckhtml->load($review_container[0]);
  2817. $as =
  2818. $this->fuckhtml
  2819. ->getElementsByTagName(
  2820. "a"
  2821. );
  2822. if(count($as) !== 0){
  2823. $description[] = [
  2824. "type" => "title",
  2825. "value" => "Ratings"
  2826. ];
  2827. foreach($as as $a){
  2828. $this->fuckhtml->load($a);
  2829. $spans =
  2830. $this->fuckhtml
  2831. ->getElementsByTagName(
  2832. "span"
  2833. );
  2834. if(count($spans) >= 2){
  2835. $value =
  2836. trim(
  2837. $this->fuckhtml
  2838. ->getTextContent(
  2839. $spans[1]
  2840. ),
  2841. "· "
  2842. );
  2843. if(
  2844. $value == "" &&
  2845. isset($spans[2])
  2846. ){
  2847. $value =
  2848. $this->fuckhtml
  2849. ->getTextContent(
  2850. $spans[2]
  2851. );
  2852. }
  2853. $description[] = [
  2854. "type" => "link",
  2855. "url" =>
  2856. $this->fuckhtml
  2857. ->getTextContent(
  2858. $a["attributes"]
  2859. ["href"]
  2860. ),
  2861. "value" => $value
  2862. ];
  2863. $description[] = [
  2864. "type" => "text",
  2865. "value" =>
  2866. ": " .
  2867. $this->fuckhtml
  2868. ->getTextContent(
  2869. $spans[0]
  2870. ) . "\n"
  2871. ];
  2872. }
  2873. }
  2874. }
  2875. // reset
  2876. $this->fuckhtml->load($rhs);
  2877. }
  2878. // initialize sublinks
  2879. $sublinks = [];
  2880. // get description from business
  2881. if(count($description) === 0){
  2882. $data_attrid =
  2883. $this->fuckhtml
  2884. ->getElementsByAttributeName(
  2885. "data-attrid"
  2886. );
  2887. $summary =
  2888. $this->fuckhtml
  2889. ->getElementsByAttributeValue(
  2890. "data-attrid",
  2891. "kc:/local:one line summary",
  2892. $data_attrid
  2893. );
  2894. if(count($summary) !== 0){
  2895. $description[] = [
  2896. "type" => "quote",
  2897. "value" =>
  2898. $this->fuckhtml
  2899. ->getTextContent(
  2900. $summary[0]
  2901. )
  2902. ];
  2903. // remove summary so it doesnt get parsed as a table
  2904. $rhs["innerHTML"] =
  2905. str_replace(
  2906. $summary[0]["outerHTML"],
  2907. "",
  2908. $rhs["innerHTML"]
  2909. );
  2910. $this->fuckhtml->load($rhs);
  2911. }
  2912. $address =
  2913. $this->fuckhtml
  2914. ->getElementsByAttributeValue(
  2915. "data-attrid",
  2916. "kc:/location/location:address",
  2917. $data_attrid
  2918. );
  2919. if(count($address) !== 0){
  2920. $description[] = [
  2921. "type" => "text",
  2922. "value" =>
  2923. $this->fuckhtml
  2924. ->getTextContent(
  2925. $address[0]
  2926. )
  2927. ];
  2928. }
  2929. // get title
  2930. $title_div =
  2931. $this->fuckhtml
  2932. ->getElementsByAttributeValue(
  2933. "data-attrid",
  2934. "title",
  2935. $data_attrid
  2936. );
  2937. if(count($title_div) !== 0){
  2938. $title =
  2939. $this->fuckhtml
  2940. ->getTextContent(
  2941. $title_div[0]
  2942. );
  2943. }
  2944. // get phone number
  2945. $phone =
  2946. $this->fuckhtml
  2947. ->getElementsByAttributeValue(
  2948. "data-attrid",
  2949. "kc:/local:alt phone",
  2950. $data_attrid
  2951. );
  2952. if(count($phone) !== 0){
  2953. $this->fuckhtml->load($phone[0]);
  2954. $sublinks["Call"] =
  2955. "tel:" .
  2956. $this->fuckhtml
  2957. ->getTextContent(
  2958. $this->fuckhtml
  2959. ->getElementsByAttributeName(
  2960. "aria-label",
  2961. "span"
  2962. )[0]
  2963. );
  2964. $this->fuckhtml->load($rhs);
  2965. }
  2966. }
  2967. if(count($description) === 0){
  2968. // still no description? abort
  2969. return $out;
  2970. }
  2971. // get table elements
  2972. $table = [];
  2973. $table_elems =
  2974. $this->fuckhtml
  2975. ->getElementsByClassName(
  2976. $this->getstyle(
  2977. [
  2978. "margin-top" => "7px"
  2979. ]
  2980. ),
  2981. "div"
  2982. );
  2983. foreach($table_elems as $elem){
  2984. $this->fuckhtml->load($elem);
  2985. $spans =
  2986. $this->fuckhtml
  2987. ->getElementsByTagName(
  2988. "span"
  2989. );
  2990. if(count($spans) === 0){
  2991. // ?? invalid
  2992. continue;
  2993. }
  2994. $elem["innerHTML"] =
  2995. str_replace(
  2996. $spans[0]["outerHTML"],
  2997. "",
  2998. $elem["innerHTML"]
  2999. );
  3000. $key =
  3001. rtrim(
  3002. $this->fuckhtml
  3003. ->getTextContent(
  3004. $spans[0]
  3005. ),
  3006. ": "
  3007. );
  3008. if(
  3009. $key == "" ||
  3010. $key == "Phone"
  3011. ){
  3012. continue;
  3013. }
  3014. if($key == "Hours"){
  3015. $hours = [];
  3016. $this->fuckhtml->load($elem);
  3017. $trs =
  3018. $this->fuckhtml
  3019. ->getElementsByTagName(
  3020. "tr"
  3021. );
  3022. foreach($trs as $tr){
  3023. $this->fuckhtml->load($tr);
  3024. $tds =
  3025. $this->fuckhtml
  3026. ->getElementsByTagName(
  3027. "td"
  3028. );
  3029. if(count($tds) === 2){
  3030. $hours[] =
  3031. $this->fuckhtml
  3032. ->getTextContent(
  3033. $tds[0]
  3034. ) . ": " .
  3035. $this->fuckhtml
  3036. ->getTextContent(
  3037. $tds[1]
  3038. );
  3039. }
  3040. }
  3041. if(count($hours) !== 0){
  3042. $hours = implode("\n", $hours);
  3043. $table["Hours"] = $hours;
  3044. }
  3045. continue;
  3046. }
  3047. $table[$key] =
  3048. preg_replace(
  3049. '/ +/',
  3050. " ",
  3051. $this->fuckhtml
  3052. ->getTextContent(
  3053. $elem
  3054. )
  3055. );
  3056. }
  3057. // reset
  3058. $this->fuckhtml->load($rhs);
  3059. // get the website div
  3060. $as =
  3061. $this->fuckhtml
  3062. ->getElementsByAttributeValue(
  3063. "data-attrid",
  3064. "visit_official_site",
  3065. "a"
  3066. );
  3067. if(count($as) !== 0){
  3068. $sublinks["Website"] =
  3069. str_replace(
  3070. "http://",
  3071. "https://",
  3072. $this->fuckhtml
  3073. ->getTextContent(
  3074. $as[0]
  3075. ["attributes"]
  3076. ["href"]
  3077. )
  3078. );
  3079. }else{
  3080. // get website through button
  3081. $button =
  3082. $this->fuckhtml
  3083. ->getElementsByClassName(
  3084. "ab_button",
  3085. "a"
  3086. );
  3087. if(count($button) !== 0){
  3088. $sublinks["Website"] =
  3089. $this->unshiturl(
  3090. $this->fuckhtml
  3091. ->getTextContent(
  3092. $button[0]
  3093. ["attributes"]
  3094. ["href"]
  3095. )
  3096. );
  3097. }
  3098. }
  3099. // get social media links
  3100. $as =
  3101. $this->fuckhtml
  3102. ->getElementsByTagName(
  3103. "g-link"
  3104. );
  3105. foreach($as as $a){
  3106. $this->fuckhtml->load($a);
  3107. $link =
  3108. $this->fuckhtml
  3109. ->getElementsByTagName(
  3110. "a"
  3111. );
  3112. if(count($link) === 0){
  3113. continue;
  3114. }
  3115. $sublink_title =
  3116. $this->fuckhtml
  3117. ->getTextContent(
  3118. $a
  3119. );
  3120. if($sublink_title == "X (Twitter)"){
  3121. $sublink_title = "Twitter";
  3122. }
  3123. $sublinks[$sublink_title] =
  3124. $this->fuckhtml
  3125. ->getTextContent(
  3126. $link[0]
  3127. ["attributes"]
  3128. ["href"]
  3129. );
  3130. }
  3131. // reset
  3132. $this->fuckhtml->load($rhs);
  3133. // get those round containers
  3134. $containers =
  3135. $this->fuckhtml
  3136. ->getElementsByClassName(
  3137. "tpa-ci"
  3138. );
  3139. foreach($containers as $container){
  3140. $this->fuckhtml->load($container);
  3141. $as =
  3142. $this->fuckhtml
  3143. ->getElementsByTagName(
  3144. "a"
  3145. );
  3146. if(count($as) === 0){
  3147. continue;
  3148. }
  3149. $sublinks[
  3150. $this->fuckhtml
  3151. ->getTextContent(
  3152. $as[0]
  3153. )
  3154. ] =
  3155. $this->fuckhtml
  3156. ->getTextContent(
  3157. $as[0]
  3158. ["attributes"]
  3159. ["href"]
  3160. );
  3161. }
  3162. $out["answer"][] = [
  3163. "title" => $title,
  3164. "description" => $description,
  3165. "url" => $url,
  3166. "thumb" => null,
  3167. "table" => $table,
  3168. "sublink" => $sublinks
  3169. ];
  3170. return $out;
  3171. }
  3172. private function scrape_dimg($html){
  3173. // get images loaded through javascript
  3174. $this->dimg = [];
  3175. preg_match_all(
  3176. '/function\(\){google\.ldi=({.*?});/',
  3177. $html,
  3178. $dimg
  3179. );
  3180. if(isset($dimg[1])){
  3181. foreach($dimg[1] as $i){
  3182. $tmp = json_decode($i, true);
  3183. foreach($tmp as $key => $value){
  3184. $this->dimg[$key] =
  3185. $this->unshit_thumb(
  3186. $value
  3187. );
  3188. }
  3189. }
  3190. }
  3191. // get additional javascript base64 images
  3192. preg_match_all(
  3193. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  3194. $html,
  3195. $dimg
  3196. );
  3197. if(isset($dimg[1])){
  3198. for($i=0; $i<count($dimg[1]); $i++){
  3199. $delims = explode(",", $dimg[2][$i]);
  3200. $string =
  3201. $this->fuckhtml
  3202. ->parseJsString(
  3203. $dimg[1][$i]
  3204. );
  3205. foreach($delims as $delim){
  3206. $this->dimg[trim($delim, "'")] = $string;
  3207. }
  3208. }
  3209. }
  3210. }
  3211. private function scrape_imagearr($html){
  3212. // get image links arrays
  3213. preg_match_all(
  3214. '/\[0,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  3215. $html,
  3216. $image_arr
  3217. );
  3218. $this->image_arr = [];
  3219. if(isset($image_arr[1])){
  3220. for($i=0; $i<count($image_arr[1]); $i++){
  3221. $this->image_arr[$image_arr[1][$i]] =
  3222. [
  3223. [
  3224. "url" =>
  3225. $this->fuckhtml
  3226. ->parseJsString(
  3227. $image_arr[5][$i]
  3228. ),
  3229. "width" => (int)$image_arr[7][$i],
  3230. "height" => (int)$image_arr[6][$i]
  3231. ],
  3232. [
  3233. "url" =>
  3234. $this->unshit_thumb(
  3235. $this->fuckhtml
  3236. ->parseJsString(
  3237. $image_arr[2][$i]
  3238. )
  3239. ),
  3240. "width" => (int)$image_arr[4][$i],
  3241. "height" => (int)$image_arr[3][$i]
  3242. ]
  3243. ];
  3244. }
  3245. }
  3246. }
  3247. private function getdimg($dimg){
  3248. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  3249. }
  3250. private function unshit_thumb($url){
  3251. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  3252. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  3253. $parts = parse_url($url);
  3254. if(
  3255. isset($parts["host"]) &&
  3256. preg_match(
  3257. '/tbn.*\.gstatic\.com/',
  3258. $parts["host"]
  3259. )
  3260. ){
  3261. parse_str($parts["query"], $params);
  3262. if(isset($params["q"])){
  3263. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  3264. }
  3265. }
  3266. return $url;
  3267. }
  3268. private function parsestyles(){
  3269. $styles = [];
  3270. $style_div =
  3271. $this->fuckhtml
  3272. ->getElementsByTagName(
  3273. "style"
  3274. );
  3275. $raw_styles = "";
  3276. foreach($style_div as $style){
  3277. $raw_styles .= $style["innerHTML"];
  3278. }
  3279. // filter out media/keyframe queries
  3280. $raw_styles =
  3281. preg_replace(
  3282. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  3283. "",
  3284. $raw_styles
  3285. );
  3286. // get styles
  3287. preg_match_all(
  3288. '/(.+?){([\S\s]*?)}/',
  3289. $raw_styles,
  3290. $matches
  3291. );
  3292. for($i=0; $i<count($matches[1]); $i++){
  3293. // get style values
  3294. preg_match_all(
  3295. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  3296. $matches[2][$i],
  3297. $values_regex
  3298. );
  3299. $values = [];
  3300. for($k=0; $k<count($values_regex[1]); $k++){
  3301. $values[trim($values_regex[1][$k])] =
  3302. trim($values_regex[2][$k]);
  3303. }
  3304. $names = explode(",", $matches[1][$i]);
  3305. // h1,h2,h3 will each get their own array index
  3306. foreach($names as $name){
  3307. $name = trim($name, "}\t\n\r\0\x0B");
  3308. foreach($values as $key => $value){
  3309. $styles[$name][$key] = $value;
  3310. }
  3311. }
  3312. }
  3313. foreach($styles as $key => $values){
  3314. $styles[$key]["_c"] = count($values);
  3315. }
  3316. $this->styles = $styles;
  3317. // get CSS colors
  3318. $this->css_colors = [];
  3319. if(isset($this->styles[":root"])){
  3320. foreach($this->styles[":root"] as $key => $value){
  3321. $this->css_colors[$value] = $key;
  3322. }
  3323. }
  3324. }
  3325. private function getstyle($styles){
  3326. $styles["_c"] = count($styles);
  3327. foreach($this->styles as $style_key => $style_values){
  3328. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  3329. $style_key =
  3330. explode(" ", $style_key);
  3331. $style_key = $style_key[count($style_key) - 1];
  3332. return
  3333. ltrim(
  3334. str_replace(
  3335. [".", "#"],
  3336. " ",
  3337. $style_key
  3338. )
  3339. );
  3340. }
  3341. }
  3342. return false;
  3343. }
  3344. private function getcolorvar($color){
  3345. if(isset($this->css_colors[$color])){
  3346. return $this->css_colors[$color];
  3347. }
  3348. return null;
  3349. }
  3350. public function web($get){
  3351. if($get["npt"]){
  3352. [$params, $proxy] = $this->backend->get($get["npt"], "web");
  3353. $params = json_decode($params, true);
  3354. $search = $params["q"];
  3355. }else{
  3356. $search = $get["s"];
  3357. $country = $get["country"];
  3358. $nsfw = $get["nsfw"];
  3359. $lang = $get["lang"];
  3360. $older = $get["older"];
  3361. $newer = $get["newer"];
  3362. $spellcheck = $get["spellcheck"];
  3363. $proxy = $this->backend->get_ip();
  3364. $offset = 0;
  3365. $params = [
  3366. "q" => $search,
  3367. "hl" => "en",
  3368. "num" => 20 // get 20 results
  3369. ];
  3370. // country
  3371. if($country != "any"){
  3372. $params["gl"] = $country;
  3373. }
  3374. // nsfw
  3375. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  3376. // language
  3377. if($lang != "any"){
  3378. $params["lr"] = "lang_" . $lang;
  3379. }
  3380. // generate tbs
  3381. $tbs = [];
  3382. // get date
  3383. $older = $older === false ? null : date("m/d/Y", $older);
  3384. $newer = $newer === false ? null : date("m/d/Y", $newer);
  3385. if(
  3386. $older !== null ||
  3387. $newer !== null
  3388. ){
  3389. $tbs["cdr"] = "1";
  3390. $tbs["cd_min"] = $newer;
  3391. $tbs["cd_max"] = $older;
  3392. }
  3393. // spellcheck filter
  3394. if($spellcheck == "no"){
  3395. $params["nfpr"] = "1";
  3396. }
  3397. if(count($tbs) !== 0){
  3398. $params["tbs"] = "";
  3399. foreach($tbs as $key => $value){
  3400. $params["tbs"] .= $key . ":" . $value . ",";
  3401. }
  3402. $params["tbs"] = rtrim($params["tbs"], ",");
  3403. }
  3404. }
  3405. try{
  3406. $html =
  3407. $this->get(
  3408. $proxy,
  3409. "https://www.google.com/search",
  3410. $params
  3411. );
  3412. }catch(Exception $error){
  3413. throw new Exception("Failed to get HTML");
  3414. }
  3415. //$html = file_get_contents("scraper/google.html");
  3416. return $this->parsepage($html, "web", $search, $proxy, $params);
  3417. }
  3418. public function video($get){
  3419. if($get["npt"]){
  3420. [$params, $proxy] = $this->backend->get($get["npt"], "web");
  3421. $params = json_decode($params, true);
  3422. $search = $params["q"];
  3423. }else{
  3424. $search = $get["s"];
  3425. $country = $get["country"];
  3426. $nsfw = $get["nsfw"];
  3427. $older = $get["older"];
  3428. $newer = $get["newer"];
  3429. $duration = $get["duration"];
  3430. $quality = $get["quality"];
  3431. $captions = $get["captions"];
  3432. $proxy = $this->backend->get_ip();
  3433. $params = [
  3434. "q" => $search,
  3435. "tbm" => "vid",
  3436. "hl" => "en",
  3437. "num" => "20"
  3438. ];
  3439. // country
  3440. if($country != "any"){
  3441. $params["gl"] = $country;
  3442. }
  3443. // nsfw
  3444. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  3445. $tbs = [];
  3446. // get date
  3447. $older = $older === false ? null : date("m/d/Y", $older);
  3448. $newer = $newer === false ? null : date("m/d/Y", $newer);
  3449. if(
  3450. $older !== null ||
  3451. $newer !== null
  3452. ){
  3453. $tbs["cdr"] = "1";
  3454. $tbs["cd_min"] = $newer;
  3455. $tbs["cd_max"] = $older;
  3456. }
  3457. // duration
  3458. if($duration != "any"){
  3459. $tbs[] = "dur:" . $duration;
  3460. }
  3461. // quality
  3462. if($quality != "any"){
  3463. $tbs[] = "hq:" . $quality;
  3464. }
  3465. // captions
  3466. if($captions != "any"){
  3467. $tbs[] = "cc:" . $captions;
  3468. }
  3469. // append tbs
  3470. if(count($tbs) !== 0){
  3471. $params["tbs"] =
  3472. implode(",", $tbs);
  3473. }
  3474. }
  3475. try{
  3476. $html =
  3477. $this->get(
  3478. $proxy,
  3479. "https://www.google.com/search",
  3480. $params
  3481. );
  3482. }catch(Exception $error){
  3483. throw new Exception("Failed to get HTML");
  3484. }
  3485. //$html = file_get_contents("scraper/google-video.html");
  3486. $response = $this->parsepage($html, "videos", $search, $proxy, $params);
  3487. $out = [
  3488. "status" => "ok",
  3489. "npt" => $response["npt"],
  3490. "video" => [],
  3491. "author" => [],
  3492. "livestream" => [],
  3493. "playlist" => [],
  3494. "reel" => []
  3495. ];
  3496. foreach($response["web"] as $result){
  3497. $out["video"][] = [
  3498. "title" => $result["title"],
  3499. "description" => $result["description"],
  3500. "author" => [
  3501. "name" => isset($result["table"]["Author"]) ? $result["table"]["Author"] : null,
  3502. "url" => null,
  3503. "avatar" => null
  3504. ],
  3505. "date" => $result["date"],
  3506. "duration" => isset($result["table"]["Duration"]) ? $this->hms2int($result["table"]["Duration"]) : null,
  3507. "views" => null,
  3508. "thumb" => $result["thumb"],
  3509. "url" => $result["url"]
  3510. ];
  3511. }
  3512. return $out;
  3513. }
  3514. public function news($get){
  3515. if($get["npt"]){
  3516. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  3517. /*parse_str(
  3518. parse_url($req, PHP_URL_QUERY),
  3519. $search
  3520. );*/
  3521. try{
  3522. $html =
  3523. $this->get(
  3524. $proxy,
  3525. "https://www.google.com" . $req,
  3526. []
  3527. );
  3528. }catch(Exception $error){
  3529. throw new Exception("Failed to get HTML");
  3530. }
  3531. }else{
  3532. $search = $get["s"];
  3533. $country = $get["country"];
  3534. $nsfw = $get["nsfw"];
  3535. $older = $get["older"];
  3536. $newer = $get["newer"];
  3537. $sort = $get["sort"];
  3538. $proxy = $this->backend->get_ip();
  3539. $params = [
  3540. "q" => $search,
  3541. "tbm" => "nws",
  3542. "hl" => "en",
  3543. "num" => "20"
  3544. ];
  3545. // country
  3546. if($country != "any"){
  3547. $params["gl"] = $country;
  3548. }
  3549. // nsfw
  3550. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  3551. $tbs = [];
  3552. // get date
  3553. $older = $older === false ? null : date("m/d/Y", $older);
  3554. $newer = $newer === false ? null : date("m/d/Y", $newer);
  3555. if(
  3556. $older !== null ||
  3557. $newer !== null
  3558. ){
  3559. $tbs["cdr"] = "1";
  3560. $tbs["cd_min"] = $newer;
  3561. $tbs["cd_max"] = $older;
  3562. }
  3563. // relevance
  3564. if($sort == "date"){
  3565. $tbs["sbd"] = "1";
  3566. }
  3567. // append tbs
  3568. if(count($tbs) !== 0){
  3569. $params["tbs"] = "";
  3570. foreach($tbs as $key => $value){
  3571. $params["tbs"] .= $key . ":" . $value . ",";
  3572. }
  3573. $params["tbs"] = rtrim($params["tbs"], ",");
  3574. }
  3575. //$html = file_get_contents("scraper/google-news.html");
  3576. $html =
  3577. $this->get(
  3578. $proxy,
  3579. "https://www.google.com/search",
  3580. $params
  3581. );
  3582. }
  3583. $out = [
  3584. "status" => "ok",
  3585. "npt" => null,
  3586. "news" => []
  3587. ];
  3588. $this->fuckhtml->load($html);
  3589. $this->detect_sorry();
  3590. // get images
  3591. $this->scrape_dimg($html);
  3592. // parse styles
  3593. $this->parsestyles();
  3594. $center_col =
  3595. $this->fuckhtml
  3596. ->getElementById(
  3597. "center_col",
  3598. "div"
  3599. );
  3600. if($center_col === null){
  3601. throw new Exception("Could not grep result div");
  3602. }
  3603. $this->fuckhtml->load($center_col);
  3604. // get next page
  3605. $npt =
  3606. $this->fuckhtml
  3607. ->getElementById(
  3608. "pnnext",
  3609. "a"
  3610. );
  3611. if($npt !== false){
  3612. $out["npt"] =
  3613. $this->backend->store(
  3614. $this->fuckhtml
  3615. ->getTextContent(
  3616. $npt["attributes"]
  3617. ["href"]
  3618. ),
  3619. "news",
  3620. $proxy
  3621. );
  3622. }
  3623. $as =
  3624. $this->fuckhtml
  3625. ->getElementsByAttributeName(
  3626. "jsname",
  3627. "a"
  3628. );
  3629. foreach($as as $a){
  3630. $this->fuckhtml->load($a);
  3631. // get title
  3632. $title =
  3633. $this->fuckhtml
  3634. ->getElementsByAttributeValue(
  3635. "role",
  3636. "heading",
  3637. "div"
  3638. );
  3639. if(count($title) === 0){
  3640. continue;
  3641. }
  3642. $title =
  3643. $this->titledots(
  3644. $this->fuckhtml
  3645. ->getTextContent(
  3646. $title[0]
  3647. )
  3648. );
  3649. // get thumbnail
  3650. $image =
  3651. $this->fuckhtml
  3652. ->getElementsByAttributeName(
  3653. "id",
  3654. "img"
  3655. );
  3656. // check for padded title node, if found, we're inside a carousel
  3657. $probe =
  3658. $this->fuckhtml
  3659. ->getElementsByClassName(
  3660. $this->getstyle(
  3661. [
  3662. "padding" => "16px 16px 40px 16px"
  3663. ]
  3664. ),
  3665. "div"
  3666. );
  3667. if(count($probe) !== 0){
  3668. $probe = true;
  3669. }else{
  3670. $probe = false;
  3671. }
  3672. if(
  3673. count($image) !== 0 &&
  3674. !isset($image[0]["attributes"]["width"])
  3675. ){
  3676. $thumb = [
  3677. "url" =>
  3678. $this->getdimg(
  3679. $image[0]["attributes"]["id"]
  3680. ),
  3681. "ratio" => $probe === true ? "16:9" : "1:1"
  3682. ];
  3683. }else{
  3684. $thumb = [
  3685. "url" => null,
  3686. "ratio" => null
  3687. ];
  3688. }
  3689. $description = null;
  3690. if($probe === false){
  3691. $desc_divs =
  3692. $this->fuckhtml
  3693. ->getElementsByAttributeName(
  3694. "style",
  3695. "div"
  3696. );
  3697. foreach($desc_divs as $desc){
  3698. if(
  3699. strpos(
  3700. $desc["attributes"]["style"],
  3701. "margin-top:"
  3702. ) !== false
  3703. ){
  3704. $description =
  3705. $this->titledots(
  3706. $this->fuckhtml
  3707. ->getTextContent(
  3708. $desc
  3709. )
  3710. );
  3711. break;
  3712. }
  3713. }
  3714. }
  3715. // get author
  3716. $author =
  3717. $this->fuckhtml
  3718. ->getElementsByClassName(
  3719. $this->getstyle(
  3720. [
  3721. "overflow" => "hidden",
  3722. "text-align" => "left",
  3723. "text-overflow" => "ellipsis",
  3724. "white-space" => "nowrap",
  3725. "margin-bottom" => "8px"
  3726. ]
  3727. ),
  3728. "div"
  3729. );
  3730. if(count($author) !== 0){
  3731. $author =
  3732. $this->fuckhtml
  3733. ->getTextContent(
  3734. $author[0]
  3735. );
  3736. }else{
  3737. $author = null;
  3738. }
  3739. // get date
  3740. $date = null;
  3741. $date_div =
  3742. $this->fuckhtml
  3743. ->getElementsByAttributeName(
  3744. "style",
  3745. "div"
  3746. );
  3747. foreach($date_div as $d){
  3748. $this->fuckhtml->load($d);
  3749. $span =
  3750. $this->fuckhtml
  3751. ->getElementsByTagName(
  3752. "span"
  3753. );
  3754. if(
  3755. strpos(
  3756. $d["attributes"]["style"],
  3757. "bottom:"
  3758. ) !== false
  3759. ){
  3760. $date =
  3761. strtotime(
  3762. $this->fuckhtml
  3763. ->getTextContent(
  3764. $span[count($span) - 1]
  3765. )
  3766. );
  3767. break;
  3768. }
  3769. }
  3770. $out["news"][] = [
  3771. "title" => $title,
  3772. "author" => $author,
  3773. "description" => $description,
  3774. "date" => $date,
  3775. "thumb" => $thumb,
  3776. "url" =>
  3777. $this->unshiturl(
  3778. $a["attributes"]
  3779. ["href"]
  3780. )
  3781. ];
  3782. }
  3783. return $out;
  3784. }
  3785. public function image($get){
  3786. // generate parameters
  3787. if($get["npt"]){
  3788. [$params, $proxy] =
  3789. $this->backend->get(
  3790. $get["npt"],
  3791. "images"
  3792. );
  3793. $params = json_decode($params, true);
  3794. }else{
  3795. $search = $get["s"];
  3796. if(strlen($search) === 0){
  3797. throw new Exception("Search term is empty!");
  3798. }
  3799. $proxy = $this->backend->get_ip();
  3800. $country = $get["country"];
  3801. $nsfw = $get["nsfw"];
  3802. $time = $get["time"];
  3803. $size = $get["size"];
  3804. $ratio = $get["ratio"];
  3805. $color = $get["color"];
  3806. $type = $get["type"];
  3807. $format = $get["format"];
  3808. $rights = $get["rights"];
  3809. $params = [
  3810. "q" => $search,
  3811. "udm" => "2" // get images
  3812. ];
  3813. // country (image search uses cr instead of gl)
  3814. if($country != "any"){
  3815. $params["cr"] = "country" . strtoupper($country);
  3816. }
  3817. // nsfw
  3818. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  3819. // generate tbs
  3820. $tbs = [];
  3821. // time
  3822. if($time != "any"){
  3823. $tbs["qdr"] = $time;
  3824. }
  3825. // size
  3826. if($size != "any"){
  3827. $params["imgsz"] = $size;
  3828. }
  3829. // ratio
  3830. if($ratio != "any"){
  3831. $params["imgar"] = $ratio;
  3832. }
  3833. // color
  3834. if($color != "any"){
  3835. if(
  3836. $color == "color" ||
  3837. $color == "trans"
  3838. ){
  3839. $params["imgc"] = $color;
  3840. }elseif($color == "bnw"){
  3841. $params["imgc"] = "gray";
  3842. }else{
  3843. $tbs["ic"] = "specific";
  3844. $tbs["isc"] = $color;
  3845. }
  3846. }
  3847. // type
  3848. if($type != "any"){
  3849. $tbs["itp"] = $type;
  3850. }
  3851. // format
  3852. if($format != "any"){
  3853. $params["as_filetype"] = $format;
  3854. }
  3855. // rights (tbs)
  3856. if($rights != "any"){
  3857. $tbs["sur"] = $rights;
  3858. }
  3859. // append tbs
  3860. if(count($tbs) !== 0){
  3861. $params["tbs"] = "";
  3862. foreach($tbs as $key => $value){
  3863. $params["tbs"] .= $key . ":" . $value . ",";
  3864. }
  3865. $params["tbs"] = rtrim($params["tbs"], ",");
  3866. }
  3867. }
  3868. /*
  3869. $handle = fopen("scraper/google-img.html", "r");
  3870. $html = fread($handle, filesize("scraper/google-img.html"));
  3871. fclose($handle);*/
  3872. try{
  3873. $html =
  3874. $this->get(
  3875. $proxy,
  3876. "https://www.google.com/search",
  3877. $params
  3878. );
  3879. }catch(Exception $error){
  3880. throw new Exception("Failed to get search page");
  3881. }
  3882. $this->fuckhtml->load($html);
  3883. $this->detect_sorry();
  3884. // get javascript images
  3885. $this->scrape_imagearr($html);
  3886. $out = [
  3887. "status" => "ok",
  3888. "npt" => null,
  3889. "image" => []
  3890. ];
  3891. $images =
  3892. $this->fuckhtml
  3893. ->getElementsByClassName(
  3894. "ivg-i",
  3895. "div"
  3896. );
  3897. foreach($images as $div){
  3898. $this->fuckhtml->load($div);
  3899. $image =
  3900. $this->fuckhtml
  3901. ->getElementsByTagName("img")[0];
  3902. $out["image"][] = [
  3903. "title" =>
  3904. $this->titledots(
  3905. $this->fuckhtml
  3906. ->getTextContent(
  3907. $image["attributes"]["alt"]
  3908. )
  3909. ),
  3910. "source" =>
  3911. $this->image_arr[
  3912. $div["attributes"]["data-docid"]
  3913. ],
  3914. "url" =>
  3915. $this->fuckhtml
  3916. ->getTextContent(
  3917. $div["attributes"]["data-lpage"]
  3918. )
  3919. ];
  3920. }
  3921. // as usual, no way to check if there is a next page reliably
  3922. if(count($out["image"]) > 50){
  3923. if(!isset($params["start"])){
  3924. $params["start"] = 10;
  3925. }else{
  3926. $params["start"] += 10;
  3927. }
  3928. $out["npt"] =
  3929. $this->backend
  3930. ->store(
  3931. json_encode($params),
  3932. "image",
  3933. $proxy
  3934. );
  3935. }
  3936. return $out;
  3937. }
  3938. private function unshiturl($url, $return_size = false){
  3939. // decode
  3940. $url =
  3941. $this->fuckhtml
  3942. ->getTextContent($url);
  3943. $url_parts = parse_url($url);
  3944. if(
  3945. !isset(
  3946. $url_parts["host"]
  3947. )
  3948. ){
  3949. // no host, we have a tracking url
  3950. parse_str($url_parts["query"], $query);
  3951. if(isset($query["imgurl"])){
  3952. $url = $query["imgurl"];
  3953. }
  3954. elseif(isset($query["q"])){
  3955. $url = $query["q"];
  3956. }
  3957. }
  3958. // rewrite URLs to remove extra tracking parameters
  3959. $domain = parse_url($url, PHP_URL_HOST);
  3960. if(
  3961. preg_match(
  3962. '/wikipedia.org$/',
  3963. $domain
  3964. )
  3965. ){
  3966. // rewrite wikipedia mobile URLs to desktop
  3967. $url =
  3968. $this->replacedomain(
  3969. $url,
  3970. preg_replace(
  3971. '/([a-z0-9]+)(\.m\.)/',
  3972. '$1.',
  3973. $domain
  3974. )
  3975. );
  3976. }
  3977. elseif(
  3978. preg_match(
  3979. '/imdb\.com$|youtube\.[^.]+$/',
  3980. $domain
  3981. )
  3982. ){
  3983. // rewrite imdb and youtube mobile URLs too
  3984. $url =
  3985. $this->replacedomain(
  3986. $url,
  3987. preg_replace(
  3988. '/^m\./',
  3989. "",
  3990. $domain
  3991. )
  3992. );
  3993. }
  3994. elseif(
  3995. preg_match(
  3996. '/play\.google\.[^.]+$/',
  3997. $domain
  3998. )
  3999. ){
  4000. // remove referrers from play.google.com
  4001. $oldquery = parse_url($url, PHP_URL_QUERY);
  4002. if($oldquery !== null){
  4003. parse_str($oldquery, $query);
  4004. if(isset($query["referrer"])){ unset($query["referrer"]); }
  4005. if(isset($query["hl"])){ unset($query["hl"]); }
  4006. if(isset($query["gl"])){ unset($query["gl"]); }
  4007. $query = http_build_query($query);
  4008. $url =
  4009. str_replace(
  4010. $oldquery,
  4011. $query,
  4012. $url
  4013. );
  4014. }
  4015. }
  4016. elseif(
  4017. preg_match(
  4018. '/twitter\.com$/',
  4019. $domain
  4020. )
  4021. ){
  4022. // remove more referrers from twitter.com
  4023. $oldquery = parse_url($url, PHP_URL_QUERY);
  4024. if($oldquery !== null){
  4025. parse_str($oldquery, $query);
  4026. if(isset($query["ref_src"])){ unset($query["ref_src"]); }
  4027. $query = http_build_query($query);
  4028. $url =
  4029. str_replace(
  4030. $oldquery,
  4031. $query,
  4032. $url
  4033. );
  4034. }
  4035. }
  4036. elseif(
  4037. preg_match(
  4038. '/maps\.google\.[^.]+/',
  4039. $domain
  4040. )
  4041. ){
  4042. if(stripos($url, "maps?") !== false){
  4043. //https://maps.google.com/maps?daddr=Johnny,+603+Rue+St+Georges,+Saint-J%C3%A9r%C3%B4me,+Quebec+J7Z+5B7
  4044. $query = parse_url($url, PHP_URL_QUERY);
  4045. if($query !== null){
  4046. parse_str($query, $query);
  4047. if(isset($query["daddr"])){
  4048. $url =
  4049. "https://maps.google.com/maps?daddr=" .
  4050. urlencode($query["daddr"]);
  4051. }
  4052. }
  4053. }
  4054. }
  4055. if($return_size){
  4056. return [
  4057. "url" => $url,
  4058. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  4059. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  4060. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  4061. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  4062. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  4063. ];
  4064. }
  4065. return $url;
  4066. }
  4067. private function replacedomain($url, $domain){
  4068. return
  4069. preg_replace(
  4070. '/(https?:\/\/)([^\/]+)/',
  4071. '$1' . $domain,
  4072. $url
  4073. );
  4074. }
  4075. private function titledots($title){
  4076. return trim($title, " .\t\n\r\0\x0B…");
  4077. }
  4078. private function hms2int($time){
  4079. $parts = explode(":", $time, 3);
  4080. $time = 0;
  4081. if(count($parts) === 3){
  4082. // hours
  4083. $time = $time + ((int)$parts[0] * 3600);
  4084. array_shift($parts);
  4085. }
  4086. if(count($parts) === 2){
  4087. // minutes
  4088. $time = $time + ((int)$parts[0] * 60);
  4089. array_shift($parts);
  4090. }
  4091. // seconds
  4092. $time = $time + (int)$parts[0];
  4093. return $time;
  4094. }
  4095. private function detect_sorry(){
  4096. $recaptcha =
  4097. $this->fuckhtml
  4098. ->getElementById(
  4099. "recaptcha",
  4100. "div"
  4101. );
  4102. if($recaptcha !== false){
  4103. throw new Exception("Google returned a captcha");
  4104. }
  4105. }
  4106. }