google.php 71 KB

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