google.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448
  1. <?php
  2. // @TODO check for consent.google.com page, if need be
  3. class google{
  4. public function __construct(){
  5. include "lib/fuckhtml.php";
  6. $this->fuckhtml = new fuckhtml();
  7. include "lib/backend.php";
  8. $this->backend = new backend("google");
  9. }
  10. public function getfilters($page){
  11. $base = [
  12. "country" => [ // gl=<country> (image: cr=countryAF)
  13. "display" => "Country",
  14. "option" => [
  15. "any" => "Instance's country",
  16. "af" => "Afghanistan",
  17. "al" => "Albania",
  18. "dz" => "Algeria",
  19. "as" => "American Samoa",
  20. "ad" => "Andorra",
  21. "ao" => "Angola",
  22. "ai" => "Anguilla",
  23. "aq" => "Antarctica",
  24. "ag" => "Antigua and Barbuda",
  25. "ar" => "Argentina",
  26. "am" => "Armenia",
  27. "aw" => "Aruba",
  28. "au" => "Australia",
  29. "at" => "Austria",
  30. "az" => "Azerbaijan",
  31. "bs" => "Bahamas",
  32. "bh" => "Bahrain",
  33. "bd" => "Bangladesh",
  34. "bb" => "Barbados",
  35. "by" => "Belarus",
  36. "be" => "Belgium",
  37. "bz" => "Belize",
  38. "bj" => "Benin",
  39. "bm" => "Bermuda",
  40. "bt" => "Bhutan",
  41. "bo" => "Bolivia",
  42. "ba" => "Bosnia and Herzegovina",
  43. "bw" => "Botswana",
  44. "bv" => "Bouvet Island",
  45. "br" => "Brazil",
  46. "io" => "British Indian Ocean Territory",
  47. "bn" => "Brunei Darussalam",
  48. "bg" => "Bulgaria",
  49. "bf" => "Burkina Faso",
  50. "bi" => "Burundi",
  51. "kh" => "Cambodia",
  52. "cm" => "Cameroon",
  53. "ca" => "Canada",
  54. "cv" => "Cape Verde",
  55. "ky" => "Cayman Islands",
  56. "cf" => "Central African Republic",
  57. "td" => "Chad",
  58. "cl" => "Chile",
  59. "cn" => "China",
  60. "cx" => "Christmas Island",
  61. "cc" => "Cocos (Keeling) Islands",
  62. "co" => "Colombia",
  63. "km" => "Comoros",
  64. "cg" => "Congo",
  65. "cd" => "Congo, the Democratic Republic",
  66. "ck" => "Cook Islands",
  67. "cr" => "Costa Rica",
  68. "ci" => "Cote D'ivoire",
  69. "hr" => "Croatia",
  70. "cu" => "Cuba",
  71. "cy" => "Cyprus",
  72. "cz" => "Czech Republic",
  73. "dk" => "Denmark",
  74. "dj" => "Djibouti",
  75. "dm" => "Dominica",
  76. "do" => "Dominican Republic",
  77. "ec" => "Ecuador",
  78. "eg" => "Egypt",
  79. "sv" => "El Salvador",
  80. "gq" => "Equatorial Guinea",
  81. "er" => "Eritrea",
  82. "ee" => "Estonia",
  83. "et" => "Ethiopia",
  84. "fk" => "Falkland Islands (Malvinas)",
  85. "fo" => "Faroe Islands",
  86. "fj" => "Fiji",
  87. "fi" => "Finland",
  88. "fr" => "France",
  89. "gf" => "French Guiana",
  90. "pf" => "French Polynesia",
  91. "tf" => "French Southern Territories",
  92. "ga" => "Gabon",
  93. "gm" => "Gambia",
  94. "ge" => "Georgia",
  95. "de" => "Germany",
  96. "gh" => "Ghana",
  97. "gi" => "Gibraltar",
  98. "gr" => "Greece",
  99. "gl" => "Greenland",
  100. "gd" => "Grenada",
  101. "gp" => "Guadeloupe",
  102. "gu" => "Guam",
  103. "gt" => "Guatemala",
  104. "gn" => "Guinea",
  105. "gw" => "Guinea-Bissau",
  106. "gy" => "Guyana",
  107. "ht" => "Haiti",
  108. "hm" => "Heard Island and Mcdonald Islands",
  109. "va" => "Holy See (Vatican City State)",
  110. "hn" => "Honduras",
  111. "hk" => "Hong Kong",
  112. "hu" => "Hungary",
  113. "is" => "Iceland",
  114. "in" => "India",
  115. "id" => "Indonesia",
  116. "ir" => "Iran, Islamic Republic",
  117. "iq" => "Iraq",
  118. "ie" => "Ireland",
  119. "il" => "Israel",
  120. "it" => "Italy",
  121. "jm" => "Jamaica",
  122. "jp" => "Japan",
  123. "jo" => "Jordan",
  124. "kz" => "Kazakhstan",
  125. "ke" => "Kenya",
  126. "ki" => "Kiribati",
  127. "kp" => "Korea, Democratic People's Republic",
  128. "kr" => "Korea, Republic",
  129. "kw" => "Kuwait",
  130. "kg" => "Kyrgyzstan",
  131. "la" => "Lao People's Democratic Republic",
  132. "lv" => "Latvia",
  133. "lb" => "Lebanon",
  134. "ls" => "Lesotho",
  135. "lr" => "Liberia",
  136. "ly" => "Libyan Arab Jamahiriya",
  137. "li" => "Liechtenstein",
  138. "lt" => "Lithuania",
  139. "lu" => "Luxembourg",
  140. "mo" => "Macao",
  141. "mk" => "Macedonia, the Former Yugosalv Republic",
  142. "mg" => "Madagascar",
  143. "mw" => "Malawi",
  144. "my" => "Malaysia",
  145. "mv" => "Maldives",
  146. "ml" => "Mali",
  147. "mt" => "Malta",
  148. "mh" => "Marshall Islands",
  149. "mq" => "Martinique",
  150. "mr" => "Mauritania",
  151. "mu" => "Mauritius",
  152. "yt" => "Mayotte",
  153. "mx" => "Mexico",
  154. "fm" => "Micronesia, Federated States",
  155. "md" => "Moldova, Republic",
  156. "mc" => "Monaco",
  157. "mn" => "Mongolia",
  158. "ms" => "Montserrat",
  159. "ma" => "Morocco",
  160. "mz" => "Mozambique",
  161. "mm" => "Myanmar",
  162. "na" => "Namibia",
  163. "nr" => "Nauru",
  164. "np" => "Nepal",
  165. "nl" => "Netherlands",
  166. "an" => "Netherlands Antilles",
  167. "nc" => "New Caledonia",
  168. "nz" => "New Zealand",
  169. "ni" => "Nicaragua",
  170. "ne" => "Niger",
  171. "ng" => "Nigeria",
  172. "nu" => "Niue",
  173. "nf" => "Norfolk Island",
  174. "mp" => "Northern Mariana Islands",
  175. "no" => "Norway",
  176. "om" => "Oman",
  177. "pk" => "Pakistan",
  178. "pw" => "Palau",
  179. "ps" => "Palestinian Territory, Occupied",
  180. "pa" => "Panama",
  181. "pg" => "Papua New Guinea",
  182. "py" => "Paraguay",
  183. "pe" => "Peru",
  184. "ph" => "Philippines",
  185. "pn" => "Pitcairn",
  186. "pl" => "Poland",
  187. "pt" => "Portugal",
  188. "pr" => "Puerto Rico",
  189. "qa" => "Qatar",
  190. "re" => "Reunion",
  191. "ro" => "Romania",
  192. "ru" => "Russian Federation",
  193. "rw" => "Rwanda",
  194. "sh" => "Saint Helena",
  195. "kn" => "Saint Kitts and Nevis",
  196. "lc" => "Saint Lucia",
  197. "pm" => "Saint Pierre and Miquelon",
  198. "vc" => "Saint Vincent and the Grenadines",
  199. "ws" => "Samoa",
  200. "sm" => "San Marino",
  201. "st" => "Sao Tome and Principe",
  202. "sa" => "Saudi Arabia",
  203. "sn" => "Senegal",
  204. "cs" => "Serbia and Montenegro",
  205. "sc" => "Seychelles",
  206. "sl" => "Sierra Leone",
  207. "sg" => "Singapore",
  208. "sk" => "Slovakia",
  209. "si" => "Slovenia",
  210. "sb" => "Solomon Islands",
  211. "so" => "Somalia",
  212. "za" => "South Africa",
  213. "gs" => "South Georgia and the South Sandwich Islands",
  214. "es" => "Spain",
  215. "lk" => "Sri Lanka",
  216. "sd" => "Sudan",
  217. "sr" => "Suriname",
  218. "sj" => "Svalbard and Jan Mayen",
  219. "sz" => "Swaziland",
  220. "se" => "Sweden",
  221. "ch" => "Switzerland",
  222. "sy" => "Syrian Arab Republic",
  223. "tw" => "Taiwan, Province of China",
  224. "tj" => "Tajikistan",
  225. "tz" => "Tanzania, United Republic",
  226. "th" => "Thailand",
  227. "tl" => "Timor-Leste",
  228. "tg" => "Togo",
  229. "tk" => "Tokelau",
  230. "to" => "Tonga",
  231. "tt" => "Trinidad and Tobago",
  232. "tn" => "Tunisia",
  233. "tr" => "Turkey",
  234. "tm" => "Turkmenistan",
  235. "tc" => "Turks and Caicos Islands",
  236. "tv" => "Tuvalu",
  237. "ug" => "Uganda",
  238. "ua" => "Ukraine",
  239. "ae" => "United Arab Emirates",
  240. "uk" => "United Kingdom",
  241. "us" => "United States",
  242. "um" => "United States Minor Outlying Islands",
  243. "uy" => "Uruguay",
  244. "uz" => "Uzbekistan",
  245. "vu" => "Vanuatu",
  246. "ve" => "Venezuela",
  247. "vn" => "Viet Nam",
  248. "vg" => "Virgin Islands, British",
  249. "vi" => "Virgin Islands, U.S.",
  250. "wf" => "Wallis and Futuna",
  251. "eh" => "Western Sahara",
  252. "ye" => "Yemen",
  253. "zm" => "Zambia",
  254. "zw" => "Zimbabwe"
  255. ]
  256. ],
  257. "nsfw" => [
  258. "display" => "NSFW",
  259. "option" => [
  260. "yes" => "Yes", // safe=active
  261. "no" => "No" // safe=off
  262. ]
  263. ]
  264. ];
  265. switch($page){
  266. case "web":
  267. return array_merge(
  268. $base,
  269. [
  270. "lang" => [ // lr=<lang> (prefix lang with "lang_")
  271. "display" => "Language",
  272. "option" => [
  273. "any" => "Any language",
  274. "ar" => "Arabic",
  275. "bg" => "Bulgarian",
  276. "ca" => "Catalan",
  277. "cs" => "Czech",
  278. "da" => "Danish",
  279. "de" => "German",
  280. "el" => "Greek",
  281. "en" => "English",
  282. "es" => "Spanish",
  283. "et" => "Estonian",
  284. "fi" => "Finnish",
  285. "fr" => "French",
  286. "hr" => "Croatian",
  287. "hu" => "Hungarian",
  288. "id" => "Indonesian",
  289. "is" => "Icelandic",
  290. "it" => "Italian",
  291. "iw" => "Hebrew",
  292. "ja" => "Japanese",
  293. "ko" => "Korean",
  294. "lt" => "Lithuanian",
  295. "lv" => "Latvian",
  296. "nl" => "Dutch",
  297. "no" => "Norwegian",
  298. "pl" => "Polish",
  299. "pt" => "Portuguese",
  300. "ro" => "Romanian",
  301. "ru" => "Russian",
  302. "sk" => "Slovak",
  303. "sl" => "Slovenian",
  304. "sr" => "Serbian",
  305. "sv" => "Swedish",
  306. "tr" => "Turkish",
  307. "zh-CN" => "Chinese (Simplified)",
  308. "zh-TW" => "Chinese (Traditional)"
  309. ]
  310. ],
  311. "newer" => [ // tbs
  312. "display" => "Newer than",
  313. "option" => "_DATE"
  314. ],
  315. "older" => [
  316. "display" => "Older than",
  317. "option" => "_DATE"
  318. ],
  319. "spellcheck" => [
  320. "display" => "Spellcheck",
  321. "option" => [
  322. "yes" => "Yes",
  323. "no" => "No"
  324. ]
  325. ]
  326. ]
  327. );
  328. break;
  329. case "images":
  330. return array_merge(
  331. $base,
  332. [
  333. "time" => [ // tbs=qdr:<time>
  334. "display" => "Time posted",
  335. "option" => [
  336. "any" => "Any time",
  337. "d" => "Past 24 hours",
  338. "w" => "Past week",
  339. "m" => "Past month",
  340. "y" => "Past year"
  341. ]
  342. ],
  343. "size" => [ // imgsz
  344. "display" => "Size",
  345. "option" => [
  346. "any" => "Any size",
  347. "l" => "Large",
  348. "m" => "Medium",
  349. "i" => "Icon",
  350. "qsvga" => "Larger than 400x300",
  351. "vga" => "Larger than 640x480",
  352. "svga" => "Larger than 800x600",
  353. "xga" => "Larger than 1024x768",
  354. "2mp" => "Larger than 2MP",
  355. "4mp" => "Larger than 4MP",
  356. "6mp" => "Larger than 6MP",
  357. "8mp" => "Larger than 8MP",
  358. "10mp" => "Larger than 10MP",
  359. "12mp" => "Larger than 12MP",
  360. "15mp" => "Larger than 15MP",
  361. "20mp" => "Larger than 20MP",
  362. "40mp" => "Larger than 40MP",
  363. "70mp" => "Larger than 70MP"
  364. ]
  365. ],
  366. "ratio" => [ // imgar
  367. "display" => "Aspect ratio",
  368. "option" => [
  369. "any" => "Any ratio",
  370. "t|xt" => "Tall",
  371. "s" => "Square",
  372. "w" => "Wide",
  373. "xw" => "Panoramic"
  374. ]
  375. ],
  376. "color" => [ // imgc
  377. "display" => "Color",
  378. "option" => [
  379. "any" => "Any color",
  380. "color" => "Full color",
  381. "bnw" => "Black & white",
  382. "trans" => "Transparent",
  383. // from here, imgcolor
  384. "red" => "Red",
  385. "orange" => "Orange",
  386. "yellow" => "Yellow",
  387. "green" => "Green",
  388. "teal" => "Teal",
  389. "blue" => "Blue",
  390. "purple" => "Purple",
  391. "pink" => "Pink",
  392. "white" => "White",
  393. "gray" => "Gray",
  394. "black" => "Black",
  395. "brown" => "Brown"
  396. ]
  397. ],
  398. "type" => [ // tbs=itp:<type>
  399. "display" => "Type",
  400. "option" => [
  401. "any" => "Any type",
  402. "clipart" => "Clip Art",
  403. "lineart" => "Line Drawing",
  404. "animated" => "Animated"
  405. ]
  406. ],
  407. "format" => [ // as_filetype
  408. "display" => "Format",
  409. "option" => [
  410. "any" => "Any format",
  411. "jpg" => "JPG",
  412. "gif" => "GIF",
  413. "png" => "PNG",
  414. "bmp" => "BMP",
  415. "svg" => "SVG",
  416. "webp" => "WEBP",
  417. "ico" => "ICO",
  418. "craw" => "RAW"
  419. ]
  420. ],
  421. "rights" => [ // tbs=sur:<rights>
  422. "display" => "Usage rights",
  423. "option" => [
  424. "any" => "Any license",
  425. "cl" => "Creative Commons licenses",
  426. "ol" => "Commercial & other licenses"
  427. ]
  428. ]
  429. ]
  430. );
  431. break;
  432. case "videos":
  433. return array_merge(
  434. $base,
  435. [
  436. "newer" => [ // tbs
  437. "display" => "Newer than",
  438. "option" => "_DATE"
  439. ],
  440. "older" => [
  441. "display" => "Older than",
  442. "option" => "_DATE"
  443. ],
  444. "duration" => [
  445. "display" => "Duration",
  446. "option" => [
  447. "any" => "Any duration",
  448. "s" => "Short (0-4min)", // tbs=dur:s
  449. "m" => "Medium (4-20min)", // tbs=dur:m
  450. "l" => "Long (20+ min)" // tbs=dur:l
  451. ]
  452. ],
  453. "quality" => [
  454. "display" => "Quality",
  455. "option" => [
  456. "any" => "Any quality",
  457. "h" => "High quality" // tbs=hq:h
  458. ]
  459. ],
  460. "captions" => [
  461. "display" => "Captions",
  462. "option" => [
  463. "any" => "No preference",
  464. "yes" => "Closed captioned" // tbs=cc:1
  465. ]
  466. ]
  467. ]
  468. );
  469. break;
  470. case "news":
  471. return array_merge(
  472. $base,
  473. [
  474. "newer" => [ // tbs
  475. "display" => "Newer than",
  476. "option" => "_DATE"
  477. ],
  478. "older" => [
  479. "display" => "Older than",
  480. "option" => "_DATE"
  481. ],
  482. "sort" => [
  483. "display" => "Sort",
  484. "option" => [
  485. "relevance" => "Relevance",
  486. "date" => "Date" // sbd:1
  487. ]
  488. ]
  489. ]
  490. );
  491. break;
  492. }
  493. }
  494. private function get($proxy, $url, $get = [], $alt_ua = false){
  495. $curlproc = curl_init();
  496. if($get !== []){
  497. $get = http_build_query($get);
  498. $url .= "?" . $get;
  499. }
  500. curl_setopt($curlproc, CURLOPT_URL, $url);
  501. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  502. if($alt_ua === true){
  503. curl_setopt($curlproc, CURLOPT_HTTPHEADER, [
  504. "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 26_0_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) GSA/406.0.862495628 Mobile/15E148 Safari/604.1",
  505. "Accept: text/html, application/xml;q=0.9, */*;q=0.8",
  506. "Accept-Language: nl,en;q=0.8",
  507. "Accept-Encoding: gzip, deflate",
  508. "Connection: Keep-Alive",
  509. "Cache-Control: no-cache"
  510. ]);
  511. }else{
  512. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  513. curl_setopt($curlproc, CURLOPT_HTTPHEADER, [
  514. "User-Agent: " . config::USER_AGENT,
  515. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  516. "Accept-Language: en-US,en;q=0.5",
  517. "Accept-Encoding: gzip",
  518. "DNT: 1",
  519. "Connection: keep-alive",
  520. "Upgrade-Insecure-Requests: 1",
  521. "Sec-Fetch-Dest: document",
  522. "Sec-Fetch-Mode: navigate",
  523. "Sec-Fetch-Site: none",
  524. "Sec-Fetch-User: ?1",
  525. "Priority: u=1",
  526. "TE: trailers"
  527. ]);
  528. }
  529. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  530. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  531. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  532. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  533. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  534. // follow redirects
  535. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  536. $this->backend->assign_proxy($curlproc, $proxy);
  537. $data = curl_exec($curlproc);
  538. if(curl_errno($curlproc)){
  539. throw new Exception(curl_error($curlproc));
  540. }
  541. curl_close($curlproc);
  542. return $data;
  543. }
  544. public function web($get){
  545. // it broke again. lasted 3 months
  546. // lets hope for another solid 3 month
  547. $out = [
  548. "status" => "ok",
  549. "spelling" => [
  550. "type" => "no_correction",
  551. "using" => null,
  552. "correction" => null
  553. ],
  554. "npt" => null,
  555. "answer" => [],
  556. "web" => [],
  557. "image" => [],
  558. "video" => [],
  559. "news" => [],
  560. "related" => []
  561. ];
  562. if($get["npt"]){
  563. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  564. try{
  565. $html =
  566. $this->get(
  567. $proxy,
  568. "https://www.google.com" . $get,
  569. [],
  570. true
  571. );
  572. }catch(Exception $error){
  573. throw new Exception("Failed to get HTML");
  574. }
  575. }else{
  576. $search = $get["s"];
  577. $country = $get["country"];
  578. $nsfw = $get["nsfw"];
  579. $lang = $get["lang"];
  580. $older = $get["older"];
  581. $newer = $get["newer"];
  582. $spellcheck = $get["spellcheck"];
  583. $proxy = $this->backend->get_ip();
  584. $offset = 0;
  585. $params = [
  586. "q" => $search,
  587. "hl" => "en"
  588. ];
  589. // country
  590. if($country != "any"){
  591. $params["gl"] = $country;
  592. }
  593. // nsfw
  594. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  595. // language
  596. if($lang != "any"){
  597. $params["lr"] = "lang_" . $lang;
  598. }
  599. // generate tbs
  600. $tbs = [];
  601. // get date
  602. $older = $older === false ? null : date("m/d/Y", $older);
  603. $newer = $newer === false ? null : date("m/d/Y", $newer);
  604. if(
  605. $older !== null ||
  606. $newer !== null
  607. ){
  608. $tbs["cdr"] = "1";
  609. $tbs["cd_min"] = $newer;
  610. $tbs["cd_max"] = $older;
  611. }
  612. // spellcheck filter
  613. if($spellcheck == "no"){
  614. $params["nfpr"] = "1";
  615. }
  616. if(count($tbs) !== 0){
  617. $params["tbs"] = "";
  618. foreach($tbs as $key => $value){
  619. $params["tbs"] .= $key . ":" . $value . ",";
  620. }
  621. $params["tbs"] = rtrim($params["tbs"], ",");
  622. }
  623. try{
  624. $html =
  625. $this->get(
  626. $proxy,
  627. "https://www.google.com/search",
  628. $params,
  629. true
  630. );
  631. }catch(Exception $error){
  632. throw new Exception("Failed to get HTML");
  633. }
  634. //$html = file_get_contents("scraper/google.html");
  635. }
  636. // init
  637. $this->fuckhtml->load($html);
  638. $this->detect_sorry();
  639. $this->parsestyles();
  640. // get javascript images
  641. $this->scrape_dimg($html);
  642. $this->scrape_imagearr($html);
  643. // get next page
  644. $npt =
  645. $this->fuckhtml
  646. ->getElementsByAttributeValue(
  647. "aria-label",
  648. "More search results",
  649. "a"
  650. );
  651. if(count($npt) === 0){
  652. // maybe we have the npt object from 2nd page, probe for that
  653. $npt =
  654. $this->fuckhtml
  655. ->getElementsByAttributeValue(
  656. "aria-label",
  657. "Next page",
  658. "a"
  659. );
  660. }
  661. if(count($npt) !== 0){
  662. $out["npt"] =
  663. $this->backend->store(
  664. $this->fuckhtml
  665. ->getTextContent(
  666. $npt[0]["attributes"]["href"]
  667. ),
  668. "web",
  669. $proxy
  670. );
  671. }
  672. // outer div is .MjjYud
  673. // inner div always contain role="presentation"
  674. $outer =
  675. $this->fuckhtml
  676. ->getElementsByClassName(
  677. "MjjYud",
  678. "div"
  679. );
  680. // used later
  681. $fancycontainer_class =
  682. explode(
  683. " ",
  684. $this->getstyle([
  685. "padding-top" => "4px",
  686. "padding-bottom" => "calc(12px*1)"
  687. ]),
  688. 2
  689. );
  690. if(count($fancycontainer_class) === 2){
  691. $fancycontainer_class = $fancycontainer_class[1];
  692. }else{
  693. $fancycontainer_class = false;
  694. }
  695. foreach($outer as $container){
  696. $this->fuckhtml->load($container);
  697. // probe for search result
  698. $sprobe =
  699. $this->fuckhtml
  700. ->getElementsByAttributeValue(
  701. "role",
  702. "presentation",
  703. "a"
  704. );
  705. if(count($sprobe) !== 0){
  706. // we found a search result
  707. $title =
  708. $this->fuckhtml
  709. ->getElementsByAttributeValue(
  710. "role",
  711. "link",
  712. "div"
  713. );
  714. if(count($title) === 0){
  715. // should not happen
  716. continue;
  717. }
  718. $title =
  719. $this->titledots(
  720. $this->fuckhtml
  721. ->getTextContent(
  722. $title[0]
  723. )
  724. );
  725. // get description
  726. // as usual, theres a thousand fucking possible divs for this one
  727. // probe for youtube-like description
  728. $description =
  729. $this->fuckhtml
  730. ->getElementsByClassName(
  731. $this->getstyle([
  732. "align-items" => "flex-start",
  733. "display" => "flex",
  734. "justify-content" => "center",
  735. "padding" => "7px 12px",
  736. "padding-right" => "0",
  737. "padding-top" => "0"
  738. ]),
  739. "div"
  740. );
  741. $ratio = "16:9";
  742. if(count($description) === 0){
  743. // fail. find the one with the image on the right handside
  744. $description =
  745. $this->fuckhtml
  746. ->getElementsByAttributeValue(
  747. "style",
  748. "padding-top:2px;padding-right:8px;padding-left:16px;padding-bottom:12px",
  749. "div"
  750. );
  751. $ratio = "1:1";
  752. if(count($description) === 0){
  753. // fail. find the one that is used the most
  754. $description =
  755. $this->fuckhtml
  756. ->getElementsByAttributeValue(
  757. "style",
  758. "-webkit-line-clamp:3",
  759. "div"
  760. );
  761. if(count($description) === 0){
  762. // last fail. this one appears with divs that have prices
  763. $description =
  764. $this->fuckhtml
  765. ->getElementsByAttributeValue(
  766. "style",
  767. "max-width:100vw;grid-area:nke7rc;padding-top:2px;padding-right:8px;padding-left:16px;padding-bottom:6px",
  768. "div"
  769. );
  770. }
  771. }
  772. }
  773. if(count($description) === 0){
  774. // should not happen but whatever
  775. $description = null;
  776. }else{
  777. $description =
  778. $this->titledots(
  779. $this->fuckhtml
  780. ->getTextContent(
  781. $description[0]
  782. )
  783. );
  784. }
  785. // probe for date
  786. $desc2 = explode("—", $description, 2);
  787. $time = null;
  788. if(count($desc2) === 2){
  789. $time = strtotime($desc2[0]);
  790. if(
  791. strlen($desc2[0]) < 16 &&
  792. $time !== false
  793. ){
  794. $description = ltrim($desc2[1]);
  795. }else{
  796. $time = null;
  797. }
  798. }
  799. $thumb = [
  800. "ratio" => null,
  801. "url" => null,
  802. ];
  803. // get thumbnail
  804. $images =
  805. $this->fuckhtml
  806. ->getElementsByTagName(
  807. "img"
  808. );
  809. foreach($images as $image){
  810. if(isset($image["attributes"]["id"])){
  811. $thumb = [
  812. "ratio" => $ratio,
  813. "url" => $this->getdimg($image["attributes"]["id"])
  814. ];
  815. }
  816. }
  817. // get sublinks
  818. $sublinks = [];
  819. // probe for the fancy version
  820. if($fancycontainer_class !== false){
  821. $fancycontainer =
  822. $this->fuckhtml
  823. ->getElementsByClassName(
  824. $fancycontainer_class,
  825. "div"
  826. );
  827. }
  828. if(
  829. $fancycontainer_class !== false &&
  830. count($fancycontainer) !== 0
  831. ){
  832. $this->fuckhtml->load($fancycontainer[0]);
  833. $as =
  834. $this->fuckhtml
  835. ->getElementsByTagName(
  836. "a"
  837. );
  838. foreach($as as $a){
  839. $sublinks[] = [
  840. "title" =>
  841. $this->fuckhtml
  842. ->getTextContent(
  843. $a
  844. ),
  845. "description" => null,
  846. "date" => null,
  847. "url" =>
  848. $this->unshiturl(
  849. $a["attributes"]["href"]
  850. )
  851. ];
  852. }
  853. }
  854. $out["web"][] = [
  855. "title" => $title,
  856. "description" => $description,
  857. "url" => $this->unshiturl($sprobe[0]["attributes"]["href"]),
  858. "date" => $time,
  859. "type" => "web",
  860. "thumb" => $thumb,
  861. "sublink" => $sublinks,
  862. "table" => []
  863. ];
  864. continue;
  865. }
  866. // probe for containers with a title header
  867. $title_header =
  868. $this->fuckhtml
  869. ->getElementsByClassName(
  870. $this->getstyle([
  871. "display" => "flex",
  872. "flex-wrap" => "wrap",
  873. "position" => "relative",
  874. "padding" => "16px"
  875. ])
  876. );
  877. if(count($title_header) !== 0){
  878. $title_header =
  879. strtolower(
  880. $this->fuckhtml
  881. ->getTextContent(
  882. $title_header[0]
  883. )
  884. );
  885. switch($title_header){
  886. case "people also search for":
  887. // get all related searches
  888. $relateds =
  889. $this->fuckhtml
  890. ->getElementsByClassName(
  891. $this->getstyle([
  892. "display" => "flex",
  893. "height" => "100%",
  894. "flex-direction" => "column",
  895. "max-width" => "100%"
  896. ])
  897. );
  898. foreach($relateds as $r){
  899. $out["related"][] =
  900. $this->fuckhtml
  901. ->getTextContent(
  902. $r
  903. );
  904. }
  905. break;
  906. }
  907. continue;
  908. }
  909. }
  910. $out["related"] = array_values(array_unique($out["related"]));
  911. return $out;
  912. }
  913. public function video($get){
  914. if($get["npt"]){
  915. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  916. $params = json_decode($params, true);
  917. }else{
  918. $search = $get["s"];
  919. $country = $get["country"];
  920. $nsfw = $get["nsfw"];
  921. $older = $get["older"];
  922. $newer = $get["newer"];
  923. $duration = $get["duration"];
  924. $quality = $get["quality"];
  925. $captions = $get["captions"];
  926. $proxy = $this->backend->get_ip();
  927. $params = [
  928. "q" => $search,
  929. "udm" => "7",
  930. "hl" => "en",
  931. "num" => 20
  932. ];
  933. // country
  934. if($country != "any"){
  935. $params["gl"] = $country;
  936. }
  937. // nsfw
  938. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  939. $tbs = [];
  940. // get date
  941. $older = $older === false ? null : date("m/d/Y", $older);
  942. $newer = $newer === false ? null : date("m/d/Y", $newer);
  943. if(
  944. $older !== null ||
  945. $newer !== null
  946. ){
  947. $tbs["cdr"] = "1";
  948. $tbs["cd_min"] = $newer;
  949. $tbs["cd_max"] = $older;
  950. }
  951. // duration
  952. if($duration != "any"){
  953. $tbs[] = "dur:" . $duration;
  954. }
  955. // quality
  956. if($quality != "any"){
  957. $tbs[] = "hq:" . $quality;
  958. }
  959. // captions
  960. if($captions != "any"){
  961. $tbs[] = "cc:" . $captions;
  962. }
  963. // append tbs
  964. if(count($tbs) !== 0){
  965. $params["tbs"] =
  966. implode(",", $tbs);
  967. }
  968. }
  969. try{
  970. $html =
  971. $this->get(
  972. $proxy,
  973. "https://www.google.com/search",
  974. $params
  975. );
  976. }catch(Exception $error){
  977. throw new Exception("Failed to get HTML");
  978. }
  979. if(!isset($params["start"])){
  980. $params["start"] = 0;
  981. }
  982. $params["start"] += 20;
  983. $this->fuckhtml->load($html);
  984. //
  985. // Parse web video page
  986. //
  987. $this->detect_sorry();
  988. // parse all <style> tags
  989. $this->parsestyles();
  990. // get javascript images
  991. $this->scrape_dimg($html);
  992. $this->scrape_imagearr($html);
  993. $out = [
  994. "status" => "ok",
  995. "npt" =>
  996. $this->backend->store(
  997. json_encode($params),
  998. "videos",
  999. $proxy
  1000. ),
  1001. "video" => [],
  1002. "author" => [],
  1003. "livestream" => [],
  1004. "playlist" => [],
  1005. "reel" => []
  1006. ];
  1007. $search_div =
  1008. $this->fuckhtml
  1009. ->getElementById(
  1010. "center_col"
  1011. );
  1012. if($search_div === false){
  1013. throw new Exception("Failed to grep search div");
  1014. }
  1015. $this->fuckhtml->load($search_div);
  1016. $results =
  1017. $this->fuckhtml
  1018. ->getElementsByClassName(
  1019. $this->getstyle([
  1020. "margin" => "0px 0px 30px"
  1021. ]),
  1022. "div"
  1023. );
  1024. foreach($results as $result){
  1025. $this->fuckhtml->load($result);
  1026. $url =
  1027. $this->fuckhtml
  1028. ->getElementsByTagName(
  1029. "a"
  1030. );
  1031. if(count($url) === 0){
  1032. // no url, weird, continue
  1033. continue;
  1034. }
  1035. $title =
  1036. $this->fuckhtml
  1037. ->getElementsByTagName(
  1038. "h3"
  1039. );
  1040. if(count($title) === 0){
  1041. // no title, weird, continue
  1042. continue;
  1043. }
  1044. // get description
  1045. $description =
  1046. $this->fuckhtml
  1047. ->getElementsByClassName(
  1048. $this->getstyle([
  1049. "-webkit-box-orient" => "vertical",
  1050. "display" => "-webkit-box",
  1051. "-webkit-line-clamp" => "2",
  1052. "overflow" => "hidden",
  1053. "word-break" => "break-word"
  1054. ]),
  1055. "div"
  1056. );
  1057. if(count($description) === 0){
  1058. $description = null;
  1059. }else{
  1060. $description =
  1061. html_entity_decode(
  1062. $this->titledots(
  1063. $this->fuckhtml
  1064. ->getTextContent(
  1065. $description[0]
  1066. )
  1067. )
  1068. );
  1069. }
  1070. // get author + date posted
  1071. $metadiv =
  1072. $this->fuckhtml
  1073. ->getElementsByClassName(
  1074. $this->getstyle([
  1075. "margin-top" => "12px"
  1076. ]),
  1077. "div"
  1078. );
  1079. $author = null;
  1080. $date = null;
  1081. if(count($metadiv) !== 0){
  1082. $metadiv =
  1083. explode(
  1084. "·",
  1085. $this->fuckhtml
  1086. ->getTextContent(
  1087. $metadiv[0]
  1088. )
  1089. );
  1090. if(count($metadiv) === 3){
  1091. $author = trim($metadiv[1]);
  1092. $date = strtotime(trim($metadiv[2]));
  1093. }elseif(count($metadiv) === 2){
  1094. $author = trim($metadiv[0]);
  1095. $date = strtotime(trim($metadiv[1]));
  1096. }
  1097. }
  1098. $thumb = [
  1099. "url" => null,
  1100. "ratio" => null
  1101. ];
  1102. $image =
  1103. $this->fuckhtml
  1104. ->getElementsByTagName(
  1105. "img"
  1106. );
  1107. $duration = null;
  1108. if(
  1109. count($image) !== 0 &&
  1110. isset($image[0]["attributes"]["id"])
  1111. ){
  1112. $thumb = [
  1113. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1114. "ratio" => "16:9"
  1115. ];
  1116. // get duration
  1117. $duration =
  1118. $this->fuckhtml
  1119. ->getElementsByClassName(
  1120. $this->getstyle([
  1121. "background-color" => "rgba(0,0,0,0.6)",
  1122. "color" => "#fff",
  1123. "fill" => "#fff"
  1124. ])
  1125. );
  1126. if(count($duration) !== 0){
  1127. $duration =
  1128. $this->hms2int(
  1129. $this->fuckhtml
  1130. ->getTextContent(
  1131. $duration[0]
  1132. ));
  1133. }else{
  1134. $duration = null;
  1135. }
  1136. }
  1137. $out["video"][] = [
  1138. "title" =>
  1139. $this->titledots(
  1140. $this->fuckhtml
  1141. ->getTextContent(
  1142. $title[0]
  1143. )
  1144. ),
  1145. "description" => $description,
  1146. "author" => [
  1147. "name" => $author,
  1148. "url" => null,
  1149. "avatar" => null
  1150. ],
  1151. "date" => $date,
  1152. "duration" => $duration,
  1153. "views" => null,
  1154. "thumb" => $thumb,
  1155. "url" =>
  1156. $this->fuckhtml
  1157. ->getTextContent(
  1158. $url[0]["attributes"]["href"]
  1159. )
  1160. ];
  1161. }
  1162. return $out;
  1163. }
  1164. public function news($get){
  1165. throw new Exception("Broke for now, fuck off lol");
  1166. if($get["npt"]){
  1167. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1168. /*parse_str(
  1169. parse_url($req, PHP_URL_QUERY),
  1170. $search
  1171. );*/
  1172. try{
  1173. $html =
  1174. $this->get(
  1175. $proxy,
  1176. "https://www.google.com" . $req,
  1177. []
  1178. );
  1179. }catch(Exception $error){
  1180. throw new Exception("Failed to get HTML");
  1181. }
  1182. }else{
  1183. $search = $get["s"];
  1184. $country = $get["country"];
  1185. $nsfw = $get["nsfw"];
  1186. $older = $get["older"];
  1187. $newer = $get["newer"];
  1188. $sort = $get["sort"];
  1189. $proxy = $this->backend->get_ip();
  1190. $params = [
  1191. "q" => $search,
  1192. "tbm" => "nws",
  1193. "hl" => "en",
  1194. "num" => "20"
  1195. ];
  1196. // country
  1197. if($country != "any"){
  1198. $params["gl"] = $country;
  1199. }
  1200. // nsfw
  1201. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1202. $tbs = [];
  1203. // get date
  1204. $older = $older === false ? null : date("m/d/Y", $older);
  1205. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1206. if(
  1207. $older !== null ||
  1208. $newer !== null
  1209. ){
  1210. $tbs["cdr"] = "1";
  1211. $tbs["cd_min"] = $newer;
  1212. $tbs["cd_max"] = $older;
  1213. }
  1214. // relevance
  1215. if($sort == "date"){
  1216. $tbs["sbd"] = "1";
  1217. }
  1218. // append tbs
  1219. if(count($tbs) !== 0){
  1220. $params["tbs"] = "";
  1221. foreach($tbs as $key => $value){
  1222. $params["tbs"] .= $key . ":" . $value . ",";
  1223. }
  1224. $params["tbs"] = rtrim($params["tbs"], ",");
  1225. }
  1226. //$html = file_get_contents("scraper/google-news.html");
  1227. $html =
  1228. $this->get(
  1229. $proxy,
  1230. "https://www.google.com/search",
  1231. $params
  1232. );
  1233. }
  1234. $out = [
  1235. "status" => "ok",
  1236. "npt" => null,
  1237. "news" => []
  1238. ];
  1239. $this->fuckhtml->load($html);
  1240. $this->detect_sorry();
  1241. // get images
  1242. $this->scrape_dimg($html);
  1243. // parse styles
  1244. $this->parsestyles();
  1245. $center_col =
  1246. $this->fuckhtml
  1247. ->getElementById(
  1248. "center_col",
  1249. "div"
  1250. );
  1251. if($center_col === null){
  1252. throw new Exception("Could not grep result div");
  1253. }
  1254. $this->fuckhtml->load($center_col);
  1255. // get next page
  1256. $npt =
  1257. $this->fuckhtml
  1258. ->getElementById(
  1259. "pnnext",
  1260. "a"
  1261. );
  1262. if($npt !== false){
  1263. $out["npt"] =
  1264. $this->backend->store(
  1265. $this->fuckhtml
  1266. ->getTextContent(
  1267. $npt["attributes"]
  1268. ["href"]
  1269. ),
  1270. "news",
  1271. $proxy
  1272. );
  1273. }
  1274. $as =
  1275. $this->fuckhtml
  1276. ->getElementsByAttributeName(
  1277. "jsname",
  1278. "a"
  1279. );
  1280. foreach($as as $a){
  1281. $this->fuckhtml->load($a);
  1282. // get title
  1283. $title =
  1284. $this->fuckhtml
  1285. ->getElementsByAttributeValue(
  1286. "role",
  1287. "heading",
  1288. "div"
  1289. );
  1290. if(count($title) === 0){
  1291. continue;
  1292. }
  1293. $title =
  1294. $this->titledots(
  1295. $this->fuckhtml
  1296. ->getTextContent(
  1297. $title[0]
  1298. )
  1299. );
  1300. // get thumbnail
  1301. $image =
  1302. $this->fuckhtml
  1303. ->getElementsByAttributeName(
  1304. "id",
  1305. "img"
  1306. );
  1307. // check for padded title node, if found, we're inside a carousel
  1308. $probe =
  1309. $this->fuckhtml
  1310. ->getElementsByClassName(
  1311. $this->getstyle(
  1312. [
  1313. "padding" => "16px 16px 40px 16px"
  1314. ]
  1315. ),
  1316. "div"
  1317. );
  1318. if(count($probe) !== 0){
  1319. $probe = true;
  1320. }else{
  1321. $probe = false;
  1322. }
  1323. if(
  1324. count($image) !== 0 &&
  1325. !isset($image[0]["attributes"]["width"])
  1326. ){
  1327. $thumb = [
  1328. "url" =>
  1329. $this->getdimg(
  1330. $image[0]["attributes"]["id"]
  1331. ),
  1332. "ratio" => $probe === true ? "16:9" : "1:1"
  1333. ];
  1334. }else{
  1335. $thumb = [
  1336. "url" => null,
  1337. "ratio" => null
  1338. ];
  1339. }
  1340. $description = null;
  1341. if($probe === false){
  1342. $desc_divs =
  1343. $this->fuckhtml
  1344. ->getElementsByAttributeName(
  1345. "style",
  1346. "div"
  1347. );
  1348. foreach($desc_divs as $desc){
  1349. if(
  1350. strpos(
  1351. $desc["attributes"]["style"],
  1352. "margin-top:"
  1353. ) !== false
  1354. ){
  1355. $description =
  1356. $this->titledots(
  1357. $this->fuckhtml
  1358. ->getTextContent(
  1359. $desc
  1360. )
  1361. );
  1362. break;
  1363. }
  1364. }
  1365. }
  1366. // get author
  1367. $author =
  1368. $this->fuckhtml
  1369. ->getElementsByClassName(
  1370. $this->getstyle(
  1371. [
  1372. "overflow" => "hidden",
  1373. "text-align" => "left",
  1374. "text-overflow" => "ellipsis",
  1375. "white-space" => "nowrap",
  1376. "margin-bottom" => "8px"
  1377. ]
  1378. ),
  1379. "div"
  1380. );
  1381. if(count($author) !== 0){
  1382. $author =
  1383. $this->fuckhtml
  1384. ->getTextContent(
  1385. $author[0]
  1386. );
  1387. }else{
  1388. $author = null;
  1389. }
  1390. // get date
  1391. $date = null;
  1392. $date_div =
  1393. $this->fuckhtml
  1394. ->getElementsByAttributeName(
  1395. "style",
  1396. "div"
  1397. );
  1398. foreach($date_div as $d){
  1399. $this->fuckhtml->load($d);
  1400. $span =
  1401. $this->fuckhtml
  1402. ->getElementsByTagName(
  1403. "span"
  1404. );
  1405. if(
  1406. strpos(
  1407. $d["attributes"]["style"],
  1408. "bottom:"
  1409. ) !== false
  1410. ){
  1411. $date =
  1412. strtotime(
  1413. $this->fuckhtml
  1414. ->getTextContent(
  1415. $span[count($span) - 1]
  1416. )
  1417. );
  1418. break;
  1419. }
  1420. }
  1421. $out["news"][] = [
  1422. "title" => $title,
  1423. "author" => $author,
  1424. "description" => $description,
  1425. "date" => $date,
  1426. "thumb" => $thumb,
  1427. "url" =>
  1428. $this->unshiturl(
  1429. $a["attributes"]
  1430. ["href"]
  1431. )
  1432. ];
  1433. }
  1434. return $out;
  1435. }
  1436. public function image($get){
  1437. // generate parameters
  1438. if($get["npt"]){
  1439. [$params, $proxy] =
  1440. $this->backend->get(
  1441. $get["npt"],
  1442. "images"
  1443. );
  1444. $params = json_decode($params, true);
  1445. }else{
  1446. $search = $get["s"];
  1447. if(strlen($search) === 0){
  1448. throw new Exception("Search term is empty!");
  1449. }
  1450. $proxy = $this->backend->get_ip();
  1451. $country = $get["country"];
  1452. $nsfw = $get["nsfw"];
  1453. $time = $get["time"];
  1454. $size = $get["size"];
  1455. $ratio = $get["ratio"];
  1456. $color = $get["color"];
  1457. $type = $get["type"];
  1458. $format = $get["format"];
  1459. $rights = $get["rights"];
  1460. $params = [
  1461. "q" => $search,
  1462. "udm" => "2" // get images
  1463. ];
  1464. // country (image search uses cr instead of gl)
  1465. if($country != "any"){
  1466. $params["cr"] = "country" . strtoupper($country);
  1467. }
  1468. // nsfw
  1469. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1470. // generate tbs
  1471. $tbs = [];
  1472. // time
  1473. if($time != "any"){
  1474. $tbs["qdr"] = $time;
  1475. }
  1476. // size
  1477. if($size != "any"){
  1478. $params["imgsz"] = $size;
  1479. }
  1480. // ratio
  1481. if($ratio != "any"){
  1482. $params["imgar"] = $ratio;
  1483. }
  1484. // color
  1485. if($color != "any"){
  1486. if(
  1487. $color == "color" ||
  1488. $color == "trans"
  1489. ){
  1490. $params["imgc"] = $color;
  1491. }elseif($color == "bnw"){
  1492. $params["imgc"] = "gray";
  1493. }else{
  1494. $tbs["ic"] = "specific";
  1495. $tbs["isc"] = $color;
  1496. }
  1497. }
  1498. // type
  1499. if($type != "any"){
  1500. $tbs["itp"] = $type;
  1501. }
  1502. // format
  1503. if($format != "any"){
  1504. $params["as_filetype"] = $format;
  1505. }
  1506. // rights (tbs)
  1507. if($rights != "any"){
  1508. $tbs["sur"] = $rights;
  1509. }
  1510. // append tbs
  1511. if(count($tbs) !== 0){
  1512. $params["tbs"] = "";
  1513. foreach($tbs as $key => $value){
  1514. $params["tbs"] .= $key . ":" . $value . ",";
  1515. }
  1516. $params["tbs"] = rtrim($params["tbs"], ",");
  1517. }
  1518. }
  1519. /*
  1520. $handle = fopen("scraper/page.html", "r");
  1521. $html = fread($handle, filesize("scraper/page.html"));
  1522. fclose($handle);*/
  1523. try{
  1524. $html =
  1525. $this->get(
  1526. $proxy,
  1527. "https://www.google.com/search",
  1528. $params
  1529. );
  1530. }catch(Exception $error){
  1531. throw new Exception("Failed to get search page");
  1532. }
  1533. $this->fuckhtml->load($html);
  1534. $this->detect_sorry();
  1535. // get javascript images
  1536. $this->scrape_imagearr($html);
  1537. $out = [
  1538. "status" => "ok",
  1539. "npt" => null,
  1540. "image" => []
  1541. ];
  1542. $images =
  1543. $this->fuckhtml
  1544. ->getElementsByClassName(
  1545. "ivg-i",
  1546. "div"
  1547. );
  1548. foreach($images as $div){
  1549. $this->fuckhtml->load($div);
  1550. $image =
  1551. $this->fuckhtml
  1552. ->getElementsByTagName("img")[0];
  1553. // make sure we dont attempt to show an image we dont have data for
  1554. if(
  1555. isset($div["attributes"]["data-docid"]) &&
  1556. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1557. ){
  1558. $source =
  1559. $this->image_arr[
  1560. $div["attributes"]["data-docid"]
  1561. ];
  1562. }else{
  1563. continue;
  1564. }
  1565. $out["image"][] = [
  1566. "title" =>
  1567. $this->titledots(
  1568. $this->fuckhtml
  1569. ->getTextContent(
  1570. $image["attributes"]["alt"]
  1571. )
  1572. ),
  1573. "source" => $source,
  1574. "url" =>
  1575. $this->fuckhtml
  1576. ->getTextContent(
  1577. $div["attributes"]["data-lpage"]
  1578. )
  1579. ];
  1580. }
  1581. // as usual, no way to check if there is a next page reliably
  1582. if(count($out["image"]) > 50){
  1583. if(!isset($params["start"])){
  1584. $params["start"] = 10;
  1585. }else{
  1586. $params["start"] += 10;
  1587. }
  1588. $out["npt"] =
  1589. $this->backend
  1590. ->store(
  1591. json_encode($params),
  1592. "image",
  1593. $proxy
  1594. );
  1595. }
  1596. return $out;
  1597. }
  1598. private function scrape_dimg($html){
  1599. // get images loaded through javascript
  1600. $this->dimg = [];
  1601. preg_match_all(
  1602. '/function\(\){google\.ldi=({.*?});/',
  1603. $html,
  1604. $dimg
  1605. );
  1606. if(isset($dimg[1])){
  1607. foreach($dimg[1] as $i){
  1608. $tmp = json_decode($i, true);
  1609. foreach($tmp as $key => $value){
  1610. $this->dimg[$key] =
  1611. $this->unshit_thumb(
  1612. $value
  1613. );
  1614. }
  1615. }
  1616. }
  1617. // get additional javascript base64 images
  1618. preg_match_all(
  1619. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1620. $html,
  1621. $dimg
  1622. );
  1623. if(isset($dimg[1])){
  1624. for($i=0; $i<count($dimg[1]); $i++){
  1625. $delims = explode(",", $dimg[2][$i]);
  1626. $string =
  1627. $this->fuckhtml
  1628. ->parseJsString(
  1629. $dimg[1][$i]
  1630. );
  1631. foreach($delims as $delim){
  1632. $this->dimg[trim($delim, "'")] = $string;
  1633. }
  1634. }
  1635. }
  1636. }
  1637. private function scrape_imagearr($html){
  1638. // get image links arrays
  1639. preg_match_all(
  1640. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1641. $html,
  1642. $image_arr
  1643. );
  1644. $this->image_arr = [];
  1645. if(isset($image_arr[1])){
  1646. for($i=0; $i<count($image_arr[1]); $i++){
  1647. $original =
  1648. $this->fuckhtml
  1649. ->parseJsString(
  1650. $image_arr[5][$i]
  1651. );
  1652. if(
  1653. preg_match(
  1654. '/^x-raw-image/',
  1655. $original
  1656. )
  1657. ){
  1658. // only add thumbnail, google doesnt have OG resolution
  1659. $this->image_arr[$image_arr[1][$i]] = [
  1660. [
  1661. "url" =>
  1662. $this->unshit_thumb(
  1663. $this->fuckhtml
  1664. ->parseJsString(
  1665. $image_arr[2][$i]
  1666. )
  1667. ),
  1668. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1669. "height" => (int)$image_arr[6][$i]
  1670. ]
  1671. ];
  1672. continue;
  1673. }
  1674. $this->image_arr[$image_arr[1][$i]] =
  1675. [
  1676. [
  1677. "url" => $original,
  1678. "width" => (int)$image_arr[7][$i],
  1679. "height" => (int)$image_arr[6][$i]
  1680. ],
  1681. [
  1682. "url" =>
  1683. $this->unshit_thumb(
  1684. $this->fuckhtml
  1685. ->parseJsString(
  1686. $image_arr[2][$i]
  1687. )
  1688. ),
  1689. "width" => (int)$image_arr[4][$i],
  1690. "height" => (int)$image_arr[3][$i]
  1691. ]
  1692. ];
  1693. }
  1694. }
  1695. }
  1696. private function getdimg($dimg){
  1697. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1698. }
  1699. private function unshit_thumb($url, $get_bigger_res = false){
  1700. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1701. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1702. $parts = parse_url($url);
  1703. if(
  1704. isset($parts["host"]) &&
  1705. preg_match(
  1706. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1707. $parts["host"]
  1708. )
  1709. ){
  1710. parse_str($parts["query"], $params);
  1711. if(isset($params["q"])){
  1712. if($get_bigger_res){
  1713. // this method doesnt always work, but does work for wiki thumbnails
  1714. return
  1715. "https://" . $parts["host"] . "/images?q=tbn:" .
  1716. $this->base64url_encode(
  1717. substr(
  1718. $this->base64url_decode(
  1719. explode(
  1720. ":",
  1721. $params["q"])[1]
  1722. ),
  1723. 0,
  1724. 29
  1725. )
  1726. );
  1727. }else{
  1728. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1729. }
  1730. }
  1731. }
  1732. return $url;
  1733. }
  1734. private function parsestyles(){
  1735. $styles = [];
  1736. $style_div =
  1737. $this->fuckhtml
  1738. ->getElementsByTagName(
  1739. "style"
  1740. );
  1741. $raw_styles = "";
  1742. foreach($style_div as $style){
  1743. $raw_styles .= $style["innerHTML"];
  1744. }
  1745. // filter out media/keyframe queries
  1746. $raw_styles =
  1747. preg_replace(
  1748. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1749. "",
  1750. $raw_styles
  1751. );
  1752. // get styles
  1753. preg_match_all(
  1754. '/(.+?){([\S\s]*?)}/',
  1755. $raw_styles,
  1756. $matches
  1757. );
  1758. for($i=0; $i<count($matches[1]); $i++){
  1759. // get style values
  1760. preg_match_all(
  1761. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1762. $matches[2][$i],
  1763. $values_regex
  1764. );
  1765. $values = [];
  1766. for($k=0; $k<count($values_regex[1]); $k++){
  1767. $values[trim($values_regex[1][$k])] =
  1768. strtolower(trim($values_regex[2][$k]));
  1769. }
  1770. $names = explode(",", $matches[1][$i]);
  1771. // h1,h2,h3 will each get their own array index
  1772. foreach($names as $name){
  1773. $name = trim($name, "}\t\n\r\0\x0B");
  1774. foreach($values as $key => $value){
  1775. $styles[$name][$key] = $value;
  1776. }
  1777. }
  1778. }
  1779. foreach($styles as $key => $values){
  1780. $styles[$key]["_c"] = count($values);
  1781. }
  1782. $this->styles = $styles;
  1783. // get CSS colors
  1784. $this->css_colors = [];
  1785. if(isset($this->styles[":root"])){
  1786. foreach($this->styles[":root"] as $key => $value){
  1787. $this->css_colors[$value] = strtolower($key);
  1788. }
  1789. }
  1790. }
  1791. private function getstyle($styles){
  1792. $styles["_c"] = count($styles);
  1793. foreach($this->styles as $style_key => $style_values){
  1794. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1795. $style_key =
  1796. explode(" ", $style_key);
  1797. $style_key = $style_key[count($style_key) - 1];
  1798. return
  1799. ltrim(
  1800. str_replace(
  1801. [".", "#"],
  1802. " ",
  1803. $style_key
  1804. )
  1805. );
  1806. }
  1807. }
  1808. return false;
  1809. }
  1810. private function getcolorvar($color){
  1811. if(isset($this->css_colors[$color])){
  1812. return $this->css_colors[$color];
  1813. }
  1814. return null;
  1815. }
  1816. private function unshiturl($url, $return_size = false){
  1817. // decode
  1818. $url =
  1819. $this->fuckhtml
  1820. ->getTextContent(
  1821. $url
  1822. );
  1823. $url_parts = parse_url($url);
  1824. if(isset($url_parts["query"])){
  1825. parse_str($url_parts["query"], $query);
  1826. }else{
  1827. $query = [];
  1828. }
  1829. if(
  1830. !isset(
  1831. $url_parts["host"]
  1832. ) ||
  1833. stripos($url_parts["host"], "google.") !== false
  1834. ){
  1835. // no host, we have a tracking url
  1836. if(isset($query["imgurl"])){
  1837. $url = $query["imgurl"];
  1838. }
  1839. elseif(isset($query["q"])){
  1840. $url = $query["q"];
  1841. }
  1842. }
  1843. // rewrite URLs to remove extra tracking parameters
  1844. $domain = parse_url($url, PHP_URL_HOST);
  1845. if(
  1846. preg_match(
  1847. '/wikipedia.org$/',
  1848. $domain
  1849. )
  1850. ){
  1851. // rewrite wikipedia mobile URLs to desktop
  1852. $url =
  1853. $this->replacedomain(
  1854. $url,
  1855. preg_replace(
  1856. '/([a-z0-9]+)(\.m\.)/',
  1857. '$1.',
  1858. $domain
  1859. )
  1860. );
  1861. }
  1862. elseif(
  1863. preg_match(
  1864. '/imdb\.com$|youtube\.[^.]+$/',
  1865. $domain
  1866. )
  1867. ){
  1868. // rewrite imdb and youtube mobile URLs too
  1869. $url =
  1870. $this->replacedomain(
  1871. $url,
  1872. preg_replace(
  1873. '/^m\./',
  1874. "",
  1875. $domain
  1876. )
  1877. );
  1878. }
  1879. elseif(
  1880. preg_match(
  1881. '/play\.google\.[^.]+$/',
  1882. $domain
  1883. )
  1884. ){
  1885. // remove referrers from play.google.com
  1886. $u_query = parse_url($url, PHP_URL_QUERY);
  1887. if($u_query !== null){
  1888. parse_str($u_query, $u_query);
  1889. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  1890. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  1891. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  1892. $query = http_build_query($query);
  1893. $url =
  1894. str_replace(
  1895. $u_query,
  1896. $u_query,
  1897. $url
  1898. );
  1899. }
  1900. }
  1901. elseif(
  1902. preg_match(
  1903. '/twitter\.com$/',
  1904. $domain
  1905. )
  1906. ){
  1907. // remove more referrers from twitter.com
  1908. $u_query = parse_url($url, PHP_URL_QUERY);
  1909. if($u_query !== null){
  1910. parse_str($u_query, $u_query);
  1911. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  1912. $u_query = http_build_query($u_query);
  1913. $url =
  1914. str_replace(
  1915. $oldquery,
  1916. $u_query,
  1917. $url
  1918. );
  1919. }
  1920. }
  1921. elseif(
  1922. preg_match(
  1923. '/maps\.google\.[^.]+/',
  1924. $domain
  1925. )
  1926. ){
  1927. if(stripos($url, "maps?") !== false){
  1928. $u_query = parse_url($url, PHP_URL_QUERY);
  1929. if($u_query !== null){
  1930. parse_str($u_query, $u_query);
  1931. if(isset($u_query["daddr"])){
  1932. $url =
  1933. "https://maps.google.com/maps?daddr=" .
  1934. urlencode($u_query["daddr"]);
  1935. }
  1936. }
  1937. }
  1938. }
  1939. if($return_size){
  1940. return [
  1941. "url" => $url,
  1942. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  1943. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  1944. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  1945. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  1946. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  1947. ];
  1948. }
  1949. return $url;
  1950. }
  1951. private function replacedomain($url, $domain){
  1952. return
  1953. preg_replace(
  1954. '/(https?:\/\/)([^\/]+)/',
  1955. '$1' . $domain,
  1956. $url
  1957. );
  1958. }
  1959. private function titledots($title){
  1960. return trim($title, " .\t\n\r\0\x0B…");
  1961. }
  1962. private function hms2int($time){
  1963. $parts = explode(":", $time, 3);
  1964. $time = 0;
  1965. if(count($parts) === 3){
  1966. // hours
  1967. $time = $time + ((int)$parts[0] * 3600);
  1968. array_shift($parts);
  1969. }
  1970. if(count($parts) === 2){
  1971. // minutes
  1972. $time = $time + ((int)$parts[0] * 60);
  1973. array_shift($parts);
  1974. }
  1975. // seconds
  1976. $time = $time + (int)$parts[0];
  1977. return $time;
  1978. }
  1979. function base64url_decode($data){
  1980. $b64 = strtr($data, "-_", "+/");
  1981. $pad = strlen($b64) % 4;
  1982. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  1983. return base64_decode($b64);
  1984. }
  1985. function base64url_encode($data){
  1986. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  1987. }
  1988. private function detect_sorry(){
  1989. $captcha_form =
  1990. $this->fuckhtml
  1991. ->getElementById(
  1992. "captcha-form",
  1993. "form"
  1994. );
  1995. if($captcha_form !== false){
  1996. throw new Exception("Google returned a captcha");
  1997. }
  1998. }
  1999. }