google.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  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/4.0 (compatible; MSIE 6.0; Windows CE; PPC; 240x320) Opera 8.65 [nl]",
  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. // this is going to break soon. I wont scrape the answers simply cause its not worth my time.
  546. // If only their API wasn't such dogshit maybe I wouldnt need to fuck with this. this isn't
  547. // just a rant. I know a Google engineer is reading this, give me real fucking results
  548. // you worthless sacks of shit
  549. $out = [
  550. "status" => "ok",
  551. "spelling" => [
  552. "type" => "no_correction",
  553. "using" => null,
  554. "correction" => null
  555. ],
  556. "npt" => null,
  557. "answer" => [],
  558. "web" => [],
  559. "image" => [],
  560. "video" => [],
  561. "news" => [],
  562. "related" => []
  563. ];
  564. if($get["npt"]){
  565. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  566. try{
  567. $html =
  568. $this->get(
  569. $proxy,
  570. "https://www.google.com" . $get,
  571. [],
  572. true
  573. );
  574. }catch(Exception $error){
  575. throw new Exception("Failed to get HTML");
  576. }
  577. }else{
  578. $search = $get["s"];
  579. $country = $get["country"];
  580. $nsfw = $get["nsfw"];
  581. $lang = $get["lang"];
  582. $older = $get["older"];
  583. $newer = $get["newer"];
  584. $spellcheck = $get["spellcheck"];
  585. $proxy = $this->backend->get_ip();
  586. $offset = 0;
  587. $params = [
  588. "q" => $search,
  589. "hl" => "en"
  590. ];
  591. // country
  592. if($country != "any"){
  593. $params["gl"] = $country;
  594. }
  595. // nsfw
  596. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  597. // language
  598. if($lang != "any"){
  599. $params["lr"] = "lang_" . $lang;
  600. }
  601. // generate tbs
  602. $tbs = [];
  603. // get date
  604. $older = $older === false ? null : date("m/d/Y", $older);
  605. $newer = $newer === false ? null : date("m/d/Y", $newer);
  606. if(
  607. $older !== null ||
  608. $newer !== null
  609. ){
  610. $tbs["cdr"] = "1";
  611. $tbs["cd_min"] = $newer;
  612. $tbs["cd_max"] = $older;
  613. }
  614. // spellcheck filter
  615. if($spellcheck == "no"){
  616. $params["nfpr"] = "1";
  617. }
  618. if(count($tbs) !== 0){
  619. $params["tbs"] = "";
  620. foreach($tbs as $key => $value){
  621. $params["tbs"] .= $key . ":" . $value . ",";
  622. }
  623. $params["tbs"] = rtrim($params["tbs"], ",");
  624. }
  625. try{
  626. $html =
  627. $this->get(
  628. $proxy,
  629. "https://www.google.com/search",
  630. $params,
  631. true
  632. );
  633. }catch(Exception $error){
  634. throw new Exception("Failed to get HTML");
  635. }
  636. //$html = file_get_contents("scraper/google.html");
  637. }
  638. // init
  639. $this->fuckhtml->load($html);
  640. $this->detect_sorry();
  641. $this->parsestyles();
  642. // iterate over results
  643. $containers =
  644. $this->fuckhtml
  645. ->getElementsByClassName(
  646. $this->getstyle([
  647. "background-color" => "#fff",
  648. "margin-bottom" => "10px",
  649. "margin" => "0px 0px 8px",
  650. "box-shadow" => "0 0 0 1px #ebedef"
  651. ])
  652. );
  653. foreach($containers as $container){
  654. $this->fuckhtml->load($container);
  655. //
  656. // Probe for next page container
  657. //
  658. $npt =
  659. $this->fuckhtml
  660. ->getElementsByAttributeValue(
  661. "aria-label",
  662. "Next page",
  663. "a"
  664. );
  665. if(count($npt) !== 0){
  666. // found next page object
  667. $out["npt"] =
  668. $this->backend->store(
  669. $this->fuckhtml
  670. ->getTextContent(
  671. $npt[0]
  672. ["attributes"]
  673. ["href"]
  674. ),
  675. "web",
  676. $proxy
  677. );
  678. continue;
  679. }
  680. //
  681. // Probe for "did you mean" bullshit
  682. //
  683. $ddm =
  684. $this->fuckhtml
  685. ->getElementsByClassName(
  686. $this->getstyle([
  687. "font-size" => "20px",
  688. "font-weight" => "bold",
  689. "line-height" => "26px",
  690. "color" => "#1f1f1f",
  691. "height" => "14px",
  692. "padding" => "16px 14px 0px 14px",
  693. "margin" => "0"
  694. ])
  695. );
  696. if(
  697. count($ddm) !== 0 &&
  698. strtolower(
  699. $this->fuckhtml
  700. ->getTextContent(
  701. $ddm[0]
  702. )
  703. ) == "people also search for"
  704. ){
  705. $as =
  706. $this->fuckhtml
  707. ->getElementsByTagName("a");
  708. foreach($as as $a){
  709. $out["related"][] =
  710. $this->fuckhtml
  711. ->getTextContent(
  712. $a
  713. );
  714. }
  715. continue;
  716. }
  717. //
  718. // Parse normal web results
  719. //
  720. // probe for website ellipsis shit
  721. $ellipsis =
  722. $this->fuckhtml
  723. ->getElementsByClassName(
  724. $this->getstyle([
  725. "text-overflow" => "ellipsis",
  726. "white-space" => "nowrap",
  727. "overflow" => "hidden"
  728. ])
  729. );
  730. if(count($ellipsis) < 1){
  731. // should not happen
  732. continue;
  733. }
  734. $title =
  735. $this->fuckhtml
  736. ->getElementsByTagName(
  737. "h3"
  738. );
  739. if(count($title) === 0){
  740. // should not happen
  741. continue;
  742. }
  743. $title =
  744. $this->fuckhtml
  745. ->getTextContent(
  746. $title[0]
  747. );
  748. // get URL
  749. $as =
  750. $this->fuckhtml
  751. ->getElementsByTagName(
  752. "a"
  753. );
  754. if(count($as) === 0){
  755. // should not happen
  756. continue;
  757. }
  758. $link =
  759. $this->unshiturl(
  760. $as[0]
  761. ["attributes"]
  762. ["href"]
  763. );
  764. // grep container separators
  765. $separator =
  766. $this->fuckhtml
  767. ->getElementsByClassName(
  768. $this->getstyle([
  769. "padding" => "16px 14px 12px"
  770. ])
  771. );
  772. if(count($separator) < 2){
  773. // should not happen
  774. continue;
  775. }
  776. $this->fuckhtml->load($separator[1]);
  777. $snippets =
  778. $this->fuckhtml
  779. ->getElementsByClassName(
  780. $this->getstyle([
  781. "white-space" => "pre-line",
  782. "word-wrap" => "break-word"
  783. ])
  784. );
  785. if(count($snippets) < 2){
  786. // should not happen
  787. continue;
  788. }
  789. // get description
  790. $description =
  791. $this->fuckhtml
  792. ->getTextContent(
  793. $snippets[1]
  794. );
  795. // get date from description
  796. $exp_description = explode(" · ", $description, 2);
  797. $date = null;
  798. if(count($exp_description) === 1){
  799. $description = $exp_description[0];
  800. }else{
  801. $date_probe = strtotime($exp_description[0]);
  802. if(
  803. strlen($exp_description[0]) <= 17 &&
  804. $date_probe !== false
  805. ){
  806. $date = $date_probe;
  807. $description = $exp_description[1];
  808. }
  809. }
  810. // get thumb
  811. $thumb_probe =
  812. $this->fuckhtml
  813. ->getElementsByTagName(
  814. "img"
  815. );
  816. // too lazy to fix this piece of shit
  817. // will probably break soon anyways idgaf
  818. /*
  819. if(count($thumb_probe) === 0){
  820. $thumb = [
  821. "ratio" => null,
  822. "url" => null
  823. ];
  824. }else{
  825. $thumb = [
  826. "ratio" => "1:1",
  827. "url" =>
  828. $this->getdimg(
  829. $thumb_probe[0]
  830. ["attributes"]
  831. ["id"]
  832. )
  833. ];
  834. }*/
  835. $thumb = [
  836. "ratio" => null,
  837. "url" => null
  838. ];
  839. // get sublinks
  840. $sublinks = [];
  841. foreach($as as $a){
  842. $this->fuckhtml->load($a);
  843. $probe =
  844. $this->fuckhtml
  845. ->getElementsByClassName(
  846. $this->getstyle([
  847. "color" => "#1558d6",
  848. "font-size" => "14px",
  849. "line-height" => "20px"
  850. ])
  851. );
  852. if(count($probe) !== 0){
  853. $sublinks[] = [
  854. "title" =>
  855. $this->titledots(
  856. $this->fuckhtml
  857. ->getTextContent(
  858. $probe[0]
  859. )
  860. ),
  861. "description" => null,
  862. "date" => null,
  863. "url" =>
  864. $this->unshiturl(
  865. $a["attributes"]["href"]
  866. )
  867. ];
  868. }
  869. }
  870. $out["web"][] = [
  871. "title" =>
  872. $this->titledots(
  873. $title
  874. ),
  875. "description" =>
  876. $this->titledots(
  877. $description
  878. ),
  879. "url" => $link,
  880. "date" => $date,
  881. "type" => "web",
  882. "thumb" => $thumb,
  883. "sublink" => $sublinks,
  884. "table" => []
  885. ];
  886. }
  887. return $out;
  888. }
  889. public function video($get){
  890. if($get["npt"]){
  891. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  892. $params = json_decode($params, true);
  893. }else{
  894. $search = $get["s"];
  895. $country = $get["country"];
  896. $nsfw = $get["nsfw"];
  897. $older = $get["older"];
  898. $newer = $get["newer"];
  899. $duration = $get["duration"];
  900. $quality = $get["quality"];
  901. $captions = $get["captions"];
  902. $proxy = $this->backend->get_ip();
  903. $params = [
  904. "q" => $search,
  905. "udm" => "7",
  906. "hl" => "en",
  907. "num" => 20
  908. ];
  909. // country
  910. if($country != "any"){
  911. $params["gl"] = $country;
  912. }
  913. // nsfw
  914. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  915. $tbs = [];
  916. // get date
  917. $older = $older === false ? null : date("m/d/Y", $older);
  918. $newer = $newer === false ? null : date("m/d/Y", $newer);
  919. if(
  920. $older !== null ||
  921. $newer !== null
  922. ){
  923. $tbs["cdr"] = "1";
  924. $tbs["cd_min"] = $newer;
  925. $tbs["cd_max"] = $older;
  926. }
  927. // duration
  928. if($duration != "any"){
  929. $tbs[] = "dur:" . $duration;
  930. }
  931. // quality
  932. if($quality != "any"){
  933. $tbs[] = "hq:" . $quality;
  934. }
  935. // captions
  936. if($captions != "any"){
  937. $tbs[] = "cc:" . $captions;
  938. }
  939. // append tbs
  940. if(count($tbs) !== 0){
  941. $params["tbs"] =
  942. implode(",", $tbs);
  943. }
  944. }
  945. try{
  946. $html =
  947. $this->get(
  948. $proxy,
  949. "https://www.google.com/search",
  950. $params
  951. );
  952. }catch(Exception $error){
  953. throw new Exception("Failed to get HTML");
  954. }
  955. if(!isset($params["start"])){
  956. $params["start"] = 0;
  957. }
  958. $params["start"] += 20;
  959. $this->fuckhtml->load($html);
  960. //
  961. // Parse web video page
  962. //
  963. $this->detect_sorry();
  964. // parse all <style> tags
  965. $this->parsestyles();
  966. // get javascript images
  967. $this->scrape_dimg($html);
  968. $this->scrape_imagearr($html);
  969. $out = [
  970. "status" => "ok",
  971. "npt" =>
  972. $this->backend->store(
  973. json_encode($params),
  974. "videos",
  975. $proxy
  976. ),
  977. "video" => [],
  978. "author" => [],
  979. "livestream" => [],
  980. "playlist" => [],
  981. "reel" => []
  982. ];
  983. $search_div =
  984. $this->fuckhtml
  985. ->getElementById(
  986. "center_col"
  987. );
  988. if($search_div === false){
  989. throw new Exception("Failed to grep search div");
  990. }
  991. $this->fuckhtml->load($search_div);
  992. $results =
  993. $this->fuckhtml
  994. ->getElementsByClassName(
  995. $this->getstyle([
  996. "margin" => "0px 0px 30px"
  997. ]),
  998. "div"
  999. );
  1000. foreach($results as $result){
  1001. $this->fuckhtml->load($result);
  1002. $url =
  1003. $this->fuckhtml
  1004. ->getElementsByTagName(
  1005. "a"
  1006. );
  1007. if(count($url) === 0){
  1008. // no url, weird, continue
  1009. continue;
  1010. }
  1011. $title =
  1012. $this->fuckhtml
  1013. ->getElementsByTagName(
  1014. "h3"
  1015. );
  1016. if(count($title) === 0){
  1017. // no title, weird, continue
  1018. continue;
  1019. }
  1020. // get description
  1021. $description =
  1022. $this->fuckhtml
  1023. ->getElementsByClassName(
  1024. $this->getstyle([
  1025. "-webkit-box-orient" => "vertical",
  1026. "display" => "-webkit-box",
  1027. "-webkit-line-clamp" => "2",
  1028. "overflow" => "hidden",
  1029. "word-break" => "break-word"
  1030. ]),
  1031. "div"
  1032. );
  1033. if(count($description) === 0){
  1034. $description = null;
  1035. }else{
  1036. $description =
  1037. html_entity_decode(
  1038. $this->titledots(
  1039. $this->fuckhtml
  1040. ->getTextContent(
  1041. $description[0]
  1042. )
  1043. )
  1044. );
  1045. }
  1046. // get author + date posted
  1047. $metadiv =
  1048. $this->fuckhtml
  1049. ->getElementsByClassName(
  1050. $this->getstyle([
  1051. "margin-top" => "12px"
  1052. ]),
  1053. "div"
  1054. );
  1055. $author = null;
  1056. $date = null;
  1057. if(count($metadiv) !== 0){
  1058. $metadiv =
  1059. explode(
  1060. "·",
  1061. $this->fuckhtml
  1062. ->getTextContent(
  1063. $metadiv[0]
  1064. )
  1065. );
  1066. if(count($metadiv) === 3){
  1067. $author = trim($metadiv[1]);
  1068. $date = strtotime(trim($metadiv[2]));
  1069. }elseif(count($metadiv) === 2){
  1070. $author = trim($metadiv[0]);
  1071. $date = strtotime(trim($metadiv[1]));
  1072. }
  1073. }
  1074. $thumb = [
  1075. "url" => null,
  1076. "ratio" => null
  1077. ];
  1078. $image =
  1079. $this->fuckhtml
  1080. ->getElementsByTagName(
  1081. "img"
  1082. );
  1083. $duration = null;
  1084. if(
  1085. count($image) !== 0 &&
  1086. isset($image[0]["attributes"]["id"])
  1087. ){
  1088. $thumb = [
  1089. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1090. "ratio" => "16:9"
  1091. ];
  1092. // get duration
  1093. $duration =
  1094. $this->fuckhtml
  1095. ->getElementsByClassName(
  1096. $this->getstyle([
  1097. "background-color" => "rgba(0,0,0,0.6)",
  1098. "color" => "#fff",
  1099. "fill" => "#fff"
  1100. ])
  1101. );
  1102. if(count($duration) !== 0){
  1103. $duration =
  1104. $this->hms2int(
  1105. $this->fuckhtml
  1106. ->getTextContent(
  1107. $duration[0]
  1108. ));
  1109. }else{
  1110. $duration = null;
  1111. }
  1112. }
  1113. $out["video"][] = [
  1114. "title" =>
  1115. $this->titledots(
  1116. $this->fuckhtml
  1117. ->getTextContent(
  1118. $title[0]
  1119. )
  1120. ),
  1121. "description" => $description,
  1122. "author" => [
  1123. "name" => $author,
  1124. "url" => null,
  1125. "avatar" => null
  1126. ],
  1127. "date" => $date,
  1128. "duration" => $duration,
  1129. "views" => null,
  1130. "thumb" => $thumb,
  1131. "url" =>
  1132. $this->fuckhtml
  1133. ->getTextContent(
  1134. $url[0]["attributes"]["href"]
  1135. )
  1136. ];
  1137. }
  1138. return $out;
  1139. }
  1140. public function news($get){
  1141. if($get["npt"]){
  1142. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1143. /*parse_str(
  1144. parse_url($req, PHP_URL_QUERY),
  1145. $search
  1146. );*/
  1147. try{
  1148. $html =
  1149. $this->get(
  1150. $proxy,
  1151. "https://www.google.com" . $req,
  1152. []
  1153. );
  1154. }catch(Exception $error){
  1155. throw new Exception("Failed to get HTML");
  1156. }
  1157. }else{
  1158. $search = $get["s"];
  1159. $country = $get["country"];
  1160. $nsfw = $get["nsfw"];
  1161. $older = $get["older"];
  1162. $newer = $get["newer"];
  1163. $sort = $get["sort"];
  1164. $proxy = $this->backend->get_ip();
  1165. $params = [
  1166. "q" => $search,
  1167. "tbm" => "nws",
  1168. "hl" => "en",
  1169. "num" => "20"
  1170. ];
  1171. // country
  1172. if($country != "any"){
  1173. $params["gl"] = $country;
  1174. }
  1175. // nsfw
  1176. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1177. $tbs = [];
  1178. // get date
  1179. $older = $older === false ? null : date("m/d/Y", $older);
  1180. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1181. if(
  1182. $older !== null ||
  1183. $newer !== null
  1184. ){
  1185. $tbs["cdr"] = "1";
  1186. $tbs["cd_min"] = $newer;
  1187. $tbs["cd_max"] = $older;
  1188. }
  1189. // relevance
  1190. if($sort == "date"){
  1191. $tbs["sbd"] = "1";
  1192. }
  1193. // append tbs
  1194. if(count($tbs) !== 0){
  1195. $params["tbs"] = "";
  1196. foreach($tbs as $key => $value){
  1197. $params["tbs"] .= $key . ":" . $value . ",";
  1198. }
  1199. $params["tbs"] = rtrim($params["tbs"], ",");
  1200. }
  1201. //$html = file_get_contents("scraper/google-news.html");
  1202. $html =
  1203. $this->get(
  1204. $proxy,
  1205. "https://www.google.com/search",
  1206. $params
  1207. );
  1208. }
  1209. $out = [
  1210. "status" => "ok",
  1211. "npt" => null,
  1212. "news" => []
  1213. ];
  1214. $this->fuckhtml->load($html);
  1215. $this->detect_sorry();
  1216. // get images
  1217. $this->scrape_dimg($html);
  1218. // parse styles
  1219. $this->parsestyles();
  1220. $center_col =
  1221. $this->fuckhtml
  1222. ->getElementById(
  1223. "center_col",
  1224. "div"
  1225. );
  1226. if($center_col === null){
  1227. throw new Exception("Could not grep result div");
  1228. }
  1229. $this->fuckhtml->load($center_col);
  1230. // get next page
  1231. $npt =
  1232. $this->fuckhtml
  1233. ->getElementById(
  1234. "pnnext",
  1235. "a"
  1236. );
  1237. if($npt !== false){
  1238. $out["npt"] =
  1239. $this->backend->store(
  1240. $this->fuckhtml
  1241. ->getTextContent(
  1242. $npt["attributes"]
  1243. ["href"]
  1244. ),
  1245. "news",
  1246. $proxy
  1247. );
  1248. }
  1249. $as =
  1250. $this->fuckhtml
  1251. ->getElementsByAttributeName(
  1252. "jsname",
  1253. "a"
  1254. );
  1255. foreach($as as $a){
  1256. $this->fuckhtml->load($a);
  1257. // get title
  1258. $title =
  1259. $this->fuckhtml
  1260. ->getElementsByAttributeValue(
  1261. "role",
  1262. "heading",
  1263. "div"
  1264. );
  1265. if(count($title) === 0){
  1266. continue;
  1267. }
  1268. $title =
  1269. $this->titledots(
  1270. $this->fuckhtml
  1271. ->getTextContent(
  1272. $title[0]
  1273. )
  1274. );
  1275. // get thumbnail
  1276. $image =
  1277. $this->fuckhtml
  1278. ->getElementsByAttributeName(
  1279. "id",
  1280. "img"
  1281. );
  1282. // check for padded title node, if found, we're inside a carousel
  1283. $probe =
  1284. $this->fuckhtml
  1285. ->getElementsByClassName(
  1286. $this->getstyle(
  1287. [
  1288. "padding" => "16px 16px 40px 16px"
  1289. ]
  1290. ),
  1291. "div"
  1292. );
  1293. if(count($probe) !== 0){
  1294. $probe = true;
  1295. }else{
  1296. $probe = false;
  1297. }
  1298. if(
  1299. count($image) !== 0 &&
  1300. !isset($image[0]["attributes"]["width"])
  1301. ){
  1302. $thumb = [
  1303. "url" =>
  1304. $this->getdimg(
  1305. $image[0]["attributes"]["id"]
  1306. ),
  1307. "ratio" => $probe === true ? "16:9" : "1:1"
  1308. ];
  1309. }else{
  1310. $thumb = [
  1311. "url" => null,
  1312. "ratio" => null
  1313. ];
  1314. }
  1315. $description = null;
  1316. if($probe === false){
  1317. $desc_divs =
  1318. $this->fuckhtml
  1319. ->getElementsByAttributeName(
  1320. "style",
  1321. "div"
  1322. );
  1323. foreach($desc_divs as $desc){
  1324. if(
  1325. strpos(
  1326. $desc["attributes"]["style"],
  1327. "margin-top:"
  1328. ) !== false
  1329. ){
  1330. $description =
  1331. $this->titledots(
  1332. $this->fuckhtml
  1333. ->getTextContent(
  1334. $desc
  1335. )
  1336. );
  1337. break;
  1338. }
  1339. }
  1340. }
  1341. // get author
  1342. $author =
  1343. $this->fuckhtml
  1344. ->getElementsByClassName(
  1345. $this->getstyle(
  1346. [
  1347. "overflow" => "hidden",
  1348. "text-align" => "left",
  1349. "text-overflow" => "ellipsis",
  1350. "white-space" => "nowrap",
  1351. "margin-bottom" => "8px"
  1352. ]
  1353. ),
  1354. "div"
  1355. );
  1356. if(count($author) !== 0){
  1357. $author =
  1358. $this->fuckhtml
  1359. ->getTextContent(
  1360. $author[0]
  1361. );
  1362. }else{
  1363. $author = null;
  1364. }
  1365. // get date
  1366. $date = null;
  1367. $date_div =
  1368. $this->fuckhtml
  1369. ->getElementsByAttributeName(
  1370. "style",
  1371. "div"
  1372. );
  1373. foreach($date_div as $d){
  1374. $this->fuckhtml->load($d);
  1375. $span =
  1376. $this->fuckhtml
  1377. ->getElementsByTagName(
  1378. "span"
  1379. );
  1380. if(
  1381. strpos(
  1382. $d["attributes"]["style"],
  1383. "bottom:"
  1384. ) !== false
  1385. ){
  1386. $date =
  1387. strtotime(
  1388. $this->fuckhtml
  1389. ->getTextContent(
  1390. $span[count($span) - 1]
  1391. )
  1392. );
  1393. break;
  1394. }
  1395. }
  1396. $out["news"][] = [
  1397. "title" => $title,
  1398. "author" => $author,
  1399. "description" => $description,
  1400. "date" => $date,
  1401. "thumb" => $thumb,
  1402. "url" =>
  1403. $this->unshiturl(
  1404. $a["attributes"]
  1405. ["href"]
  1406. )
  1407. ];
  1408. }
  1409. return $out;
  1410. }
  1411. public function image($get){
  1412. // generate parameters
  1413. if($get["npt"]){
  1414. [$params, $proxy] =
  1415. $this->backend->get(
  1416. $get["npt"],
  1417. "images"
  1418. );
  1419. $params = json_decode($params, true);
  1420. }else{
  1421. $search = $get["s"];
  1422. if(strlen($search) === 0){
  1423. throw new Exception("Search term is empty!");
  1424. }
  1425. $proxy = $this->backend->get_ip();
  1426. $country = $get["country"];
  1427. $nsfw = $get["nsfw"];
  1428. $time = $get["time"];
  1429. $size = $get["size"];
  1430. $ratio = $get["ratio"];
  1431. $color = $get["color"];
  1432. $type = $get["type"];
  1433. $format = $get["format"];
  1434. $rights = $get["rights"];
  1435. $params = [
  1436. "q" => $search,
  1437. "udm" => "2" // get images
  1438. ];
  1439. // country (image search uses cr instead of gl)
  1440. if($country != "any"){
  1441. $params["cr"] = "country" . strtoupper($country);
  1442. }
  1443. // nsfw
  1444. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1445. // generate tbs
  1446. $tbs = [];
  1447. // time
  1448. if($time != "any"){
  1449. $tbs["qdr"] = $time;
  1450. }
  1451. // size
  1452. if($size != "any"){
  1453. $params["imgsz"] = $size;
  1454. }
  1455. // ratio
  1456. if($ratio != "any"){
  1457. $params["imgar"] = $ratio;
  1458. }
  1459. // color
  1460. if($color != "any"){
  1461. if(
  1462. $color == "color" ||
  1463. $color == "trans"
  1464. ){
  1465. $params["imgc"] = $color;
  1466. }elseif($color == "bnw"){
  1467. $params["imgc"] = "gray";
  1468. }else{
  1469. $tbs["ic"] = "specific";
  1470. $tbs["isc"] = $color;
  1471. }
  1472. }
  1473. // type
  1474. if($type != "any"){
  1475. $tbs["itp"] = $type;
  1476. }
  1477. // format
  1478. if($format != "any"){
  1479. $params["as_filetype"] = $format;
  1480. }
  1481. // rights (tbs)
  1482. if($rights != "any"){
  1483. $tbs["sur"] = $rights;
  1484. }
  1485. // append tbs
  1486. if(count($tbs) !== 0){
  1487. $params["tbs"] = "";
  1488. foreach($tbs as $key => $value){
  1489. $params["tbs"] .= $key . ":" . $value . ",";
  1490. }
  1491. $params["tbs"] = rtrim($params["tbs"], ",");
  1492. }
  1493. }
  1494. /*
  1495. $handle = fopen("scraper/page.html", "r");
  1496. $html = fread($handle, filesize("scraper/page.html"));
  1497. fclose($handle);*/
  1498. try{
  1499. $html =
  1500. $this->get(
  1501. $proxy,
  1502. "https://www.google.com/search",
  1503. $params
  1504. );
  1505. }catch(Exception $error){
  1506. throw new Exception("Failed to get search page");
  1507. }
  1508. $this->fuckhtml->load($html);
  1509. $this->detect_sorry();
  1510. // get javascript images
  1511. $this->scrape_imagearr($html);
  1512. $out = [
  1513. "status" => "ok",
  1514. "npt" => null,
  1515. "image" => []
  1516. ];
  1517. $images =
  1518. $this->fuckhtml
  1519. ->getElementsByClassName(
  1520. "ivg-i",
  1521. "div"
  1522. );
  1523. foreach($images as $div){
  1524. $this->fuckhtml->load($div);
  1525. $image =
  1526. $this->fuckhtml
  1527. ->getElementsByTagName("img")[0];
  1528. // make sure we dont attempt to show an image we dont have data for
  1529. if(
  1530. isset($div["attributes"]["data-docid"]) &&
  1531. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1532. ){
  1533. $source =
  1534. $this->image_arr[
  1535. $div["attributes"]["data-docid"]
  1536. ];
  1537. }else{
  1538. continue;
  1539. }
  1540. $out["image"][] = [
  1541. "title" =>
  1542. $this->titledots(
  1543. $this->fuckhtml
  1544. ->getTextContent(
  1545. $image["attributes"]["alt"]
  1546. )
  1547. ),
  1548. "source" => $source,
  1549. "url" =>
  1550. $this->fuckhtml
  1551. ->getTextContent(
  1552. $div["attributes"]["data-lpage"]
  1553. )
  1554. ];
  1555. }
  1556. // as usual, no way to check if there is a next page reliably
  1557. if(count($out["image"]) > 50){
  1558. if(!isset($params["start"])){
  1559. $params["start"] = 10;
  1560. }else{
  1561. $params["start"] += 10;
  1562. }
  1563. $out["npt"] =
  1564. $this->backend
  1565. ->store(
  1566. json_encode($params),
  1567. "image",
  1568. $proxy
  1569. );
  1570. }
  1571. return $out;
  1572. }
  1573. private function scrape_dimg($html){
  1574. // get images loaded through javascript
  1575. $this->dimg = [];
  1576. preg_match_all(
  1577. '/function\(\){google\.ldi=({.*?});/',
  1578. $html,
  1579. $dimg
  1580. );
  1581. if(isset($dimg[1])){
  1582. foreach($dimg[1] as $i){
  1583. $tmp = json_decode($i, true);
  1584. foreach($tmp as $key => $value){
  1585. $this->dimg[$key] =
  1586. $this->unshit_thumb(
  1587. $value
  1588. );
  1589. }
  1590. }
  1591. }
  1592. // get additional javascript base64 images
  1593. preg_match_all(
  1594. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1595. $html,
  1596. $dimg
  1597. );
  1598. if(isset($dimg[1])){
  1599. for($i=0; $i<count($dimg[1]); $i++){
  1600. $delims = explode(",", $dimg[2][$i]);
  1601. $string =
  1602. $this->fuckhtml
  1603. ->parseJsString(
  1604. $dimg[1][$i]
  1605. );
  1606. foreach($delims as $delim){
  1607. $this->dimg[trim($delim, "'")] = $string;
  1608. }
  1609. }
  1610. }
  1611. }
  1612. private function scrape_imagearr($html){
  1613. // get image links arrays
  1614. preg_match_all(
  1615. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1616. $html,
  1617. $image_arr
  1618. );
  1619. $this->image_arr = [];
  1620. if(isset($image_arr[1])){
  1621. for($i=0; $i<count($image_arr[1]); $i++){
  1622. $original =
  1623. $this->fuckhtml
  1624. ->parseJsString(
  1625. $image_arr[5][$i]
  1626. );
  1627. if(
  1628. preg_match(
  1629. '/^x-raw-image/',
  1630. $original
  1631. )
  1632. ){
  1633. // only add thumbnail, google doesnt have OG resolution
  1634. $this->image_arr[$image_arr[1][$i]] = [
  1635. [
  1636. "url" =>
  1637. $this->unshit_thumb(
  1638. $this->fuckhtml
  1639. ->parseJsString(
  1640. $image_arr[2][$i]
  1641. )
  1642. ),
  1643. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1644. "height" => (int)$image_arr[6][$i]
  1645. ]
  1646. ];
  1647. continue;
  1648. }
  1649. $this->image_arr[$image_arr[1][$i]] =
  1650. [
  1651. [
  1652. "url" => $original,
  1653. "width" => (int)$image_arr[7][$i],
  1654. "height" => (int)$image_arr[6][$i]
  1655. ],
  1656. [
  1657. "url" =>
  1658. $this->unshit_thumb(
  1659. $this->fuckhtml
  1660. ->parseJsString(
  1661. $image_arr[2][$i]
  1662. )
  1663. ),
  1664. "width" => (int)$image_arr[4][$i],
  1665. "height" => (int)$image_arr[3][$i]
  1666. ]
  1667. ];
  1668. }
  1669. }
  1670. }
  1671. private function getdimg($dimg){
  1672. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1673. }
  1674. private function unshit_thumb($url, $get_bigger_res = false){
  1675. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1676. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1677. $parts = parse_url($url);
  1678. if(
  1679. isset($parts["host"]) &&
  1680. preg_match(
  1681. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1682. $parts["host"]
  1683. )
  1684. ){
  1685. parse_str($parts["query"], $params);
  1686. if(isset($params["q"])){
  1687. if($get_bigger_res){
  1688. // this method doesnt always work, but does work for wiki thumbnails
  1689. return
  1690. "https://" . $parts["host"] . "/images?q=tbn:" .
  1691. $this->base64url_encode(
  1692. substr(
  1693. $this->base64url_decode(
  1694. explode(
  1695. ":",
  1696. $params["q"])[1]
  1697. ),
  1698. 0,
  1699. 29
  1700. )
  1701. );
  1702. }else{
  1703. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1704. }
  1705. }
  1706. }
  1707. return $url;
  1708. }
  1709. private function parsestyles(){
  1710. $styles = [];
  1711. $style_div =
  1712. $this->fuckhtml
  1713. ->getElementsByTagName(
  1714. "style"
  1715. );
  1716. $raw_styles = "";
  1717. foreach($style_div as $style){
  1718. $raw_styles .= $style["innerHTML"];
  1719. }
  1720. // filter out media/keyframe queries
  1721. $raw_styles =
  1722. preg_replace(
  1723. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1724. "",
  1725. $raw_styles
  1726. );
  1727. // get styles
  1728. preg_match_all(
  1729. '/(.+?){([\S\s]*?)}/',
  1730. $raw_styles,
  1731. $matches
  1732. );
  1733. for($i=0; $i<count($matches[1]); $i++){
  1734. // get style values
  1735. preg_match_all(
  1736. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1737. $matches[2][$i],
  1738. $values_regex
  1739. );
  1740. $values = [];
  1741. for($k=0; $k<count($values_regex[1]); $k++){
  1742. $values[trim($values_regex[1][$k])] =
  1743. strtolower(trim($values_regex[2][$k]));
  1744. }
  1745. $names = explode(",", $matches[1][$i]);
  1746. // h1,h2,h3 will each get their own array index
  1747. foreach($names as $name){
  1748. $name = trim($name, "}\t\n\r\0\x0B");
  1749. foreach($values as $key => $value){
  1750. $styles[$name][$key] = $value;
  1751. }
  1752. }
  1753. }
  1754. foreach($styles as $key => $values){
  1755. $styles[$key]["_c"] = count($values);
  1756. }
  1757. $this->styles = $styles;
  1758. // get CSS colors
  1759. $this->css_colors = [];
  1760. if(isset($this->styles[":root"])){
  1761. foreach($this->styles[":root"] as $key => $value){
  1762. $this->css_colors[$value] = strtolower($key);
  1763. }
  1764. }
  1765. }
  1766. private function getstyle($styles){
  1767. $styles["_c"] = count($styles);
  1768. foreach($this->styles as $style_key => $style_values){
  1769. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1770. $style_key =
  1771. explode(" ", $style_key);
  1772. $style_key = $style_key[count($style_key) - 1];
  1773. return
  1774. ltrim(
  1775. str_replace(
  1776. [".", "#"],
  1777. " ",
  1778. $style_key
  1779. )
  1780. );
  1781. }
  1782. }
  1783. return false;
  1784. }
  1785. private function getcolorvar($color){
  1786. if(isset($this->css_colors[$color])){
  1787. return $this->css_colors[$color];
  1788. }
  1789. return null;
  1790. }
  1791. private function unshiturl($url, $return_size = false){
  1792. // decode
  1793. $url =
  1794. $this->fuckhtml
  1795. ->getTextContent(
  1796. $url
  1797. );
  1798. $url_parts = parse_url($url);
  1799. if(isset($url_parts["query"])){
  1800. parse_str($url_parts["query"], $query);
  1801. }else{
  1802. $query = [];
  1803. }
  1804. if(
  1805. !isset(
  1806. $url_parts["host"]
  1807. ) ||
  1808. stripos($url_parts["host"], "google.") !== false
  1809. ){
  1810. // no host, we have a tracking url
  1811. if(isset($query["imgurl"])){
  1812. $url = $query["imgurl"];
  1813. }
  1814. elseif(isset($query["q"])){
  1815. $url = $query["q"];
  1816. }
  1817. }
  1818. // rewrite URLs to remove extra tracking parameters
  1819. $domain = parse_url($url, PHP_URL_HOST);
  1820. if(
  1821. preg_match(
  1822. '/wikipedia.org$/',
  1823. $domain
  1824. )
  1825. ){
  1826. // rewrite wikipedia mobile URLs to desktop
  1827. $url =
  1828. $this->replacedomain(
  1829. $url,
  1830. preg_replace(
  1831. '/([a-z0-9]+)(\.m\.)/',
  1832. '$1.',
  1833. $domain
  1834. )
  1835. );
  1836. }
  1837. elseif(
  1838. preg_match(
  1839. '/imdb\.com$|youtube\.[^.]+$/',
  1840. $domain
  1841. )
  1842. ){
  1843. // rewrite imdb and youtube mobile URLs too
  1844. $url =
  1845. $this->replacedomain(
  1846. $url,
  1847. preg_replace(
  1848. '/^m\./',
  1849. "",
  1850. $domain
  1851. )
  1852. );
  1853. }
  1854. elseif(
  1855. preg_match(
  1856. '/play\.google\.[^.]+$/',
  1857. $domain
  1858. )
  1859. ){
  1860. // remove referrers from play.google.com
  1861. $u_query = parse_url($url, PHP_URL_QUERY);
  1862. if($u_query !== null){
  1863. parse_str($u_query, $u_query);
  1864. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  1865. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  1866. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  1867. $query = http_build_query($query);
  1868. $url =
  1869. str_replace(
  1870. $u_query,
  1871. $u_query,
  1872. $url
  1873. );
  1874. }
  1875. }
  1876. elseif(
  1877. preg_match(
  1878. '/twitter\.com$/',
  1879. $domain
  1880. )
  1881. ){
  1882. // remove more referrers from twitter.com
  1883. $u_query = parse_url($url, PHP_URL_QUERY);
  1884. if($u_query !== null){
  1885. parse_str($u_query, $u_query);
  1886. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  1887. $u_query = http_build_query($u_query);
  1888. $url =
  1889. str_replace(
  1890. $oldquery,
  1891. $u_query,
  1892. $url
  1893. );
  1894. }
  1895. }
  1896. elseif(
  1897. preg_match(
  1898. '/maps\.google\.[^.]+/',
  1899. $domain
  1900. )
  1901. ){
  1902. if(stripos($url, "maps?") !== false){
  1903. $u_query = parse_url($url, PHP_URL_QUERY);
  1904. if($u_query !== null){
  1905. parse_str($u_query, $u_query);
  1906. if(isset($u_query["daddr"])){
  1907. $url =
  1908. "https://maps.google.com/maps?daddr=" .
  1909. urlencode($u_query["daddr"]);
  1910. }
  1911. }
  1912. }
  1913. }
  1914. if($return_size){
  1915. return [
  1916. "url" => $url,
  1917. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  1918. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  1919. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  1920. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  1921. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  1922. ];
  1923. }
  1924. return $url;
  1925. }
  1926. private function replacedomain($url, $domain){
  1927. return
  1928. preg_replace(
  1929. '/(https?:\/\/)([^\/]+)/',
  1930. '$1' . $domain,
  1931. $url
  1932. );
  1933. }
  1934. private function titledots($title){
  1935. return trim($title, " .\t\n\r\0\x0B…");
  1936. }
  1937. private function hms2int($time){
  1938. $parts = explode(":", $time, 3);
  1939. $time = 0;
  1940. if(count($parts) === 3){
  1941. // hours
  1942. $time = $time + ((int)$parts[0] * 3600);
  1943. array_shift($parts);
  1944. }
  1945. if(count($parts) === 2){
  1946. // minutes
  1947. $time = $time + ((int)$parts[0] * 60);
  1948. array_shift($parts);
  1949. }
  1950. // seconds
  1951. $time = $time + (int)$parts[0];
  1952. return $time;
  1953. }
  1954. function base64url_decode($data){
  1955. $b64 = strtr($data, "-_", "+/");
  1956. $pad = strlen($b64) % 4;
  1957. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  1958. return base64_decode($b64);
  1959. }
  1960. function base64url_encode($data){
  1961. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  1962. }
  1963. private function detect_sorry(){
  1964. $captcha_form =
  1965. $this->fuckhtml
  1966. ->getElementById(
  1967. "captcha-form",
  1968. "form"
  1969. );
  1970. if($captcha_form !== false){
  1971. throw new Exception("Google returned a captcha");
  1972. }
  1973. }
  1974. }