1
0

google.php 72 KB

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