google.php 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657
  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. "Connection: keep-alive",
  511. "Upgrade-Insecure-Requests: 1",
  512. "Sec-Fetch-Dest: document",
  513. "Sec-Fetch-Mode: navigate",
  514. "Sec-Fetch-Site: none",
  515. "Sec-Fetch-User: ?1"
  516. ];
  517. $curlproc = curl_init();
  518. if($get !== []){
  519. $get = http_build_query($get);
  520. $url .= "?" . $get;
  521. }
  522. curl_setopt($curlproc, CURLOPT_URL, $url);
  523. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  524. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  525. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  526. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  527. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  528. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  529. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  530. $this->backend->assign_proxy($curlproc, $proxy);
  531. $data = curl_exec($curlproc);
  532. if(curl_errno($curlproc)){
  533. throw new Exception(curl_error($curlproc));
  534. }
  535. curl_close($curlproc);
  536. return $data;
  537. }
  538. public function web($get){
  539. if($get["npt"]){
  540. [$req, $ip] = $this->backend->get($get["npt"], "web");
  541. parse_str(
  542. parse_url($req, PHP_URL_QUERY),
  543. $search
  544. );
  545. if(isset($search["q"])){
  546. $search = $search["q"];
  547. }else{
  548. $search = "a"; // lol
  549. }
  550. try{
  551. $html =
  552. $this->get(
  553. $ip,
  554. "https://www.google.com" . $req,
  555. []
  556. );
  557. }catch(Exception $error){
  558. throw new Exception("Failed to get HTML");
  559. }
  560. }else{
  561. $search = $get["s"];
  562. $country = $get["country"];
  563. $nsfw = $get["nsfw"];
  564. $lang = $get["lang"];
  565. $older = $get["older"];
  566. $newer = $get["newer"];
  567. $ip = $this->backend->get_ip();
  568. $params = [
  569. "q" => $search,
  570. "hl" => "en",
  571. "num" => 20 // get 20 results
  572. ];
  573. // country
  574. if($country != "any"){
  575. $params["gl"] = $country;
  576. }
  577. // nsfw
  578. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  579. // language
  580. if($lang != "any"){
  581. $params["lr"] = "lang_" . $lang;
  582. }
  583. // &sort=review-date:r:20090301:20090430
  584. $older = $older === false ? false : date("Ymd", $older);
  585. $newer = $newer === false ? false : date("Ymd", $newer);
  586. if(
  587. $older !== false &&
  588. $newer === false
  589. ){
  590. $newer = date("Ymd", time());
  591. }
  592. if(
  593. $older !== false ||
  594. $newer !== false
  595. ){
  596. $params["sort"] = "review-date:r:" . $older . ":" . $newer;
  597. }
  598. try{
  599. $html =
  600. $this->get(
  601. $ip,
  602. "https://www.google.com/search",
  603. $params
  604. );
  605. }catch(Exception $error){
  606. throw new Exception("Failed to get HTML");
  607. }
  608. //$html = file_get_contents("scraper/google.html");
  609. }
  610. return $this->parsepage($html, "web", $search, $ip);
  611. }
  612. public function video($get){
  613. if($get["npt"]){
  614. [$req, $ip] = $this->backend->get($get["npt"], "videos");
  615. parse_str(
  616. parse_url($req, PHP_URL_QUERY),
  617. $search
  618. );
  619. if(isset($search["q"])){
  620. $search = $search["q"];
  621. }else{
  622. $search = "a"; // lol
  623. }
  624. try{
  625. $html =
  626. $this->get(
  627. $ip,
  628. "https://www.google.com" . $req,
  629. []
  630. );
  631. }catch(Exception $error){
  632. throw new Exception("Failed to get HTML");
  633. }
  634. }else{
  635. $search = $get["s"];
  636. $country = $get["country"];
  637. $nsfw = $get["nsfw"];
  638. $lang = $get["lang"];
  639. $time = $get["time"];
  640. $duration = $get["duration"];
  641. $quality = $get["quality"];
  642. $captions = $get["captions"];
  643. $ip = $this->backend->get_ip();
  644. $params = [
  645. "q" => $search,
  646. "tbm" => "vid",
  647. "hl" => "en",
  648. "num" => "20"
  649. ];
  650. // country
  651. if($country != "any"){
  652. $params["gl"] = $country;
  653. }
  654. // nsfw
  655. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  656. // language
  657. if($lang != "any"){
  658. $params["lr"] = "lang_" . $lang;
  659. }
  660. $tbs = [];
  661. // time
  662. if($time != "any"){
  663. $tbs[] = "qdr:" . $time;
  664. }
  665. // duration
  666. if($duration != "any"){
  667. $tbs[] = "dur:" . $duration;
  668. }
  669. // quality
  670. if($quality != "any"){
  671. $tbs[] = "hq:" . $quality;
  672. }
  673. // captions
  674. if($captions != "any"){
  675. $tbs[] = "cc:" . $captions;
  676. }
  677. // append tbs
  678. if(count($tbs) !== 0){
  679. $params["tbs"] =
  680. implode(",", $tbs);
  681. }
  682. try{
  683. $html =
  684. $this->get(
  685. $ip,
  686. "https://www.google.com/search",
  687. $params
  688. );
  689. }catch(Exception $error){
  690. throw new Exception("Failed to get HTML");
  691. }
  692. }
  693. $json = $this->parsepage($html, "videos", $search, $ip);
  694. $out = [
  695. "status" => "ok",
  696. "npt" => $json["npt"],
  697. "video" => [],
  698. "author" => [],
  699. "livestream" => [],
  700. "playlist" => [],
  701. "reel" => []
  702. ];
  703. foreach($json["web"] as $item){
  704. $out["video"][] = [
  705. "title" => $item["title"],
  706. "description" => $item["description"],
  707. "author" => [
  708. "name" => null,
  709. "url" => null,
  710. "avatar" => null
  711. ],
  712. "date" => isset($item["table"]["Posted"]) ? strtotime($item["table"]["Posted"]) : null,
  713. "duration" => isset($item["table"]["Duration"]) ? $this->hms2int($item["table"]["Duration"]) : null,
  714. "views" => null,
  715. "thumb" =>
  716. $item["thumb"]["url"] === null ?
  717. [
  718. "url" => null,
  719. "ratio" => null
  720. ] :
  721. [
  722. "url" => $item["thumb"]["url"],
  723. "ratio" => "16:9"
  724. ],
  725. "url" => $item["url"]
  726. ];
  727. }
  728. return $out;
  729. }
  730. public function news($get){
  731. if($get["npt"]){
  732. [$req, $ip] = $this->backend->get($get["npt"], "news");
  733. parse_str(
  734. parse_url($req, PHP_URL_QUERY),
  735. $search
  736. );
  737. if(isset($search["q"])){
  738. $search = $search["q"];
  739. }else{
  740. $search = "a"; // lol
  741. }
  742. try{
  743. $html =
  744. $this->get(
  745. $ip,
  746. "https://www.google.com" . $req,
  747. []
  748. );
  749. }catch(Exception $error){
  750. throw new Exception("Failed to get HTML");
  751. }
  752. }else{
  753. $search = $get["s"];
  754. $country = $get["country"];
  755. $nsfw = $get["nsfw"];
  756. $lang = $get["lang"];
  757. $time = $get["time"];
  758. $sort = $get["sort"];
  759. $ip = $this->backend->get_ip();
  760. $params = [
  761. "q" => $search,
  762. "tbm" => "nws",
  763. "hl" => "en",
  764. "num" => "20"
  765. ];
  766. // country
  767. if($country != "any"){
  768. $params["gl"] = $country;
  769. }
  770. // nsfw
  771. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  772. // language
  773. if($lang != "any"){
  774. $params["lr"] = "lang_" . $lang;
  775. }
  776. $tbs = [];
  777. // time
  778. if($time != "any"){
  779. if($time == "a"){
  780. $tbs[] = "ar:1";
  781. }else{
  782. $tbs[] = "qdr:" . $time;
  783. }
  784. }
  785. // relevance
  786. if($sort == "date"){
  787. $tbs[] = "sbd:1";
  788. }
  789. // append tbs
  790. if(count($tbs) !== 0){
  791. $params["tbs"] =
  792. implode(",", $tbs);
  793. }
  794. $html =
  795. $this->get(
  796. $ip,
  797. "https://www.google.com/search",
  798. $params
  799. );
  800. }
  801. $json = $this->parsepage($html, "news", $search, $ip);
  802. $out = [
  803. "status" => "ok",
  804. "npt" => $json["npt"],
  805. "news" => []
  806. ];
  807. foreach($json["web"] as $item){
  808. $description = array_key_first($item["table"]);
  809. if($description !== null){
  810. $date = $item["table"][$description];
  811. }else{
  812. $date = null;
  813. }
  814. $out["news"][] = [
  815. "title" => $item["title"],
  816. "author" => $item["author"],
  817. "description" => $description,
  818. "date" => strtotime($date),
  819. "thumb" =>
  820. $item["thumb"]["url"] === null ?
  821. [
  822. "url" => null,
  823. "ratio" => null
  824. ] :
  825. [
  826. "url" => $item["thumb"]["url"],
  827. "ratio" => "16:9"
  828. ],
  829. "url" => $item["url"]
  830. ];
  831. }
  832. return $out;
  833. }
  834. private function parsepage($html, $pagetype, $search, $ip){
  835. /*
  836. $handle = fopen("scraper/google.html", "r");
  837. $html = fread($handle, filesize("scraper/google.html"));
  838. fclose($handle);
  839. */
  840. $out = [
  841. "status" => "ok",
  842. "spelling" => [
  843. "type" => "no_correction",
  844. "using" => null,
  845. "correction" => null
  846. ],
  847. "npt" => null,
  848. "answer" => [],
  849. "web" => [],
  850. "image" => [],
  851. "video" => [],
  852. "news" => [],
  853. "related" => []
  854. ];
  855. if($this->detect_sorry($html)){
  856. throw new Exception("Google blocked this 4get instance. Please set up a proxy!");
  857. }
  858. $this->parsejavascript($html);
  859. //
  860. // parse accdef's
  861. //
  862. $has_appended_accdef = false;
  863. preg_match_all(
  864. '/window\.jsl\.dh\(\'(accdef_[0-9]+)\',\'(.*)\'\);/',
  865. $html,
  866. $accdefs_regex
  867. );
  868. $accdefs = [];
  869. for($i=0; $i<count($accdefs_regex[0]); $i++){
  870. // decode UTF-16 string
  871. $answer =
  872. $this->fuckhtml
  873. ->parseJsString(
  874. $accdefs_regex[2][$i]
  875. );
  876. $this->fuckhtml->load($answer);
  877. // get description
  878. $description =
  879. $this->fuckhtml
  880. ->getElementsByClassName(
  881. $this->findstyles(
  882. [
  883. "padding" => "12px 16px 12px",
  884. ],
  885. self::is_class
  886. ),
  887. "div"
  888. )[1];
  889. // get date (rare)
  890. $date =
  891. $this->fuckhtml
  892. ->getElementsByTagName("sub");
  893. if(count($date) !== 0){
  894. $description =
  895. str_replace(
  896. $date[0]["outerHTML"],
  897. "",
  898. $description["innerHTML"]
  899. );
  900. $date =
  901. strtotime(
  902. $this->fuckhtml
  903. ->getTextContent(
  904. $date[0]
  905. )
  906. );
  907. }else{
  908. $date = null;
  909. }
  910. // get information table
  911. $table = [];
  912. $tbody =
  913. $this->fuckhtml
  914. ->getElementsByTagName("tbody");
  915. if(count($tbody) !== 0){
  916. $this->fuckhtml->load($tbody[0]);
  917. $trs =
  918. $this->fuckhtml
  919. ->getElementsByTagName("tr");
  920. foreach($trs as $tr){
  921. $this->fuckhtml->load($tr);
  922. $tds =
  923. $this->fuckhtml
  924. ->getElementsByTagName("td");
  925. if(count($tds) === 2){
  926. $table[
  927. $this->fuckhtml
  928. ->getTextContent(
  929. $tds[0]
  930. )
  931. ] =
  932. $this->fuckhtml
  933. ->getTextContent(
  934. $tds[1]
  935. );
  936. }
  937. }
  938. // load back what we had
  939. $this->fuckhtml->load($answer);
  940. }
  941. // get title & link
  942. $a =
  943. $this->fuckhtml
  944. ->getElementsByTagName("a")[0];
  945. $this->fuckhtml->load($a);
  946. $title =
  947. $this->fuckhtml
  948. ->getElementsByTagName("span");
  949. if(count($title) === 0){
  950. continue;
  951. }
  952. $accdefs[] = [
  953. "title" =>
  954. $this->titledots(
  955. $this->fuckhtml
  956. ->getTextContent(
  957. $title[0]
  958. )
  959. ),
  960. "description" =>
  961. $this->fuckhtml
  962. ->getTextContent(
  963. $description
  964. ),
  965. "url" =>
  966. $this->unshiturl(
  967. $a["attributes"]["href"]
  968. ),
  969. "date" => $date,
  970. "type" => "web",
  971. "thumb" => [
  972. "url" => null,
  973. "ratio" => null
  974. ],
  975. "sublink" => [],
  976. "table" => $table
  977. ];
  978. }
  979. $this->fuckhtml->load($html);
  980. $containers =
  981. $this->fuckhtml
  982. ->getElementsByClassName(
  983. $this->findstyles(
  984. [
  985. "background-color" => "#fff",
  986. "margin-bottom" => "10px",
  987. "-webkit-box-shadow" => "0 1px 6px rgba(32,33,36,0.28)",
  988. "border-radius" => "8px"
  989. ],
  990. self::is_class
  991. ),
  992. "div"
  993. );
  994. foreach($containers as $container){
  995. $this->fuckhtml->load($container);
  996. // detect spelling
  997. $spelling =
  998. $this->fuckhtml
  999. ->getElementsByClassName(
  1000. $this->findstyles(
  1001. [
  1002. "font-size" => "20px",
  1003. "line-height" => "26px",
  1004. "padding-top" => "2px",
  1005. "margin-bottom" => "1px"
  1006. ],
  1007. self::is_class
  1008. ),
  1009. "div"
  1010. );
  1011. if(count($spelling) !== 0){
  1012. $a =
  1013. $this->fuckhtml
  1014. ->getElementsByTagName("a");
  1015. if(count($a) !== 0){
  1016. $scripts =
  1017. $this->fuckhtml
  1018. ->getElementsByTagName("script");
  1019. foreach($scripts as $script){
  1020. $container["innerHTML"] =
  1021. str_replace(
  1022. $script["outerHTML"],
  1023. "",
  1024. $container["innerHTML"]
  1025. );
  1026. }
  1027. $container["innerHTML"] =
  1028. $this->fuckhtml
  1029. ->getTextContent(
  1030. str_replace(
  1031. $a[0]["outerHTML"],
  1032. "",
  1033. $container["innerHTML"]
  1034. )
  1035. );
  1036. if(
  1037. preg_match(
  1038. '/^did you mean/i',
  1039. $container["innerHTML"]
  1040. )
  1041. ){
  1042. $out["spelling"] = [
  1043. "type" => "not_many",
  1044. "using" => $search,
  1045. "correction" =>
  1046. $this->fuckhtml
  1047. ->getTextContent(
  1048. $a[0]
  1049. )
  1050. ];
  1051. }
  1052. elseif(
  1053. preg_match(
  1054. '/^showing results for/i',
  1055. $container["innerHTML"]
  1056. )
  1057. ){
  1058. $out["spelling"] = [
  1059. "type" => "including",
  1060. "using" =>
  1061. $this->fuckhtml
  1062. ->getTextContent(
  1063. $a[0]
  1064. ),
  1065. "correction" => $search
  1066. ];
  1067. }
  1068. }
  1069. continue;
  1070. }
  1071. $title =
  1072. $this->fuckhtml
  1073. ->getElementsByClassName(
  1074. $this->findstyles(
  1075. [
  1076. "color" => "#1967d2",
  1077. "font-size" => "20px",
  1078. "line-height" => "26px"
  1079. ],
  1080. self::is_class
  1081. ),
  1082. "div"
  1083. );
  1084. if(count($title) !== 0){
  1085. //
  1086. // Container is a web link
  1087. //
  1088. $web = [
  1089. "title" =>
  1090. $this->titledots(
  1091. $this->fuckhtml
  1092. ->getTextContent(
  1093. $title[0]
  1094. )
  1095. ),
  1096. "description" => null,
  1097. "url" => null,
  1098. "date" => null,
  1099. "type" => "web",
  1100. "thumb" => [
  1101. "url" => null,
  1102. "ratio" => null
  1103. ],
  1104. "sublink" => [],
  1105. "table" => []
  1106. ];
  1107. // get link
  1108. $web["url"] =
  1109. $this->unshiturl(
  1110. $this->fuckhtml
  1111. ->getElementsByTagName("a")
  1112. [0]
  1113. ["attributes"]
  1114. ["href"]
  1115. );
  1116. //
  1117. // check if link contains a carousel
  1118. //
  1119. $carousels = $this->parsecarousels();
  1120. if(count($carousels) !== 0){
  1121. $first = true;
  1122. foreach($carousels as $carousel_cat){
  1123. foreach($carousel_cat as $carousel){
  1124. if($first === true){
  1125. $first = false;
  1126. }elseif($carousel["image"] !== null){
  1127. $out["image"][] = [
  1128. "title" => $carousel["title"],
  1129. "source" => [
  1130. [
  1131. "url" => $carousel["image"],
  1132. "width" => null,
  1133. "height" => null
  1134. ]
  1135. ],
  1136. "url" => $carousel["url"]
  1137. ];
  1138. }
  1139. $web["sublink"][] = [
  1140. "title" => $carousel["title"],
  1141. "date" => $carousel["date"],
  1142. "description" => $carousel["description"],
  1143. "url" => $carousel["url"]
  1144. ];
  1145. }
  1146. }
  1147. if($carousels[0][0]["image"] !== null){
  1148. $web["thumb"] = [
  1149. "url" => $carousels[0][0]["image"],
  1150. "ratio" => "16:9"
  1151. ];
  1152. }
  1153. $out["web"][] = $web;
  1154. continue;
  1155. }
  1156. //
  1157. // no carousel entries, parse as normal link
  1158. //
  1159. $this->fuckhtml->load($container);
  1160. // parse URL
  1161. $web["url"] =
  1162. $this->unshiturl(
  1163. $this->fuckhtml
  1164. ->getElementsByTagName("a")
  1165. [0]
  1166. ["attributes"]
  1167. ["href"]
  1168. );
  1169. $container = $container["innerHTML"];
  1170. $line_detect =
  1171. $this->fuckhtml
  1172. ->getElementsByClassName(
  1173. $this->findstyles(
  1174. [
  1175. "height" => "1px",
  1176. "background-color" => "#dadce0",
  1177. "margin" => "0 16px"
  1178. ],
  1179. self::is_class
  1180. ),
  1181. "div"
  1182. );
  1183. if(count($line_detect) !== 0){
  1184. // we found a line, this means we're dealing with a
  1185. // "featured snippet"
  1186. $featured = true;
  1187. $description_container =
  1188. $this->fuckhtml
  1189. ->getElementsByClassName(
  1190. $this->findstyles(
  1191. [
  1192. "white-space" => "pre-line",
  1193. "word-wrap" => "break-word"
  1194. ],
  1195. self::is_class
  1196. ),
  1197. "div"
  1198. )[1];
  1199. // get date node for it
  1200. $date =
  1201. $this->fuckhtml
  1202. ->getElementsByTagName("sub");
  1203. if(count($date) !== 0){
  1204. $web["date"] =
  1205. strtotime(
  1206. $this->fuckhtml
  1207. ->getTextContent(
  1208. $date[0]
  1209. )
  1210. );
  1211. }
  1212. }else{
  1213. // we're dealing with a normal link
  1214. $featured = false;
  1215. $description_container =
  1216. $this->fuckhtml
  1217. ->getElementsByClassName(
  1218. $this->findstyles(
  1219. [
  1220. "padding" => "12px 16px 12px"
  1221. ],
  1222. self::is_class
  1223. ),
  1224. "div"
  1225. )[1];
  1226. }
  1227. //
  1228. // Get author if we're parsing news
  1229. //
  1230. if($pagetype == "news"){
  1231. $author =
  1232. $this->fuckhtml
  1233. ->getElementsByClassName(
  1234. $this->findstyles(
  1235. [
  1236. "position" => "absolute",
  1237. "width" => "100%",
  1238. "top" => "0",
  1239. "left" => "0",
  1240. "padding-top" => "1px",
  1241. "margin-bottom" => "-1px"
  1242. ],
  1243. self::is_class
  1244. ),
  1245. "div"
  1246. );
  1247. if(count($author) !== 0){
  1248. $web["author"] =
  1249. $this->fuckhtml
  1250. ->getTextContent(
  1251. $author[0]
  1252. );
  1253. }else{
  1254. $web["author"] = null;
  1255. }
  1256. }
  1257. $description =
  1258. $description_container["innerHTML"];
  1259. $this->fuckhtml->load($description);
  1260. //
  1261. // get thumbnail before we call loadhtml again
  1262. //
  1263. $img =
  1264. $this->fuckhtml
  1265. ->getElementsByTagName("img");
  1266. if(count($img) !== 0){
  1267. $skip = true;
  1268. if(
  1269. isset($img[0]["attributes"]["alt"]) &&
  1270. stripos($img[0]["attributes"]["alt"], "Video for") !== false
  1271. ){
  1272. // is a video thumbnail
  1273. $web["thumb"]["ratio"] = "16:9";
  1274. }else{
  1275. // is a google thumbnail
  1276. $web["thumb"]["ratio"] = "1:1";
  1277. }
  1278. $web["thumb"]["url"] =
  1279. $this->getimage(
  1280. $img[0]["attributes"]["id"]
  1281. );
  1282. }else{
  1283. $skip = false;
  1284. }
  1285. //
  1286. // get sublinks
  1287. //
  1288. $links =
  1289. $this->fuckhtml
  1290. ->getElementsByTagName("a");
  1291. foreach($links as $link){
  1292. if($skip === true){
  1293. $skip = false;
  1294. continue;
  1295. }
  1296. $description =
  1297. str_replace(
  1298. $link["outerHTML"],
  1299. "",
  1300. $description
  1301. );
  1302. $sublink = [
  1303. "title" => null,
  1304. "description" => null,
  1305. "url" => null,
  1306. "date" => null
  1307. ];
  1308. $sublink["title"] =
  1309. $this->titledots(
  1310. $this->fuckhtml
  1311. ->getTextContent(
  1312. $link
  1313. )
  1314. );
  1315. $sublink["url"] =
  1316. $this->unshiturl(
  1317. $link
  1318. ["attributes"]
  1319. ["href"]
  1320. );
  1321. if(parse_url($sublink["url"], PHP_URL_HOST) !== null){
  1322. $web["sublink"][] = $sublink;
  1323. }
  1324. }
  1325. //
  1326. // Parse spans in description
  1327. //
  1328. $this->fuckhtml->load($description);
  1329. if($featured === false){
  1330. $levels =
  1331. $this->fuckhtml
  1332. ->getElementsByClassName(
  1333. $this->findstyles(
  1334. [
  1335. "padding-bottom" => "8px"
  1336. ],
  1337. self::is_class
  1338. ),
  1339. "div"
  1340. );
  1341. // oh my god yes, fucking great, sometimes there are NO levels
  1342. // hahahahahhahahahahahahahahahhahaa
  1343. if(count($levels) === 0){
  1344. $levels = [$description];
  1345. }
  1346. foreach($levels as $level){
  1347. $this->fuckhtml->load($level);
  1348. $spans =
  1349. $this->fuckhtml
  1350. ->getElementsByTagName(
  1351. "span"
  1352. );
  1353. $is_rating = -1;
  1354. foreach($spans as $span){
  1355. $innertext =
  1356. trim(
  1357. $this->fuckhtml
  1358. ->getTextContent(
  1359. $span
  1360. ),
  1361. " ·."
  1362. );
  1363. if($innertext == ""){ continue; }
  1364. if(
  1365. strtolower($innertext)
  1366. == "rating"
  1367. ){
  1368. $is_rating = 0;
  1369. // clean up before we go
  1370. $description =
  1371. str_replace(
  1372. $span["outerHTML"],
  1373. "",
  1374. $description
  1375. );
  1376. continue;
  1377. }
  1378. //
  1379. // Parse rating object
  1380. //
  1381. if($is_rating >= 0){
  1382. // clean up description
  1383. $description =
  1384. str_replace(
  1385. $span["outerHTML"],
  1386. "",
  1387. $description
  1388. );
  1389. if($span["level"] !== 1){ continue; }
  1390. $is_rating++;
  1391. // 10/10 (123)
  1392. if($is_rating === 1){
  1393. $innertext = explode(" ", $innertext, 2);
  1394. $web["table"]["Rating"] = $innertext[0];
  1395. if(count($innertext) === 2){
  1396. $web["table"]["Hits"] =
  1397. trim(
  1398. str_replace(
  1399. [
  1400. "(",
  1401. ")"
  1402. ],
  1403. "",
  1404. $innertext[1]
  1405. )
  1406. );
  1407. if($web["table"]["Hits"] == ""){
  1408. unset($web["table"]["Hits"]);
  1409. }
  1410. }
  1411. continue;
  1412. }
  1413. // US$4.99
  1414. // MYR 50.00
  1415. // $38.34
  1416. // JP¥6,480
  1417. // Reviewed by your mom
  1418. if($is_rating === 2){
  1419. if(
  1420. preg_match(
  1421. '/^Review by (.+)/',
  1422. $innertext,
  1423. $match
  1424. )
  1425. ){
  1426. $web["table"]["Author"] = $match[1];
  1427. continue;
  1428. }
  1429. $web["table"]["Price"] = $innertext;
  1430. continue;
  1431. }
  1432. // Android / In stock
  1433. if($is_rating === 3){
  1434. $web["table"]["Support"] = $innertext;
  1435. continue;
  1436. }
  1437. // ignore the rest
  1438. continue;
  1439. }
  1440. //
  1441. // Parse standalone text
  1442. //
  1443. // If we reach this point:
  1444. // 1. Ratings have been parsed
  1445. // 2. We're parsing a WEB link, not some shitty piece of shit
  1446. // check for date
  1447. // if span has no text before it, assume it's a date
  1448. $desc_split =
  1449. explode(
  1450. $span["outerHTML"],
  1451. $description,
  1452. 2
  1453. );
  1454. if(
  1455. $this->fuckhtml
  1456. ->getTextContent(
  1457. $desc_split[0]
  1458. ) == ""
  1459. ){
  1460. // has no text before
  1461. $date = strtotime($innertext);
  1462. if($date){
  1463. $web["date"] = $date;
  1464. }
  1465. // cleanup
  1466. $description =
  1467. str_replace(
  1468. $span["outerHTML"],
  1469. "",
  1470. $description
  1471. );
  1472. continue;
  1473. }
  1474. // Ready to parse table
  1475. if(count($desc_split) === 2){
  1476. $this->fuckhtml->load($desc_split[1]);
  1477. $web["table"][
  1478. $this->fuckhtml
  1479. ->getTextContent(
  1480. trim($desc_split[0], ": ")
  1481. )
  1482. ] = $innertext;
  1483. // cleanup
  1484. $description =
  1485. str_replace(
  1486. $desc_split[0] . $span["outerHTML"],
  1487. "",
  1488. $description
  1489. );
  1490. }
  1491. }
  1492. }
  1493. }
  1494. $web["description"] =
  1495. trim(
  1496. $this->fuckhtml
  1497. ->getTextContent(
  1498. $description
  1499. ),
  1500. " ·."
  1501. );
  1502. if($web["description"] == ""){
  1503. $web["description"] = null;
  1504. }
  1505. $out["web"][] = $web;
  1506. continue;
  1507. }
  1508. //
  1509. // Detect wikipedia shit
  1510. //
  1511. $wiki_title =
  1512. $this->fuckhtml
  1513. ->getElementsByTagName("h3");
  1514. if(count($wiki_title) !== 0){
  1515. $description_after = [];
  1516. $description = [];
  1517. $table = [];
  1518. $sublink = [];
  1519. $as =
  1520. $this->fuckhtml
  1521. ->getElementsByTagName("a");
  1522. foreach($as as $a){
  1523. if(
  1524. isset($a["attributes"]["href"]) &&
  1525. parse_url($a["attributes"]["href"], PHP_URL_HOST) == "maps.google.com"
  1526. ){
  1527. // detected maps embed, ignore
  1528. continue 2;
  1529. }
  1530. }
  1531. // get carousels and remove them from container for image grepper
  1532. $carousels = $this->parsecarousels($container["innerHTML"]);
  1533. $this->fuckhtml->load($container);
  1534. // add images to image tab, if applicable
  1535. for($i=0; $i<count($carousels); $i++){
  1536. foreach($carousels[$i] as $item){
  1537. if(
  1538. $item["url"] !== null &&
  1539. $item["ref"] !== null &&
  1540. $item["image"] !== null &&
  1541. $item["title"] !== null
  1542. ){
  1543. $out["image"][] = [
  1544. "title" => $item["title"],
  1545. "source" => [
  1546. [
  1547. "url" => $item["url"],
  1548. "width" => $item["image_width"],
  1549. "height" => $item["image_height"]
  1550. ],
  1551. [
  1552. "url" => $item["image"],
  1553. "width" => $item["thumb_width"],
  1554. "height" => $item["thumb_height"]
  1555. ]
  1556. ],
  1557. "url" => $item["ref"]
  1558. ];
  1559. unset($carousels[$i]);
  1560. }
  1561. }
  1562. }
  1563. $carousels = array_values($carousels);
  1564. // interpret remaining carousels as title + carousel
  1565. $titles =
  1566. $this->fuckhtml
  1567. ->getElementsByClassName(
  1568. $this->findstyles(
  1569. [
  1570. "font-weight" => "700",
  1571. "letter-spacing" => "0.75px",
  1572. "text-transform" => "uppercase"
  1573. ],
  1574. self::is_class
  1575. )
  1576. );
  1577. for($i=0; $i<count($titles); $i++){
  1578. if(!isset($carousels[$i])){
  1579. break;
  1580. }
  1581. $description_after[] = [
  1582. "type" => "title",
  1583. "value" =>
  1584. $this->fuckhtml
  1585. ->getTextContent(
  1586. $titles[$i]
  1587. )
  1588. ];
  1589. foreach($carousels[$i] as $carousel){
  1590. $description_after[] = [
  1591. "type" => "link",
  1592. "url" => "web?s=" . urlencode($carousel["description"]) . "&scraper=google",
  1593. "value" => $carousel["description"]
  1594. ];
  1595. if($carousel["subtext"] !== null){
  1596. $description_after[] = [
  1597. "type" => "quote",
  1598. "value" => $carousel["subtext"]
  1599. ];
  1600. }
  1601. $description_after[] = [
  1602. "type" => "image",
  1603. "url" => $carousel["image"]
  1604. ];
  1605. }
  1606. }
  1607. $categories =
  1608. $this->fuckhtml
  1609. ->getElementsByClassName(
  1610. $this->findstyles(
  1611. [
  1612. "padding" => "12px 16px 12px"
  1613. ],
  1614. self::is_class
  1615. )
  1616. );
  1617. $image =
  1618. $this->fuckhtml
  1619. ->getElementsByTagName("img");
  1620. if(count($image) !== 0){
  1621. $image = $this->getimage($image[0]["attributes"]["id"]);
  1622. }else{
  1623. $image = null;
  1624. }
  1625. $url = null;
  1626. for($i=0; $i<count($categories); $i++){
  1627. $this->fuckhtml->load($categories[$i]);
  1628. if($i === 0){
  1629. // first node. this should be the header with the small
  1630. // information snippet
  1631. $url =
  1632. $this->fuckhtml
  1633. ->getElementsByTagName("a");
  1634. if(count($url) !== 0){
  1635. $url =
  1636. $this->unshiturl(
  1637. $url[0]["attributes"]["href"]
  1638. );
  1639. if(parse_url($url, PHP_URL_HOST) == "encrypted-tbn0.gstatic.com"){
  1640. $image = $url;
  1641. $url = null;
  1642. }
  1643. }else{
  1644. $url = null;
  1645. }
  1646. $categories[$i]["innerHTML"] =
  1647. str_replace(
  1648. $wiki_title[0]["outerHTML"],
  1649. "",
  1650. $categories[$i]["innerHTML"]
  1651. );
  1652. $subtext =
  1653. $this->fuckhtml
  1654. ->getTextContent(
  1655. $categories[$i]["innerHTML"]
  1656. );
  1657. if(strlen($subtext) !== 0){
  1658. $description[] = [
  1659. "type" => "quote",
  1660. "value" =>
  1661. $this->fuckhtml
  1662. ->getTextContent(
  1663. $categories[$i]["innerHTML"]
  1664. )
  1665. ];
  1666. }
  1667. // detect audio file
  1668. $audio =
  1669. $this->fuckhtml
  1670. ->getElementsByTagName("audio");
  1671. if(count($audio) !== 0){
  1672. $description[] = [
  1673. "type" => "audio",
  1674. "url" =>
  1675. $this->fuckhtml
  1676. ->getTextContent(
  1677. $audio[0]["attributes"]["src"]
  1678. )
  1679. ];
  1680. }
  1681. }else{
  1682. // check for separator elements IN THERE
  1683. $separators =
  1684. $this->fuckhtml
  1685. ->getElementsByClassName(
  1686. $this->findstyles(
  1687. [
  1688. "white-space" => "pre-line",
  1689. "word-wrap" => "break-word"
  1690. ],
  1691. self::is_class
  1692. ),
  1693. "div"
  1694. );
  1695. // detect container type
  1696. foreach($separators as $separator){
  1697. $this->fuckhtml->load($separator);
  1698. // ignore wrong levels
  1699. if($separator["level"] !== 2){
  1700. continue;
  1701. }
  1702. //
  1703. // Detect word definition
  1704. //
  1705. $wordwraps =
  1706. $this->fuckhtml
  1707. ->getElementsByClassName(
  1708. $this->findstyles(
  1709. [
  1710. "padding-bottom" => "12px"
  1711. ],
  1712. self::is_class
  1713. ),
  1714. "div"
  1715. );
  1716. if(count($wordwraps) !== 0){
  1717. foreach($wordwraps as $word){
  1718. $this->fuckhtml->load($word);
  1719. // detect title
  1720. $span =
  1721. $this->fuckhtml
  1722. ->getElementsByTagName(
  1723. "span"
  1724. );
  1725. if(
  1726. count($span) === 1 &&
  1727. $this->fuckhtml
  1728. ->getTextContent(
  1729. str_replace(
  1730. $span[0]["outerHTML"],
  1731. "",
  1732. $word["innerHTML"]
  1733. )
  1734. ) == ""
  1735. ){
  1736. $description[] = [
  1737. "type" => "title",
  1738. "value" =>
  1739. $this->fuckhtml
  1740. ->getTextContent(
  1741. $span[0]
  1742. )
  1743. ];
  1744. continue;
  1745. }
  1746. // detect list element
  1747. $lists =
  1748. $this->fuckhtml
  1749. ->getElementsByTagName("ol");
  1750. if(count($lists) !== 0){
  1751. foreach($lists as $list){
  1752. $this->fuckhtml->load($list);
  1753. $items =
  1754. $this->fuckhtml
  1755. ->getElementsByTagName("li");
  1756. $w = 0;
  1757. foreach($items as $item){
  1758. $w++;
  1759. $this->fuckhtml->load($item);
  1760. // get subnodes
  1761. $subnodes =
  1762. $this->fuckhtml
  1763. ->getElementsByClassName(
  1764. $this->findstyles(
  1765. [
  1766. "white-space" => "pre-line",
  1767. "word-wrap" => "break-word"
  1768. ],
  1769. self::is_class
  1770. ),
  1771. "div"
  1772. );
  1773. foreach($subnodes as $subnode){
  1774. $this->fuckhtml->load($subnode);
  1775. $spans =
  1776. $this->fuckhtml
  1777. ->getElementsByTagName("span");
  1778. if(count($spans) !== 0){
  1779. // append quote
  1780. $description[] = [
  1781. "type" => "quote",
  1782. "value" =>
  1783. $this->fuckhtml
  1784. ->getTextContent(
  1785. $subnode
  1786. )
  1787. ];
  1788. }else{
  1789. // append text
  1790. $description[] = [
  1791. "type" => "text",
  1792. "value" =>
  1793. $w . ". " .
  1794. $this->fuckhtml
  1795. ->getTextContent(
  1796. $subnode
  1797. )
  1798. ];
  1799. }
  1800. }
  1801. }
  1802. }
  1803. }else{
  1804. // parse without list
  1805. // get subnodes
  1806. $subnodes =
  1807. $this->fuckhtml
  1808. ->getElementsByClassName(
  1809. $this->findstyles(
  1810. [
  1811. "white-space" => "pre-line",
  1812. "word-wrap" => "break-word"
  1813. ],
  1814. self::is_class
  1815. ),
  1816. "div"
  1817. );
  1818. foreach($subnodes as $subnode){
  1819. $this->fuckhtml->load($subnode);
  1820. $spans =
  1821. $this->fuckhtml
  1822. ->getElementsByTagName("span");
  1823. if(count($spans) !== 0){
  1824. // append quote
  1825. $description[] = [
  1826. "type" => "quote",
  1827. "value" =>
  1828. $this->fuckhtml
  1829. ->getTextContent(
  1830. $subnode
  1831. )
  1832. ];
  1833. }else{
  1834. // append text
  1835. $description[] = [
  1836. "type" => "text",
  1837. "value" =>
  1838. $this->fuckhtml
  1839. ->getTextContent(
  1840. $subnode
  1841. )
  1842. ];
  1843. }
  1844. }
  1845. }
  1846. }
  1847. }else{
  1848. //
  1849. // Parse table
  1850. //
  1851. $spans =
  1852. $this->fuckhtml
  1853. ->getElementsByTagName("span");
  1854. foreach($spans as $span){
  1855. if(!isset($span["attributes"]["class"])){
  1856. // found table
  1857. $row =
  1858. explode(
  1859. ":",
  1860. $this->fuckhtml
  1861. ->getTextContent(
  1862. $separator
  1863. ),
  1864. 2
  1865. );
  1866. if(count($row) === 2){
  1867. $table[rtrim($row[0])] =
  1868. ltrim($row[1]);
  1869. }
  1870. continue 2;
  1871. }
  1872. }
  1873. //
  1874. // Parse normal description
  1875. //
  1876. $links_rem =
  1877. $this->fuckhtml
  1878. ->getElementsByTagName("a");
  1879. foreach($links_rem as $rem){
  1880. $separator["innerHTML"] =
  1881. str_replace(
  1882. $rem["outerHTML"],
  1883. "",
  1884. $separator["innerHTML"]
  1885. );
  1886. }
  1887. $description[] = [
  1888. "type" => "text",
  1889. "value" =>
  1890. rtrim(
  1891. $this->fuckhtml
  1892. ->getTextContent(
  1893. $separator
  1894. ),
  1895. " .,"
  1896. )
  1897. ];
  1898. }
  1899. }
  1900. }
  1901. // detect huge buttons
  1902. $buttons =
  1903. $this->fuckhtml
  1904. ->getElementsByClassName(
  1905. $this->findstyles(
  1906. [
  1907. "display" => "table-cell",
  1908. "vertical-align" => "middle",
  1909. "height" => "52px",
  1910. "text-align" => "center"
  1911. ],
  1912. self::is_class
  1913. ),
  1914. "a"
  1915. );
  1916. if(count($buttons) !== 0){
  1917. foreach($buttons as $button){
  1918. if(isset($button["attributes"]["href"])){
  1919. $sublink[
  1920. $this->fuckhtml
  1921. ->getTextContent(
  1922. $button
  1923. )
  1924. ] =
  1925. $this->unshiturl(
  1926. $button["attributes"]["href"]
  1927. );
  1928. }
  1929. }
  1930. }
  1931. }
  1932. // append description_after (contains carousel info)
  1933. $description = array_merge(
  1934. $description,
  1935. $description_after
  1936. );
  1937. $out["answer"][] = [
  1938. "title" =>
  1939. $this->fuckhtml
  1940. ->getTextContent(
  1941. $wiki_title[0]
  1942. ),
  1943. "description" => $description,
  1944. "url" => $url,
  1945. "thumb" => $image,
  1946. "table" => $table,
  1947. "sublink" => $sublink
  1948. ];
  1949. continue;
  1950. }
  1951. //
  1952. // Detect related searches containers
  1953. //
  1954. $container_title =
  1955. $this->fuckhtml
  1956. ->getElementsByClassName(
  1957. $this->findstyles(
  1958. [
  1959. "color" => "#000",
  1960. "font-size" => "16px",
  1961. "font-weight" => "bold",
  1962. "margin" => "0",
  1963. "padding" => "12px 16px 0px 16px"
  1964. ],
  1965. self::is_class
  1966. ),
  1967. "div"
  1968. );
  1969. if(count($container_title) !== 0){
  1970. // get carousel entries
  1971. $carousels = $this->parsecarousels($container["innerHTML"]);
  1972. $this->fuckhtml->load($container);
  1973. foreach($carousels as $carousel){
  1974. foreach($carousel as $item){
  1975. if($item["url"] !== null){
  1976. $out["related"][] = $item["url"];
  1977. }
  1978. }
  1979. }
  1980. $container_title =
  1981. strtolower(
  1982. $this->fuckhtml
  1983. ->getTextContent(
  1984. $container_title[0]
  1985. )
  1986. );
  1987. switch($container_title){
  1988. case "related searches":
  1989. case "people also search for":
  1990. //
  1991. // Parse related searches
  1992. //
  1993. $as =
  1994. $this->fuckhtml
  1995. ->getElementsByTagName("a");
  1996. foreach($as as $a){
  1997. $out["related"][] =
  1998. $this->fuckhtml
  1999. ->getTextContent($a);
  2000. }
  2001. break;
  2002. case "people also ask":
  2003. // get related queries
  2004. $divs =
  2005. $this->fuckhtml
  2006. ->getElementsByTagName("div");
  2007. foreach($divs as $div){
  2008. // add accdef's here
  2009. if($has_appended_accdef === false){
  2010. $out["web"] = array_merge($out["web"], $accdefs);
  2011. $has_appended_accdef = true;
  2012. }
  2013. // add accdef's questions
  2014. if(isset($div["attributes"]["role"])){
  2015. $out["related"][] =
  2016. $this->fuckhtml
  2017. ->getTextContent($div);
  2018. continue;
  2019. }
  2020. }
  2021. break;
  2022. }
  2023. continue;
  2024. }
  2025. //
  2026. // Parse news
  2027. //
  2028. $title =
  2029. $this->fuckhtml
  2030. ->getElementsByClassName(
  2031. $this->findstyles(
  2032. [
  2033. "font-size" => "16px",
  2034. "line-height" => "20px",
  2035. "font-weight" => "400"
  2036. ],
  2037. self::is_class
  2038. ),
  2039. "div"
  2040. );
  2041. if(count($title) !== 0){
  2042. $carousels = $this->parsecarousels();
  2043. $this->fuckhtml->load($container);
  2044. if(count($carousels) === 0){
  2045. // no carousels found
  2046. continue;
  2047. }
  2048. $title =
  2049. strtolower(
  2050. $this->fuckhtml
  2051. ->getTextContent(
  2052. $title[0]
  2053. )
  2054. );
  2055. if(
  2056. preg_match(
  2057. '/^latest from|^top stories/',
  2058. $title
  2059. )
  2060. ){
  2061. // Found news article
  2062. foreach($carousels[0] as $carousel){
  2063. if($carousel["image"] !== null){
  2064. $thumb = [
  2065. "url" => $carousel["image"],
  2066. "ratio" => "16:9"
  2067. ];
  2068. }else{
  2069. $thumb = [
  2070. "url" => null,
  2071. "ratio" => null
  2072. ];
  2073. }
  2074. $out["news"][] = [
  2075. "title" => $carousel["title"],
  2076. "description" => $carousel["description"],
  2077. "date" => $carousel["date"],
  2078. "thumb" => $thumb,
  2079. "url" => $carousel["url"]
  2080. ];
  2081. }
  2082. }
  2083. elseif(
  2084. $title == "images"
  2085. ){
  2086. foreach($carousels as $carousel){
  2087. foreach($carousel as $item){
  2088. $out["image"][] = [
  2089. "title" => $item["title"],
  2090. "source" => [
  2091. [
  2092. "url" => $item["url"],
  2093. "width" => $item["image_width"],
  2094. "height" => $item["image_height"]
  2095. ],
  2096. [
  2097. "url" => $item["image"],
  2098. "width" => $item["thumb_width"],
  2099. "height" => $item["thumb_height"]
  2100. ]
  2101. ],
  2102. "url" => $item["ref"]
  2103. ];
  2104. }
  2105. }
  2106. }
  2107. continue;
  2108. }
  2109. //
  2110. // Detect nodes with only text + links
  2111. //
  2112. // ignore elements with <style> tags
  2113. $style =
  2114. $this->fuckhtml
  2115. ->getElementsByTagName("style");
  2116. if(count($style) !== 0){
  2117. continue;
  2118. }
  2119. $as =
  2120. $this->fuckhtml
  2121. ->getElementsByTagName("a");
  2122. $description = [];
  2123. $pcitems =
  2124. $this->fuckhtml
  2125. ->getElementsByClassName(
  2126. "pcitem",
  2127. "div"
  2128. );
  2129. if(count($pcitems) !== 0){
  2130. // ignore elements with carousels in them
  2131. continue;
  2132. }
  2133. foreach($as as $a){
  2134. //
  2135. // Detect next page
  2136. //
  2137. if(
  2138. isset($a["attributes"]["aria-label"]) &&
  2139. strtolower($a["attributes"]["aria-label"]) == "next page"
  2140. ){
  2141. $out["npt"] =
  2142. $this->backend->store(
  2143. $this->fuckhtml
  2144. ->getTextContent(
  2145. $a["attributes"]["href"]
  2146. ),
  2147. $pagetype,
  2148. $ip
  2149. );
  2150. continue 2;
  2151. }
  2152. //
  2153. // Parse as text node
  2154. //
  2155. $container["innerHTML"] =
  2156. explode(
  2157. $a["outerHTML"],
  2158. $container["innerHTML"],
  2159. 2
  2160. );
  2161. $before =
  2162. $this->fuckhtml
  2163. ->getTextContent(
  2164. $container["innerHTML"][0],
  2165. false,
  2166. false
  2167. );
  2168. // set after
  2169. if(count($container["innerHTML"]) === 2){
  2170. $container["innerHTML"] =
  2171. $container["innerHTML"][1];
  2172. }else{
  2173. $container["innerHTML"] = "";
  2174. }
  2175. if($before != ""){
  2176. $description[] = [
  2177. "type" => "text",
  2178. "value" => $before
  2179. ];
  2180. }
  2181. // add link
  2182. $description[] = [
  2183. "type" => "link",
  2184. "url" =>
  2185. $this->unshiturl(
  2186. $a["attributes"]
  2187. ["href"]
  2188. ),
  2189. "value" =>
  2190. $this->fuckhtml
  2191. ->getTextContent(
  2192. $a
  2193. )
  2194. ];
  2195. }
  2196. if($container["innerHTML"] != ""){
  2197. $description[] = [
  2198. "type" => "text",
  2199. "value" =>
  2200. $this->fuckhtml
  2201. ->getTextContent(
  2202. $container["innerHTML"]
  2203. )
  2204. ];
  2205. }
  2206. $out["answer"][] = [
  2207. "title" => "Notice",
  2208. "description" => $description,
  2209. "url" => null,
  2210. "thumb" => null,
  2211. "table" => [],
  2212. "sublink" => []
  2213. ];
  2214. }
  2215. //
  2216. // remove duplicate web links cause instant answers
  2217. // sometimes contains duplicates
  2218. //
  2219. $c = count($out["web"]);
  2220. $links = [];
  2221. for($i=0; $i<$c; $i++){
  2222. foreach($links as $link){
  2223. if($out["web"][$i]["url"] == $link){
  2224. unset($out["web"][$i]);
  2225. continue 2;
  2226. }
  2227. }
  2228. $links[] = $out["web"][$i]["url"];
  2229. }
  2230. $out["web"] = array_values($out["web"]);
  2231. return $out;
  2232. }
  2233. public function image($get){
  2234. // generate parameters
  2235. if($get["npt"]){
  2236. [$params, $proxy] =
  2237. $this->backend->get(
  2238. $get["npt"],
  2239. "images"
  2240. );
  2241. $params = json_decode($params, true);
  2242. }else{
  2243. $search = $get["s"];
  2244. if(strlen($search) === 0){
  2245. throw new Exception("Search term is empty!");
  2246. }
  2247. $proxy = $this->backend->get_ip();
  2248. $country = $get["country"];
  2249. $nsfw = $get["nsfw"];
  2250. $lang = $get["lang"];
  2251. $time = $get["time"];
  2252. $size = $get["size"];
  2253. $ratio = $get["ratio"];
  2254. $color = $get["color"];
  2255. $type = $get["type"];
  2256. $format = $get["format"];
  2257. $rights = $get["rights"];
  2258. $params = [
  2259. "q" => $search,
  2260. "tbm" => "isch"
  2261. ];
  2262. // country
  2263. if($country != "any"){
  2264. $params["gl"] = $country;
  2265. }
  2266. // nsfw
  2267. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  2268. // language
  2269. if($lang != "any"){
  2270. $params["lr"] = "lang_" . $lang;
  2271. }
  2272. $tbs = [];
  2273. // time
  2274. if($time != "any"){
  2275. $tbs[] = "qrd:" . $time;
  2276. }
  2277. // size
  2278. if($size != "any"){
  2279. if(
  2280. in_array(
  2281. $size,
  2282. ["l", "s", "i"]
  2283. )
  2284. ){
  2285. $tbs[] = "isz:" . $size;
  2286. }else{
  2287. $tbs[] = "tbz:lt";
  2288. $tbs[] = "islt:" . $size;
  2289. }
  2290. }
  2291. // ratio
  2292. if($ratio != "any"){
  2293. $tbs[] = "iar:" . $ratio;
  2294. }
  2295. // color
  2296. if($color != "any"){
  2297. if(
  2298. in_array(
  2299. $color,
  2300. ["color", "gray", "trans"]
  2301. )
  2302. ){
  2303. $tbs[] = "ic:" . $color;
  2304. }else{
  2305. $tbs[] = "ic:specific";
  2306. $tbs[] = "isc:" . $color;
  2307. }
  2308. }
  2309. // type
  2310. if($type != "any"){
  2311. $tbs[] = "itp:" . $type;
  2312. }
  2313. // format
  2314. if($format != "any"){
  2315. $tbs[] = "ift:" . $format;
  2316. }
  2317. // rights
  2318. if($rights != "any"){
  2319. $tbs[] = "il:" . $rights;
  2320. }
  2321. // append tbs
  2322. if(count($tbs) !== 0){
  2323. $params["tbs"] =
  2324. implode(",", $tbs);
  2325. }
  2326. }
  2327. /*
  2328. $handle = fopen("scraper/google-img.html", "r");
  2329. $html = fread($handle, filesize("scraper/google-img.html"));
  2330. fclose($handle);*/
  2331. // scrape images
  2332. try{
  2333. $html =
  2334. $this->get(
  2335. $proxy,
  2336. "https://www.google.com/search",
  2337. $params
  2338. );
  2339. }catch(Exception $error){
  2340. throw new Exception("Failed to get search page");
  2341. }
  2342. if($this->detect_sorry($html)){
  2343. throw new Exception("Google blocked this 4get instance. Please set up a proxy!");
  2344. }
  2345. $out = [
  2346. "status" => "ok",
  2347. "npt" => null,
  2348. "image" => []
  2349. ];
  2350. $images =
  2351. $this->fuckhtml
  2352. ->getElementsByClassName(
  2353. "islrtb isv-r",
  2354. "div"
  2355. );
  2356. foreach($images as $image){
  2357. $this->fuckhtml->load($image);
  2358. $img =
  2359. $this->fuckhtml
  2360. ->getElementsByTagName("img")[0];
  2361. $og_width = (int)$image["attributes"]["data-ow"];
  2362. $og_height = (int)$image["attributes"]["data-oh"];
  2363. $thumb_width = (int)$image["attributes"]["data-tw"];
  2364. $ratio = $og_width / $og_height;
  2365. if(isset($img["attributes"]["data-src"])){
  2366. $src = &$img["attributes"]["data-src"];
  2367. }else{
  2368. $src = &$img["attributes"]["src"];
  2369. }
  2370. $thumb_height = floor($thumb_width / $ratio);
  2371. $out["image"][] = [
  2372. "title" =>
  2373. $this->titledots(
  2374. $this->fuckhtml
  2375. ->getTextContent(
  2376. $image["attributes"]["data-pt"]
  2377. )
  2378. ),
  2379. "source" => [
  2380. [
  2381. "url" =>
  2382. $this->fuckhtml
  2383. ->getTextContent(
  2384. $image["attributes"]["data-ou"]
  2385. ),
  2386. "width" => $og_width,
  2387. "height" => $og_height
  2388. ],
  2389. [
  2390. "url" =>
  2391. $this->fuckhtml
  2392. ->getTextContent(
  2393. $src
  2394. ),
  2395. "width" => $thumb_width,
  2396. "height" => $thumb_height
  2397. ]
  2398. ],
  2399. "url" =>
  2400. $this->fuckhtml
  2401. ->getTextContent(
  2402. $image["attributes"]["data-ru"]
  2403. )
  2404. ];
  2405. }
  2406. // get next page
  2407. // https://www.google.com/search
  2408. // ?q=higurashi
  2409. // &tbm=isch
  2410. // &async=_id%3Aislrg_c%2C_fmt%3Ahtml
  2411. // &asearch=ichunklite
  2412. // &ved=0ahUKEwidjYXJqJSAAxWrElkFHZ07CDwQtDIIQygA
  2413. if(count($out["image"]) !== 100){
  2414. // no more results
  2415. return $out;
  2416. }
  2417. if($get["npt"]){
  2418. // update nextpage information
  2419. $params["start"] = (int)$params["start"] + count($out["image"]);
  2420. $params["ijn"] = (int)$params["ijn"] + 1;
  2421. $out["npt"] =
  2422. $this->backend->store(
  2423. json_encode($params),
  2424. "images",
  2425. $proxy
  2426. );
  2427. }else{
  2428. // scrape nextpage information
  2429. $this->fuckhtml->load($html);
  2430. $ved =
  2431. $this->fuckhtml
  2432. ->getElementById("islrg", "div");
  2433. if($ved){
  2434. $ved =
  2435. $this->fuckhtml
  2436. ->getTextContent(
  2437. $ved["attributes"]["data-ved"]
  2438. );
  2439. // &vet=1{$ved}..i (10ahUKEwidjYXJqJSAAxWrElkFHZ07CDwQtDIIQygA..i)
  2440. /*
  2441. These 2 are handled by us
  2442. start = start + number of results
  2443. ijn = current page number
  2444. */
  2445. // &start=100
  2446. // &ijn=1
  2447. // &imgvl=CAEY7gQgBSj3Aji8VTjXVUC4AUC3AUgAYNdV
  2448. preg_match(
  2449. '/var e=\'([A-z0-9]+)\';/',
  2450. $html,
  2451. $imgvl
  2452. );
  2453. if(isset($imgvl[1])){
  2454. $imgvl = $imgvl[1];
  2455. $params["async"] = "_id:islrg_c,_fmt:html";
  2456. $params["asearch"] = "ichunklite";
  2457. $params["ved"] = $ved;
  2458. $params["vet"] = "1" . $ved . "..i";
  2459. $params["start"] = 100;
  2460. $params["ijn"] = 1;
  2461. $params["imgvl"] = $imgvl;
  2462. $out["npt"] =
  2463. $this->backend->store(
  2464. json_encode($params),
  2465. "images",
  2466. $proxy
  2467. );
  2468. }
  2469. }
  2470. }
  2471. return $out;
  2472. }
  2473. private function hms2int($time){
  2474. $parts = explode(":", $time, 3);
  2475. $time = 0;
  2476. if(count($parts) === 3){
  2477. // hours
  2478. $time = $time + ((int)$parts[0] * 3600);
  2479. array_shift($parts);
  2480. }
  2481. if(count($parts) === 2){
  2482. // minutes
  2483. $time = $time + ((int)$parts[0] * 60);
  2484. array_shift($parts);
  2485. }
  2486. // seconds
  2487. $time = $time + (int)$parts[0];
  2488. return $time;
  2489. }
  2490. private function parsejavascript($html){
  2491. $this->fuckhtml->load($html);
  2492. $styles =
  2493. $this->fuckhtml
  2494. ->getElementsByTagName("style");
  2495. $this->computedstyle = [];
  2496. $this->ask = [];
  2497. foreach($styles as $style){
  2498. $this->computedstyle =
  2499. array_merge(
  2500. $this->computedstyle,
  2501. $this->parsestyles($style["innerHTML"])
  2502. );
  2503. }
  2504. // get images in javascript var
  2505. preg_match(
  2506. '/google\.ldi=({[^}]+})/',
  2507. $html,
  2508. $this->js_image
  2509. );
  2510. if(count($this->js_image) !== 0){
  2511. $this->js_image = json_decode($this->js_image[1], true);
  2512. }else{
  2513. $this->js_image = [];
  2514. }
  2515. // additional js_images present in <script> tags
  2516. // ugh i fucking hate you
  2517. $scripts =
  2518. $this->fuckhtml
  2519. ->getElementsByTagName("script");
  2520. foreach($scripts as $script){
  2521. if(!isset($script["innerHTML"])){
  2522. continue;
  2523. }
  2524. preg_match_all(
  2525. '/var s=\'(data:image[^\']+)\';var i=\[(\'[^\;]*\')];/',
  2526. $script["innerHTML"],
  2527. $image_grep
  2528. );
  2529. if(count($image_grep[0]) !== 0){
  2530. $items = explode(",", $image_grep[2][0]);
  2531. $value =
  2532. $this->fuckhtml
  2533. ->getTextContent(
  2534. $image_grep[1][0]
  2535. );
  2536. foreach($items as $item){
  2537. $this->js_image[trim($item, "' ")] = $value;
  2538. }
  2539. }
  2540. // even more javascript crap
  2541. // "People also ask" node is loaded trough javascript
  2542. preg_match_all(
  2543. '/window\.jsl\.dh\(\'([^\']+)\',\'(.+)\'\);/',
  2544. $script["innerHTML"],
  2545. $ask_grep
  2546. );
  2547. for($i=0; $i<count($ask_grep[0]); $i++){
  2548. $this->ask[trim($ask_grep[1][$i])] =
  2549. $this->fuckhtml->parseJsString(
  2550. $ask_grep[2][$i]
  2551. );
  2552. }
  2553. }
  2554. }
  2555. private function findstyles($rules, $is){
  2556. $c = count($rules);
  2557. foreach($this->computedstyle as $classname => $styles){
  2558. if($classname[0] != $is){
  2559. // not a class, skip
  2560. continue;
  2561. }
  2562. $i = 0;
  2563. foreach($styles as $stylename => $stylevalue){
  2564. if(
  2565. isset($rules[$stylename]) &&
  2566. $rules[$stylename] == $stylevalue
  2567. ){
  2568. $i++;
  2569. }else{
  2570. continue 2;
  2571. }
  2572. }
  2573. if($c === $i){
  2574. return ltrim($classname, $is);
  2575. }
  2576. }
  2577. // fail, did not find classname.
  2578. return false;
  2579. }
  2580. private function parsestyles($style){
  2581. // get style tags
  2582. preg_match_all(
  2583. '/([^{]+){([^}]*)}/',
  2584. $style,
  2585. $tags_regex
  2586. );
  2587. $tags = [];
  2588. for($i=0; $i<count($tags_regex[0]); $i++){
  2589. $tagnames = explode(",", trim($tags_regex[1][$i]));
  2590. foreach($tagnames as $tagname){
  2591. $tagname = trim($tagname);
  2592. if(!isset($tags[$tagname])){
  2593. $tags[$tagname] = [];
  2594. }
  2595. $values = explode(";", $tags_regex[2][$i]);
  2596. foreach($values as $value){
  2597. $value = explode(":", $value, 2);
  2598. if(count($value) !== 2){
  2599. continue;
  2600. }
  2601. $tags[$tagname][trim($value[0])] =
  2602. trim($value[1]);
  2603. }
  2604. }
  2605. }
  2606. return $tags;
  2607. }
  2608. private function getimage($id){
  2609. if(isset($this->js_image[$id])){
  2610. $return = $this->fuckhtml->parseJsString($this->js_image[$id]);
  2611. if(
  2612. $return != "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAABAUlEQVR4AWMYesChoYElLjkzPj4lY3d8csZjIL4MxPNjUzPcSTYsISFLAqj5NBD/h+LPQPwbiT87NCuLh2gDgRr2QzXuT0jNMoBYksARn5zuHJ+UcR0kB6RXE2VYXHJGOlTDZmzyIJcB5e+D1CSkZDgQNBAaZv+jU1JkcKpJygiGeZ0I76a/Byq8jU9NZFqaCNTA48SE33/iDcw8TIyBt0GKQTFN0Msp6f2EIyUpo57YSIlLSrMhIg0WCIBcCfXSdlzJBsheTHQ6jEnOUgEFOLaEDbMIlhZBOYrorAdJk+nroVnvPsSgdGdoOF7HZyhZ2XPoGQoqjbCpIbt0AiejIQMArVLI7k/DXFkAAAAASUVORK5CYII=" &&
  2613. $return != "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAA6ElEQVR4Ae2UvQ7BYBSGW4uFxlVICLsYDA0D14NEunRxHSJ+BkYG9ibiHtgkuvpZWOod3uFESqpOF/ElT/q958h5OtQx/iexY/evY9ACJjBewUtkXHEPyBYUNQQuCETuggNrF2DHF3A4kfUMmLB+BoUYAg4nIX0TTNnbfCjg8HDBGuR4z4Ij+813giAC8rcrkXusjdQEpMpcYt5rCzrMaea7tqD9JLhpCyrMZeadpmApssPaUOszXQALGHz67De0/2gpMGPP014VFpizfgJ5zWXnAF8MryW1rj3x5l8LJANQF1lZQH5f8AAWpNcUs6HAEAAAAABJRU5ErkJggg=="
  2614. ){
  2615. if(
  2616. preg_match(
  2617. '/^\/\//',
  2618. $return
  2619. )
  2620. ){
  2621. return 'https:' . $return;
  2622. }
  2623. return $return;
  2624. }
  2625. return null;
  2626. }
  2627. }
  2628. private function parsecarousels(&$item_to_remove = false){
  2629. $carousels =
  2630. $this->fuckhtml
  2631. ->getElementsByClassName(
  2632. $this->findstyles(
  2633. [
  2634. "padding" => "16px",
  2635. "position" => "relative"
  2636. ],
  2637. self::is_class
  2638. )
  2639. );
  2640. $return = [];
  2641. for($i=0; $i<count($carousels); $i++){
  2642. if(!isset($carousels[$i]["outerHTML"])){
  2643. continue;
  2644. }
  2645. $this->fuckhtml->load($carousels[$i]);
  2646. if($item_to_remove !== false){
  2647. $item_to_remove =
  2648. str_replace(
  2649. $carousels[$i]["outerHTML"],
  2650. "",
  2651. $item_to_remove
  2652. );
  2653. }
  2654. $pcitems =
  2655. $this->fuckhtml
  2656. ->getElementsByClassName(
  2657. "pcitem",
  2658. "div"
  2659. );
  2660. foreach($pcitems as $pcitem){
  2661. $this->fuckhtml->load($pcitem);
  2662. $out = [
  2663. "url" => null,
  2664. "ref" => null,
  2665. "image" => null,
  2666. "thumb_width" => null,
  2667. "thumb_height" => null,
  2668. "image_width" => null,
  2669. "image_height" => null,
  2670. "title" => null,
  2671. "description" => null,
  2672. "subtext" => null,
  2673. "date" => null
  2674. ];
  2675. $url =
  2676. $this->unshiturl(
  2677. $this->fuckhtml
  2678. ->getElementsByTagName("a")
  2679. [0]
  2680. ["attributes"]
  2681. ["href"],
  2682. true
  2683. );
  2684. // set ref
  2685. $out["ref"] = $url["ref"];
  2686. // set url
  2687. $out["url"] = $url["url"];
  2688. // set sizes
  2689. $out["thumb_width"] = $url["thumb_width"];
  2690. $out["thumb_height"] = $url["thumb_height"];
  2691. $out["image_width"] = $url["image_width"];
  2692. $out["image_height"] = $url["image_height"];
  2693. // get image
  2694. $out["image"] =
  2695. $this->fuckhtml
  2696. ->getElementsByTagName(
  2697. "img"
  2698. );
  2699. if(count($out["image"]) !== 0){
  2700. // get title from image
  2701. if(isset($out["image"][0]["attributes"]["alt"])){
  2702. $out["title"] =
  2703. $this->titledots(
  2704. $this->fuckhtml
  2705. ->getTextContent(
  2706. $out["image"][0]["attributes"]["alt"]
  2707. )
  2708. );
  2709. }
  2710. // get image url
  2711. if(isset($out["image"][0]["attributes"]["id"])){
  2712. $out["image"] = $this->getimage($out["image"][0]["attributes"]["id"]);
  2713. }
  2714. elseif(isset($out["image"][0]["attributes"]["data-ll"])){
  2715. $out["image"] =
  2716. $this->fuckhtml
  2717. ->getTextContent(
  2718. $out["image"][0]["attributes"]["data-ll"]
  2719. );
  2720. }else{
  2721. // failed to get image information
  2722. $out["image"] = null;
  2723. }
  2724. if($out["image"] == 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYAgMAAACdGdVrAAAADFBMVEVMaXFChfRChfRChfT0tCPZAAAAA3RSTlMAgFJEkGxNAAAAL0lEQVR4AWPADxgdwBT3BTDF9AUiuhdC6WNK/v///y+UggrClSA07EWVglmEFwAA5eYSExeCwigAAAAASUVORK5CYII='){
  2725. // found arrow image base64, skip entry
  2726. continue;
  2727. }
  2728. }else{
  2729. // Could not find any image in node
  2730. $out["image"] = null;
  2731. }
  2732. // get title from spans
  2733. $title =
  2734. $this->fuckhtml
  2735. ->getElementsByClassName(
  2736. $this->findstyles(
  2737. [
  2738. "color" => "#1967d2"
  2739. ],
  2740. self::is_class
  2741. ),
  2742. "span"
  2743. );
  2744. if(count($title) !== 0){
  2745. $out["title"] =
  2746. $this->fuckhtml
  2747. ->getTextContent(
  2748. $title[0]
  2749. );
  2750. }
  2751. // get textnodes
  2752. $textnodes =
  2753. $this->fuckhtml
  2754. ->getElementsByClassName(
  2755. $this->findstyles(
  2756. [
  2757. "white-space" => "pre-line",
  2758. "word-wrap" => "break-word"
  2759. ],
  2760. self::is_class
  2761. )
  2762. );
  2763. $subtext = null;
  2764. if(count($textnodes) !== 0){
  2765. // get date
  2766. $date =
  2767. $this->fuckhtml
  2768. ->getTextContent(
  2769. $textnodes[count($textnodes) - 1],
  2770. true
  2771. );
  2772. if(str_replace("\n", " ", $date) == $title){
  2773. $date = null;
  2774. }else{
  2775. if(strpos($date, "\n") !== false){
  2776. $date = explode("\n", $date);
  2777. $date = $date[count($date) - 1];
  2778. }
  2779. elseif(strpos($date, "•") !== false){
  2780. $date = explode("•", $date);
  2781. $date = ltrim($date[count($date) - 1]);
  2782. }else{
  2783. $date = null;
  2784. }
  2785. }
  2786. if($date !== null){
  2787. $date = strtotime($date);
  2788. }
  2789. // get description
  2790. $description =
  2791. $this->fuckhtml
  2792. ->getTextContent(
  2793. $textnodes[0]
  2794. );
  2795. if($out["title"] === null){
  2796. if($date === null){
  2797. $out["title"] = $description;
  2798. $description = null;
  2799. }else{
  2800. $out["title"] = parse_url($out["url"], PHP_URL_HOST);
  2801. }
  2802. }
  2803. if(isset($textnodes[1])){
  2804. $out["subtext"] =
  2805. $this->fuckhtml
  2806. ->getTextContent(
  2807. $textnodes[1]
  2808. );
  2809. }
  2810. }else{
  2811. $date = null;
  2812. $description = null;
  2813. }
  2814. $out["date"] = $date;
  2815. $out["description"] = $this->titledots($description);
  2816. if($out["url"] === null){
  2817. $out["url"] = $out["title"];
  2818. }
  2819. if($out["title"] == $out["description"]){
  2820. $out["description"] = null;
  2821. }
  2822. $return[$i][] = $out;
  2823. }
  2824. }
  2825. return $return;
  2826. }
  2827. private function unshiturl($url, $return_size = false){
  2828. // get parameters from URL
  2829. $url =
  2830. $this->fuckhtml
  2831. ->getTextContent($url);
  2832. $newurl = parse_url($url, PHP_URL_QUERY);
  2833. if($newurl == ""){
  2834. // probably telephone number
  2835. return $url;
  2836. }
  2837. $url = $newurl;
  2838. unset($newurl);
  2839. parse_str($url, $query);
  2840. if(isset($query["imgurl"])){
  2841. $url = $query["imgurl"];
  2842. }
  2843. elseif(isset($query["q"])){
  2844. $url = $query["q"];
  2845. }
  2846. // rewrite URLs to remove extra tracking parameters
  2847. $domain = parse_url($url, PHP_URL_HOST);
  2848. if(
  2849. preg_match(
  2850. '/wikipedia.org$/',
  2851. $domain
  2852. )
  2853. ){
  2854. // rewrite wikipedia mobile URLs to desktop
  2855. $url =
  2856. $this->replacedomain(
  2857. $url,
  2858. preg_replace(
  2859. '/([a-z0-9]+)(\.m\.)/',
  2860. '$1.',
  2861. $domain
  2862. )
  2863. );
  2864. }
  2865. elseif(
  2866. preg_match(
  2867. '/imdb\.com$|youtube\.[^.]+$/',
  2868. $domain
  2869. )
  2870. ){
  2871. // rewrite imdb and youtube mobile URLs too
  2872. $url =
  2873. $this->replacedomain(
  2874. $url,
  2875. preg_replace(
  2876. '/^m\./',
  2877. "",
  2878. $domain
  2879. )
  2880. );
  2881. }
  2882. elseif(
  2883. preg_match(
  2884. '/play\.google\.[^.]+$/',
  2885. $domain
  2886. )
  2887. ){
  2888. // remove referrers from play.google.com
  2889. $oldquery = parse_url($url, PHP_URL_QUERY);
  2890. if($oldquery !== null){
  2891. parse_str($oldquery, $query);
  2892. if(isset($query["referrer"])){ unset($query["referrer"]); }
  2893. if(isset($query["hl"])){ unset($query["hl"]); }
  2894. if(isset($query["gl"])){ unset($query["gl"]); }
  2895. $query = http_build_query($query);
  2896. $url =
  2897. str_replace(
  2898. $oldquery,
  2899. $query,
  2900. $url
  2901. );
  2902. }
  2903. }
  2904. elseif(
  2905. preg_match(
  2906. '/twitter\.com$/',
  2907. $domain
  2908. )
  2909. ){
  2910. // remove more referrers from twitter.com
  2911. $oldquery = parse_url($url, PHP_URL_QUERY);
  2912. if($oldquery !== null){
  2913. parse_str($oldquery, $query);
  2914. if(isset($query["ref_src"])){ unset($query["ref_src"]); }
  2915. $query = http_build_query($query);
  2916. if($query != ""){
  2917. $query .= "?" . $query;
  2918. }
  2919. $url =
  2920. str_replace(
  2921. '?' . $oldquery,
  2922. $query,
  2923. $url
  2924. );
  2925. }
  2926. }
  2927. elseif(
  2928. preg_match(
  2929. '/maps\.google\.[^.]+/',
  2930. $domain
  2931. )
  2932. ){
  2933. if(stripos($url, "maps?") !== false){
  2934. //https://maps.google.com/maps?daddr=Johnny,+603+Rue+St+Georges,+Saint-J%C3%A9r%C3%B4me,+Quebec+J7Z+5B7
  2935. $query = parse_url($url, PHP_URL_QUERY);
  2936. if($query !== null){
  2937. parse_str($query, $query);
  2938. if(isset($query["daddr"])){
  2939. $url =
  2940. "https://maps.google.com/maps?daddr=" .
  2941. urlencode($query["daddr"]);
  2942. }
  2943. }
  2944. }
  2945. }
  2946. if($return_size){
  2947. return [
  2948. "url" => $url,
  2949. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2950. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2951. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2952. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2953. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2954. ];
  2955. }
  2956. return $url;
  2957. }
  2958. private function replacedomain($url, $domain){
  2959. return
  2960. preg_replace(
  2961. '/(https?:\/\/)([^\/]+)/',
  2962. '$1' . $domain,
  2963. $url
  2964. );
  2965. }
  2966. private function titledots($title){
  2967. return rtrim($title, ". \t\n\r\0\x0B");
  2968. }
  2969. private function detect_sorry($html){
  2970. $this->fuckhtml->load($html);
  2971. $detect_sorry =
  2972. $this->fuckhtml
  2973. ->getElementsByTagName("title");
  2974. if(
  2975. isset($detect_sorry[0]) &&
  2976. $detect_sorry[0]["innerHTML"] == "302 Moved"
  2977. ){
  2978. return true;
  2979. }
  2980. return false;
  2981. }
  2982. }