google.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  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. $url =
  853. $this->unshiturl(
  854. $a["attributes"]["href"]
  855. );
  856. if(
  857. preg_match(
  858. '/^http/',
  859. $url
  860. )
  861. ){
  862. if(count($probe) !== 0){
  863. $sublinks[] = [
  864. "title" =>
  865. $this->titledots(
  866. $this->fuckhtml
  867. ->getTextContent(
  868. $probe[0]
  869. )
  870. ),
  871. "description" => null,
  872. "date" => null,
  873. "url" => $url
  874. ];
  875. }
  876. }
  877. }
  878. $out["web"][] = [
  879. "title" =>
  880. $this->titledots(
  881. $title
  882. ),
  883. "description" =>
  884. $this->titledots(
  885. $description
  886. ),
  887. "url" => $link,
  888. "date" => $date,
  889. "type" => "web",
  890. "thumb" => $thumb,
  891. "sublink" => $sublinks,
  892. "table" => []
  893. ];
  894. }
  895. return $out;
  896. }
  897. public function video($get){
  898. if($get["npt"]){
  899. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  900. $params = json_decode($params, true);
  901. }else{
  902. $search = $get["s"];
  903. $country = $get["country"];
  904. $nsfw = $get["nsfw"];
  905. $older = $get["older"];
  906. $newer = $get["newer"];
  907. $duration = $get["duration"];
  908. $quality = $get["quality"];
  909. $captions = $get["captions"];
  910. $proxy = $this->backend->get_ip();
  911. $params = [
  912. "q" => $search,
  913. "udm" => "7",
  914. "hl" => "en",
  915. "num" => 20
  916. ];
  917. // country
  918. if($country != "any"){
  919. $params["gl"] = $country;
  920. }
  921. // nsfw
  922. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  923. $tbs = [];
  924. // get date
  925. $older = $older === false ? null : date("m/d/Y", $older);
  926. $newer = $newer === false ? null : date("m/d/Y", $newer);
  927. if(
  928. $older !== null ||
  929. $newer !== null
  930. ){
  931. $tbs["cdr"] = "1";
  932. $tbs["cd_min"] = $newer;
  933. $tbs["cd_max"] = $older;
  934. }
  935. // duration
  936. if($duration != "any"){
  937. $tbs[] = "dur:" . $duration;
  938. }
  939. // quality
  940. if($quality != "any"){
  941. $tbs[] = "hq:" . $quality;
  942. }
  943. // captions
  944. if($captions != "any"){
  945. $tbs[] = "cc:" . $captions;
  946. }
  947. // append tbs
  948. if(count($tbs) !== 0){
  949. $params["tbs"] =
  950. implode(",", $tbs);
  951. }
  952. }
  953. try{
  954. $html =
  955. $this->get(
  956. $proxy,
  957. "https://www.google.com/search",
  958. $params
  959. );
  960. }catch(Exception $error){
  961. throw new Exception("Failed to get HTML");
  962. }
  963. if(!isset($params["start"])){
  964. $params["start"] = 0;
  965. }
  966. $params["start"] += 20;
  967. $this->fuckhtml->load($html);
  968. //
  969. // Parse web video page
  970. //
  971. $this->detect_sorry();
  972. // parse all <style> tags
  973. $this->parsestyles();
  974. // get javascript images
  975. $this->scrape_dimg($html);
  976. $this->scrape_imagearr($html);
  977. $out = [
  978. "status" => "ok",
  979. "npt" =>
  980. $this->backend->store(
  981. json_encode($params),
  982. "videos",
  983. $proxy
  984. ),
  985. "video" => [],
  986. "author" => [],
  987. "livestream" => [],
  988. "playlist" => [],
  989. "reel" => []
  990. ];
  991. $search_div =
  992. $this->fuckhtml
  993. ->getElementById(
  994. "center_col"
  995. );
  996. if($search_div === false){
  997. throw new Exception("Failed to grep search div");
  998. }
  999. $this->fuckhtml->load($search_div);
  1000. $results =
  1001. $this->fuckhtml
  1002. ->getElementsByClassName(
  1003. $this->getstyle([
  1004. "margin" => "0px 0px 30px"
  1005. ]),
  1006. "div"
  1007. );
  1008. foreach($results as $result){
  1009. $this->fuckhtml->load($result);
  1010. $url =
  1011. $this->fuckhtml
  1012. ->getElementsByTagName(
  1013. "a"
  1014. );
  1015. if(count($url) === 0){
  1016. // no url, weird, continue
  1017. continue;
  1018. }
  1019. $title =
  1020. $this->fuckhtml
  1021. ->getElementsByTagName(
  1022. "h3"
  1023. );
  1024. if(count($title) === 0){
  1025. // no title, weird, continue
  1026. continue;
  1027. }
  1028. // get description
  1029. $description =
  1030. $this->fuckhtml
  1031. ->getElementsByClassName(
  1032. $this->getstyle([
  1033. "-webkit-box-orient" => "vertical",
  1034. "display" => "-webkit-box",
  1035. "-webkit-line-clamp" => "2",
  1036. "overflow" => "hidden",
  1037. "word-break" => "break-word"
  1038. ]),
  1039. "div"
  1040. );
  1041. if(count($description) === 0){
  1042. $description = null;
  1043. }else{
  1044. $description =
  1045. html_entity_decode(
  1046. $this->titledots(
  1047. $this->fuckhtml
  1048. ->getTextContent(
  1049. $description[0]
  1050. )
  1051. )
  1052. );
  1053. }
  1054. // get author + date posted
  1055. $metadiv =
  1056. $this->fuckhtml
  1057. ->getElementsByClassName(
  1058. $this->getstyle([
  1059. "margin-top" => "12px"
  1060. ]),
  1061. "div"
  1062. );
  1063. $author = null;
  1064. $date = null;
  1065. if(count($metadiv) !== 0){
  1066. $metadiv =
  1067. explode(
  1068. "·",
  1069. $this->fuckhtml
  1070. ->getTextContent(
  1071. $metadiv[0]
  1072. )
  1073. );
  1074. if(count($metadiv) === 3){
  1075. $author = trim($metadiv[1]);
  1076. $date = strtotime(trim($metadiv[2]));
  1077. }elseif(count($metadiv) === 2){
  1078. $author = trim($metadiv[0]);
  1079. $date = strtotime(trim($metadiv[1]));
  1080. }
  1081. }
  1082. $thumb = [
  1083. "url" => null,
  1084. "ratio" => null
  1085. ];
  1086. $image =
  1087. $this->fuckhtml
  1088. ->getElementsByTagName(
  1089. "img"
  1090. );
  1091. $duration = null;
  1092. if(
  1093. count($image) !== 0 &&
  1094. isset($image[0]["attributes"]["id"])
  1095. ){
  1096. $thumb = [
  1097. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1098. "ratio" => "16:9"
  1099. ];
  1100. // get duration
  1101. $duration =
  1102. $this->fuckhtml
  1103. ->getElementsByClassName(
  1104. $this->getstyle([
  1105. "background-color" => "rgba(0,0,0,0.6)",
  1106. "color" => "#fff",
  1107. "fill" => "#fff"
  1108. ])
  1109. );
  1110. if(count($duration) !== 0){
  1111. $duration =
  1112. $this->hms2int(
  1113. $this->fuckhtml
  1114. ->getTextContent(
  1115. $duration[0]
  1116. ));
  1117. }else{
  1118. $duration = null;
  1119. }
  1120. }
  1121. $out["video"][] = [
  1122. "title" =>
  1123. $this->titledots(
  1124. $this->fuckhtml
  1125. ->getTextContent(
  1126. $title[0]
  1127. )
  1128. ),
  1129. "description" => $description,
  1130. "author" => [
  1131. "name" => $author,
  1132. "url" => null,
  1133. "avatar" => null
  1134. ],
  1135. "date" => $date,
  1136. "duration" => $duration,
  1137. "views" => null,
  1138. "thumb" => $thumb,
  1139. "url" =>
  1140. $this->fuckhtml
  1141. ->getTextContent(
  1142. $url[0]["attributes"]["href"]
  1143. )
  1144. ];
  1145. }
  1146. return $out;
  1147. }
  1148. public function news($get){
  1149. if($get["npt"]){
  1150. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1151. /*parse_str(
  1152. parse_url($req, PHP_URL_QUERY),
  1153. $search
  1154. );*/
  1155. try{
  1156. $html =
  1157. $this->get(
  1158. $proxy,
  1159. "https://www.google.com" . $req,
  1160. []
  1161. );
  1162. }catch(Exception $error){
  1163. throw new Exception("Failed to get HTML");
  1164. }
  1165. }else{
  1166. $search = $get["s"];
  1167. $country = $get["country"];
  1168. $nsfw = $get["nsfw"];
  1169. $older = $get["older"];
  1170. $newer = $get["newer"];
  1171. $sort = $get["sort"];
  1172. $proxy = $this->backend->get_ip();
  1173. $params = [
  1174. "q" => $search,
  1175. "tbm" => "nws",
  1176. "hl" => "en",
  1177. "num" => "20"
  1178. ];
  1179. // country
  1180. if($country != "any"){
  1181. $params["gl"] = $country;
  1182. }
  1183. // nsfw
  1184. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1185. $tbs = [];
  1186. // get date
  1187. $older = $older === false ? null : date("m/d/Y", $older);
  1188. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1189. if(
  1190. $older !== null ||
  1191. $newer !== null
  1192. ){
  1193. $tbs["cdr"] = "1";
  1194. $tbs["cd_min"] = $newer;
  1195. $tbs["cd_max"] = $older;
  1196. }
  1197. // relevance
  1198. if($sort == "date"){
  1199. $tbs["sbd"] = "1";
  1200. }
  1201. // append tbs
  1202. if(count($tbs) !== 0){
  1203. $params["tbs"] = "";
  1204. foreach($tbs as $key => $value){
  1205. $params["tbs"] .= $key . ":" . $value . ",";
  1206. }
  1207. $params["tbs"] = rtrim($params["tbs"], ",");
  1208. }
  1209. //$html = file_get_contents("scraper/google-news.html");
  1210. $html =
  1211. $this->get(
  1212. $proxy,
  1213. "https://www.google.com/search",
  1214. $params
  1215. );
  1216. }
  1217. $out = [
  1218. "status" => "ok",
  1219. "npt" => null,
  1220. "news" => []
  1221. ];
  1222. $this->fuckhtml->load($html);
  1223. $this->detect_sorry();
  1224. // get images
  1225. $this->scrape_dimg($html);
  1226. // parse styles
  1227. $this->parsestyles();
  1228. $center_col =
  1229. $this->fuckhtml
  1230. ->getElementById(
  1231. "center_col",
  1232. "div"
  1233. );
  1234. if($center_col === null){
  1235. throw new Exception("Could not grep result div");
  1236. }
  1237. $this->fuckhtml->load($center_col);
  1238. // get next page
  1239. $npt =
  1240. $this->fuckhtml
  1241. ->getElementById(
  1242. "pnnext",
  1243. "a"
  1244. );
  1245. if($npt !== false){
  1246. $out["npt"] =
  1247. $this->backend->store(
  1248. $this->fuckhtml
  1249. ->getTextContent(
  1250. $npt["attributes"]
  1251. ["href"]
  1252. ),
  1253. "news",
  1254. $proxy
  1255. );
  1256. }
  1257. $as =
  1258. $this->fuckhtml
  1259. ->getElementsByAttributeName(
  1260. "jsname",
  1261. "a"
  1262. );
  1263. foreach($as as $a){
  1264. $this->fuckhtml->load($a);
  1265. // get title
  1266. $title =
  1267. $this->fuckhtml
  1268. ->getElementsByAttributeValue(
  1269. "role",
  1270. "heading",
  1271. "div"
  1272. );
  1273. if(count($title) === 0){
  1274. continue;
  1275. }
  1276. $title =
  1277. $this->titledots(
  1278. $this->fuckhtml
  1279. ->getTextContent(
  1280. $title[0]
  1281. )
  1282. );
  1283. // get thumbnail
  1284. $image =
  1285. $this->fuckhtml
  1286. ->getElementsByAttributeName(
  1287. "id",
  1288. "img"
  1289. );
  1290. // check for padded title node, if found, we're inside a carousel
  1291. $probe =
  1292. $this->fuckhtml
  1293. ->getElementsByClassName(
  1294. $this->getstyle(
  1295. [
  1296. "padding" => "16px 16px 40px 16px"
  1297. ]
  1298. ),
  1299. "div"
  1300. );
  1301. if(count($probe) !== 0){
  1302. $probe = true;
  1303. }else{
  1304. $probe = false;
  1305. }
  1306. if(
  1307. count($image) !== 0 &&
  1308. !isset($image[0]["attributes"]["width"])
  1309. ){
  1310. $thumb = [
  1311. "url" =>
  1312. $this->getdimg(
  1313. $image[0]["attributes"]["id"]
  1314. ),
  1315. "ratio" => $probe === true ? "16:9" : "1:1"
  1316. ];
  1317. }else{
  1318. $thumb = [
  1319. "url" => null,
  1320. "ratio" => null
  1321. ];
  1322. }
  1323. $description = null;
  1324. if($probe === false){
  1325. $desc_divs =
  1326. $this->fuckhtml
  1327. ->getElementsByAttributeName(
  1328. "style",
  1329. "div"
  1330. );
  1331. foreach($desc_divs as $desc){
  1332. if(
  1333. strpos(
  1334. $desc["attributes"]["style"],
  1335. "margin-top:"
  1336. ) !== false
  1337. ){
  1338. $description =
  1339. $this->titledots(
  1340. $this->fuckhtml
  1341. ->getTextContent(
  1342. $desc
  1343. )
  1344. );
  1345. break;
  1346. }
  1347. }
  1348. }
  1349. // get author
  1350. $author =
  1351. $this->fuckhtml
  1352. ->getElementsByClassName(
  1353. $this->getstyle(
  1354. [
  1355. "overflow" => "hidden",
  1356. "text-align" => "left",
  1357. "text-overflow" => "ellipsis",
  1358. "white-space" => "nowrap",
  1359. "margin-bottom" => "8px"
  1360. ]
  1361. ),
  1362. "div"
  1363. );
  1364. if(count($author) !== 0){
  1365. $author =
  1366. $this->fuckhtml
  1367. ->getTextContent(
  1368. $author[0]
  1369. );
  1370. }else{
  1371. $author = null;
  1372. }
  1373. // get date
  1374. $date = null;
  1375. $date_div =
  1376. $this->fuckhtml
  1377. ->getElementsByAttributeName(
  1378. "style",
  1379. "div"
  1380. );
  1381. foreach($date_div as $d){
  1382. $this->fuckhtml->load($d);
  1383. $span =
  1384. $this->fuckhtml
  1385. ->getElementsByTagName(
  1386. "span"
  1387. );
  1388. if(
  1389. strpos(
  1390. $d["attributes"]["style"],
  1391. "bottom:"
  1392. ) !== false
  1393. ){
  1394. $date =
  1395. strtotime(
  1396. $this->fuckhtml
  1397. ->getTextContent(
  1398. $span[count($span) - 1]
  1399. )
  1400. );
  1401. break;
  1402. }
  1403. }
  1404. $out["news"][] = [
  1405. "title" => $title,
  1406. "author" => $author,
  1407. "description" => $description,
  1408. "date" => $date,
  1409. "thumb" => $thumb,
  1410. "url" =>
  1411. $this->unshiturl(
  1412. $a["attributes"]
  1413. ["href"]
  1414. )
  1415. ];
  1416. }
  1417. return $out;
  1418. }
  1419. public function image($get){
  1420. // generate parameters
  1421. if($get["npt"]){
  1422. [$params, $proxy] =
  1423. $this->backend->get(
  1424. $get["npt"],
  1425. "images"
  1426. );
  1427. $params = json_decode($params, true);
  1428. }else{
  1429. $search = $get["s"];
  1430. if(strlen($search) === 0){
  1431. throw new Exception("Search term is empty!");
  1432. }
  1433. $proxy = $this->backend->get_ip();
  1434. $country = $get["country"];
  1435. $nsfw = $get["nsfw"];
  1436. $time = $get["time"];
  1437. $size = $get["size"];
  1438. $ratio = $get["ratio"];
  1439. $color = $get["color"];
  1440. $type = $get["type"];
  1441. $format = $get["format"];
  1442. $rights = $get["rights"];
  1443. $params = [
  1444. "q" => $search,
  1445. "udm" => "2" // get images
  1446. ];
  1447. // country (image search uses cr instead of gl)
  1448. if($country != "any"){
  1449. $params["cr"] = "country" . strtoupper($country);
  1450. }
  1451. // nsfw
  1452. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1453. // generate tbs
  1454. $tbs = [];
  1455. // time
  1456. if($time != "any"){
  1457. $tbs["qdr"] = $time;
  1458. }
  1459. // size
  1460. if($size != "any"){
  1461. $params["imgsz"] = $size;
  1462. }
  1463. // ratio
  1464. if($ratio != "any"){
  1465. $params["imgar"] = $ratio;
  1466. }
  1467. // color
  1468. if($color != "any"){
  1469. if(
  1470. $color == "color" ||
  1471. $color == "trans"
  1472. ){
  1473. $params["imgc"] = $color;
  1474. }elseif($color == "bnw"){
  1475. $params["imgc"] = "gray";
  1476. }else{
  1477. $tbs["ic"] = "specific";
  1478. $tbs["isc"] = $color;
  1479. }
  1480. }
  1481. // type
  1482. if($type != "any"){
  1483. $tbs["itp"] = $type;
  1484. }
  1485. // format
  1486. if($format != "any"){
  1487. $params["as_filetype"] = $format;
  1488. }
  1489. // rights (tbs)
  1490. if($rights != "any"){
  1491. $tbs["sur"] = $rights;
  1492. }
  1493. // append tbs
  1494. if(count($tbs) !== 0){
  1495. $params["tbs"] = "";
  1496. foreach($tbs as $key => $value){
  1497. $params["tbs"] .= $key . ":" . $value . ",";
  1498. }
  1499. $params["tbs"] = rtrim($params["tbs"], ",");
  1500. }
  1501. }
  1502. /*
  1503. $handle = fopen("scraper/page.html", "r");
  1504. $html = fread($handle, filesize("scraper/page.html"));
  1505. fclose($handle);*/
  1506. try{
  1507. $html =
  1508. $this->get(
  1509. $proxy,
  1510. "https://www.google.com/search",
  1511. $params
  1512. );
  1513. }catch(Exception $error){
  1514. throw new Exception("Failed to get search page");
  1515. }
  1516. $this->fuckhtml->load($html);
  1517. $this->detect_sorry();
  1518. // get javascript images
  1519. $this->scrape_imagearr($html);
  1520. $out = [
  1521. "status" => "ok",
  1522. "npt" => null,
  1523. "image" => []
  1524. ];
  1525. $images =
  1526. $this->fuckhtml
  1527. ->getElementsByClassName(
  1528. "ivg-i",
  1529. "div"
  1530. );
  1531. foreach($images as $div){
  1532. $this->fuckhtml->load($div);
  1533. $image =
  1534. $this->fuckhtml
  1535. ->getElementsByTagName("img")[0];
  1536. // make sure we dont attempt to show an image we dont have data for
  1537. if(
  1538. isset($div["attributes"]["data-docid"]) &&
  1539. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1540. ){
  1541. $source =
  1542. $this->image_arr[
  1543. $div["attributes"]["data-docid"]
  1544. ];
  1545. }else{
  1546. continue;
  1547. }
  1548. $out["image"][] = [
  1549. "title" =>
  1550. $this->titledots(
  1551. $this->fuckhtml
  1552. ->getTextContent(
  1553. $image["attributes"]["alt"]
  1554. )
  1555. ),
  1556. "source" => $source,
  1557. "url" =>
  1558. $this->fuckhtml
  1559. ->getTextContent(
  1560. $div["attributes"]["data-lpage"]
  1561. )
  1562. ];
  1563. }
  1564. // as usual, no way to check if there is a next page reliably
  1565. if(count($out["image"]) > 50){
  1566. if(!isset($params["start"])){
  1567. $params["start"] = 10;
  1568. }else{
  1569. $params["start"] += 10;
  1570. }
  1571. $out["npt"] =
  1572. $this->backend
  1573. ->store(
  1574. json_encode($params),
  1575. "image",
  1576. $proxy
  1577. );
  1578. }
  1579. return $out;
  1580. }
  1581. private function scrape_dimg($html){
  1582. // get images loaded through javascript
  1583. $this->dimg = [];
  1584. preg_match_all(
  1585. '/function\(\){google\.ldi=({.*?});/',
  1586. $html,
  1587. $dimg
  1588. );
  1589. if(isset($dimg[1])){
  1590. foreach($dimg[1] as $i){
  1591. $tmp = json_decode($i, true);
  1592. foreach($tmp as $key => $value){
  1593. $this->dimg[$key] =
  1594. $this->unshit_thumb(
  1595. $value
  1596. );
  1597. }
  1598. }
  1599. }
  1600. // get additional javascript base64 images
  1601. preg_match_all(
  1602. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1603. $html,
  1604. $dimg
  1605. );
  1606. if(isset($dimg[1])){
  1607. for($i=0; $i<count($dimg[1]); $i++){
  1608. $delims = explode(",", $dimg[2][$i]);
  1609. $string =
  1610. $this->fuckhtml
  1611. ->parseJsString(
  1612. $dimg[1][$i]
  1613. );
  1614. foreach($delims as $delim){
  1615. $this->dimg[trim($delim, "'")] = $string;
  1616. }
  1617. }
  1618. }
  1619. }
  1620. private function scrape_imagearr($html){
  1621. // get image links arrays
  1622. preg_match_all(
  1623. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1624. $html,
  1625. $image_arr
  1626. );
  1627. $this->image_arr = [];
  1628. if(isset($image_arr[1])){
  1629. for($i=0; $i<count($image_arr[1]); $i++){
  1630. $original =
  1631. $this->fuckhtml
  1632. ->parseJsString(
  1633. $image_arr[5][$i]
  1634. );
  1635. if(
  1636. preg_match(
  1637. '/^x-raw-image/',
  1638. $original
  1639. )
  1640. ){
  1641. // only add thumbnail, google doesnt have OG resolution
  1642. $this->image_arr[$image_arr[1][$i]] = [
  1643. [
  1644. "url" =>
  1645. $this->unshit_thumb(
  1646. $this->fuckhtml
  1647. ->parseJsString(
  1648. $image_arr[2][$i]
  1649. )
  1650. ),
  1651. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1652. "height" => (int)$image_arr[6][$i]
  1653. ]
  1654. ];
  1655. continue;
  1656. }
  1657. $this->image_arr[$image_arr[1][$i]] =
  1658. [
  1659. [
  1660. "url" => $original,
  1661. "width" => (int)$image_arr[7][$i],
  1662. "height" => (int)$image_arr[6][$i]
  1663. ],
  1664. [
  1665. "url" =>
  1666. $this->unshit_thumb(
  1667. $this->fuckhtml
  1668. ->parseJsString(
  1669. $image_arr[2][$i]
  1670. )
  1671. ),
  1672. "width" => (int)$image_arr[4][$i],
  1673. "height" => (int)$image_arr[3][$i]
  1674. ]
  1675. ];
  1676. }
  1677. }
  1678. }
  1679. private function getdimg($dimg){
  1680. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1681. }
  1682. private function unshit_thumb($url, $get_bigger_res = false){
  1683. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1684. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1685. $parts = parse_url($url);
  1686. if(
  1687. isset($parts["host"]) &&
  1688. preg_match(
  1689. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1690. $parts["host"]
  1691. )
  1692. ){
  1693. parse_str($parts["query"], $params);
  1694. if(isset($params["q"])){
  1695. if($get_bigger_res){
  1696. // this method doesnt always work, but does work for wiki thumbnails
  1697. return
  1698. "https://" . $parts["host"] . "/images?q=tbn:" .
  1699. $this->base64url_encode(
  1700. substr(
  1701. $this->base64url_decode(
  1702. explode(
  1703. ":",
  1704. $params["q"])[1]
  1705. ),
  1706. 0,
  1707. 29
  1708. )
  1709. );
  1710. }else{
  1711. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1712. }
  1713. }
  1714. }
  1715. return $url;
  1716. }
  1717. private function parsestyles(){
  1718. $styles = [];
  1719. $style_div =
  1720. $this->fuckhtml
  1721. ->getElementsByTagName(
  1722. "style"
  1723. );
  1724. $raw_styles = "";
  1725. foreach($style_div as $style){
  1726. $raw_styles .= $style["innerHTML"];
  1727. }
  1728. // filter out media/keyframe queries
  1729. $raw_styles =
  1730. preg_replace(
  1731. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1732. "",
  1733. $raw_styles
  1734. );
  1735. // get styles
  1736. preg_match_all(
  1737. '/(.+?){([\S\s]*?)}/',
  1738. $raw_styles,
  1739. $matches
  1740. );
  1741. for($i=0; $i<count($matches[1]); $i++){
  1742. // get style values
  1743. preg_match_all(
  1744. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1745. $matches[2][$i],
  1746. $values_regex
  1747. );
  1748. $values = [];
  1749. for($k=0; $k<count($values_regex[1]); $k++){
  1750. $values[trim($values_regex[1][$k])] =
  1751. strtolower(trim($values_regex[2][$k]));
  1752. }
  1753. $names = explode(",", $matches[1][$i]);
  1754. // h1,h2,h3 will each get their own array index
  1755. foreach($names as $name){
  1756. $name = trim($name, "}\t\n\r\0\x0B");
  1757. foreach($values as $key => $value){
  1758. $styles[$name][$key] = $value;
  1759. }
  1760. }
  1761. }
  1762. foreach($styles as $key => $values){
  1763. $styles[$key]["_c"] = count($values);
  1764. }
  1765. $this->styles = $styles;
  1766. // get CSS colors
  1767. $this->css_colors = [];
  1768. if(isset($this->styles[":root"])){
  1769. foreach($this->styles[":root"] as $key => $value){
  1770. $this->css_colors[$value] = strtolower($key);
  1771. }
  1772. }
  1773. }
  1774. private function getstyle($styles){
  1775. $styles["_c"] = count($styles);
  1776. foreach($this->styles as $style_key => $style_values){
  1777. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1778. $style_key =
  1779. explode(" ", $style_key);
  1780. $style_key = $style_key[count($style_key) - 1];
  1781. return
  1782. ltrim(
  1783. str_replace(
  1784. [".", "#"],
  1785. " ",
  1786. $style_key
  1787. )
  1788. );
  1789. }
  1790. }
  1791. return false;
  1792. }
  1793. private function getcolorvar($color){
  1794. if(isset($this->css_colors[$color])){
  1795. return $this->css_colors[$color];
  1796. }
  1797. return null;
  1798. }
  1799. private function unshiturl($url, $return_size = false){
  1800. // decode
  1801. $url =
  1802. $this->fuckhtml
  1803. ->getTextContent(
  1804. $url
  1805. );
  1806. $url_parts = parse_url($url);
  1807. if(isset($url_parts["query"])){
  1808. parse_str($url_parts["query"], $query);
  1809. }else{
  1810. $query = [];
  1811. }
  1812. if(
  1813. !isset(
  1814. $url_parts["host"]
  1815. ) ||
  1816. stripos($url_parts["host"], "google.") !== false
  1817. ){
  1818. // no host, we have a tracking url
  1819. if(isset($query["imgurl"])){
  1820. $url = $query["imgurl"];
  1821. }
  1822. elseif(isset($query["q"])){
  1823. $url = $query["q"];
  1824. }
  1825. }
  1826. // rewrite URLs to remove extra tracking parameters
  1827. $domain = parse_url($url, PHP_URL_HOST);
  1828. if(
  1829. preg_match(
  1830. '/wikipedia.org$/',
  1831. $domain
  1832. )
  1833. ){
  1834. // rewrite wikipedia mobile URLs to desktop
  1835. $url =
  1836. $this->replacedomain(
  1837. $url,
  1838. preg_replace(
  1839. '/([a-z0-9]+)(\.m\.)/',
  1840. '$1.',
  1841. $domain
  1842. )
  1843. );
  1844. }
  1845. elseif(
  1846. preg_match(
  1847. '/imdb\.com$|youtube\.[^.]+$/',
  1848. $domain
  1849. )
  1850. ){
  1851. // rewrite imdb and youtube mobile URLs too
  1852. $url =
  1853. $this->replacedomain(
  1854. $url,
  1855. preg_replace(
  1856. '/^m\./',
  1857. "",
  1858. $domain
  1859. )
  1860. );
  1861. }
  1862. elseif(
  1863. preg_match(
  1864. '/play\.google\.[^.]+$/',
  1865. $domain
  1866. )
  1867. ){
  1868. // remove referrers from play.google.com
  1869. $u_query = parse_url($url, PHP_URL_QUERY);
  1870. if($u_query !== null){
  1871. parse_str($u_query, $u_query);
  1872. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  1873. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  1874. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  1875. $query = http_build_query($query);
  1876. $url =
  1877. str_replace(
  1878. $u_query,
  1879. $u_query,
  1880. $url
  1881. );
  1882. }
  1883. }
  1884. elseif(
  1885. preg_match(
  1886. '/twitter\.com$/',
  1887. $domain
  1888. )
  1889. ){
  1890. // remove more referrers from twitter.com
  1891. $u_query = parse_url($url, PHP_URL_QUERY);
  1892. if($u_query !== null){
  1893. parse_str($u_query, $u_query);
  1894. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  1895. $u_query = http_build_query($u_query);
  1896. $url =
  1897. str_replace(
  1898. $oldquery,
  1899. $u_query,
  1900. $url
  1901. );
  1902. }
  1903. }
  1904. elseif(
  1905. preg_match(
  1906. '/maps\.google\.[^.]+/',
  1907. $domain
  1908. )
  1909. ){
  1910. if(stripos($url, "maps?") !== false){
  1911. $u_query = parse_url($url, PHP_URL_QUERY);
  1912. if($u_query !== null){
  1913. parse_str($u_query, $u_query);
  1914. if(isset($u_query["daddr"])){
  1915. $url =
  1916. "https://maps.google.com/maps?daddr=" .
  1917. urlencode($u_query["daddr"]);
  1918. }
  1919. }
  1920. }
  1921. }
  1922. if($return_size){
  1923. return [
  1924. "url" => $url,
  1925. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  1926. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  1927. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  1928. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  1929. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  1930. ];
  1931. }
  1932. return $url;
  1933. }
  1934. private function replacedomain($url, $domain){
  1935. return
  1936. preg_replace(
  1937. '/(https?:\/\/)([^\/]+)/',
  1938. '$1' . $domain,
  1939. $url
  1940. );
  1941. }
  1942. private function titledots($title){
  1943. return trim($title, " .\t\n\r\0\x0B…");
  1944. }
  1945. private function hms2int($time){
  1946. $parts = explode(":", $time, 3);
  1947. $time = 0;
  1948. if(count($parts) === 3){
  1949. // hours
  1950. $time = $time + ((int)$parts[0] * 3600);
  1951. array_shift($parts);
  1952. }
  1953. if(count($parts) === 2){
  1954. // minutes
  1955. $time = $time + ((int)$parts[0] * 60);
  1956. array_shift($parts);
  1957. }
  1958. // seconds
  1959. $time = $time + (int)$parts[0];
  1960. return $time;
  1961. }
  1962. function base64url_decode($data){
  1963. $b64 = strtr($data, "-_", "+/");
  1964. $pad = strlen($b64) % 4;
  1965. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  1966. return base64_decode($b64);
  1967. }
  1968. function base64url_encode($data){
  1969. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  1970. }
  1971. private function detect_sorry(){
  1972. $captcha_form =
  1973. $this->fuckhtml
  1974. ->getElementById(
  1975. "captcha-form",
  1976. "form"
  1977. );
  1978. if($captcha_form !== false){
  1979. throw new Exception("Google returned a captcha");
  1980. }
  1981. }
  1982. }