google.php 71 KB

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