google.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  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 = [], $use_lynx = false){
  495. $curlproc = curl_init();
  496. if($use_lynx === false){
  497. $headers = [
  498. "User-Agent: " . config::USER_AGENT,
  499. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  500. "Accept-Language: en-US,en;q=0.5",
  501. "Accept-Encoding: gzip",
  502. "DNT: 1",
  503. //"Cookie: SOCS=CAESNQgCEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjQwMzE3LjA4X3AwGgJlbiAEGgYIgM7orwY",
  504. "Connection: keep-alive",
  505. "Upgrade-Insecure-Requests: 1",
  506. "Sec-Fetch-Dest: document",
  507. "Sec-Fetch-Mode: navigate",
  508. "Sec-Fetch-Site: none",
  509. "Sec-Fetch-User: ?1",
  510. "Priority: u=1",
  511. "TE: trailers"
  512. ];
  513. // use http2
  514. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  515. }else{
  516. $headers = [
  517. "Accept: text/html, text/plain, text/sgml, */*;q=0.01",
  518. "Accept-Encoding: gzip, compress, bzip2",
  519. "Accept-Language: en",
  520. "User-Agent: Lynx/2.9.0dev.12 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/3.7.8"
  521. ];
  522. }
  523. if($get !== []){
  524. $get = http_build_query($get);
  525. $url .= "?" . $get;
  526. }
  527. curl_setopt($curlproc, CURLOPT_URL, $url);
  528. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  529. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  530. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  531. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  532. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  533. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  534. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  535. // follow redirects
  536. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  537. $this->backend->assign_proxy($curlproc, $proxy);
  538. $data = curl_exec($curlproc);
  539. if(curl_errno($curlproc)){
  540. throw new Exception(curl_error($curlproc));
  541. }
  542. curl_close($curlproc);
  543. if($use_lynx){
  544. return mb_convert_encoding($data, "UTF-8", "ISO-8859-1");
  545. }
  546. return $data;
  547. }
  548. private function scrape_dimg($html){
  549. // get images loaded through javascript
  550. $this->dimg = [];
  551. preg_match_all(
  552. '/function\(\){google\.ldi=({.*?});/',
  553. $html,
  554. $dimg
  555. );
  556. if(isset($dimg[1])){
  557. foreach($dimg[1] as $i){
  558. $tmp = json_decode($i, true);
  559. foreach($tmp as $key => $value){
  560. $this->dimg[$key] =
  561. $this->unshit_thumb(
  562. $value
  563. );
  564. }
  565. }
  566. }
  567. // get additional javascript base64 images
  568. preg_match_all(
  569. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  570. $html,
  571. $dimg
  572. );
  573. if(isset($dimg[1])){
  574. for($i=0; $i<count($dimg[1]); $i++){
  575. $delims = explode(",", $dimg[2][$i]);
  576. $string =
  577. $this->fuckhtml
  578. ->parseJsString(
  579. $dimg[1][$i]
  580. );
  581. foreach($delims as $delim){
  582. $this->dimg[trim($delim, "'")] = $string;
  583. }
  584. }
  585. }
  586. }
  587. private function scrape_imagearr($html){
  588. // get image links arrays
  589. preg_match_all(
  590. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  591. $html,
  592. $image_arr
  593. );
  594. $this->image_arr = [];
  595. if(isset($image_arr[1])){
  596. for($i=0; $i<count($image_arr[1]); $i++){
  597. $original =
  598. $this->fuckhtml
  599. ->parseJsString(
  600. $image_arr[5][$i]
  601. );
  602. if(
  603. preg_match(
  604. '/^x-raw-image/',
  605. $original
  606. )
  607. ){
  608. // only add thumbnail, google doesnt have OG resolution
  609. $this->image_arr[$image_arr[1][$i]] = [
  610. [
  611. "url" =>
  612. $this->unshit_thumb(
  613. $this->fuckhtml
  614. ->parseJsString(
  615. $image_arr[2][$i]
  616. )
  617. ),
  618. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  619. "height" => (int)$image_arr[6][$i]
  620. ]
  621. ];
  622. continue;
  623. }
  624. $this->image_arr[$image_arr[1][$i]] =
  625. [
  626. [
  627. "url" => $original,
  628. "width" => (int)$image_arr[7][$i],
  629. "height" => (int)$image_arr[6][$i]
  630. ],
  631. [
  632. "url" =>
  633. $this->unshit_thumb(
  634. $this->fuckhtml
  635. ->parseJsString(
  636. $image_arr[2][$i]
  637. )
  638. ),
  639. "width" => (int)$image_arr[4][$i],
  640. "height" => (int)$image_arr[3][$i]
  641. ]
  642. ];
  643. }
  644. }
  645. }
  646. private function getdimg($dimg){
  647. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  648. }
  649. private function unshit_thumb($url){
  650. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  651. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  652. $parts = parse_url($url);
  653. if(
  654. isset($parts["host"]) &&
  655. preg_match(
  656. '/tbn.*\.gstatic\.com/',
  657. $parts["host"]
  658. )
  659. ){
  660. parse_str($parts["query"], $params);
  661. if(isset($params["q"])){
  662. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  663. }
  664. }
  665. return $url;
  666. }
  667. private function parsestyles(){
  668. $styles = [];
  669. $style_div =
  670. $this->fuckhtml
  671. ->getElementsByTagName(
  672. "style"
  673. );
  674. $raw_styles = "";
  675. foreach($style_div as $style){
  676. $raw_styles .= $style["innerHTML"];
  677. }
  678. // filter out media/keyframe queries
  679. $raw_styles =
  680. preg_replace(
  681. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  682. "",
  683. $raw_styles
  684. );
  685. // get styles
  686. preg_match_all(
  687. '/(.+?){([\S\s]*?)}/',
  688. $raw_styles,
  689. $matches
  690. );
  691. for($i=0; $i<count($matches[1]); $i++){
  692. // get style values
  693. preg_match_all(
  694. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  695. $matches[2][$i],
  696. $values_regex
  697. );
  698. $values = [];
  699. for($k=0; $k<count($values_regex[1]); $k++){
  700. $values[trim($values_regex[1][$k])] =
  701. strtolower(trim($values_regex[2][$k]));
  702. }
  703. $names = explode(",", $matches[1][$i]);
  704. // h1,h2,h3 will each get their own array index
  705. foreach($names as $name){
  706. $name = trim($name, "}\t\n\r\0\x0B");
  707. foreach($values as $key => $value){
  708. $styles[$name][$key] = $value;
  709. }
  710. }
  711. }
  712. foreach($styles as $key => $values){
  713. $styles[$key]["_c"] = count($values);
  714. }
  715. $this->styles = $styles;
  716. // get CSS colors
  717. $this->css_colors = [];
  718. if(isset($this->styles[":root"])){
  719. foreach($this->styles[":root"] as $key => $value){
  720. $this->css_colors[$value] = strtolower($key);
  721. }
  722. }
  723. }
  724. private function getstyle($styles){
  725. $styles["_c"] = count($styles);
  726. foreach($this->styles as $style_key => $style_values){
  727. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  728. $style_key =
  729. explode(" ", $style_key);
  730. $style_key = $style_key[count($style_key) - 1];
  731. return
  732. ltrim(
  733. str_replace(
  734. [".", "#"],
  735. " ",
  736. $style_key
  737. )
  738. );
  739. }
  740. }
  741. return false;
  742. }
  743. private function getcolorvar($color){
  744. if(isset($this->css_colors[$color])){
  745. return $this->css_colors[$color];
  746. }
  747. return null;
  748. }
  749. public function web($get){
  750. if($get["npt"]){
  751. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  752. try{
  753. $html =
  754. $this->get(
  755. $proxy,
  756. "https://www.google.com" . $get,
  757. [],
  758. true
  759. );
  760. }catch(Exception $error){
  761. throw new Exception("Failed to get HTML");
  762. }
  763. }else{
  764. $search = $get["s"];
  765. $country = $get["country"];
  766. $nsfw = $get["nsfw"];
  767. $lang = $get["lang"];
  768. $older = $get["older"];
  769. $newer = $get["newer"];
  770. $spellcheck = $get["spellcheck"];
  771. $proxy = $this->backend->get_ip();
  772. $offset = 0;
  773. $params = [
  774. "q" => $search,
  775. "hl" => "en",
  776. "num" => 20
  777. ];
  778. // country
  779. if($country != "any"){
  780. $params["gl"] = $country;
  781. }
  782. // nsfw
  783. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  784. // language
  785. if($lang != "any"){
  786. $params["lr"] = "lang_" . $lang;
  787. }
  788. // generate tbs
  789. $tbs = [];
  790. // get date
  791. $older = $older === false ? null : date("m/d/Y", $older);
  792. $newer = $newer === false ? null : date("m/d/Y", $newer);
  793. if(
  794. $older !== null ||
  795. $newer !== null
  796. ){
  797. $tbs["cdr"] = "1";
  798. $tbs["cd_min"] = $newer;
  799. $tbs["cd_max"] = $older;
  800. }
  801. // spellcheck filter
  802. if($spellcheck == "no"){
  803. $params["nfpr"] = "1";
  804. }
  805. if(count($tbs) !== 0){
  806. $params["tbs"] = "";
  807. foreach($tbs as $key => $value){
  808. $params["tbs"] .= $key . ":" . $value . ",";
  809. }
  810. $params["tbs"] = rtrim($params["tbs"], ",");
  811. }
  812. try{
  813. $html =
  814. $this->get(
  815. $proxy,
  816. "https://www.google.com/search",
  817. $params,
  818. true
  819. );
  820. }catch(Exception $error){
  821. throw new Exception("Failed to get HTML");
  822. }
  823. //$html = file_get_contents("scraper/google.html");
  824. }
  825. $out = [
  826. "status" => "ok",
  827. "spelling" => [
  828. "type" => "no_correction",
  829. "using" => null,
  830. "correction" => null
  831. ],
  832. "npt" => null,
  833. "answer" => [],
  834. "web" => [],
  835. "image" => [],
  836. "video" => [],
  837. "news" => [],
  838. "related" => []
  839. ];
  840. $this->fuckhtml->load($html);
  841. $this->detect_sorry();
  842. $this->parsestyles();
  843. $boxes =
  844. $this->fuckhtml
  845. ->getElementsByClassName(
  846. $this->getstyle([
  847. "border" => "thin solid #dadce0",
  848. "padding" => "12px 16px 12px 16px",
  849. "margin-bottom" => "10px",
  850. "font-family" => "sans-serif"
  851. ]),
  852. "div"
  853. );
  854. $skip_next = false;
  855. // get next page token
  856. $npt =
  857. $this->fuckhtml
  858. ->getElementsByClassName(
  859. $this->getstyle([
  860. "border" => "thin solid #dadce0",
  861. "color" => "#70757a",
  862. "font-size" => "14px",
  863. "text-align" => "center",
  864. "table-layout" => "fixed",
  865. "width" => "100%"
  866. ]),
  867. "table"
  868. );
  869. if(count($npt) !== 0){
  870. $this->fuckhtml->load($npt[0]);
  871. $as =
  872. $this->fuckhtml
  873. ->getElementsByTagName(
  874. "a"
  875. );
  876. foreach($as as $a){
  877. $text =
  878. $this->fuckhtml
  879. ->getTextContent(
  880. $a
  881. );
  882. if(
  883. $text == "Next&nbsp;>" ||
  884. $text == ">"
  885. ){
  886. $out["npt"] =
  887. $this->backend->store(
  888. $this->fuckhtml
  889. ->getTextContent(
  890. $a["attributes"]["href"]
  891. ),
  892. "web",
  893. $proxy
  894. );
  895. }
  896. }
  897. $this->fuckhtml->load($html);
  898. }
  899. $first_box = true;
  900. foreach($boxes as $box){
  901. $this->fuckhtml->load($box);
  902. if($first_box){
  903. //
  904. // Probe for word correction
  905. //
  906. $first_box = false;
  907. $txt =
  908. $this->fuckhtml
  909. ->getTextContent($box);
  910. if(
  911. preg_match(
  912. '/^Showing results for /',
  913. $txt
  914. )
  915. ){
  916. $as =
  917. $this->fuckhtml
  918. ->getElementsByTagName(
  919. "a"
  920. );
  921. if(count($as) === 2){
  922. $out["spelling"] = [
  923. "type" => "including",
  924. "using" =>
  925. $this->fuckhtml
  926. ->getTextContent(
  927. $as[0]
  928. ),
  929. "correction" =>
  930. $this->fuckhtml
  931. ->getTextContent(
  932. $as[1]
  933. )
  934. ];
  935. }
  936. continue;
  937. }
  938. }
  939. // probe for custom container
  940. $container_title =
  941. $this->fuckhtml
  942. ->getElementsByClassName(
  943. $this->getstyle([
  944. "font-weight" => "bold"
  945. ])
  946. );
  947. if(count($container_title) !== 0){
  948. $container_title =
  949. strtolower(
  950. $this->fuckhtml
  951. ->getTextContent(
  952. $container_title[0]
  953. )
  954. );
  955. if($container_title == "images"){
  956. //
  957. // Parse image carousel
  958. //
  959. $images =
  960. $this->fuckhtml
  961. ->getElementsByClassName(
  962. $this->getstyle([
  963. "display" => "inline-block",
  964. "padding" => "2px",
  965. "padding-bottom" => "4px"
  966. ]),
  967. "a"
  968. );
  969. foreach($images as $image){
  970. $this->fuckhtml->load($image);
  971. $image_data =
  972. $this->unshiturl(
  973. $image["attributes"]["href"],
  974. true
  975. );
  976. $img =
  977. $this->fuckhtml
  978. ->getElementsByTagName(
  979. "img"
  980. )[0];
  981. $out["image"][] = [
  982. "title" =>
  983. $this->titledots(
  984. $this->fuckhtml
  985. ->getTextContent(
  986. $img["attributes"]["alt"]
  987. )
  988. ),
  989. "source" => [
  990. [
  991. "url" => $image_data["url"],
  992. "width" => $image_data["image_width"],
  993. "height" => $image_data["image_height"]
  994. ],
  995. [
  996. "url" =>
  997. $this->fuckhtml
  998. ->getTextContent(
  999. $img["attributes"]["src"]
  1000. ),
  1001. "width" => $image_data["thumb_width"],
  1002. "height" => $image_data["thumb_height"]
  1003. ]
  1004. ],
  1005. "url" => $image_data["ref"]
  1006. ];
  1007. }
  1008. continue;
  1009. }
  1010. if(
  1011. $container_title == "related searches" ||
  1012. $container_title == "people also search for"
  1013. ){
  1014. $as =
  1015. $this->fuckhtml
  1016. ->getElementsByClassName(
  1017. $this->getstyle([
  1018. "color" => "#202124",
  1019. "font-size" => "13px",
  1020. "line-height" => "20px"
  1021. ]),
  1022. "span"
  1023. );
  1024. foreach($as as $a){
  1025. $out["related"][] =
  1026. $this->fuckhtml
  1027. ->getTextContent(
  1028. $a
  1029. );
  1030. }
  1031. continue;
  1032. }
  1033. }
  1034. // probe for website link
  1035. $link =
  1036. $this->fuckhtml
  1037. ->getElementsByClassName(
  1038. $this->getstyle([
  1039. "color" => "#1967d2",
  1040. "font-size" => "18px",
  1041. "line-height" => "24px"
  1042. ]),
  1043. "a"
  1044. );
  1045. if(count($link) !== 0){
  1046. //
  1047. // Parse search result
  1048. //
  1049. $this->fuckhtml->load($link[0]);
  1050. $title =
  1051. $this->fuckhtml
  1052. ->getElementsByClassName(
  1053. $this->getstyle([
  1054. "color" => "#1967d2",
  1055. "font-size" => "18px",
  1056. "line-height" => "24px"
  1057. ]),
  1058. "span"
  1059. );
  1060. if(count($title) === 0){
  1061. continue;
  1062. }
  1063. $this->fuckhtml->load($box);
  1064. $sublinks = [];
  1065. $table = [];
  1066. $categories =
  1067. $this->fuckhtml
  1068. ->getElementsByClassName(
  1069. $this->getstyle([
  1070. "color" => "#202124",
  1071. "font-size" => "13px",
  1072. "line-height" => "20px"
  1073. ]),
  1074. "span"
  1075. );
  1076. $i = 0;
  1077. foreach($categories as $category){
  1078. $this->fuckhtml->load($category);
  1079. // probe for sublinks
  1080. $subs =
  1081. $this->fuckhtml
  1082. ->getElementsByClassName(
  1083. $this->getstyle([
  1084. "color" => "#1967d2"
  1085. ]),
  1086. "a"
  1087. );
  1088. if(count($subs) !== 0){
  1089. foreach($subs as $sub){
  1090. $url =
  1091. $this->unshiturl(
  1092. $this->fuckhtml
  1093. ->getTextContent(
  1094. $sub["attributes"]["href"]
  1095. )
  1096. );
  1097. if(
  1098. preg_match(
  1099. '/^https?:\/\//',
  1100. $url
  1101. )
  1102. ){
  1103. $sublinks[] = [
  1104. "title" =>
  1105. $this->titledots(
  1106. $this->fuckhtml
  1107. ->getTextContent(
  1108. $sub
  1109. )
  1110. ),
  1111. "description" => null,
  1112. "url" =>
  1113. $this->unshiturl(
  1114. $this->fuckhtml
  1115. ->getTextContent(
  1116. $sub["attributes"]["href"]
  1117. )
  1118. ),
  1119. "date" => null
  1120. ];
  1121. }
  1122. }
  1123. unset($categories[$i]);
  1124. }
  1125. $i++;
  1126. }
  1127. // get description & date
  1128. $date = null;
  1129. $categories = array_values($categories);
  1130. //print_r($categories);
  1131. $c = count($categories) - 1;
  1132. $description =
  1133. $this->fuckhtml
  1134. ->getTextContent(
  1135. $categories[$c]
  1136. );
  1137. // remove last category since we're done with it
  1138. unset($categories[$c]);
  1139. // probe for date
  1140. $description_tmp = explode("·", $description, 2);
  1141. $date_tmp = strtotime(trim($description_tmp[0]));
  1142. if(
  1143. count($description_tmp) === 2 &&
  1144. strlen($description_tmp[0]) <= 20 &&
  1145. $date_tmp !== false
  1146. ){
  1147. $description =
  1148. ltrim(
  1149. $this->titledots(
  1150. $description_tmp[1]
  1151. )
  1152. );
  1153. $date = $date_tmp;
  1154. }else{
  1155. $description =
  1156. $this->titledots(
  1157. $description
  1158. );
  1159. }
  1160. // remaining categories should all be greytext
  1161. if(count($categories) !== 0){
  1162. $texts =
  1163. explode(
  1164. "·",
  1165. preg_replace(
  1166. '/\s+/',
  1167. " ",
  1168. $this->fuckhtml
  1169. ->getTextContent(
  1170. $categories[0]
  1171. )
  1172. )
  1173. );
  1174. foreach($texts as $text){
  1175. $text = trim($text);
  1176. if(
  1177. preg_match(
  1178. '/^Rating ([0-9.]+)(?: \(([0-9,]+)\))?/',
  1179. $text,
  1180. $rating
  1181. )
  1182. ){
  1183. $table["Rating"] = $rating[1];
  1184. if(isset($rating[2])){
  1185. $table["Rating"] .= " (" . $rating[2] . " votes)";
  1186. }
  1187. continue;
  1188. }
  1189. if(stripos($text, "stock") !== false){
  1190. $table["Stock"] = $text;
  1191. continue;
  1192. }
  1193. }
  1194. }
  1195. $out["web"][] = [
  1196. "title" =>
  1197. $this->titledots(
  1198. $this->fuckhtml
  1199. ->getTextContent(
  1200. $title[0]
  1201. )
  1202. ),
  1203. "description" => $description,
  1204. "url" =>
  1205. $this->unshiturl(
  1206. $link[0]["attributes"]["href"]
  1207. ),
  1208. "date" => $date,
  1209. "type" => "web",
  1210. "thumb" => [
  1211. "url" => null,
  1212. "ratio" => null
  1213. ],
  1214. "sublink" => $sublinks,
  1215. "table" => $table
  1216. ];
  1217. continue;
  1218. }
  1219. // parse wikipedia heads
  1220. $wiki_title =
  1221. $this->fuckhtml
  1222. ->getElementsByClassName(
  1223. $this->getstyle([
  1224. "color" => "#202124",
  1225. "font-size" => "18px",
  1226. "line-height" => "24px"
  1227. ]),
  1228. "span"
  1229. );
  1230. if(count($wiki_title) !== 0){
  1231. $wiki_title =
  1232. $this->fuckhtml
  1233. ->getTextContent(
  1234. $wiki_title[0]
  1235. );
  1236. if($wiki_title == "See results about"){
  1237. // ignore
  1238. continue;
  1239. }
  1240. if($wiki_title == "Top stories"){
  1241. //
  1242. // Parse news
  1243. //
  1244. $tds =
  1245. $this->fuckhtml
  1246. ->getElementsByTagName(
  1247. "td"
  1248. );
  1249. foreach($tds as $td){
  1250. $this->fuckhtml->load($td);
  1251. $a =
  1252. $this->fuckhtml
  1253. ->getElementsByTagName(
  1254. "a"
  1255. );
  1256. if(count($a) === 0){
  1257. continue;
  1258. }
  1259. $title =
  1260. $this->fuckhtml
  1261. ->getElementsByClassName(
  1262. $this->getstyle([
  1263. "color" => "#1967d2"
  1264. ]),
  1265. "span"
  1266. );
  1267. if(count($title) === 0){
  1268. continue;
  1269. }
  1270. $date = null;
  1271. $meta_div =
  1272. $this->fuckhtml
  1273. ->getElementsByClassName(
  1274. $this->getstyle([
  1275. "color" => "#70757a",
  1276. "font-size" => "13px",
  1277. "line-height" => "20px"
  1278. ]),
  1279. "span"
  1280. );
  1281. $meta_div =
  1282. explode(
  1283. "·",
  1284. $this->fuckhtml
  1285. ->getTextContent(
  1286. $meta_div[count($meta_div) - 1]
  1287. ),
  1288. 2
  1289. );
  1290. if(count($meta_div) === 2){
  1291. $date = strtotime($meta_div[count($meta_div) - 1]);
  1292. if($date === false){
  1293. $date = null;
  1294. }
  1295. }
  1296. $out["news"][] = [
  1297. "title" =>
  1298. $this->titledots(
  1299. $this->fuckhtml
  1300. ->getTextContent(
  1301. $title[0]
  1302. )
  1303. ),
  1304. "description" => null,
  1305. "date" => $date,
  1306. "thumb" => [
  1307. "url" => null,
  1308. "ratio" => null
  1309. ],
  1310. "url" =>
  1311. $this->unshiturl(
  1312. $a[0]["attributes"]["href"]
  1313. )
  1314. ];
  1315. }
  1316. continue;
  1317. }
  1318. //
  1319. // Parse wikipedia heads
  1320. //
  1321. $table_div =
  1322. $this->fuckhtml
  1323. ->getElementsByTagName(
  1324. "table"
  1325. );
  1326. if(count($table_div) === 0){
  1327. continue;
  1328. }
  1329. $this->fuckhtml->load($table_div[0]);
  1330. // remove table from box
  1331. $box["innerHTML"] =
  1332. str_replace(
  1333. $table_div[0]["outerHTML"],
  1334. "",
  1335. $box["innerHTML"]
  1336. );
  1337. // find wiki image
  1338. $thumb = null;
  1339. $img =
  1340. $this->fuckhtml
  1341. ->getElementsByTagName(
  1342. "img"
  1343. );
  1344. if(count($img) !== 0){
  1345. $thumb =
  1346. $this->fuckhtml
  1347. ->getTextContent(
  1348. $img[0]["attributes"]["src"]
  1349. );
  1350. }
  1351. $tds =
  1352. $this->fuckhtml
  1353. ->getElementsByTagName(
  1354. "td"
  1355. );
  1356. $description = [];
  1357. foreach($tds as $td){
  1358. // probe for subtitle
  1359. $this->fuckhtml->load($td);
  1360. $subtext =
  1361. $this->fuckhtml
  1362. ->getElementsByClassName(
  1363. $this->getstyle([
  1364. "color" => "#70757a",
  1365. "font-size" => "13px",
  1366. "line-height" => "20px"
  1367. ])
  1368. );
  1369. if(count($subtext) !== 0){
  1370. $description[] = [
  1371. "type" => "quote",
  1372. "value" =>
  1373. $this->fuckhtml
  1374. ->getTextContent(
  1375. $subtext[0]
  1376. )
  1377. ];
  1378. break;
  1379. }
  1380. }
  1381. $this->fuckhtml->load($box);
  1382. // probe for word definition
  1383. $lists =
  1384. $this->fuckhtml
  1385. ->getElementsByTagName(
  1386. "ol"
  1387. );
  1388. if(count($lists) !== 0){
  1389. $description = [];
  1390. foreach($lists as $list){
  1391. $box["innerHTML"] =
  1392. explode(
  1393. $list["outerHTML"],
  1394. $box["innerHTML"],
  1395. 2
  1396. );
  1397. if(
  1398. count($box["innerHTML"]) === 1 ||
  1399. trim($box["innerHTML"][0]) == ""
  1400. ){
  1401. break;
  1402. }
  1403. $description[] = [
  1404. "type" => "title",
  1405. "value" =>
  1406. $this->fuckhtml
  1407. ->getTextContent(
  1408. $box["innerHTML"][0]
  1409. )
  1410. ];
  1411. $this->fuckhtml->load($list);
  1412. $lis =
  1413. $this->fuckhtml
  1414. ->getElementsByTagName(
  1415. "li"
  1416. );
  1417. $increment = 1;
  1418. foreach($lis as $li){
  1419. $this->fuckhtml->load($li);
  1420. $list_items =
  1421. $this->fuckhtml
  1422. ->getElementsByClassName(
  1423. $this->getstyle([
  1424. "color" => "#202124",
  1425. "font-size" => "13px",
  1426. "line-height" => "20px"
  1427. ])
  1428. );
  1429. $first_item = true;
  1430. foreach($list_items as $it){
  1431. if($first_item){
  1432. $first_item = false;
  1433. $c = count($description);
  1434. if(
  1435. $c !== 0 &&
  1436. $description[$c - 1]["type"] == "text"
  1437. ){
  1438. $description[$c - 1]["value"] .=
  1439. "\n\n" .
  1440. $increment . ". " . $this->fuckhtml
  1441. ->getTextContent(
  1442. $it
  1443. );
  1444. }else{
  1445. $description[] = [
  1446. "type" => "text",
  1447. "value" =>
  1448. $increment . ". " . $this->fuckhtml
  1449. ->getTextContent(
  1450. $it
  1451. )
  1452. ];
  1453. }
  1454. }else{
  1455. $description[] = [
  1456. "type" => "quote",
  1457. "value" =>
  1458. $this->fuckhtml
  1459. ->getTextContent(
  1460. $it
  1461. )
  1462. ];
  1463. }
  1464. $increment++;
  1465. }
  1466. }
  1467. $box["innerHTML"] = $box["innerHTML"][1];
  1468. }
  1469. $out["answer"][] = [
  1470. "title" => $wiki_title,
  1471. "description" => $description,
  1472. "url" => null,
  1473. "thumb" => null,
  1474. "table" => [],
  1475. "sublink" => []
  1476. ];
  1477. continue;
  1478. }
  1479. // get separator between description and facts
  1480. $separator =
  1481. $this->fuckhtml
  1482. ->getElementsByClassName(
  1483. $this->getstyle([
  1484. "height" => "4px"
  1485. ]),
  1486. "div"
  1487. );
  1488. $box_html = [];
  1489. $table = [];
  1490. if(count($separator) !== 0){
  1491. $box_html =
  1492. explode(
  1493. $separator[0]["outerHTML"],
  1494. $box["innerHTML"],
  1495. 2
  1496. );
  1497. if(count($box_html) === 2){
  1498. $box["innerHTML"] = $box_html[0];
  1499. }
  1500. $this->fuckhtml->load($box_html[1]);
  1501. // get all facts
  1502. $facts =
  1503. $this->fuckhtml
  1504. ->getElementsByTagName(
  1505. "div"
  1506. );
  1507. foreach($facts as $fact){
  1508. if($fact["level"] !== 1){ continue; }
  1509. $fact =
  1510. explode(
  1511. ":",
  1512. $this->fuckhtml
  1513. ->getTextContent(
  1514. $fact
  1515. )
  1516. );
  1517. $table[trim(preg_replace('/\s+/', " ", $fact[0]))] =
  1518. trim(preg_replace('/\s+/', " ", $fact[1]));
  1519. }
  1520. $this->fuckhtml->load($box);
  1521. }
  1522. // remove wikipedia link
  1523. $wiki_link =
  1524. $this->fuckhtml
  1525. ->getElementsByClassName(
  1526. $this->getstyle([
  1527. "color" => "#1967d2"
  1528. ]),
  1529. "a"
  1530. );
  1531. $url = null;
  1532. if(count($wiki_link) !== 0){
  1533. foreach($wiki_link as $link){
  1534. if(
  1535. strtolower(
  1536. $this->fuckhtml
  1537. ->getTextContent(
  1538. $link
  1539. )
  1540. ) == "wikipedia"
  1541. ){
  1542. $box["innerHTML"] =
  1543. str_replace(
  1544. $link["outerHTML"],
  1545. "",
  1546. $box["innerHTML"]
  1547. );
  1548. $url =
  1549. $this->unshiturl(
  1550. $link["attributes"]["href"]
  1551. );
  1552. $this->fuckhtml->load($box);
  1553. break;
  1554. }
  1555. }
  1556. }
  1557. // remains of box should be description
  1558. $description[] = [
  1559. "type" => "text",
  1560. "value" =>
  1561. $this->titledots(
  1562. $this->fuckhtml
  1563. ->getTextContent(
  1564. $box
  1565. )
  1566. )
  1567. ];
  1568. $out["answer"][] = [
  1569. "title" => $wiki_title,
  1570. "description" => $description,
  1571. "url" => $url,
  1572. "thumb" => $thumb,
  1573. "table" => $table,
  1574. "sublink" => []
  1575. ];
  1576. }
  1577. }
  1578. return $out;
  1579. }
  1580. public function video($get){
  1581. if($get["npt"]){
  1582. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1583. $params = json_decode($params, true);
  1584. }else{
  1585. $search = $get["s"];
  1586. $country = $get["country"];
  1587. $nsfw = $get["nsfw"];
  1588. $older = $get["older"];
  1589. $newer = $get["newer"];
  1590. $duration = $get["duration"];
  1591. $quality = $get["quality"];
  1592. $captions = $get["captions"];
  1593. $proxy = $this->backend->get_ip();
  1594. $params = [
  1595. "q" => $search,
  1596. "udm" => "7",
  1597. "hl" => "en",
  1598. "num" => 20
  1599. ];
  1600. // country
  1601. if($country != "any"){
  1602. $params["gl"] = $country;
  1603. }
  1604. // nsfw
  1605. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1606. $tbs = [];
  1607. // get date
  1608. $older = $older === false ? null : date("m/d/Y", $older);
  1609. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1610. if(
  1611. $older !== null ||
  1612. $newer !== null
  1613. ){
  1614. $tbs["cdr"] = "1";
  1615. $tbs["cd_min"] = $newer;
  1616. $tbs["cd_max"] = $older;
  1617. }
  1618. // duration
  1619. if($duration != "any"){
  1620. $tbs[] = "dur:" . $duration;
  1621. }
  1622. // quality
  1623. if($quality != "any"){
  1624. $tbs[] = "hq:" . $quality;
  1625. }
  1626. // captions
  1627. if($captions != "any"){
  1628. $tbs[] = "cc:" . $captions;
  1629. }
  1630. // append tbs
  1631. if(count($tbs) !== 0){
  1632. $params["tbs"] =
  1633. implode(",", $tbs);
  1634. }
  1635. }
  1636. try{
  1637. $html =
  1638. $this->get(
  1639. $proxy,
  1640. "https://www.google.com/search",
  1641. $params
  1642. );
  1643. }catch(Exception $error){
  1644. throw new Exception("Failed to get HTML");
  1645. }
  1646. if(!isset($params["start"])){
  1647. $params["start"] = 0;
  1648. }
  1649. $params["start"] += 20;
  1650. $this->fuckhtml->load($html);
  1651. //
  1652. // Parse web video page
  1653. //
  1654. $this->detect_sorry();
  1655. // parse all <style> tags
  1656. $this->parsestyles();
  1657. // get javascript images
  1658. $this->scrape_dimg($html);
  1659. $this->scrape_imagearr($html);
  1660. $out = [
  1661. "status" => "ok",
  1662. "npt" =>
  1663. $this->backend->store(
  1664. json_encode($params),
  1665. "videos",
  1666. $proxy
  1667. ),
  1668. "video" => [],
  1669. "author" => [],
  1670. "livestream" => [],
  1671. "playlist" => [],
  1672. "reel" => []
  1673. ];
  1674. $search_div =
  1675. $this->fuckhtml
  1676. ->getElementById(
  1677. "center_col"
  1678. );
  1679. if($search_div === false){
  1680. throw new Exception("Failed to grep search div");
  1681. }
  1682. $this->fuckhtml->load($search_div);
  1683. $results =
  1684. $this->fuckhtml
  1685. ->getElementsByClassName(
  1686. $this->getstyle([
  1687. "margin" => "0px 0px 30px"
  1688. ]),
  1689. "div"
  1690. );
  1691. foreach($results as $result){
  1692. $this->fuckhtml->load($result);
  1693. $url =
  1694. $this->fuckhtml
  1695. ->getElementsByTagName(
  1696. "a"
  1697. );
  1698. if(count($url) === 0){
  1699. // no url, weird, continue
  1700. continue;
  1701. }
  1702. $title =
  1703. $this->fuckhtml
  1704. ->getElementsByTagName(
  1705. "h3"
  1706. );
  1707. if(count($title) === 0){
  1708. // no title, weird, continue
  1709. continue;
  1710. }
  1711. // get description
  1712. $description =
  1713. $this->fuckhtml
  1714. ->getElementsByClassName(
  1715. $this->getstyle([
  1716. "-webkit-box-orient" => "vertical",
  1717. "display" => "-webkit-box",
  1718. "-webkit-line-clamp" => "2",
  1719. "overflow" => "hidden",
  1720. "word-break" => "break-word"
  1721. ]),
  1722. "div"
  1723. );
  1724. if(count($description) === 0){
  1725. $description = null;
  1726. }else{
  1727. $description =
  1728. html_entity_decode(
  1729. $this->titledots(
  1730. $this->fuckhtml
  1731. ->getTextContent(
  1732. $description[0]
  1733. )
  1734. )
  1735. );
  1736. }
  1737. // get author + date posted
  1738. $metadiv =
  1739. $this->fuckhtml
  1740. ->getElementsByClassName(
  1741. $this->getstyle([
  1742. "margin-top" => "12px"
  1743. ]),
  1744. "div"
  1745. );
  1746. $author = null;
  1747. $date = null;
  1748. if(count($metadiv) !== 0){
  1749. $metadiv =
  1750. explode(
  1751. "·",
  1752. $this->fuckhtml
  1753. ->getTextContent(
  1754. $metadiv[0]
  1755. )
  1756. );
  1757. if(count($metadiv) === 3){
  1758. $author = trim($metadiv[1]);
  1759. $date = strtotime(trim($metadiv[2]));
  1760. }elseif(count($metadiv) === 2){
  1761. $author = trim($metadiv[0]);
  1762. $date = strtotime(trim($metadiv[1]));
  1763. }
  1764. }
  1765. $thumb = [
  1766. "url" => null,
  1767. "ratio" => null
  1768. ];
  1769. $image =
  1770. $this->fuckhtml
  1771. ->getElementsByTagName(
  1772. "img"
  1773. );
  1774. $duration = null;
  1775. if(
  1776. count($image) !== 0 &&
  1777. isset($image[0]["attributes"]["id"])
  1778. ){
  1779. $thumb = [
  1780. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1781. "ratio" => "16:9"
  1782. ];
  1783. // get duration
  1784. $duration =
  1785. $this->fuckhtml
  1786. ->getElementsByClassName(
  1787. $this->getstyle([
  1788. "background-color" => "rgba(0,0,0,0.6)",
  1789. "color" => "#fff",
  1790. "fill" => "#fff"
  1791. ])
  1792. );
  1793. if(count($duration) !== 0){
  1794. $duration =
  1795. $this->hms2int(
  1796. $this->fuckhtml
  1797. ->getTextContent(
  1798. $duration[0]
  1799. ));
  1800. }else{
  1801. $duration = null;
  1802. }
  1803. }
  1804. $out["video"][] = [
  1805. "title" =>
  1806. $this->titledots(
  1807. $this->fuckhtml
  1808. ->getTextContent(
  1809. $title[0]
  1810. )
  1811. ),
  1812. "description" => $description,
  1813. "author" => [
  1814. "name" => $author,
  1815. "url" => null,
  1816. "avatar" => null
  1817. ],
  1818. "date" => $date,
  1819. "duration" => $duration,
  1820. "views" => null,
  1821. "thumb" => $thumb,
  1822. "url" =>
  1823. $this->fuckhtml
  1824. ->getTextContent(
  1825. $url[0]["attributes"]["href"]
  1826. )
  1827. ];
  1828. }
  1829. return $out;
  1830. }
  1831. public function news($get){
  1832. if($get["npt"]){
  1833. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1834. /*parse_str(
  1835. parse_url($req, PHP_URL_QUERY),
  1836. $search
  1837. );*/
  1838. try{
  1839. $html =
  1840. $this->get(
  1841. $proxy,
  1842. "https://www.google.com" . $req,
  1843. []
  1844. );
  1845. }catch(Exception $error){
  1846. throw new Exception("Failed to get HTML");
  1847. }
  1848. }else{
  1849. $search = $get["s"];
  1850. $country = $get["country"];
  1851. $nsfw = $get["nsfw"];
  1852. $older = $get["older"];
  1853. $newer = $get["newer"];
  1854. $sort = $get["sort"];
  1855. $proxy = $this->backend->get_ip();
  1856. $params = [
  1857. "q" => $search,
  1858. "tbm" => "nws",
  1859. "hl" => "en",
  1860. "num" => "20"
  1861. ];
  1862. // country
  1863. if($country != "any"){
  1864. $params["gl"] = $country;
  1865. }
  1866. // nsfw
  1867. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1868. $tbs = [];
  1869. // get date
  1870. $older = $older === false ? null : date("m/d/Y", $older);
  1871. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1872. if(
  1873. $older !== null ||
  1874. $newer !== null
  1875. ){
  1876. $tbs["cdr"] = "1";
  1877. $tbs["cd_min"] = $newer;
  1878. $tbs["cd_max"] = $older;
  1879. }
  1880. // relevance
  1881. if($sort == "date"){
  1882. $tbs["sbd"] = "1";
  1883. }
  1884. // append tbs
  1885. if(count($tbs) !== 0){
  1886. $params["tbs"] = "";
  1887. foreach($tbs as $key => $value){
  1888. $params["tbs"] .= $key . ":" . $value . ",";
  1889. }
  1890. $params["tbs"] = rtrim($params["tbs"], ",");
  1891. }
  1892. //$html = file_get_contents("scraper/google-news.html");
  1893. $html =
  1894. $this->get(
  1895. $proxy,
  1896. "https://www.google.com/search",
  1897. $params
  1898. );
  1899. }
  1900. $out = [
  1901. "status" => "ok",
  1902. "npt" => null,
  1903. "news" => []
  1904. ];
  1905. $this->fuckhtml->load($html);
  1906. $this->detect_sorry();
  1907. // get images
  1908. $this->scrape_dimg($html);
  1909. // parse styles
  1910. $this->parsestyles();
  1911. $center_col =
  1912. $this->fuckhtml
  1913. ->getElementById(
  1914. "center_col",
  1915. "div"
  1916. );
  1917. if($center_col === null){
  1918. throw new Exception("Could not grep result div");
  1919. }
  1920. $this->fuckhtml->load($center_col);
  1921. // get next page
  1922. $npt =
  1923. $this->fuckhtml
  1924. ->getElementById(
  1925. "pnnext",
  1926. "a"
  1927. );
  1928. if($npt !== false){
  1929. $out["npt"] =
  1930. $this->backend->store(
  1931. $this->fuckhtml
  1932. ->getTextContent(
  1933. $npt["attributes"]
  1934. ["href"]
  1935. ),
  1936. "news",
  1937. $proxy
  1938. );
  1939. }
  1940. $as =
  1941. $this->fuckhtml
  1942. ->getElementsByAttributeName(
  1943. "jsname",
  1944. "a"
  1945. );
  1946. foreach($as as $a){
  1947. $this->fuckhtml->load($a);
  1948. // get title
  1949. $title =
  1950. $this->fuckhtml
  1951. ->getElementsByAttributeValue(
  1952. "role",
  1953. "heading",
  1954. "div"
  1955. );
  1956. if(count($title) === 0){
  1957. continue;
  1958. }
  1959. $title =
  1960. $this->titledots(
  1961. $this->fuckhtml
  1962. ->getTextContent(
  1963. $title[0]
  1964. )
  1965. );
  1966. // get thumbnail
  1967. $image =
  1968. $this->fuckhtml
  1969. ->getElementsByAttributeName(
  1970. "id",
  1971. "img"
  1972. );
  1973. // check for padded title node, if found, we're inside a carousel
  1974. $probe =
  1975. $this->fuckhtml
  1976. ->getElementsByClassName(
  1977. $this->getstyle(
  1978. [
  1979. "padding" => "16px 16px 40px 16px"
  1980. ]
  1981. ),
  1982. "div"
  1983. );
  1984. if(count($probe) !== 0){
  1985. $probe = true;
  1986. }else{
  1987. $probe = false;
  1988. }
  1989. if(
  1990. count($image) !== 0 &&
  1991. !isset($image[0]["attributes"]["width"])
  1992. ){
  1993. $thumb = [
  1994. "url" =>
  1995. $this->getdimg(
  1996. $image[0]["attributes"]["id"]
  1997. ),
  1998. "ratio" => $probe === true ? "16:9" : "1:1"
  1999. ];
  2000. }else{
  2001. $thumb = [
  2002. "url" => null,
  2003. "ratio" => null
  2004. ];
  2005. }
  2006. $description = null;
  2007. if($probe === false){
  2008. $desc_divs =
  2009. $this->fuckhtml
  2010. ->getElementsByAttributeName(
  2011. "style",
  2012. "div"
  2013. );
  2014. foreach($desc_divs as $desc){
  2015. if(
  2016. strpos(
  2017. $desc["attributes"]["style"],
  2018. "margin-top:"
  2019. ) !== false
  2020. ){
  2021. $description =
  2022. $this->titledots(
  2023. $this->fuckhtml
  2024. ->getTextContent(
  2025. $desc
  2026. )
  2027. );
  2028. break;
  2029. }
  2030. }
  2031. }
  2032. // get author
  2033. $author =
  2034. $this->fuckhtml
  2035. ->getElementsByClassName(
  2036. $this->getstyle(
  2037. [
  2038. "overflow" => "hidden",
  2039. "text-align" => "left",
  2040. "text-overflow" => "ellipsis",
  2041. "white-space" => "nowrap",
  2042. "margin-bottom" => "8px"
  2043. ]
  2044. ),
  2045. "div"
  2046. );
  2047. if(count($author) !== 0){
  2048. $author =
  2049. $this->fuckhtml
  2050. ->getTextContent(
  2051. $author[0]
  2052. );
  2053. }else{
  2054. $author = null;
  2055. }
  2056. // get date
  2057. $date = null;
  2058. $date_div =
  2059. $this->fuckhtml
  2060. ->getElementsByAttributeName(
  2061. "style",
  2062. "div"
  2063. );
  2064. foreach($date_div as $d){
  2065. $this->fuckhtml->load($d);
  2066. $span =
  2067. $this->fuckhtml
  2068. ->getElementsByTagName(
  2069. "span"
  2070. );
  2071. if(
  2072. strpos(
  2073. $d["attributes"]["style"],
  2074. "bottom:"
  2075. ) !== false
  2076. ){
  2077. $date =
  2078. strtotime(
  2079. $this->fuckhtml
  2080. ->getTextContent(
  2081. $span[count($span) - 1]
  2082. )
  2083. );
  2084. break;
  2085. }
  2086. }
  2087. $out["news"][] = [
  2088. "title" => $title,
  2089. "author" => $author,
  2090. "description" => $description,
  2091. "date" => $date,
  2092. "thumb" => $thumb,
  2093. "url" =>
  2094. $this->unshiturl(
  2095. $a["attributes"]
  2096. ["href"]
  2097. )
  2098. ];
  2099. }
  2100. return $out;
  2101. }
  2102. public function image($get){
  2103. // generate parameters
  2104. if($get["npt"]){
  2105. [$params, $proxy] =
  2106. $this->backend->get(
  2107. $get["npt"],
  2108. "images"
  2109. );
  2110. $params = json_decode($params, true);
  2111. }else{
  2112. $search = $get["s"];
  2113. if(strlen($search) === 0){
  2114. throw new Exception("Search term is empty!");
  2115. }
  2116. $proxy = $this->backend->get_ip();
  2117. $country = $get["country"];
  2118. $nsfw = $get["nsfw"];
  2119. $time = $get["time"];
  2120. $size = $get["size"];
  2121. $ratio = $get["ratio"];
  2122. $color = $get["color"];
  2123. $type = $get["type"];
  2124. $format = $get["format"];
  2125. $rights = $get["rights"];
  2126. $params = [
  2127. "q" => $search,
  2128. "udm" => "2" // get images
  2129. ];
  2130. // country (image search uses cr instead of gl)
  2131. if($country != "any"){
  2132. $params["cr"] = "country" . strtoupper($country);
  2133. }
  2134. // nsfw
  2135. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  2136. // generate tbs
  2137. $tbs = [];
  2138. // time
  2139. if($time != "any"){
  2140. $tbs["qdr"] = $time;
  2141. }
  2142. // size
  2143. if($size != "any"){
  2144. $params["imgsz"] = $size;
  2145. }
  2146. // ratio
  2147. if($ratio != "any"){
  2148. $params["imgar"] = $ratio;
  2149. }
  2150. // color
  2151. if($color != "any"){
  2152. if(
  2153. $color == "color" ||
  2154. $color == "trans"
  2155. ){
  2156. $params["imgc"] = $color;
  2157. }elseif($color == "bnw"){
  2158. $params["imgc"] = "gray";
  2159. }else{
  2160. $tbs["ic"] = "specific";
  2161. $tbs["isc"] = $color;
  2162. }
  2163. }
  2164. // type
  2165. if($type != "any"){
  2166. $tbs["itp"] = $type;
  2167. }
  2168. // format
  2169. if($format != "any"){
  2170. $params["as_filetype"] = $format;
  2171. }
  2172. // rights (tbs)
  2173. if($rights != "any"){
  2174. $tbs["sur"] = $rights;
  2175. }
  2176. // append tbs
  2177. if(count($tbs) !== 0){
  2178. $params["tbs"] = "";
  2179. foreach($tbs as $key => $value){
  2180. $params["tbs"] .= $key . ":" . $value . ",";
  2181. }
  2182. $params["tbs"] = rtrim($params["tbs"], ",");
  2183. }
  2184. }
  2185. /*
  2186. $handle = fopen("scraper/page.html", "r");
  2187. $html = fread($handle, filesize("scraper/page.html"));
  2188. fclose($handle);*/
  2189. try{
  2190. $html =
  2191. $this->get(
  2192. $proxy,
  2193. "https://www.google.com/search",
  2194. $params
  2195. );
  2196. }catch(Exception $error){
  2197. throw new Exception("Failed to get search page");
  2198. }
  2199. $this->fuckhtml->load($html);
  2200. $this->detect_sorry();
  2201. // get javascript images
  2202. $this->scrape_imagearr($html);
  2203. $out = [
  2204. "status" => "ok",
  2205. "npt" => null,
  2206. "image" => []
  2207. ];
  2208. $images =
  2209. $this->fuckhtml
  2210. ->getElementsByClassName(
  2211. "ivg-i",
  2212. "div"
  2213. );
  2214. foreach($images as $div){
  2215. $this->fuckhtml->load($div);
  2216. $image =
  2217. $this->fuckhtml
  2218. ->getElementsByTagName("img")[0];
  2219. // make sure we dont attempt to show an image we dont have data for
  2220. if(
  2221. isset($div["attributes"]["data-docid"]) &&
  2222. isset($this->image_arr[$div["attributes"]["data-docid"]])
  2223. ){
  2224. $source =
  2225. $this->image_arr[
  2226. $div["attributes"]["data-docid"]
  2227. ];
  2228. }else{
  2229. continue;
  2230. }
  2231. $out["image"][] = [
  2232. "title" =>
  2233. $this->titledots(
  2234. $this->fuckhtml
  2235. ->getTextContent(
  2236. $image["attributes"]["alt"]
  2237. )
  2238. ),
  2239. "source" => $source,
  2240. "url" =>
  2241. $this->fuckhtml
  2242. ->getTextContent(
  2243. $div["attributes"]["data-lpage"]
  2244. )
  2245. ];
  2246. }
  2247. // as usual, no way to check if there is a next page reliably
  2248. if(count($out["image"]) > 50){
  2249. if(!isset($params["start"])){
  2250. $params["start"] = 10;
  2251. }else{
  2252. $params["start"] += 10;
  2253. }
  2254. $out["npt"] =
  2255. $this->backend
  2256. ->store(
  2257. json_encode($params),
  2258. "image",
  2259. $proxy
  2260. );
  2261. }
  2262. return $out;
  2263. }
  2264. private function unshiturl($url, $return_size = false){
  2265. // decode
  2266. $url =
  2267. $this->fuckhtml
  2268. ->getTextContent(
  2269. $url
  2270. );
  2271. $url_parts = parse_url($url);
  2272. if(isset($url_parts["query"])){
  2273. parse_str($url_parts["query"], $query);
  2274. }else{
  2275. $query = [];
  2276. }
  2277. if(
  2278. !isset(
  2279. $url_parts["host"]
  2280. ) ||
  2281. stripos($url_parts["host"], "google.") !== false
  2282. ){
  2283. // no host, we have a tracking url
  2284. if(isset($query["imgurl"])){
  2285. $url = $query["imgurl"];
  2286. }
  2287. elseif(isset($query["q"])){
  2288. $url = $query["q"];
  2289. }
  2290. }
  2291. // rewrite URLs to remove extra tracking parameters
  2292. $domain = parse_url($url, PHP_URL_HOST);
  2293. if(
  2294. preg_match(
  2295. '/wikipedia.org$/',
  2296. $domain
  2297. )
  2298. ){
  2299. // rewrite wikipedia mobile URLs to desktop
  2300. $url =
  2301. $this->replacedomain(
  2302. $url,
  2303. preg_replace(
  2304. '/([a-z0-9]+)(\.m\.)/',
  2305. '$1.',
  2306. $domain
  2307. )
  2308. );
  2309. }
  2310. elseif(
  2311. preg_match(
  2312. '/imdb\.com$|youtube\.[^.]+$/',
  2313. $domain
  2314. )
  2315. ){
  2316. // rewrite imdb and youtube mobile URLs too
  2317. $url =
  2318. $this->replacedomain(
  2319. $url,
  2320. preg_replace(
  2321. '/^m\./',
  2322. "",
  2323. $domain
  2324. )
  2325. );
  2326. }
  2327. elseif(
  2328. preg_match(
  2329. '/play\.google\.[^.]+$/',
  2330. $domain
  2331. )
  2332. ){
  2333. // remove referrers from play.google.com
  2334. $u_query = parse_url($url, PHP_URL_QUERY);
  2335. if($u_query !== null){
  2336. parse_str($u_query, $u_query);
  2337. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2338. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2339. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2340. $query = http_build_query($query);
  2341. $url =
  2342. str_replace(
  2343. $u_query,
  2344. $u_query,
  2345. $url
  2346. );
  2347. }
  2348. }
  2349. elseif(
  2350. preg_match(
  2351. '/twitter\.com$/',
  2352. $domain
  2353. )
  2354. ){
  2355. // remove more referrers from twitter.com
  2356. $u_query = parse_url($url, PHP_URL_QUERY);
  2357. if($u_query !== null){
  2358. parse_str($u_query, $u_query);
  2359. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  2360. $u_query = http_build_query($u_query);
  2361. $url =
  2362. str_replace(
  2363. $oldquery,
  2364. $u_query,
  2365. $url
  2366. );
  2367. }
  2368. }
  2369. elseif(
  2370. preg_match(
  2371. '/maps\.google\.[^.]+/',
  2372. $domain
  2373. )
  2374. ){
  2375. if(stripos($url, "maps?") !== false){
  2376. $u_query = parse_url($url, PHP_URL_QUERY);
  2377. if($u_query !== null){
  2378. parse_str($u_query, $u_query);
  2379. if(isset($u_query["daddr"])){
  2380. $url =
  2381. "https://maps.google.com/maps?daddr=" .
  2382. urlencode($u_query["daddr"]);
  2383. }
  2384. }
  2385. }
  2386. }
  2387. if($return_size){
  2388. return [
  2389. "url" => $url,
  2390. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2391. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2392. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2393. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2394. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2395. ];
  2396. }
  2397. return $url;
  2398. }
  2399. private function replacedomain($url, $domain){
  2400. return
  2401. preg_replace(
  2402. '/(https?:\/\/)([^\/]+)/',
  2403. '$1' . $domain,
  2404. $url
  2405. );
  2406. }
  2407. private function titledots($title){
  2408. return trim($title, " .\t\n\r\0\x0B…");
  2409. }
  2410. private function hms2int($time){
  2411. $parts = explode(":", $time, 3);
  2412. $time = 0;
  2413. if(count($parts) === 3){
  2414. // hours
  2415. $time = $time + ((int)$parts[0] * 3600);
  2416. array_shift($parts);
  2417. }
  2418. if(count($parts) === 2){
  2419. // minutes
  2420. $time = $time + ((int)$parts[0] * 60);
  2421. array_shift($parts);
  2422. }
  2423. // seconds
  2424. $time = $time + (int)$parts[0];
  2425. return $time;
  2426. }
  2427. private function detect_sorry(){
  2428. $captcha_form =
  2429. $this->fuckhtml
  2430. ->getElementById(
  2431. "captcha-form",
  2432. "form"
  2433. );
  2434. if($captcha_form !== false){
  2435. throw new Exception("Google returned a captcha");
  2436. }
  2437. }
  2438. }