google.php 71 KB

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