google.php 71 KB

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