1
0

google.php 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690
  1. <?php
  2. // todo:
  3. // aliexpress tracking links
  4. // enhanced msx notice
  5. class google{
  6. private const is_class = ".";
  7. private const is_id = "#";
  8. public function __construct(){
  9. include "lib/fuckhtml.php";
  10. $this->fuckhtml = new fuckhtml();
  11. include "lib/backend.php";
  12. $this->backend = new backend("google");
  13. }
  14. public function getfilters($page){
  15. $base = [
  16. "country" => [ // gl=<country>
  17. "display" => "Country",
  18. "option" => [
  19. "any" => "Instance's country",
  20. "af" => "Afghanistan",
  21. "al" => "Albania",
  22. "dz" => "Algeria",
  23. "as" => "American Samoa",
  24. "ad" => "Andorra",
  25. "ao" => "Angola",
  26. "ai" => "Anguilla",
  27. "aq" => "Antarctica",
  28. "ag" => "Antigua and Barbuda",
  29. "ar" => "Argentina",
  30. "am" => "Armenia",
  31. "aw" => "Aruba",
  32. "au" => "Australia",
  33. "at" => "Austria",
  34. "az" => "Azerbaijan",
  35. "bs" => "Bahamas",
  36. "bh" => "Bahrain",
  37. "bd" => "Bangladesh",
  38. "bb" => "Barbados",
  39. "by" => "Belarus",
  40. "be" => "Belgium",
  41. "bz" => "Belize",
  42. "bj" => "Benin",
  43. "bm" => "Bermuda",
  44. "bt" => "Bhutan",
  45. "bo" => "Bolivia",
  46. "ba" => "Bosnia and Herzegovina",
  47. "bw" => "Botswana",
  48. "bv" => "Bouvet Island",
  49. "br" => "Brazil",
  50. "io" => "British Indian Ocean Territory",
  51. "bn" => "Brunei Darussalam",
  52. "bg" => "Bulgaria",
  53. "bf" => "Burkina Faso",
  54. "bi" => "Burundi",
  55. "kh" => "Cambodia",
  56. "cm" => "Cameroon",
  57. "ca" => "Canada",
  58. "cv" => "Cape Verde",
  59. "ky" => "Cayman Islands",
  60. "cf" => "Central African Republic",
  61. "td" => "Chad",
  62. "cl" => "Chile",
  63. "cn" => "China",
  64. "cx" => "Christmas Island",
  65. "cc" => "Cocos (Keeling) Islands",
  66. "co" => "Colombia",
  67. "km" => "Comoros",
  68. "cg" => "Congo",
  69. "cd" => "Congo, the Democratic Republic",
  70. "ck" => "Cook Islands",
  71. "cr" => "Costa Rica",
  72. "ci" => "Cote D'ivoire",
  73. "hr" => "Croatia",
  74. "cu" => "Cuba",
  75. "cy" => "Cyprus",
  76. "cz" => "Czech Republic",
  77. "dk" => "Denmark",
  78. "dj" => "Djibouti",
  79. "dm" => "Dominica",
  80. "do" => "Dominican Republic",
  81. "ec" => "Ecuador",
  82. "eg" => "Egypt",
  83. "sv" => "El Salvador",
  84. "gq" => "Equatorial Guinea",
  85. "er" => "Eritrea",
  86. "ee" => "Estonia",
  87. "et" => "Ethiopia",
  88. "fk" => "Falkland Islands (Malvinas)",
  89. "fo" => "Faroe Islands",
  90. "fj" => "Fiji",
  91. "fi" => "Finland",
  92. "fr" => "France",
  93. "gf" => "French Guiana",
  94. "pf" => "French Polynesia",
  95. "tf" => "French Southern Territories",
  96. "ga" => "Gabon",
  97. "gm" => "Gambia",
  98. "ge" => "Georgia",
  99. "de" => "Germany",
  100. "gh" => "Ghana",
  101. "gi" => "Gibraltar",
  102. "gr" => "Greece",
  103. "gl" => "Greenland",
  104. "gd" => "Grenada",
  105. "gp" => "Guadeloupe",
  106. "gu" => "Guam",
  107. "gt" => "Guatemala",
  108. "gn" => "Guinea",
  109. "gw" => "Guinea-Bissau",
  110. "gy" => "Guyana",
  111. "ht" => "Haiti",
  112. "hm" => "Heard Island and Mcdonald Islands",
  113. "va" => "Holy See (Vatican City State)",
  114. "hn" => "Honduras",
  115. "hk" => "Hong Kong",
  116. "hu" => "Hungary",
  117. "is" => "Iceland",
  118. "in" => "India",
  119. "id" => "Indonesia",
  120. "ir" => "Iran, Islamic Republic",
  121. "iq" => "Iraq",
  122. "ie" => "Ireland",
  123. "il" => "Israel",
  124. "it" => "Italy",
  125. "jm" => "Jamaica",
  126. "jp" => "Japan",
  127. "jo" => "Jordan",
  128. "kz" => "Kazakhstan",
  129. "ke" => "Kenya",
  130. "ki" => "Kiribati",
  131. "kp" => "Korea, Democratic People's Republic",
  132. "kr" => "Korea, Republic",
  133. "kw" => "Kuwait",
  134. "kg" => "Kyrgyzstan",
  135. "la" => "Lao People's Democratic Republic",
  136. "lv" => "Latvia",
  137. "lb" => "Lebanon",
  138. "ls" => "Lesotho",
  139. "lr" => "Liberia",
  140. "ly" => "Libyan Arab Jamahiriya",
  141. "li" => "Liechtenstein",
  142. "lt" => "Lithuania",
  143. "lu" => "Luxembourg",
  144. "mo" => "Macao",
  145. "mk" => "Macedonia, the Former Yugosalv Republic",
  146. "mg" => "Madagascar",
  147. "mw" => "Malawi",
  148. "my" => "Malaysia",
  149. "mv" => "Maldives",
  150. "ml" => "Mali",
  151. "mt" => "Malta",
  152. "mh" => "Marshall Islands",
  153. "mq" => "Martinique",
  154. "mr" => "Mauritania",
  155. "mu" => "Mauritius",
  156. "yt" => "Mayotte",
  157. "mx" => "Mexico",
  158. "fm" => "Micronesia, Federated States",
  159. "md" => "Moldova, Republic",
  160. "mc" => "Monaco",
  161. "mn" => "Mongolia",
  162. "ms" => "Montserrat",
  163. "ma" => "Morocco",
  164. "mz" => "Mozambique",
  165. "mm" => "Myanmar",
  166. "na" => "Namibia",
  167. "nr" => "Nauru",
  168. "np" => "Nepal",
  169. "nl" => "Netherlands",
  170. "an" => "Netherlands Antilles",
  171. "nc" => "New Caledonia",
  172. "nz" => "New Zealand",
  173. "ni" => "Nicaragua",
  174. "ne" => "Niger",
  175. "ng" => "Nigeria",
  176. "nu" => "Niue",
  177. "nf" => "Norfolk Island",
  178. "mp" => "Northern Mariana Islands",
  179. "no" => "Norway",
  180. "om" => "Oman",
  181. "pk" => "Pakistan",
  182. "pw" => "Palau",
  183. "ps" => "Palestinian Territory, Occupied",
  184. "pa" => "Panama",
  185. "pg" => "Papua New Guinea",
  186. "py" => "Paraguay",
  187. "pe" => "Peru",
  188. "ph" => "Philippines",
  189. "pn" => "Pitcairn",
  190. "pl" => "Poland",
  191. "pt" => "Portugal",
  192. "pr" => "Puerto Rico",
  193. "qa" => "Qatar",
  194. "re" => "Reunion",
  195. "ro" => "Romania",
  196. "ru" => "Russian Federation",
  197. "rw" => "Rwanda",
  198. "sh" => "Saint Helena",
  199. "kn" => "Saint Kitts and Nevis",
  200. "lc" => "Saint Lucia",
  201. "pm" => "Saint Pierre and Miquelon",
  202. "vc" => "Saint Vincent and the Grenadines",
  203. "ws" => "Samoa",
  204. "sm" => "San Marino",
  205. "st" => "Sao Tome and Principe",
  206. "sa" => "Saudi Arabia",
  207. "sn" => "Senegal",
  208. "cs" => "Serbia and Montenegro",
  209. "sc" => "Seychelles",
  210. "sl" => "Sierra Leone",
  211. "sg" => "Singapore",
  212. "sk" => "Slovakia",
  213. "si" => "Slovenia",
  214. "sb" => "Solomon Islands",
  215. "so" => "Somalia",
  216. "za" => "South Africa",
  217. "gs" => "South Georgia and the South Sandwich Islands",
  218. "es" => "Spain",
  219. "lk" => "Sri Lanka",
  220. "sd" => "Sudan",
  221. "sr" => "Suriname",
  222. "sj" => "Svalbard and Jan Mayen",
  223. "sz" => "Swaziland",
  224. "se" => "Sweden",
  225. "ch" => "Switzerland",
  226. "sy" => "Syrian Arab Republic",
  227. "tw" => "Taiwan, Province of China",
  228. "tj" => "Tajikistan",
  229. "tz" => "Tanzania, United Republic",
  230. "th" => "Thailand",
  231. "tl" => "Timor-Leste",
  232. "tg" => "Togo",
  233. "tk" => "Tokelau",
  234. "to" => "Tonga",
  235. "tt" => "Trinidad and Tobago",
  236. "tn" => "Tunisia",
  237. "tr" => "Turkey",
  238. "tm" => "Turkmenistan",
  239. "tc" => "Turks and Caicos Islands",
  240. "tv" => "Tuvalu",
  241. "ug" => "Uganda",
  242. "ua" => "Ukraine",
  243. "ae" => "United Arab Emirates",
  244. "uk" => "United Kingdom",
  245. "us" => "United States",
  246. "um" => "United States Minor Outlying Islands",
  247. "uy" => "Uruguay",
  248. "uz" => "Uzbekistan",
  249. "vu" => "Vanuatu",
  250. "ve" => "Venezuela",
  251. "vn" => "Viet Nam",
  252. "vg" => "Virgin Islands, British",
  253. "vi" => "Virgin Islands, U.S.",
  254. "wf" => "Wallis and Futuna",
  255. "eh" => "Western Sahara",
  256. "ye" => "Yemen",
  257. "zm" => "Zambia",
  258. "zw" => "Zimbabwe"
  259. ]
  260. ],
  261. "nsfw" => [
  262. "display" => "NSFW",
  263. "option" => [
  264. "yes" => "Yes", // safe=active
  265. "no" => "No" // safe=off
  266. ]
  267. ],
  268. "lang" => [ // lr=<lang> (prefix lang with "lang_")
  269. "display" => "Language",
  270. "option" => [
  271. "any" => "Any language",
  272. "ar" => "Arabic",
  273. "bg" => "Bulgarian",
  274. "ca" => "Catalan",
  275. "cs" => "Czech",
  276. "da" => "Danish",
  277. "de" => "German",
  278. "el" => "Greek",
  279. "en" => "English",
  280. "es" => "Spanish",
  281. "et" => "Estonian",
  282. "fi" => "Finnish",
  283. "fr" => "French",
  284. "hr" => "Croatian",
  285. "hu" => "Hungarian",
  286. "id" => "Indonesian",
  287. "is" => "Icelandic",
  288. "it" => "Italian",
  289. "iw" => "Hebrew",
  290. "ja" => "Japanese",
  291. "ko" => "Korean",
  292. "lt" => "Lithuanian",
  293. "lv" => "Latvian",
  294. "nl" => "Dutch",
  295. "no" => "Norwegian",
  296. "pl" => "Polish",
  297. "pt" => "Portuguese",
  298. "ro" => "Romanian",
  299. "ru" => "Russian",
  300. "sk" => "Slovak",
  301. "sl" => "Slovenian",
  302. "sr" => "Serbian",
  303. "sv" => "Swedish",
  304. "tr" => "Turkish",
  305. "zh-CN" => "Chinese (Simplified)",
  306. "zh-TW" => "Chinese (Traditional)"
  307. ]
  308. ]
  309. ];
  310. switch($page){
  311. case "web":
  312. return array_merge(
  313. $base,
  314. [
  315. "newer" => [ // &sort=review-date:r:20090301:20090430
  316. "display" => "Newer than",
  317. "option" => "_DATE"
  318. ],
  319. "older" => [
  320. "display" => "Older than",
  321. "option" => "_DATE"
  322. ]
  323. ]
  324. );
  325. break;
  326. case "images":
  327. return array_merge(
  328. $base,
  329. [
  330. "time" => [ // tbs=qrd:<size>
  331. "display" => "Time posted",
  332. "option" => [
  333. "any" => "Any time",
  334. "d" => "Past 24 hours",
  335. "w" => "Past week",
  336. "m" => "Past month",
  337. "y" => "Past year"
  338. ]
  339. ],
  340. "size" => [
  341. "display" => "Size",
  342. "option" => [
  343. // tbs=isz:<size>
  344. "any" => "Any size",
  345. "l" => "Large",
  346. "m" => "Medium",
  347. "i" => "Icon",
  348. // from here
  349. // tbz:lt,islt:<size>
  350. "qsvga" => "Larger than 400x300",
  351. "vga" => "Larger than 640x480",
  352. "qsvga" => "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" => [ // tbs=iar:<size>
  367. "display" => "Aspect ratio",
  368. "option" => [
  369. "any" => "Any ratio",
  370. "t" => "Tall",
  371. "s" => "Square",
  372. "w" => "Wide",
  373. "xw" => "Panoramic"
  374. ]
  375. ],
  376. "color" => [ // tbs=ic:<color>
  377. "display" => "Color",
  378. "option" => [
  379. "any" => "Any color",
  380. "color" => "Full color",
  381. "gray" => "Black & white",
  382. "trans" => "Transparent",
  383. // from there, its ic:specific,isc:<color>
  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. "face" => "Faces",
  403. "clipart" => "Clip Art",
  404. "lineart" => "Line Drawing",
  405. "stock" => "Stock",
  406. "animated" => "Animated"
  407. ]
  408. ],
  409. "format" => [ // tbs=ift:<format>
  410. "display" => "Format",
  411. "option" => [
  412. "any" => "Any format",
  413. "jpg" => "JPG",
  414. "gif" => "GIF",
  415. "png" => "PNG",
  416. "bmp" => "BMP",
  417. "svg" => "SVG",
  418. "webp" => "WEBP",
  419. "ico" => "ICO",
  420. "craw" => "RAW"
  421. ]
  422. ],
  423. "rights" => [ // tbs=il:<rights>
  424. "display" => "Usage rights",
  425. "option" => [
  426. "any" => "Any license",
  427. "cl" => "Creative Commons licenses",
  428. "ol" => "Commercial & other licenses"
  429. ]
  430. ]
  431. ]
  432. );
  433. break;
  434. case "videos":
  435. return array_merge(
  436. $base,
  437. [
  438. "time" => [
  439. "display" => "Time posted",
  440. "option" => [ // tbs=qdr
  441. "any" => "Any time",
  442. "h" => "Past hour",
  443. "d" => "Past 24 hours",
  444. "w" => "Past week",
  445. "m" => "Past month",
  446. "y" => "Past year"
  447. ]
  448. ],
  449. "duration" => [
  450. "display" => "Duration",
  451. "option" => [
  452. "any" => "Any duration",
  453. "s" => "Short (0-4min)", // tbs=dur:s
  454. "m" => "Medium (4-20min)", // tbs=dur:m
  455. "l" => "Long (20+ min)" // tbs=dur:l
  456. ]
  457. ],
  458. "quality" => [
  459. "display" => "Quality",
  460. "option" => [
  461. "any" => "Any quality",
  462. "h" => "High quality" // tbs=hq:h
  463. ]
  464. ],
  465. "captions" => [
  466. "display" => "Captions",
  467. "option" => [
  468. "any" => "No preference",
  469. "yes" => "Closed captioned" // tbs=cc:1
  470. ]
  471. ]
  472. ]
  473. );
  474. break;
  475. case "news":
  476. return array_merge(
  477. $base,
  478. [
  479. "time" => [
  480. "display" => "Time posted",
  481. "option" => [ // tbs=qdr
  482. "any" => "Any time",
  483. "h" => "Past hour",
  484. "d" => "Past 24 hours",
  485. "w" => "Past week",
  486. "m" => "Past month",
  487. "y" => "Past year",
  488. "a" => "Archives" // tbs=ar:1
  489. ]
  490. ],
  491. "sort" => [
  492. "display" => "Sort",
  493. "option" => [
  494. "relevance" => "Relevance",
  495. "date" => "Date" // sbd:1
  496. ]
  497. ]
  498. ]
  499. );
  500. break;
  501. }
  502. }
  503. private function get($proxy, $url, $get = []){
  504. $headers = [
  505. "User-Agent: Mozilla/5.0 (Linux; U; Android 2.3.3; pt-pt; LG-P500h-parrot Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MMS/LG-Android-MMS-V1.0/1.2",
  506. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  507. "Accept-Language: en-US,en;q=0.5",
  508. "Accept-Encoding: gzip",
  509. "DNT: 1",
  510. "Cookie: SOCS=CAESNQgCEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjQwMzE3LjA4X3AwGgJlbiAEGgYIgM7orwY",
  511. "Connection: keep-alive",
  512. "Upgrade-Insecure-Requests: 1",
  513. "Sec-Fetch-Dest: document",
  514. "Sec-Fetch-Mode: navigate",
  515. "Sec-Fetch-Site: none",
  516. "Sec-Fetch-User: ?1"
  517. ];
  518. $curlproc = curl_init();
  519. if($get !== []){
  520. $get = http_build_query($get);
  521. $url .= "?" . $get;
  522. }
  523. curl_setopt($curlproc, CURLOPT_URL, $url);
  524. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  525. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  526. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  527. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  528. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  529. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  530. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  531. $this->backend->assign_proxy($curlproc, $proxy);
  532. $data = curl_exec($curlproc);
  533. if(curl_errno($curlproc)){
  534. throw new Exception(curl_error($curlproc));
  535. }
  536. curl_close($curlproc);
  537. return $data;
  538. }
  539. public function web($get){
  540. if($get["npt"]){
  541. [$req, $ip] = $this->backend->get($get["npt"], "web");
  542. parse_str(
  543. parse_url($req, PHP_URL_QUERY),
  544. $search
  545. );
  546. if(isset($search["q"])){
  547. $search = $search["q"];
  548. }else{
  549. $search = "a"; // lol
  550. }
  551. try{
  552. $html =
  553. $this->get(
  554. $ip,
  555. "https://www.google.com" . $req,
  556. []
  557. );
  558. }catch(Exception $error){
  559. throw new Exception("Failed to get HTML");
  560. }
  561. }else{
  562. $search = $get["s"];
  563. $country = $get["country"];
  564. $nsfw = $get["nsfw"];
  565. $lang = $get["lang"];
  566. $older = $get["older"];
  567. $newer = $get["newer"];
  568. $ip = $this->backend->get_ip();
  569. $params = [
  570. "q" => $search,
  571. "hl" => "en",
  572. "num" => 20 // get 20 results
  573. ];
  574. // country
  575. if($country != "any"){
  576. $params["gl"] = $country;
  577. }
  578. // nsfw
  579. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  580. // language
  581. if($lang != "any"){
  582. $params["lr"] = "lang_" . $lang;
  583. }
  584. // &sort=review-date:r:20090301:20090430
  585. $older = $older === false ? false : date("Ymd", $older);
  586. $newer = $newer === false ? false : date("Ymd", $newer);
  587. if(
  588. $older !== false &&
  589. $newer === false
  590. ){
  591. $newer = date("Ymd", time());
  592. }
  593. if(
  594. $older !== false ||
  595. $newer !== false
  596. ){
  597. $params["sort"] = "review-date:r:" . $older . ":" . $newer;
  598. }
  599. try{
  600. $html =
  601. $this->get(
  602. $ip,
  603. "https://www.google.com/search",
  604. $params
  605. );
  606. }catch(Exception $error){
  607. throw new Exception("Failed to get HTML");
  608. }
  609. //$html = file_get_contents("scraper/google.html");
  610. }
  611. return $this->parsepage($html, "web", $search, $ip);
  612. }
  613. public function video($get){
  614. if($get["npt"]){
  615. [$req, $ip] = $this->backend->get($get["npt"], "videos");
  616. parse_str(
  617. parse_url($req, PHP_URL_QUERY),
  618. $search
  619. );
  620. if(isset($search["q"])){
  621. $search = $search["q"];
  622. }else{
  623. $search = "a"; // lol
  624. }
  625. try{
  626. $html =
  627. $this->get(
  628. $ip,
  629. "https://www.google.com" . $req,
  630. []
  631. );
  632. }catch(Exception $error){
  633. throw new Exception("Failed to get HTML");
  634. }
  635. }else{
  636. $search = $get["s"];
  637. $country = $get["country"];
  638. $nsfw = $get["nsfw"];
  639. $lang = $get["lang"];
  640. $time = $get["time"];
  641. $duration = $get["duration"];
  642. $quality = $get["quality"];
  643. $captions = $get["captions"];
  644. $ip = $this->backend->get_ip();
  645. $params = [
  646. "q" => $search,
  647. "tbm" => "vid",
  648. "hl" => "en",
  649. "num" => "20"
  650. ];
  651. // country
  652. if($country != "any"){
  653. $params["gl"] = $country;
  654. }
  655. // nsfw
  656. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  657. // language
  658. if($lang != "any"){
  659. $params["lr"] = "lang_" . $lang;
  660. }
  661. $tbs = [];
  662. // time
  663. if($time != "any"){
  664. $tbs[] = "qdr:" . $time;
  665. }
  666. // duration
  667. if($duration != "any"){
  668. $tbs[] = "dur:" . $duration;
  669. }
  670. // quality
  671. if($quality != "any"){
  672. $tbs[] = "hq:" . $quality;
  673. }
  674. // captions
  675. if($captions != "any"){
  676. $tbs[] = "cc:" . $captions;
  677. }
  678. // append tbs
  679. if(count($tbs) !== 0){
  680. $params["tbs"] =
  681. implode(",", $tbs);
  682. }
  683. try{
  684. $html =
  685. $this->get(
  686. $ip,
  687. "https://www.google.com/search",
  688. $params
  689. );
  690. }catch(Exception $error){
  691. throw new Exception("Failed to get HTML");
  692. }
  693. }
  694. $json = $this->parsepage($html, "videos", $search, $ip);
  695. $out = [
  696. "status" => "ok",
  697. "npt" => $json["npt"],
  698. "video" => [],
  699. "author" => [],
  700. "livestream" => [],
  701. "playlist" => [],
  702. "reel" => []
  703. ];
  704. foreach($json["web"] as $item){
  705. $out["video"][] = [
  706. "title" => $item["title"],
  707. "description" => $item["description"],
  708. "author" => [
  709. "name" => null,
  710. "url" => null,
  711. "avatar" => null
  712. ],
  713. "date" => isset($item["table"]["Posted"]) ? strtotime($item["table"]["Posted"]) : null,
  714. "duration" => isset($item["table"]["Duration"]) ? $this->hms2int($item["table"]["Duration"]) : null,
  715. "views" => null,
  716. "thumb" =>
  717. $item["thumb"]["url"] === null ?
  718. [
  719. "url" => null,
  720. "ratio" => null
  721. ] :
  722. [
  723. "url" => $item["thumb"]["url"],
  724. "ratio" => "16:9"
  725. ],
  726. "url" => $item["url"]
  727. ];
  728. }
  729. return $out;
  730. }
  731. public function news($get){
  732. if($get["npt"]){
  733. [$req, $ip] = $this->backend->get($get["npt"], "news");
  734. parse_str(
  735. parse_url($req, PHP_URL_QUERY),
  736. $search
  737. );
  738. if(isset($search["q"])){
  739. $search = $search["q"];
  740. }else{
  741. $search = "a"; // lol
  742. }
  743. try{
  744. $html =
  745. $this->get(
  746. $ip,
  747. "https://www.google.com" . $req,
  748. []
  749. );
  750. }catch(Exception $error){
  751. throw new Exception("Failed to get HTML");
  752. }
  753. }else{
  754. $search = $get["s"];
  755. $country = $get["country"];
  756. $nsfw = $get["nsfw"];
  757. $lang = $get["lang"];
  758. $time = $get["time"];
  759. $sort = $get["sort"];
  760. $ip = $this->backend->get_ip();
  761. $params = [
  762. "q" => $search,
  763. "tbm" => "nws",
  764. "hl" => "en",
  765. "num" => "20"
  766. ];
  767. // country
  768. if($country != "any"){
  769. $params["gl"] = $country;
  770. }
  771. // nsfw
  772. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  773. // language
  774. if($lang != "any"){
  775. $params["lr"] = "lang_" . $lang;
  776. }
  777. $tbs = [];
  778. // time
  779. if($time != "any"){
  780. if($time == "a"){
  781. $tbs[] = "ar:1";
  782. }else{
  783. $tbs[] = "qdr:" . $time;
  784. }
  785. }
  786. // relevance
  787. if($sort == "date"){
  788. $tbs[] = "sbd:1";
  789. }
  790. // append tbs
  791. if(count($tbs) !== 0){
  792. $params["tbs"] =
  793. implode(",", $tbs);
  794. }
  795. $html =
  796. $this->get(
  797. $ip,
  798. "https://www.google.com/search",
  799. $params
  800. );
  801. }
  802. $json = $this->parsepage($html, "news", $search, $ip);
  803. $out = [
  804. "status" => "ok",
  805. "npt" => $json["npt"],
  806. "news" => []
  807. ];
  808. foreach($json["web"] as $item){
  809. $description = array_key_first($item["table"]);
  810. if($description !== null){
  811. $date = $item["table"][$description];
  812. }else{
  813. $date = null;
  814. }
  815. $out["news"][] = [
  816. "title" => $item["title"],
  817. "author" => $item["author"],
  818. "description" => $description,
  819. "date" => strtotime($date),
  820. "thumb" =>
  821. $item["thumb"]["url"] === null ?
  822. [
  823. "url" => null,
  824. "ratio" => null
  825. ] :
  826. [
  827. "url" => $item["thumb"]["url"],
  828. "ratio" => "16:9"
  829. ],
  830. "url" => $item["url"]
  831. ];
  832. }
  833. return $out;
  834. }
  835. private function parsepage($html, $pagetype, $search, $ip){
  836. /*
  837. $handle = fopen("scraper/google.html", "r");
  838. $html = fread($handle, filesize("scraper/google.html"));
  839. fclose($handle);
  840. */
  841. $out = [
  842. "status" => "ok",
  843. "spelling" => [
  844. "type" => "no_correction",
  845. "using" => null,
  846. "correction" => null
  847. ],
  848. "npt" => null,
  849. "answer" => [],
  850. "web" => [],
  851. "image" => [],
  852. "video" => [],
  853. "news" => [],
  854. "related" => []
  855. ];
  856. if($error = $this->detect_sorry($html)){
  857. throw new Exception($error);
  858. }
  859. $this->parsejavascript($html);
  860. //
  861. // parse accdef's
  862. //
  863. $has_appended_accdef = false;
  864. preg_match_all(
  865. '/window\.jsl\.dh\(\'(accdef_[0-9]+)\',\'(.*)\'\);/',
  866. $html,
  867. $accdefs_regex
  868. );
  869. $accdefs = [];
  870. for($i=0; $i<count($accdefs_regex[0]); $i++){
  871. // decode UTF-16 string
  872. $answer =
  873. $this->fuckhtml
  874. ->parseJsString(
  875. $accdefs_regex[2][$i]
  876. );
  877. $this->fuckhtml->load($answer);
  878. // get description
  879. $description =
  880. $this->fuckhtml
  881. ->getElementsByClassName(
  882. $this->findstyles(
  883. [
  884. "padding" => "12px 16px 12px",
  885. ],
  886. self::is_class
  887. ),
  888. "div"
  889. );
  890. if(!isset($description[1])){
  891. throw new Exception("Google returned an unsupported page format (will fix)");
  892. }else{
  893. $description = $description[1];
  894. }
  895. // get date (rare)
  896. $date =
  897. $this->fuckhtml
  898. ->getElementsByTagName("sub");
  899. if(count($date) !== 0){
  900. $description =
  901. str_replace(
  902. $date[0]["outerHTML"],
  903. "",
  904. $description["innerHTML"]
  905. );
  906. $date =
  907. strtotime(
  908. $this->fuckhtml
  909. ->getTextContent(
  910. $date[0]
  911. )
  912. );
  913. }else{
  914. $date = null;
  915. }
  916. // get information table
  917. $table = [];
  918. $tbody =
  919. $this->fuckhtml
  920. ->getElementsByTagName("tbody");
  921. if(count($tbody) !== 0){
  922. $this->fuckhtml->load($tbody[0]);
  923. $trs =
  924. $this->fuckhtml
  925. ->getElementsByTagName("tr");
  926. foreach($trs as $tr){
  927. $this->fuckhtml->load($tr);
  928. $tds =
  929. $this->fuckhtml
  930. ->getElementsByTagName("td");
  931. if(count($tds) === 2){
  932. $table[
  933. $this->fuckhtml
  934. ->getTextContent(
  935. $tds[0]
  936. )
  937. ] =
  938. $this->fuckhtml
  939. ->getTextContent(
  940. $tds[1]
  941. );
  942. }
  943. }
  944. // load back what we had
  945. $this->fuckhtml->load($answer);
  946. }
  947. // get title & link
  948. $a =
  949. $this->fuckhtml
  950. ->getElementsByTagName("a")[0];
  951. $this->fuckhtml->load($a);
  952. $title =
  953. $this->fuckhtml
  954. ->getElementsByTagName("span");
  955. if(count($title) === 0){
  956. continue;
  957. }
  958. $accdefs[] = [
  959. "title" =>
  960. $this->titledots(
  961. $this->fuckhtml
  962. ->getTextContent(
  963. $title[0]
  964. )
  965. ),
  966. "description" =>
  967. $this->fuckhtml
  968. ->getTextContent(
  969. $description
  970. ),
  971. "url" =>
  972. $this->unshiturl(
  973. $a["attributes"]["href"]
  974. ),
  975. "date" => $date,
  976. "type" => "web",
  977. "thumb" => [
  978. "url" => null,
  979. "ratio" => null
  980. ],
  981. "sublink" => [],
  982. "table" => $table
  983. ];
  984. }
  985. $this->fuckhtml->load($html);
  986. $containers =
  987. $this->fuckhtml
  988. ->getElementsByClassName(
  989. $this->findstyles(
  990. [
  991. "background-color" => "#fff",
  992. "margin-bottom" => "10px",
  993. "-webkit-box-shadow" => "0 1px 6px rgba(32,33,36,0.28)",
  994. "border-radius" => "8px"
  995. ],
  996. self::is_class
  997. ),
  998. "div"
  999. );
  1000. foreach($containers as $container){
  1001. $this->fuckhtml->load($container);
  1002. // detect spelling
  1003. $spelling =
  1004. $this->fuckhtml
  1005. ->getElementsByClassName(
  1006. $this->findstyles(
  1007. [
  1008. "font-size" => "20px",
  1009. "line-height" => "26px",
  1010. "padding-top" => "2px",
  1011. "margin-bottom" => "1px"
  1012. ],
  1013. self::is_class
  1014. ),
  1015. "div"
  1016. );
  1017. if(count($spelling) !== 0){
  1018. $a =
  1019. $this->fuckhtml
  1020. ->getElementsByTagName("a");
  1021. if(count($a) !== 0){
  1022. $scripts =
  1023. $this->fuckhtml
  1024. ->getElementsByTagName("script");
  1025. foreach($scripts as $script){
  1026. $container["innerHTML"] =
  1027. str_replace(
  1028. $script["outerHTML"],
  1029. "",
  1030. $container["innerHTML"]
  1031. );
  1032. }
  1033. $container["innerHTML"] =
  1034. $this->fuckhtml
  1035. ->getTextContent(
  1036. str_replace(
  1037. $a[0]["outerHTML"],
  1038. "",
  1039. $container["innerHTML"]
  1040. )
  1041. );
  1042. if(
  1043. preg_match(
  1044. '/^did you mean/i',
  1045. $container["innerHTML"]
  1046. )
  1047. ){
  1048. $out["spelling"] = [
  1049. "type" => "not_many",
  1050. "using" => $search,
  1051. "correction" =>
  1052. $this->fuckhtml
  1053. ->getTextContent(
  1054. $a[0]
  1055. )
  1056. ];
  1057. }
  1058. elseif(
  1059. preg_match(
  1060. '/^showing results for/i',
  1061. $container["innerHTML"]
  1062. )
  1063. ){
  1064. $out["spelling"] = [
  1065. "type" => "including",
  1066. "using" =>
  1067. $this->fuckhtml
  1068. ->getTextContent(
  1069. $a[0]
  1070. ),
  1071. "correction" => $search
  1072. ];
  1073. }
  1074. }
  1075. continue;
  1076. }
  1077. $title =
  1078. $this->fuckhtml
  1079. ->getElementsByClassName(
  1080. $this->findstyles(
  1081. [
  1082. "color" => "#1967d2",
  1083. "font-size" => "20px",
  1084. "line-height" => "26px"
  1085. ],
  1086. self::is_class
  1087. ),
  1088. "div"
  1089. );
  1090. if(count($title) !== 0){
  1091. //
  1092. // Container is a web link
  1093. //
  1094. $web = [
  1095. "title" =>
  1096. $this->titledots(
  1097. $this->fuckhtml
  1098. ->getTextContent(
  1099. $title[0]
  1100. )
  1101. ),
  1102. "description" => null,
  1103. "url" => null,
  1104. "date" => null,
  1105. "type" => "web",
  1106. "thumb" => [
  1107. "url" => null,
  1108. "ratio" => null
  1109. ],
  1110. "sublink" => [],
  1111. "table" => []
  1112. ];
  1113. // get link
  1114. $web["url"] =
  1115. $this->unshiturl(
  1116. $this->fuckhtml
  1117. ->getElementsByTagName("a")
  1118. [0]
  1119. ["attributes"]
  1120. ["href"]
  1121. );
  1122. //
  1123. // check if link contains a carousel
  1124. //
  1125. $carousels = $this->parsecarousels();
  1126. if(count($carousels) !== 0){
  1127. $first = true;
  1128. foreach($carousels as $carousel_cat){
  1129. foreach($carousel_cat as $carousel){
  1130. if($first === true){
  1131. $first = false;
  1132. }elseif($carousel["image"] !== null){
  1133. $out["image"][] = [
  1134. "title" => $carousel["title"],
  1135. "source" => [
  1136. [
  1137. "url" => $carousel["image"],
  1138. "width" => null,
  1139. "height" => null
  1140. ]
  1141. ],
  1142. "url" => $carousel["url"]
  1143. ];
  1144. }
  1145. $web["sublink"][] = [
  1146. "title" => $carousel["title"],
  1147. "date" => $carousel["date"],
  1148. "description" => $carousel["description"],
  1149. "url" => $carousel["url"]
  1150. ];
  1151. }
  1152. }
  1153. if($carousels[0][0]["image"] !== null){
  1154. $web["thumb"] = [
  1155. "url" => $carousels[0][0]["image"],
  1156. "ratio" => "16:9"
  1157. ];
  1158. }
  1159. $out["web"][] = $web;
  1160. continue;
  1161. }
  1162. //
  1163. // no carousel entries, parse as normal link
  1164. //
  1165. $this->fuckhtml->load($container);
  1166. // parse URL
  1167. $web["url"] =
  1168. $this->unshiturl(
  1169. $this->fuckhtml
  1170. ->getElementsByTagName("a")
  1171. [0]
  1172. ["attributes"]
  1173. ["href"]
  1174. );
  1175. $container = $container["innerHTML"];
  1176. $line_detect =
  1177. $this->fuckhtml
  1178. ->getElementsByClassName(
  1179. $this->findstyles(
  1180. [
  1181. "height" => "1px",
  1182. "background-color" => "#dadce0",
  1183. "margin" => "0 16px"
  1184. ],
  1185. self::is_class
  1186. ),
  1187. "div"
  1188. );
  1189. if(count($line_detect) !== 0){
  1190. // we found a line, this means we're dealing with a
  1191. // "featured snippet"
  1192. $featured = true;
  1193. $description_container =
  1194. $this->fuckhtml
  1195. ->getElementsByClassName(
  1196. $this->findstyles(
  1197. [
  1198. "white-space" => "pre-line",
  1199. "word-wrap" => "break-word"
  1200. ],
  1201. self::is_class
  1202. ),
  1203. "div"
  1204. )[1];
  1205. // get date node for it
  1206. $date =
  1207. $this->fuckhtml
  1208. ->getElementsByTagName("sub");
  1209. if(count($date) !== 0){
  1210. $web["date"] =
  1211. strtotime(
  1212. $this->fuckhtml
  1213. ->getTextContent(
  1214. $date[0]
  1215. )
  1216. );
  1217. }
  1218. }else{
  1219. // we're dealing with a normal link
  1220. $featured = false;
  1221. $description_container =
  1222. $this->fuckhtml
  1223. ->getElementsByClassName(
  1224. $this->findstyles(
  1225. [
  1226. "padding" => "12px 16px 12px"
  1227. ],
  1228. self::is_class
  1229. ),
  1230. "div"
  1231. )[1];
  1232. }
  1233. //
  1234. // Get author if we're parsing news
  1235. //
  1236. if($pagetype == "news"){
  1237. $author =
  1238. $this->fuckhtml
  1239. ->getElementsByClassName(
  1240. $this->findstyles(
  1241. [
  1242. "position" => "absolute",
  1243. "width" => "100%",
  1244. "top" => "0",
  1245. "left" => "0",
  1246. "padding-top" => "1px",
  1247. "margin-bottom" => "-1px"
  1248. ],
  1249. self::is_class
  1250. ),
  1251. "div"
  1252. );
  1253. if(count($author) !== 0){
  1254. $web["author"] =
  1255. $this->fuckhtml
  1256. ->getTextContent(
  1257. $author[0]
  1258. );
  1259. }else{
  1260. $web["author"] = null;
  1261. }
  1262. }
  1263. $description =
  1264. $description_container["innerHTML"];
  1265. $this->fuckhtml->load($description);
  1266. //
  1267. // get thumbnail before we call loadhtml again
  1268. //
  1269. $img =
  1270. $this->fuckhtml
  1271. ->getElementsByTagName("img");
  1272. if(count($img) !== 0){
  1273. $skip = true;
  1274. if(
  1275. isset($img[0]["attributes"]["alt"]) &&
  1276. stripos($img[0]["attributes"]["alt"], "Video for") !== false
  1277. ){
  1278. // is a video thumbnail
  1279. $web["thumb"]["ratio"] = "16:9";
  1280. }else{
  1281. // is a google thumbnail
  1282. $web["thumb"]["ratio"] = "1:1";
  1283. }
  1284. $web["thumb"]["url"] =
  1285. $this->getimage(
  1286. $img[0]["attributes"]["id"]
  1287. );
  1288. }else{
  1289. $skip = false;
  1290. }
  1291. //
  1292. // get sublinks
  1293. //
  1294. $links =
  1295. $this->fuckhtml
  1296. ->getElementsByTagName("a");
  1297. foreach($links as $link){
  1298. if($skip === true){
  1299. $skip = false;
  1300. continue;
  1301. }
  1302. $description =
  1303. str_replace(
  1304. $link["outerHTML"],
  1305. "",
  1306. $description
  1307. );
  1308. $sublink = [
  1309. "title" => null,
  1310. "description" => null,
  1311. "url" => null,
  1312. "date" => null
  1313. ];
  1314. $sublink["title"] =
  1315. $this->titledots(
  1316. $this->fuckhtml
  1317. ->getTextContent(
  1318. $link
  1319. )
  1320. );
  1321. $sublink["url"] =
  1322. $this->unshiturl(
  1323. $link
  1324. ["attributes"]
  1325. ["href"]
  1326. );
  1327. if(parse_url($sublink["url"], PHP_URL_HOST) !== null){
  1328. $web["sublink"][] = $sublink;
  1329. }
  1330. }
  1331. //
  1332. // Parse spans in description
  1333. //
  1334. $this->fuckhtml->load($description);
  1335. if($featured === false){
  1336. $levels =
  1337. $this->fuckhtml
  1338. ->getElementsByClassName(
  1339. $this->findstyles(
  1340. [
  1341. "padding-bottom" => "8px"
  1342. ],
  1343. self::is_class
  1344. ),
  1345. "div"
  1346. );
  1347. // oh my god yes, fucking great, sometimes there are NO levels
  1348. // hahahahahhahahahahahahahahahhahaa
  1349. if(count($levels) === 0){
  1350. $levels = [$description];
  1351. }
  1352. foreach($levels as $level){
  1353. $this->fuckhtml->load($level);
  1354. $spans =
  1355. $this->fuckhtml
  1356. ->getElementsByTagName(
  1357. "span"
  1358. );
  1359. $is_rating = -1;
  1360. foreach($spans as $span){
  1361. $innertext =
  1362. trim(
  1363. $this->fuckhtml
  1364. ->getTextContent(
  1365. $span
  1366. ),
  1367. " ·."
  1368. );
  1369. if($innertext == ""){ continue; }
  1370. if(
  1371. strtolower($innertext)
  1372. == "rating"
  1373. ){
  1374. $is_rating = 0;
  1375. // clean up before we go
  1376. $description =
  1377. str_replace(
  1378. $span["outerHTML"],
  1379. "",
  1380. $description
  1381. );
  1382. continue;
  1383. }
  1384. //
  1385. // Parse rating object
  1386. //
  1387. if($is_rating >= 0){
  1388. // clean up description
  1389. $description =
  1390. str_replace(
  1391. $span["outerHTML"],
  1392. "",
  1393. $description
  1394. );
  1395. if($span["level"] !== 1){ continue; }
  1396. $is_rating++;
  1397. // 10/10 (123)
  1398. if($is_rating === 1){
  1399. $innertext = explode(" ", $innertext, 2);
  1400. $web["table"]["Rating"] = $innertext[0];
  1401. if(count($innertext) === 2){
  1402. $web["table"]["Hits"] =
  1403. trim(
  1404. str_replace(
  1405. [
  1406. "(",
  1407. ")"
  1408. ],
  1409. "",
  1410. $innertext[1]
  1411. )
  1412. );
  1413. if($web["table"]["Hits"] == ""){
  1414. unset($web["table"]["Hits"]);
  1415. }
  1416. }
  1417. continue;
  1418. }
  1419. // US$4.99
  1420. // MYR 50.00
  1421. // $38.34
  1422. // JP¥6,480
  1423. // Reviewed by your mom
  1424. if($is_rating === 2){
  1425. if(
  1426. preg_match(
  1427. '/^Review by (.+)/',
  1428. $innertext,
  1429. $match
  1430. )
  1431. ){
  1432. $web["table"]["Author"] = $match[1];
  1433. continue;
  1434. }
  1435. $web["table"]["Price"] = $innertext;
  1436. continue;
  1437. }
  1438. // Android / In stock
  1439. if($is_rating === 3){
  1440. $web["table"]["Support"] = $innertext;
  1441. continue;
  1442. }
  1443. // ignore the rest
  1444. continue;
  1445. }
  1446. //
  1447. // Parse standalone text
  1448. //
  1449. // If we reach this point:
  1450. // 1. Ratings have been parsed
  1451. // 2. We're parsing a WEB link, not some shitty piece of shit
  1452. // check for date
  1453. // if span has no text before it, assume it's a date
  1454. $desc_split =
  1455. explode(
  1456. $span["outerHTML"],
  1457. $description,
  1458. 2
  1459. );
  1460. if(
  1461. $this->fuckhtml
  1462. ->getTextContent(
  1463. $desc_split[0]
  1464. ) == ""
  1465. ){
  1466. // has no text before
  1467. $date = strtotime($innertext);
  1468. if($date){
  1469. $web["date"] = $date;
  1470. }
  1471. // cleanup
  1472. $description =
  1473. str_replace(
  1474. $span["outerHTML"],
  1475. "",
  1476. $description
  1477. );
  1478. continue;
  1479. }
  1480. // Ready to parse table
  1481. if(count($desc_split) === 2){
  1482. $this->fuckhtml->load($desc_split[1]);
  1483. $web["table"][
  1484. $this->fuckhtml
  1485. ->getTextContent(
  1486. trim($desc_split[0], ": ")
  1487. )
  1488. ] = $innertext;
  1489. // cleanup
  1490. $description =
  1491. str_replace(
  1492. $desc_split[0] . $span["outerHTML"],
  1493. "",
  1494. $description
  1495. );
  1496. }
  1497. }
  1498. }
  1499. }
  1500. $web["description"] =
  1501. trim(
  1502. $this->fuckhtml
  1503. ->getTextContent(
  1504. $description
  1505. ),
  1506. " ·."
  1507. );
  1508. if($web["description"] == ""){
  1509. $web["description"] = null;
  1510. }
  1511. $out["web"][] = $web;
  1512. continue;
  1513. }
  1514. //
  1515. // Detect wikipedia shit
  1516. //
  1517. $wiki_title =
  1518. $this->fuckhtml
  1519. ->getElementsByTagName("h3");
  1520. if(count($wiki_title) !== 0){
  1521. $description_after = [];
  1522. $description = [];
  1523. $table = [];
  1524. $sublink = [];
  1525. $as =
  1526. $this->fuckhtml
  1527. ->getElementsByTagName("a");
  1528. foreach($as as $a){
  1529. if(
  1530. isset($a["attributes"]["href"]) &&
  1531. parse_url($a["attributes"]["href"], PHP_URL_HOST) == "maps.google.com"
  1532. ){
  1533. // detected maps embed, ignore
  1534. continue 2;
  1535. }
  1536. }
  1537. // get carousels and remove them from container for image grepper
  1538. $carousels = $this->parsecarousels($container["innerHTML"]);
  1539. $this->fuckhtml->load($container);
  1540. // add images to image tab, if applicable
  1541. for($i=0; $i<count($carousels); $i++){
  1542. foreach($carousels[$i] as $item){
  1543. if(
  1544. $item["url"] !== null &&
  1545. $item["ref"] !== null &&
  1546. $item["image"] !== null &&
  1547. $item["title"] !== null
  1548. ){
  1549. $out["image"][] = [
  1550. "title" => $item["title"],
  1551. "source" => [
  1552. [
  1553. "url" => $item["url"],
  1554. "width" => $item["image_width"],
  1555. "height" => $item["image_height"]
  1556. ],
  1557. [
  1558. "url" => $item["image"],
  1559. "width" => $item["thumb_width"],
  1560. "height" => $item["thumb_height"]
  1561. ]
  1562. ],
  1563. "url" => $item["ref"]
  1564. ];
  1565. unset($carousels[$i]);
  1566. }
  1567. }
  1568. }
  1569. $carousels = array_values($carousels);
  1570. // interpret remaining carousels as title + carousel
  1571. $titles =
  1572. $this->fuckhtml
  1573. ->getElementsByClassName(
  1574. $this->findstyles(
  1575. [
  1576. "font-weight" => "700",
  1577. "letter-spacing" => "0.75px",
  1578. "text-transform" => "uppercase"
  1579. ],
  1580. self::is_class
  1581. )
  1582. );
  1583. for($i=0; $i<count($titles); $i++){
  1584. if(!isset($carousels[$i])){
  1585. break;
  1586. }
  1587. $description_after[] = [
  1588. "type" => "title",
  1589. "value" =>
  1590. $this->fuckhtml
  1591. ->getTextContent(
  1592. $titles[$i]
  1593. )
  1594. ];
  1595. foreach($carousels[$i] as $carousel){
  1596. $description_after[] = [
  1597. "type" => "link",
  1598. "url" => "web?s=" . urlencode($carousel["description"]) . "&scraper=google",
  1599. "value" => $carousel["description"]
  1600. ];
  1601. if($carousel["subtext"] !== null){
  1602. $description_after[] = [
  1603. "type" => "quote",
  1604. "value" => $carousel["subtext"]
  1605. ];
  1606. }
  1607. $description_after[] = [
  1608. "type" => "image",
  1609. "url" => $carousel["image"]
  1610. ];
  1611. }
  1612. }
  1613. $categories =
  1614. $this->fuckhtml
  1615. ->getElementsByClassName(
  1616. $this->findstyles(
  1617. [
  1618. "padding" => "12px 16px 12px"
  1619. ],
  1620. self::is_class
  1621. )
  1622. );
  1623. $image =
  1624. $this->fuckhtml
  1625. ->getElementsByTagName("img");
  1626. if(count($image) !== 0){
  1627. $image = $this->getimage($image[0]["attributes"]["id"]);
  1628. }else{
  1629. $image = null;
  1630. }
  1631. $url = null;
  1632. for($i=0; $i<count($categories); $i++){
  1633. $this->fuckhtml->load($categories[$i]);
  1634. if($i === 0){
  1635. // first node. this should be the header with the small
  1636. // information snippet
  1637. $url =
  1638. $this->fuckhtml
  1639. ->getElementsByTagName("a");
  1640. if(count($url) !== 0){
  1641. $url =
  1642. $this->unshiturl(
  1643. $url[0]["attributes"]["href"]
  1644. );
  1645. if(parse_url($url, PHP_URL_HOST) == "encrypted-tbn0.gstatic.com"){
  1646. $image = $url;
  1647. $url = null;
  1648. }
  1649. }else{
  1650. $url = null;
  1651. }
  1652. $categories[$i]["innerHTML"] =
  1653. str_replace(
  1654. $wiki_title[0]["outerHTML"],
  1655. "",
  1656. $categories[$i]["innerHTML"]
  1657. );
  1658. $subtext =
  1659. $this->fuckhtml
  1660. ->getTextContent(
  1661. $categories[$i]["innerHTML"]
  1662. );
  1663. if(strlen($subtext) !== 0){
  1664. $description[] = [
  1665. "type" => "quote",
  1666. "value" =>
  1667. $this->fuckhtml
  1668. ->getTextContent(
  1669. $categories[$i]["innerHTML"]
  1670. )
  1671. ];
  1672. }
  1673. // detect audio file
  1674. $audio =
  1675. $this->fuckhtml
  1676. ->getElementsByTagName("audio");
  1677. if(count($audio) !== 0){
  1678. $description[] = [
  1679. "type" => "audio",
  1680. "url" =>
  1681. $this->fuckhtml
  1682. ->getTextContent(
  1683. $audio[0]["attributes"]["src"]
  1684. )
  1685. ];
  1686. }
  1687. }else{
  1688. // check for separator elements IN THERE
  1689. $separators =
  1690. $this->fuckhtml
  1691. ->getElementsByClassName(
  1692. $this->findstyles(
  1693. [
  1694. "white-space" => "pre-line",
  1695. "word-wrap" => "break-word"
  1696. ],
  1697. self::is_class
  1698. ),
  1699. "div"
  1700. );
  1701. // detect container type
  1702. foreach($separators as $separator){
  1703. $this->fuckhtml->load($separator);
  1704. // ignore wrong levels
  1705. if($separator["level"] !== 2){
  1706. continue;
  1707. }
  1708. //
  1709. // Detect word definition
  1710. //
  1711. $wordwraps =
  1712. $this->fuckhtml
  1713. ->getElementsByClassName(
  1714. $this->findstyles(
  1715. [
  1716. "padding-bottom" => "12px"
  1717. ],
  1718. self::is_class
  1719. ),
  1720. "div"
  1721. );
  1722. if(count($wordwraps) !== 0){
  1723. foreach($wordwraps as $word){
  1724. $this->fuckhtml->load($word);
  1725. // detect title
  1726. $span =
  1727. $this->fuckhtml
  1728. ->getElementsByTagName(
  1729. "span"
  1730. );
  1731. if(
  1732. count($span) === 1 &&
  1733. $this->fuckhtml
  1734. ->getTextContent(
  1735. str_replace(
  1736. $span[0]["outerHTML"],
  1737. "",
  1738. $word["innerHTML"]
  1739. )
  1740. ) == ""
  1741. ){
  1742. $description[] = [
  1743. "type" => "title",
  1744. "value" =>
  1745. $this->fuckhtml
  1746. ->getTextContent(
  1747. $span[0]
  1748. )
  1749. ];
  1750. continue;
  1751. }
  1752. // detect list element
  1753. $lists =
  1754. $this->fuckhtml
  1755. ->getElementsByTagName("ol");
  1756. if(count($lists) !== 0){
  1757. foreach($lists as $list){
  1758. $this->fuckhtml->load($list);
  1759. $items =
  1760. $this->fuckhtml
  1761. ->getElementsByTagName("li");
  1762. $w = 0;
  1763. foreach($items as $item){
  1764. $w++;
  1765. $this->fuckhtml->load($item);
  1766. // get subnodes
  1767. $subnodes =
  1768. $this->fuckhtml
  1769. ->getElementsByClassName(
  1770. $this->findstyles(
  1771. [
  1772. "white-space" => "pre-line",
  1773. "word-wrap" => "break-word"
  1774. ],
  1775. self::is_class
  1776. ),
  1777. "div"
  1778. );
  1779. foreach($subnodes as $subnode){
  1780. $this->fuckhtml->load($subnode);
  1781. $spans =
  1782. $this->fuckhtml
  1783. ->getElementsByTagName("span");
  1784. if(count($spans) !== 0){
  1785. // append quote
  1786. $description[] = [
  1787. "type" => "quote",
  1788. "value" =>
  1789. $this->fuckhtml
  1790. ->getTextContent(
  1791. $subnode
  1792. )
  1793. ];
  1794. }else{
  1795. // append text
  1796. $description[] = [
  1797. "type" => "text",
  1798. "value" =>
  1799. $w . ". " .
  1800. $this->fuckhtml
  1801. ->getTextContent(
  1802. $subnode
  1803. )
  1804. ];
  1805. }
  1806. }
  1807. }
  1808. }
  1809. }else{
  1810. // parse without list
  1811. // get subnodes
  1812. $subnodes =
  1813. $this->fuckhtml
  1814. ->getElementsByClassName(
  1815. $this->findstyles(
  1816. [
  1817. "white-space" => "pre-line",
  1818. "word-wrap" => "break-word"
  1819. ],
  1820. self::is_class
  1821. ),
  1822. "div"
  1823. );
  1824. foreach($subnodes as $subnode){
  1825. $this->fuckhtml->load($subnode);
  1826. $spans =
  1827. $this->fuckhtml
  1828. ->getElementsByTagName("span");
  1829. if(count($spans) !== 0){
  1830. // append quote
  1831. $description[] = [
  1832. "type" => "quote",
  1833. "value" =>
  1834. $this->fuckhtml
  1835. ->getTextContent(
  1836. $subnode
  1837. )
  1838. ];
  1839. }else{
  1840. // append text
  1841. $description[] = [
  1842. "type" => "text",
  1843. "value" =>
  1844. $this->fuckhtml
  1845. ->getTextContent(
  1846. $subnode
  1847. )
  1848. ];
  1849. }
  1850. }
  1851. }
  1852. }
  1853. }else{
  1854. //
  1855. // Parse table
  1856. //
  1857. $spans =
  1858. $this->fuckhtml
  1859. ->getElementsByTagName("span");
  1860. foreach($spans as $span){
  1861. if(!isset($span["attributes"]["class"])){
  1862. // found table
  1863. $row =
  1864. explode(
  1865. ":",
  1866. $this->fuckhtml
  1867. ->getTextContent(
  1868. $separator
  1869. ),
  1870. 2
  1871. );
  1872. if(count($row) === 2){
  1873. $table[rtrim($row[0])] =
  1874. ltrim($row[1]);
  1875. }
  1876. continue 2;
  1877. }
  1878. }
  1879. //
  1880. // Parse normal description
  1881. //
  1882. $links_rem =
  1883. $this->fuckhtml
  1884. ->getElementsByTagName("a");
  1885. foreach($links_rem as $rem){
  1886. $separator["innerHTML"] =
  1887. str_replace(
  1888. $rem["outerHTML"],
  1889. "",
  1890. $separator["innerHTML"]
  1891. );
  1892. }
  1893. $description[] = [
  1894. "type" => "text",
  1895. "value" =>
  1896. rtrim(
  1897. $this->fuckhtml
  1898. ->getTextContent(
  1899. $separator
  1900. ),
  1901. " .,"
  1902. )
  1903. ];
  1904. }
  1905. }
  1906. }
  1907. // detect huge buttons
  1908. $buttons =
  1909. $this->fuckhtml
  1910. ->getElementsByClassName(
  1911. $this->findstyles(
  1912. [
  1913. "display" => "table-cell",
  1914. "vertical-align" => "middle",
  1915. "height" => "52px",
  1916. "text-align" => "center"
  1917. ],
  1918. self::is_class
  1919. ),
  1920. "a"
  1921. );
  1922. if(count($buttons) !== 0){
  1923. foreach($buttons as $button){
  1924. if(isset($button["attributes"]["href"])){
  1925. $sublink[
  1926. $this->fuckhtml
  1927. ->getTextContent(
  1928. $button
  1929. )
  1930. ] =
  1931. $this->unshiturl(
  1932. $button["attributes"]["href"]
  1933. );
  1934. }
  1935. }
  1936. }
  1937. }
  1938. // append description_after (contains carousel info)
  1939. $description = array_merge(
  1940. $description,
  1941. $description_after
  1942. );
  1943. $out["answer"][] = [
  1944. "title" =>
  1945. $this->fuckhtml
  1946. ->getTextContent(
  1947. $wiki_title[0]
  1948. ),
  1949. "description" => $description,
  1950. "url" => $url,
  1951. "thumb" => $image,
  1952. "table" => $table,
  1953. "sublink" => $sublink
  1954. ];
  1955. continue;
  1956. }
  1957. //
  1958. // Detect related searches containers
  1959. //
  1960. $container_title =
  1961. $this->fuckhtml
  1962. ->getElementsByClassName(
  1963. $this->findstyles(
  1964. [
  1965. "color" => "#000",
  1966. "font-size" => "16px",
  1967. "font-weight" => "bold",
  1968. "margin" => "0",
  1969. "padding" => "12px 16px 0px 16px"
  1970. ],
  1971. self::is_class
  1972. ),
  1973. "div"
  1974. );
  1975. if(count($container_title) !== 0){
  1976. // get carousel entries
  1977. $carousels = $this->parsecarousels($container["innerHTML"]);
  1978. $this->fuckhtml->load($container);
  1979. foreach($carousels as $carousel){
  1980. foreach($carousel as $item){
  1981. if($item["url"] !== null){
  1982. $out["related"][] = $item["url"];
  1983. }
  1984. }
  1985. }
  1986. $container_title =
  1987. strtolower(
  1988. $this->fuckhtml
  1989. ->getTextContent(
  1990. $container_title[0]
  1991. )
  1992. );
  1993. switch($container_title){
  1994. case "related searches":
  1995. case "people also search for":
  1996. //
  1997. // Parse related searches
  1998. //
  1999. $as =
  2000. $this->fuckhtml
  2001. ->getElementsByTagName("a");
  2002. foreach($as as $a){
  2003. $out["related"][] =
  2004. $this->fuckhtml
  2005. ->getTextContent($a);
  2006. }
  2007. break;
  2008. case "people also ask":
  2009. // get related queries
  2010. $divs =
  2011. $this->fuckhtml
  2012. ->getElementsByTagName("div");
  2013. foreach($divs as $div){
  2014. // add accdef's here
  2015. if($has_appended_accdef === false){
  2016. $out["web"] = array_merge($out["web"], $accdefs);
  2017. $has_appended_accdef = true;
  2018. }
  2019. // add accdef's questions
  2020. if(isset($div["attributes"]["role"])){
  2021. $out["related"][] =
  2022. $this->fuckhtml
  2023. ->getTextContent($div);
  2024. continue;
  2025. }
  2026. }
  2027. break;
  2028. }
  2029. continue;
  2030. }
  2031. //
  2032. // Parse news
  2033. //
  2034. $title =
  2035. $this->fuckhtml
  2036. ->getElementsByClassName(
  2037. $this->findstyles(
  2038. [
  2039. "font-size" => "16px",
  2040. "line-height" => "20px",
  2041. "font-weight" => "400"
  2042. ],
  2043. self::is_class
  2044. ),
  2045. "div"
  2046. );
  2047. if(count($title) !== 0){
  2048. $carousels = $this->parsecarousels();
  2049. $this->fuckhtml->load($container);
  2050. if(count($carousels) === 0){
  2051. // no carousels found
  2052. continue;
  2053. }
  2054. $title =
  2055. strtolower(
  2056. $this->fuckhtml
  2057. ->getTextContent(
  2058. $title[0]
  2059. )
  2060. );
  2061. if(
  2062. preg_match(
  2063. '/^latest from|^top stories/',
  2064. $title
  2065. )
  2066. ){
  2067. // Found news article
  2068. foreach($carousels[0] as $carousel){
  2069. if($carousel["image"] !== null){
  2070. $thumb = [
  2071. "url" => $carousel["image"],
  2072. "ratio" => "16:9"
  2073. ];
  2074. }else{
  2075. $thumb = [
  2076. "url" => null,
  2077. "ratio" => null
  2078. ];
  2079. }
  2080. $out["news"][] = [
  2081. "title" => $carousel["title"],
  2082. "description" => $carousel["description"],
  2083. "date" => $carousel["date"],
  2084. "thumb" => $thumb,
  2085. "url" => $carousel["url"]
  2086. ];
  2087. }
  2088. }
  2089. elseif(
  2090. $title == "images"
  2091. ){
  2092. foreach($carousels as $carousel){
  2093. foreach($carousel as $item){
  2094. $out["image"][] = [
  2095. "title" => $item["title"],
  2096. "source" => [
  2097. [
  2098. "url" => $item["url"],
  2099. "width" => $item["image_width"],
  2100. "height" => $item["image_height"]
  2101. ],
  2102. [
  2103. "url" => $item["image"],
  2104. "width" => $item["thumb_width"],
  2105. "height" => $item["thumb_height"]
  2106. ]
  2107. ],
  2108. "url" => $item["ref"]
  2109. ];
  2110. }
  2111. }
  2112. }
  2113. continue;
  2114. }
  2115. //
  2116. // Detect nodes with only text + links
  2117. //
  2118. // ignore elements with <style> tags
  2119. $style =
  2120. $this->fuckhtml
  2121. ->getElementsByTagName("style");
  2122. if(count($style) !== 0){
  2123. continue;
  2124. }
  2125. $as =
  2126. $this->fuckhtml
  2127. ->getElementsByTagName("a");
  2128. $description = [];
  2129. $pcitems =
  2130. $this->fuckhtml
  2131. ->getElementsByClassName(
  2132. "pcitem",
  2133. "div"
  2134. );
  2135. if(count($pcitems) !== 0){
  2136. // ignore elements with carousels in them
  2137. continue;
  2138. }
  2139. foreach($as as $a){
  2140. //
  2141. // Detect next page
  2142. //
  2143. if(
  2144. isset($a["attributes"]["aria-label"]) &&
  2145. strtolower($a["attributes"]["aria-label"]) == "next page"
  2146. ){
  2147. $out["npt"] =
  2148. $this->backend->store(
  2149. $this->fuckhtml
  2150. ->getTextContent(
  2151. $a["attributes"]["href"]
  2152. ),
  2153. $pagetype,
  2154. $ip
  2155. );
  2156. continue 2;
  2157. }
  2158. //
  2159. // Parse as text node
  2160. //
  2161. $container["innerHTML"] =
  2162. explode(
  2163. $a["outerHTML"],
  2164. $container["innerHTML"],
  2165. 2
  2166. );
  2167. $before =
  2168. $this->fuckhtml
  2169. ->getTextContent(
  2170. $container["innerHTML"][0],
  2171. false,
  2172. false
  2173. );
  2174. // set after
  2175. if(count($container["innerHTML"]) === 2){
  2176. $container["innerHTML"] =
  2177. $container["innerHTML"][1];
  2178. }else{
  2179. $container["innerHTML"] = "";
  2180. }
  2181. if($before != ""){
  2182. $description[] = [
  2183. "type" => "text",
  2184. "value" => $before
  2185. ];
  2186. }
  2187. // add link
  2188. $description[] = [
  2189. "type" => "link",
  2190. "url" =>
  2191. $this->unshiturl(
  2192. $a["attributes"]
  2193. ["href"]
  2194. ),
  2195. "value" =>
  2196. $this->fuckhtml
  2197. ->getTextContent(
  2198. $a
  2199. )
  2200. ];
  2201. }
  2202. if($container["innerHTML"] != ""){
  2203. $description[] = [
  2204. "type" => "text",
  2205. "value" =>
  2206. $this->fuckhtml
  2207. ->getTextContent(
  2208. $container["innerHTML"]
  2209. )
  2210. ];
  2211. }
  2212. $out["answer"][] = [
  2213. "title" => "Notice",
  2214. "description" => $description,
  2215. "url" => null,
  2216. "thumb" => null,
  2217. "table" => [],
  2218. "sublink" => []
  2219. ];
  2220. }
  2221. //
  2222. // remove duplicate web links cause instant answers
  2223. // sometimes contains duplicates
  2224. //
  2225. $c = count($out["web"]);
  2226. $links = [];
  2227. for($i=0; $i<$c; $i++){
  2228. foreach($links as $link){
  2229. if($out["web"][$i]["url"] == $link){
  2230. unset($out["web"][$i]);
  2231. continue 2;
  2232. }
  2233. }
  2234. $links[] = $out["web"][$i]["url"];
  2235. }
  2236. $out["web"] = array_values($out["web"]);
  2237. return $out;
  2238. }
  2239. public function image($get){
  2240. // generate parameters
  2241. if($get["npt"]){
  2242. [$params, $proxy] =
  2243. $this->backend->get(
  2244. $get["npt"],
  2245. "images"
  2246. );
  2247. $params = json_decode($params, true);
  2248. }else{
  2249. $search = $get["s"];
  2250. if(strlen($search) === 0){
  2251. throw new Exception("Search term is empty!");
  2252. }
  2253. $proxy = $this->backend->get_ip();
  2254. $country = $get["country"];
  2255. $nsfw = $get["nsfw"];
  2256. $lang = $get["lang"];
  2257. $time = $get["time"];
  2258. $size = $get["size"];
  2259. $ratio = $get["ratio"];
  2260. $color = $get["color"];
  2261. $type = $get["type"];
  2262. $format = $get["format"];
  2263. $rights = $get["rights"];
  2264. $params = [
  2265. "q" => $search,
  2266. "tbm" => "isch"
  2267. ];
  2268. // country
  2269. if($country != "any"){
  2270. $params["gl"] = $country;
  2271. }
  2272. // nsfw
  2273. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  2274. // language
  2275. if($lang != "any"){
  2276. $params["lr"] = "lang_" . $lang;
  2277. }
  2278. $tbs = [];
  2279. // time
  2280. if($time != "any"){
  2281. $tbs[] = "qrd:" . $time;
  2282. }
  2283. // size
  2284. if($size != "any"){
  2285. if(
  2286. in_array(
  2287. $size,
  2288. ["l", "s", "i"]
  2289. )
  2290. ){
  2291. $tbs[] = "isz:" . $size;
  2292. }else{
  2293. $tbs[] = "tbz:lt";
  2294. $tbs[] = "islt:" . $size;
  2295. }
  2296. }
  2297. // ratio
  2298. if($ratio != "any"){
  2299. $tbs[] = "iar:" . $ratio;
  2300. }
  2301. // color
  2302. if($color != "any"){
  2303. if(
  2304. in_array(
  2305. $color,
  2306. ["color", "gray", "trans"]
  2307. )
  2308. ){
  2309. $tbs[] = "ic:" . $color;
  2310. }else{
  2311. $tbs[] = "ic:specific";
  2312. $tbs[] = "isc:" . $color;
  2313. }
  2314. }
  2315. // type
  2316. if($type != "any"){
  2317. $tbs[] = "itp:" . $type;
  2318. }
  2319. // format
  2320. if($format != "any"){
  2321. $tbs[] = "ift:" . $format;
  2322. }
  2323. // rights
  2324. if($rights != "any"){
  2325. $tbs[] = "il:" . $rights;
  2326. }
  2327. // append tbs
  2328. if(count($tbs) !== 0){
  2329. $params["tbs"] =
  2330. implode(",", $tbs);
  2331. }
  2332. }
  2333. /*
  2334. $handle = fopen("scraper/google-img.html", "r");
  2335. $html = fread($handle, filesize("scraper/google-img.html"));
  2336. fclose($handle);*/
  2337. // scrape images
  2338. try{
  2339. $html =
  2340. $this->get(
  2341. $proxy,
  2342. "https://www.google.com/search",
  2343. $params
  2344. );
  2345. }catch(Exception $error){
  2346. throw new Exception("Failed to get search page");
  2347. }
  2348. if($error = $this->detect_sorry($html)){
  2349. throw new Exception($error);
  2350. }
  2351. $out = [
  2352. "status" => "ok",
  2353. "npt" => null,
  2354. "image" => []
  2355. ];
  2356. $images =
  2357. $this->fuckhtml
  2358. ->getElementsByClassName(
  2359. "islrtb isv-r",
  2360. "div"
  2361. );
  2362. foreach($images as $image){
  2363. $this->fuckhtml->load($image);
  2364. $img =
  2365. $this->fuckhtml
  2366. ->getElementsByTagName("img")[0];
  2367. $og_width = (int)$image["attributes"]["data-ow"];
  2368. $og_height = (int)$image["attributes"]["data-oh"];
  2369. $thumb_width = (int)$image["attributes"]["data-tw"];
  2370. $ratio = $og_width / $og_height;
  2371. if(isset($img["attributes"]["data-src"])){
  2372. $src = &$img["attributes"]["data-src"];
  2373. }else{
  2374. $src = &$img["attributes"]["src"];
  2375. }
  2376. $thumb_height = floor($thumb_width / $ratio);
  2377. $out["image"][] = [
  2378. "title" =>
  2379. $this->titledots(
  2380. $this->fuckhtml
  2381. ->getTextContent(
  2382. $image["attributes"]["data-pt"]
  2383. )
  2384. ),
  2385. "source" => [
  2386. [
  2387. "url" =>
  2388. $this->fuckhtml
  2389. ->getTextContent(
  2390. $image["attributes"]["data-ou"]
  2391. ),
  2392. "width" => $og_width,
  2393. "height" => $og_height
  2394. ],
  2395. [
  2396. "url" =>
  2397. $this->fuckhtml
  2398. ->getTextContent(
  2399. $src
  2400. ),
  2401. "width" => $thumb_width,
  2402. "height" => $thumb_height
  2403. ]
  2404. ],
  2405. "url" =>
  2406. $this->fuckhtml
  2407. ->getTextContent(
  2408. $image["attributes"]["data-ru"]
  2409. )
  2410. ];
  2411. }
  2412. // get next page
  2413. // https://www.google.com/search
  2414. // ?q=higurashi
  2415. // &tbm=isch
  2416. // &async=_id%3Aislrg_c%2C_fmt%3Ahtml
  2417. // &asearch=ichunklite
  2418. // &ved=0ahUKEwidjYXJqJSAAxWrElkFHZ07CDwQtDIIQygA
  2419. if(count($out["image"]) !== 100){
  2420. // no more results
  2421. return $out;
  2422. }
  2423. if($get["npt"]){
  2424. // update nextpage information
  2425. $params["start"] = (int)$params["start"] + count($out["image"]);
  2426. $params["ijn"] = (int)$params["ijn"] + 1;
  2427. $out["npt"] =
  2428. $this->backend->store(
  2429. json_encode($params),
  2430. "images",
  2431. $proxy
  2432. );
  2433. }else{
  2434. // scrape nextpage information
  2435. $this->fuckhtml->load($html);
  2436. $ved =
  2437. $this->fuckhtml
  2438. ->getElementById("islrg", "div");
  2439. if($ved){
  2440. $ved =
  2441. $this->fuckhtml
  2442. ->getTextContent(
  2443. $ved["attributes"]["data-ved"]
  2444. );
  2445. // &vet=1{$ved}..i (10ahUKEwidjYXJqJSAAxWrElkFHZ07CDwQtDIIQygA..i)
  2446. /*
  2447. These 2 are handled by us
  2448. start = start + number of results
  2449. ijn = current page number
  2450. */
  2451. // &start=100
  2452. // &ijn=1
  2453. // &imgvl=CAEY7gQgBSj3Aji8VTjXVUC4AUC3AUgAYNdV
  2454. preg_match(
  2455. '/var e=\'([A-z0-9]+)\';/',
  2456. $html,
  2457. $imgvl
  2458. );
  2459. if(isset($imgvl[1])){
  2460. $imgvl = $imgvl[1];
  2461. $params["async"] = "_id:islrg_c,_fmt:html";
  2462. $params["asearch"] = "ichunklite";
  2463. $params["ved"] = $ved;
  2464. $params["vet"] = "1" . $ved . "..i";
  2465. $params["start"] = 100;
  2466. $params["ijn"] = 1;
  2467. $params["imgvl"] = $imgvl;
  2468. $out["npt"] =
  2469. $this->backend->store(
  2470. json_encode($params),
  2471. "images",
  2472. $proxy
  2473. );
  2474. }
  2475. }
  2476. }
  2477. return $out;
  2478. }
  2479. private function hms2int($time){
  2480. $parts = explode(":", $time, 3);
  2481. $time = 0;
  2482. if(count($parts) === 3){
  2483. // hours
  2484. $time = $time + ((int)$parts[0] * 3600);
  2485. array_shift($parts);
  2486. }
  2487. if(count($parts) === 2){
  2488. // minutes
  2489. $time = $time + ((int)$parts[0] * 60);
  2490. array_shift($parts);
  2491. }
  2492. // seconds
  2493. $time = $time + (int)$parts[0];
  2494. return $time;
  2495. }
  2496. private function parsejavascript($html){
  2497. $this->fuckhtml->load($html);
  2498. $styles =
  2499. $this->fuckhtml
  2500. ->getElementsByTagName("style");
  2501. $this->computedstyle = [];
  2502. $this->ask = [];
  2503. foreach($styles as $style){
  2504. $this->computedstyle =
  2505. array_merge(
  2506. $this->computedstyle,
  2507. $this->parsestyles($style["innerHTML"])
  2508. );
  2509. }
  2510. // get images in javascript var
  2511. preg_match(
  2512. '/google\.ldi=({[^}]+})/',
  2513. $html,
  2514. $this->js_image
  2515. );
  2516. if(count($this->js_image) !== 0){
  2517. $this->js_image = json_decode($this->js_image[1], true);
  2518. }else{
  2519. $this->js_image = [];
  2520. }
  2521. // additional js_images present in <script> tags
  2522. // ugh i fucking hate you
  2523. $scripts =
  2524. $this->fuckhtml
  2525. ->getElementsByTagName("script");
  2526. foreach($scripts as $script){
  2527. if(!isset($script["innerHTML"])){
  2528. continue;
  2529. }
  2530. preg_match_all(
  2531. '/var s=\'(data:image[^\']+)\';var i=\[(\'[^\;]*\')];/',
  2532. $script["innerHTML"],
  2533. $image_grep
  2534. );
  2535. if(count($image_grep[0]) !== 0){
  2536. $items = explode(",", $image_grep[2][0]);
  2537. $value =
  2538. $this->fuckhtml
  2539. ->getTextContent(
  2540. $image_grep[1][0]
  2541. );
  2542. foreach($items as $item){
  2543. $this->js_image[trim($item, "' ")] = $value;
  2544. }
  2545. }
  2546. // even more javascript crap
  2547. // "People also ask" node is loaded trough javascript
  2548. preg_match_all(
  2549. '/window\.jsl\.dh\(\'([^\']+)\',\'(.+)\'\);/',
  2550. $script["innerHTML"],
  2551. $ask_grep
  2552. );
  2553. for($i=0; $i<count($ask_grep[0]); $i++){
  2554. $this->ask[trim($ask_grep[1][$i])] =
  2555. $this->fuckhtml->parseJsString(
  2556. $ask_grep[2][$i]
  2557. );
  2558. }
  2559. }
  2560. }
  2561. private function findstyles($rules, $is){
  2562. $c = count($rules);
  2563. foreach($this->computedstyle as $classname => $styles){
  2564. if($classname[0] != $is){
  2565. // not a class, skip
  2566. continue;
  2567. }
  2568. $i = 0;
  2569. foreach($styles as $stylename => $stylevalue){
  2570. if(
  2571. isset($rules[$stylename]) &&
  2572. $rules[$stylename] == $stylevalue
  2573. ){
  2574. $i++;
  2575. }else{
  2576. continue 2;
  2577. }
  2578. }
  2579. if($c === $i){
  2580. return ltrim($classname, $is);
  2581. }
  2582. }
  2583. // fail, did not find classname.
  2584. return false;
  2585. }
  2586. private function parsestyles($style){
  2587. // get style tags
  2588. preg_match_all(
  2589. '/([^{]+){([^}]*)}/',
  2590. $style,
  2591. $tags_regex
  2592. );
  2593. $tags = [];
  2594. for($i=0; $i<count($tags_regex[0]); $i++){
  2595. $tagnames = explode(",", trim($tags_regex[1][$i]));
  2596. foreach($tagnames as $tagname){
  2597. $tagname = trim($tagname);
  2598. if(!isset($tags[$tagname])){
  2599. $tags[$tagname] = [];
  2600. }
  2601. $values = explode(";", $tags_regex[2][$i]);
  2602. foreach($values as $value){
  2603. $value = explode(":", $value, 2);
  2604. if(count($value) !== 2){
  2605. continue;
  2606. }
  2607. $tags[$tagname][trim($value[0])] =
  2608. trim($value[1]);
  2609. }
  2610. }
  2611. }
  2612. return $tags;
  2613. }
  2614. private function getimage($id){
  2615. if(isset($this->js_image[$id])){
  2616. $return = $this->fuckhtml->parseJsString($this->js_image[$id]);
  2617. if(
  2618. $return != "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAABAUlEQVR4AWMYesChoYElLjkzPj4lY3d8csZjIL4MxPNjUzPcSTYsISFLAqj5NBD/h+LPQPwbiT87NCuLh2gDgRr2QzXuT0jNMoBYksARn5zuHJ+UcR0kB6RXE2VYXHJGOlTDZmzyIJcB5e+D1CSkZDgQNBAaZv+jU1JkcKpJygiGeZ0I76a/Byq8jU9NZFqaCNTA48SE33/iDcw8TIyBt0GKQTFN0Msp6f2EIyUpo57YSIlLSrMhIg0WCIBcCfXSdlzJBsheTHQ6jEnOUgEFOLaEDbMIlhZBOYrorAdJk+nroVnvPsSgdGdoOF7HZyhZ2XPoGQoqjbCpIbt0AiejIQMArVLI7k/DXFkAAAAASUVORK5CYII=" &&
  2619. $return != "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAA6ElEQVR4Ae2UvQ7BYBSGW4uFxlVICLsYDA0D14NEunRxHSJ+BkYG9ibiHtgkuvpZWOod3uFESqpOF/ElT/q958h5OtQx/iexY/evY9ACJjBewUtkXHEPyBYUNQQuCETuggNrF2DHF3A4kfUMmLB+BoUYAg4nIX0TTNnbfCjg8HDBGuR4z4Ij+813giAC8rcrkXusjdQEpMpcYt5rCzrMaea7tqD9JLhpCyrMZeadpmApssPaUOszXQALGHz67De0/2gpMGPP014VFpizfgJ5zWXnAF8MryW1rj3x5l8LJANQF1lZQH5f8AAWpNcUs6HAEAAAAABJRU5ErkJggg=="
  2620. ){
  2621. if(
  2622. preg_match(
  2623. '/^\/\//',
  2624. $return
  2625. )
  2626. ){
  2627. return 'https:' . $return;
  2628. }
  2629. return $return;
  2630. }
  2631. return null;
  2632. }
  2633. }
  2634. private function parsecarousels(&$item_to_remove = false){
  2635. $carousels =
  2636. $this->fuckhtml
  2637. ->getElementsByClassName(
  2638. $this->findstyles(
  2639. [
  2640. "padding" => "16px",
  2641. "position" => "relative"
  2642. ],
  2643. self::is_class
  2644. )
  2645. );
  2646. $return = [];
  2647. for($i=0; $i<count($carousels); $i++){
  2648. if(!isset($carousels[$i]["outerHTML"])){
  2649. continue;
  2650. }
  2651. $this->fuckhtml->load($carousels[$i]);
  2652. if($item_to_remove !== false){
  2653. $item_to_remove =
  2654. str_replace(
  2655. $carousels[$i]["outerHTML"],
  2656. "",
  2657. $item_to_remove
  2658. );
  2659. }
  2660. $pcitems =
  2661. $this->fuckhtml
  2662. ->getElementsByClassName(
  2663. "pcitem",
  2664. "div"
  2665. );
  2666. foreach($pcitems as $pcitem){
  2667. $this->fuckhtml->load($pcitem);
  2668. $out = [
  2669. "url" => null,
  2670. "ref" => null,
  2671. "image" => null,
  2672. "thumb_width" => null,
  2673. "thumb_height" => null,
  2674. "image_width" => null,
  2675. "image_height" => null,
  2676. "title" => null,
  2677. "description" => null,
  2678. "subtext" => null,
  2679. "date" => null
  2680. ];
  2681. $url =
  2682. $this->unshiturl(
  2683. $this->fuckhtml
  2684. ->getElementsByTagName("a")
  2685. [0]
  2686. ["attributes"]
  2687. ["href"],
  2688. true
  2689. );
  2690. // set ref
  2691. $out["ref"] = $url["ref"];
  2692. // set url
  2693. $out["url"] = $url["url"];
  2694. // set sizes
  2695. $out["thumb_width"] = $url["thumb_width"];
  2696. $out["thumb_height"] = $url["thumb_height"];
  2697. $out["image_width"] = $url["image_width"];
  2698. $out["image_height"] = $url["image_height"];
  2699. // get image
  2700. $out["image"] =
  2701. $this->fuckhtml
  2702. ->getElementsByTagName(
  2703. "img"
  2704. );
  2705. if(count($out["image"]) !== 0){
  2706. // get title from image
  2707. if(isset($out["image"][0]["attributes"]["alt"])){
  2708. $out["title"] =
  2709. $this->titledots(
  2710. $this->fuckhtml
  2711. ->getTextContent(
  2712. $out["image"][0]["attributes"]["alt"]
  2713. )
  2714. );
  2715. }
  2716. // get image url
  2717. if(isset($out["image"][0]["attributes"]["id"])){
  2718. $out["image"] = $this->getimage($out["image"][0]["attributes"]["id"]);
  2719. }
  2720. elseif(isset($out["image"][0]["attributes"]["data-ll"])){
  2721. $out["image"] =
  2722. $this->fuckhtml
  2723. ->getTextContent(
  2724. $out["image"][0]["attributes"]["data-ll"]
  2725. );
  2726. }else{
  2727. // failed to get image information
  2728. $out["image"] = null;
  2729. }
  2730. if($out["image"] == 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYAgMAAACdGdVrAAAADFBMVEVMaXFChfRChfRChfT0tCPZAAAAA3RSTlMAgFJEkGxNAAAAL0lEQVR4AWPADxgdwBT3BTDF9AUiuhdC6WNK/v///y+UggrClSA07EWVglmEFwAA5eYSExeCwigAAAAASUVORK5CYII='){
  2731. // found arrow image base64, skip entry
  2732. continue;
  2733. }
  2734. }else{
  2735. // Could not find any image in node
  2736. $out["image"] = null;
  2737. }
  2738. // get title from spans
  2739. $title =
  2740. $this->fuckhtml
  2741. ->getElementsByClassName(
  2742. $this->findstyles(
  2743. [
  2744. "color" => "#1967d2"
  2745. ],
  2746. self::is_class
  2747. ),
  2748. "span"
  2749. );
  2750. if(count($title) !== 0){
  2751. $out["title"] =
  2752. $this->fuckhtml
  2753. ->getTextContent(
  2754. $title[0]
  2755. );
  2756. }
  2757. // get textnodes
  2758. $textnodes =
  2759. $this->fuckhtml
  2760. ->getElementsByClassName(
  2761. $this->findstyles(
  2762. [
  2763. "white-space" => "pre-line",
  2764. "word-wrap" => "break-word"
  2765. ],
  2766. self::is_class
  2767. )
  2768. );
  2769. $subtext = null;
  2770. if(count($textnodes) !== 0){
  2771. // get date
  2772. $date =
  2773. $this->fuckhtml
  2774. ->getTextContent(
  2775. $textnodes[count($textnodes) - 1],
  2776. true
  2777. );
  2778. if(str_replace("\n", " ", $date) == $title){
  2779. $date = null;
  2780. }else{
  2781. if(strpos($date, "\n") !== false){
  2782. $date = explode("\n", $date);
  2783. $date = $date[count($date) - 1];
  2784. }
  2785. elseif(strpos($date, "•") !== false){
  2786. $date = explode("•", $date);
  2787. $date = ltrim($date[count($date) - 1]);
  2788. }else{
  2789. $date = null;
  2790. }
  2791. }
  2792. if($date !== null){
  2793. $date = strtotime($date);
  2794. }
  2795. // get description
  2796. $description =
  2797. $this->fuckhtml
  2798. ->getTextContent(
  2799. $textnodes[0]
  2800. );
  2801. if($out["title"] === null){
  2802. if($date === null){
  2803. $out["title"] = $description;
  2804. $description = null;
  2805. }else{
  2806. $out["title"] = parse_url($out["url"], PHP_URL_HOST);
  2807. }
  2808. }
  2809. if(isset($textnodes[1])){
  2810. $out["subtext"] =
  2811. $this->fuckhtml
  2812. ->getTextContent(
  2813. $textnodes[1]
  2814. );
  2815. }
  2816. }else{
  2817. $date = null;
  2818. $description = null;
  2819. }
  2820. $out["date"] = $date;
  2821. $out["description"] = $this->titledots($description);
  2822. if($out["url"] === null){
  2823. $out["url"] = $out["title"];
  2824. }
  2825. if($out["title"] == $out["description"]){
  2826. $out["description"] = null;
  2827. }
  2828. $return[$i][] = $out;
  2829. }
  2830. }
  2831. return $return;
  2832. }
  2833. private function unshiturl($url, $return_size = false){
  2834. // get parameters from URL
  2835. $url =
  2836. $this->fuckhtml
  2837. ->getTextContent($url);
  2838. $newurl = parse_url($url, PHP_URL_QUERY);
  2839. if($newurl == ""){
  2840. // probably telephone number
  2841. return $url;
  2842. }
  2843. $url = $newurl;
  2844. unset($newurl);
  2845. parse_str($url, $query);
  2846. if(isset($query["imgurl"])){
  2847. $url = $query["imgurl"];
  2848. }
  2849. elseif(isset($query["q"])){
  2850. $url = $query["q"];
  2851. }
  2852. // rewrite URLs to remove extra tracking parameters
  2853. $domain = parse_url($url, PHP_URL_HOST);
  2854. if(
  2855. preg_match(
  2856. '/wikipedia.org$/',
  2857. $domain
  2858. )
  2859. ){
  2860. // rewrite wikipedia mobile URLs to desktop
  2861. $url =
  2862. $this->replacedomain(
  2863. $url,
  2864. preg_replace(
  2865. '/([a-z0-9]+)(\.m\.)/',
  2866. '$1.',
  2867. $domain
  2868. )
  2869. );
  2870. }
  2871. elseif(
  2872. preg_match(
  2873. '/imdb\.com$|youtube\.[^.]+$/',
  2874. $domain
  2875. )
  2876. ){
  2877. // rewrite imdb and youtube mobile URLs too
  2878. $url =
  2879. $this->replacedomain(
  2880. $url,
  2881. preg_replace(
  2882. '/^m\./',
  2883. "",
  2884. $domain
  2885. )
  2886. );
  2887. }
  2888. elseif(
  2889. preg_match(
  2890. '/play\.google\.[^.]+$/',
  2891. $domain
  2892. )
  2893. ){
  2894. // remove referrers from play.google.com
  2895. $oldquery = parse_url($url, PHP_URL_QUERY);
  2896. if($oldquery !== null){
  2897. parse_str($oldquery, $query);
  2898. if(isset($query["referrer"])){ unset($query["referrer"]); }
  2899. if(isset($query["hl"])){ unset($query["hl"]); }
  2900. if(isset($query["gl"])){ unset($query["gl"]); }
  2901. $query = http_build_query($query);
  2902. $url =
  2903. str_replace(
  2904. $oldquery,
  2905. $query,
  2906. $url
  2907. );
  2908. }
  2909. }
  2910. elseif(
  2911. preg_match(
  2912. '/twitter\.com$/',
  2913. $domain
  2914. )
  2915. ){
  2916. // remove more referrers from twitter.com
  2917. $oldquery = parse_url($url, PHP_URL_QUERY);
  2918. if($oldquery !== null){
  2919. parse_str($oldquery, $query);
  2920. if(isset($query["ref_src"])){ unset($query["ref_src"]); }
  2921. $query = http_build_query($query);
  2922. if($query != ""){
  2923. $query .= "?" . $query;
  2924. }
  2925. $url =
  2926. str_replace(
  2927. '?' . $oldquery,
  2928. $query,
  2929. $url
  2930. );
  2931. }
  2932. }
  2933. elseif(
  2934. preg_match(
  2935. '/maps\.google\.[^.]+/',
  2936. $domain
  2937. )
  2938. ){
  2939. if(stripos($url, "maps?") !== false){
  2940. //https://maps.google.com/maps?daddr=Johnny,+603+Rue+St+Georges,+Saint-J%C3%A9r%C3%B4me,+Quebec+J7Z+5B7
  2941. $query = parse_url($url, PHP_URL_QUERY);
  2942. if($query !== null){
  2943. parse_str($query, $query);
  2944. if(isset($query["daddr"])){
  2945. $url =
  2946. "https://maps.google.com/maps?daddr=" .
  2947. urlencode($query["daddr"]);
  2948. }
  2949. }
  2950. }
  2951. }
  2952. if($return_size){
  2953. return [
  2954. "url" => $url,
  2955. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2956. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2957. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2958. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2959. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2960. ];
  2961. }
  2962. return $url;
  2963. }
  2964. private function replacedomain($url, $domain){
  2965. return
  2966. preg_replace(
  2967. '/(https?:\/\/)([^\/]+)/',
  2968. '$1' . $domain,
  2969. $url
  2970. );
  2971. }
  2972. private function titledots($title){
  2973. return rtrim($title, ". \t\n\r\0\x0B");
  2974. }
  2975. private function detect_sorry($html){
  2976. $this->fuckhtml->load($html);
  2977. $detect_sorry =
  2978. $this->fuckhtml
  2979. ->getElementsByTagName("title");
  2980. if(
  2981. isset($detect_sorry[0]) &&
  2982. $detect_sorry[0]["innerHTML"] == "302 Moved"
  2983. ){
  2984. // may be consent.google.com in europe or /sorry captcha page
  2985. $url =
  2986. $this->fuckhtml
  2987. ->getElementsByTagName("a");
  2988. if(
  2989. strpos(
  2990. parse_url(
  2991. $this->fuckhtml
  2992. ->getTextContent(
  2993. $url[0]["attributes"]["href"]
  2994. ),
  2995. PHP_URL_PATH
  2996. ),
  2997. "/sorry"
  2998. ) === 0
  2999. ){
  3000. // found /sorry
  3001. return "Google blocked this 4get instance. Please setup a proxy!";
  3002. }
  3003. // found consent.google, should not happen anymore
  3004. return "Google served a GPDR consent form. This should not happen, please report if you encounter this message";
  3005. }
  3006. return false;
  3007. }
  3008. }