google.php 58 KB

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