1
0

google.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046
  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/sublink.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. foreach($as as $a){
  902. if(!isset($a["attributes"]["href"])){ continue; }
  903. $link =
  904. $this->fuckhtml
  905. ->getTextContent(
  906. $a["attributes"]["href"]
  907. );
  908. $link_text =
  909. strtolower(
  910. $this->fuckhtml
  911. ->getTextContent($a)
  912. );
  913. if(
  914. str_contains(
  915. $link_text,
  916. "complaint"
  917. )
  918. ){
  919. // sometimes the link is an encrypted redirect, handle redirection securely
  920. $notices[] = $this->redirect_add_url($link);
  921. }
  922. }
  923. // push notices to dom
  924. if(count($notices) !== 0){
  925. $notices_dom = [];
  926. $i = 0;
  927. $c = count($notices);
  928. $notices_dom = [
  929. [
  930. "type" => "text",
  931. "value" => "Google removed results from this page, but you can get them anyways by consulting these takedown notices: "
  932. ]
  933. ];
  934. foreach($notices as $n){
  935. $i++;
  936. $notices_dom[] = [
  937. "type" => "link",
  938. "value" => "Notice #{$i}",
  939. "url" => $n
  940. ];
  941. if($i !== count($notices)){
  942. $notices_dom[] = [
  943. "type" => "text",
  944. "value" => ", "
  945. ];
  946. }
  947. }
  948. $out["answer"][] = [
  949. "title" => "LumenDB notices",
  950. "description" => $notices_dom,
  951. "url" => null,
  952. "thumb" => null,
  953. "table" => [],
  954. "sublink" => []
  955. ];
  956. }
  957. }
  958. $this->fuckhtml->load($content);
  959. // get search results
  960. $results =
  961. $this->fuckhtml
  962. ->getElementsByAttributeName(
  963. "data-rpos",
  964. "div"
  965. );
  966. foreach($results as $result){
  967. $this->fuckhtml->load($result);
  968. // get title
  969. $title =
  970. $this->fuckhtml
  971. ->getElementsByTagName("h3");
  972. if(count($title) === 0){
  973. // should not happen
  974. continue;
  975. }
  976. $title =
  977. $this->titledots(
  978. $this->fuckhtml
  979. ->getTextContent(
  980. $title[0]
  981. )
  982. );
  983. $url =
  984. $this->fuckhtml
  985. ->getElementsByTagName(
  986. "a"
  987. );
  988. if(
  989. count($url) === 0 ||
  990. !isset($url[0]["attributes"]["href"])
  991. ){
  992. continue;
  993. }
  994. $url =
  995. $this->fuckhtml
  996. ->getTextContent(
  997. $url[0]["attributes"]["href"]
  998. );
  999. if(
  1000. preg_match(
  1001. '/^\/goto\?url/',
  1002. $url
  1003. )
  1004. ){
  1005. // encrypted url detected
  1006. // get reference div
  1007. $refdivs =
  1008. $this->fuckhtml
  1009. ->getElementsByAttributeName(
  1010. "id",
  1011. "div"
  1012. );
  1013. foreach($refdivs as $r){
  1014. if(
  1015. preg_match(
  1016. '/^atritem-/',
  1017. $r["attributes"]["id"]
  1018. ) &&
  1019. isset($r["attributes"]["jsdata"])
  1020. ){
  1021. $ref = explode(";", $r["attributes"]["jsdata"]);
  1022. $ref = $ref[count($ref) - 1];
  1023. if(isset($encoded_links[$ref][32][3][0])){
  1024. $url = $encoded_links[$ref][32][3][0];
  1025. }
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. // get citation text. contains table elements
  1031. $table = [];
  1032. $date = null;
  1033. $cite =
  1034. $this->fuckhtml
  1035. ->getElementsByTagName("cite");
  1036. if(isset($cite[0])){
  1037. $texts =
  1038. explode(
  1039. " · ",
  1040. $this->fuckhtml
  1041. ->getTextContent($cite[0])
  1042. );
  1043. foreach($texts as $text){
  1044. $text = trim($text);
  1045. preg_match(
  1046. '/([0-9+kmb,.]+) +(likes?|views?|comments?|followers?|subscribers?|(?:hours?|days?|months?|years?) +ago)/i',
  1047. $text,
  1048. $probe
  1049. );
  1050. if(isset($probe[1])){
  1051. // is it date?
  1052. if(
  1053. preg_match(
  1054. '/ago$/i',
  1055. $text
  1056. )
  1057. ){
  1058. $time = strtotime($text);
  1059. if($time !== false){ $date = $time; }
  1060. continue;
  1061. }
  1062. // anything else?
  1063. $table[ucfirst($probe[2])] = trim($probe[1]);
  1064. }
  1065. }
  1066. }
  1067. // get description/image wrapper
  1068. $desc_img_wrappers =
  1069. $this->fuckhtml
  1070. ->getElementsByAttributeName(
  1071. "data-sncf",
  1072. "div"
  1073. );
  1074. $description = null;
  1075. $thumb = [
  1076. "url" => null,
  1077. "ratio" => null
  1078. ];
  1079. foreach($desc_img_wrappers as $wrapper){
  1080. $this->fuckhtml->load($wrapper);
  1081. // probe for image
  1082. $image =
  1083. $this->fuckhtml
  1084. ->getElementsByTagName(
  1085. "img"
  1086. );
  1087. if(count($image) !== 0){
  1088. // handle thumbnails
  1089. $i = null;
  1090. if(isset($image[0]["attributes"]["id"])){
  1091. $i = $this->getdimg($image[0]["attributes"]["id"]);
  1092. }
  1093. if($i !== null){
  1094. $thumb = [
  1095. "ratio" => "1:1",
  1096. "url" => $i
  1097. ];
  1098. }
  1099. }else{
  1100. if($description === null){
  1101. // parse it as description
  1102. $description =
  1103. $this->titledots(
  1104. $this->fuckhtml
  1105. ->getTextContent(
  1106. $wrapper
  1107. )
  1108. );
  1109. }else{
  1110. // parse table elements
  1111. $ts =
  1112. explode(
  1113. " · ",
  1114. $this->fuckhtml
  1115. ->getTextContent(
  1116. $wrapper
  1117. )
  1118. );
  1119. foreach($ts as $t){
  1120. $t = trim(html_entity_decode($t));
  1121. if(preg_match("/\p{Sc}/u", $t)){
  1122. $table["Price"] = $t;
  1123. continue;
  1124. }
  1125. if(str_contains($t, "stock")){
  1126. $table["Stock"] = $t;
  1127. continue;
  1128. }
  1129. if(str_contains($t, "return")){
  1130. $table["Returns"] = $t;
  1131. continue;
  1132. }
  1133. if(str_contains($t, "delivery")){
  1134. $table["Delivery"] = $t;
  1135. continue;
  1136. }
  1137. // 5.0(590,004)
  1138. if(preg_match('/^([0-9kmb\/%.,]+) *(\([0-9kmb,.]+\))?/', $t, $match)){
  1139. if(isset($match[2])){
  1140. $table["Ratings"] = "{$match[1]} {$match[2]}";
  1141. continue;
  1142. }
  1143. }
  1144. if(str_contains($t, "rating")){
  1145. $table["Ratings"] = $t;
  1146. continue;
  1147. }
  1148. }
  1149. }
  1150. }
  1151. }
  1152. // extract date from description
  1153. // 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
  1154. if($date === null){
  1155. $date_probe =
  1156. explode(
  1157. " — ",
  1158. $description,
  1159. 2
  1160. );
  1161. if(count($date_probe) === 2){
  1162. $time = strtotime($date_probe[0]);
  1163. if($time !== false){
  1164. $description = $date_probe[1];
  1165. $date = $time;
  1166. }
  1167. }
  1168. }
  1169. $sublinks = [];
  1170. // get sublinks (rare)
  1171. $as =
  1172. $this->fuckhtml
  1173. ->getElementsByClassName(
  1174. $this->getstyle([
  1175. "padding-right" => "6px",
  1176. "margin-top" => "4px",
  1177. "display" => "inline-block"
  1178. ]),
  1179. "a"
  1180. );
  1181. foreach($as as $sublink){
  1182. $sublink_url =
  1183. $this->fuckhtml
  1184. ->getTextContent(
  1185. $sublink["attributes"]["href"]
  1186. );
  1187. $sublinks[] = [
  1188. "title" =>
  1189. $this->fuckhtml
  1190. ->getTextContent($sublink),
  1191. "date" => null,
  1192. "url" => $this->redirect_add_url($sublink_url)
  1193. ];
  1194. }
  1195. $out["web"][] = [
  1196. "title" => $title,
  1197. "description" => $description,
  1198. "url" => $this->redirect_add_url($url),
  1199. "date" => $date,
  1200. "type" => "web",
  1201. "thumb" => $thumb,
  1202. "sublink" => $sublinks,
  1203. "table" => $table
  1204. ];
  1205. }
  1206. $this->resolve_urls($out);
  1207. return $out;
  1208. }
  1209. public function image($get){
  1210. // generate parameters
  1211. if($get["npt"]){
  1212. [$params, $proxy] =
  1213. $this->backend->get(
  1214. $get["npt"],
  1215. "images"
  1216. );
  1217. $params = json_decode($params, true);
  1218. $container = $params["c"];
  1219. unset($params["c"]);
  1220. }else{
  1221. $search = $get["s"];
  1222. if(strlen($search) === 0){
  1223. throw new Exception("Search term is empty!");
  1224. }
  1225. $proxy = $this->backend->get_ip();
  1226. $country = $get["country"];
  1227. $nsfw = $get["nsfw"];
  1228. $time = $get["time"];
  1229. $size = $get["size"];
  1230. $ratio = $get["ratio"];
  1231. $color = $get["color"];
  1232. $type = $get["type"];
  1233. $format = $get["format"];
  1234. $rights = $get["rights"];
  1235. $params = [
  1236. "q" => $search,
  1237. "udm" => "2", // get images
  1238. "start" => 0
  1239. ];
  1240. // country (image search uses cr instead of gl)
  1241. if($country != "any"){
  1242. $params["cr"] = "country" . strtoupper($country);
  1243. }
  1244. // nsfw
  1245. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1246. // generate tbs
  1247. $tbs = [];
  1248. // time
  1249. if($time != "any"){
  1250. $tbs["qdr"] = $time;
  1251. }
  1252. // size
  1253. if($size != "any"){
  1254. $params["imgsz"] = $size;
  1255. }
  1256. // ratio
  1257. if($ratio != "any"){
  1258. $params["imgar"] = $ratio;
  1259. }
  1260. // color
  1261. if($color != "any"){
  1262. if(
  1263. $color == "color" ||
  1264. $color == "trans"
  1265. ){
  1266. $params["imgc"] = $color;
  1267. }elseif($color == "bnw"){
  1268. $params["imgc"] = "gray";
  1269. }else{
  1270. $tbs["ic"] = "specific";
  1271. $tbs["isc"] = $color;
  1272. }
  1273. }
  1274. // type
  1275. if($type != "any"){
  1276. $tbs["itp"] = $type;
  1277. }
  1278. // format
  1279. if($format != "any"){
  1280. $params["as_filetype"] = $format;
  1281. }
  1282. // rights (tbs)
  1283. if($rights != "any"){
  1284. $tbs["sur"] = $rights;
  1285. }
  1286. // append tbs
  1287. if(count($tbs) !== 0){
  1288. $params["tbs"] = "";
  1289. foreach($tbs as $key => $value){
  1290. $params["tbs"] .= $key . ":" . $value . ",";
  1291. }
  1292. $params["tbs"] = rtrim($params["tbs"], ",");
  1293. }
  1294. }
  1295. //$html = file_get_contents("scraper/google.html");
  1296. $data = $this->get(
  1297. $proxy,
  1298. "https://www.google.com/search",
  1299. $params
  1300. );
  1301. $html = &$data["data"];
  1302. $container = &$data["container"];
  1303. $this->fuckhtml->load($html);
  1304. // parse all <style> tags
  1305. $this->parsestyles();
  1306. // solve softcaptcha
  1307. $softcaptcha = $this->detect_sorry();
  1308. if($softcaptcha !== false){
  1309. $params["sei"] = $softcaptcha["sei"];
  1310. $data2 = $this->get(
  1311. $proxy,
  1312. "https://www.google.com/search",
  1313. $params,
  1314. $container
  1315. );
  1316. $html = &$data2["data"];
  1317. $this->fuckhtml->load($html);
  1318. $this->parsestyles();
  1319. }
  1320. // get javascript images
  1321. $this->scrape_imagearr($html);
  1322. $out = [
  1323. "status" => "ok",
  1324. "npt" => null,
  1325. "image" => []
  1326. ];
  1327. $images =
  1328. $this->fuckhtml
  1329. ->getElementsByClassName(
  1330. "ivg-i",
  1331. "div"
  1332. );
  1333. foreach($images as $div){
  1334. $this->fuckhtml->load($div);
  1335. $image =
  1336. $this->fuckhtml
  1337. ->getElementsByTagName("img")[0];
  1338. // make sure we dont attempt to show an image we dont have data for
  1339. if(
  1340. isset($div["attributes"]["data-docid"]) &&
  1341. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1342. ){
  1343. $source =
  1344. $this->image_arr[
  1345. $div["attributes"]["data-docid"]
  1346. ];
  1347. }else{
  1348. continue;
  1349. }
  1350. $out["image"][] = [
  1351. "title" =>
  1352. $this->titledots(
  1353. $this->fuckhtml
  1354. ->getTextContent(
  1355. $image["attributes"]["alt"]
  1356. )
  1357. ),
  1358. "source" => $source,
  1359. "url" =>
  1360. $this->redirect_add_url(
  1361. $this->fuckhtml
  1362. ->getTextContent(
  1363. $div["attributes"]["data-lpage"]
  1364. )
  1365. )
  1366. ];
  1367. }
  1368. // as usual, no way to check if there is a next page reliably
  1369. if(count($out["image"]) > 50){
  1370. if(!isset($params["start"])){
  1371. $params["start"] = 10;
  1372. }else{
  1373. $params["start"] += 10;
  1374. }
  1375. $out["npt"] =
  1376. $this->backend
  1377. ->store(
  1378. json_encode($params),
  1379. "image",
  1380. $proxy
  1381. );
  1382. }
  1383. $this->resolve_urls($out);
  1384. return $out;
  1385. }
  1386. public function video($get){
  1387. if($get["npt"]){
  1388. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1389. $params = json_decode($params, true);
  1390. $container = $params["c"];
  1391. unset($params["c"]);
  1392. }else{
  1393. $search = $get["s"];
  1394. $country = $get["country"];
  1395. $nsfw = $get["nsfw"];
  1396. $older = $get["older"];
  1397. $newer = $get["newer"];
  1398. $duration = $get["duration"];
  1399. $quality = $get["quality"];
  1400. $captions = $get["captions"];
  1401. $container = null;
  1402. $proxy = $this->backend->get_ip();
  1403. $params = [
  1404. "q" => $search,
  1405. "udm" => "7",
  1406. "hl" => "en"
  1407. ];
  1408. // country
  1409. if($country != "any"){
  1410. $params["gl"] = $country;
  1411. }
  1412. // nsfw
  1413. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1414. $tbs = [];
  1415. // get date
  1416. $older = $older === false ? null : date("m/d/Y", $older);
  1417. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1418. if(
  1419. $older !== null ||
  1420. $newer !== null
  1421. ){
  1422. $tbs["cdr"] = "1";
  1423. $tbs["cd_min"] = $newer;
  1424. $tbs["cd_max"] = $older;
  1425. }
  1426. // duration
  1427. if($duration != "any"){
  1428. $tbs[] = "dur:" . $duration;
  1429. }
  1430. // quality
  1431. if($quality != "any"){
  1432. $tbs[] = "hq:" . $quality;
  1433. }
  1434. // captions
  1435. if($captions != "any"){
  1436. $tbs[] = "cc:" . $captions;
  1437. }
  1438. // append tbs
  1439. if(count($tbs) !== 0){
  1440. $params["tbs"] =
  1441. implode(",", $tbs);
  1442. }
  1443. }
  1444. $data = $this->get(
  1445. $proxy,
  1446. "https://www.google.com/search",
  1447. $params,
  1448. $container
  1449. );
  1450. $html = &$data["data"];
  1451. $container = &$data["container"];
  1452. //$html = file_get_contents("scraper/google.html");
  1453. if(!isset($params["start"])){
  1454. $params["start"] = 0;
  1455. }
  1456. $params["start"] += 20;
  1457. $this->fuckhtml->load($html);
  1458. // parse all <style> tags
  1459. $this->parsestyles();
  1460. // solve softcaptcha
  1461. $softcaptcha = $this->detect_sorry();
  1462. if($softcaptcha !== false){
  1463. $params["sei"] = $softcaptcha["sei"];
  1464. $data2 = $this->get(
  1465. $proxy,
  1466. "https://www.google.com/search",
  1467. $params,
  1468. $container
  1469. );
  1470. $html = &$data2["data"];
  1471. $this->fuckhtml->load($html);
  1472. $this->parsestyles();
  1473. }
  1474. //
  1475. // Parse web video page
  1476. //
  1477. // get javascript images
  1478. $this->scrape_dimg($html);
  1479. $this->scrape_imagearr($html);
  1480. $params["c"] = $container;
  1481. // get encoded links
  1482. $encoded_links =
  1483. explode(
  1484. "function(){var m={",
  1485. $html,
  1486. 2
  1487. );
  1488. if(count($encoded_links) === 2){
  1489. $encoded_links =
  1490. json_decode(
  1491. $this->fuckhtml
  1492. ->extract_json(
  1493. "{" . $encoded_links[1]
  1494. ),
  1495. true
  1496. );
  1497. }else{
  1498. $encoded_links = [];
  1499. }
  1500. $out = [
  1501. "status" => "ok",
  1502. "npt" =>
  1503. $this->backend->store(
  1504. json_encode($params),
  1505. "videos",
  1506. $proxy
  1507. ),
  1508. "video" => [],
  1509. "author" => [],
  1510. "livestream" => [],
  1511. "playlist" => [],
  1512. "reel" => []
  1513. ];
  1514. $search_div =
  1515. $this->fuckhtml
  1516. ->getElementById(
  1517. "center_col"
  1518. );
  1519. if($search_div === false){
  1520. throw new Exception("Failed to grep search div");
  1521. }
  1522. $this->fuckhtml->load($search_div);
  1523. $results =
  1524. $this->fuckhtml
  1525. ->getElementsByClassName(
  1526. $this->getstyle([
  1527. "margin" => "0px 0px 30px"
  1528. ]),
  1529. "div"
  1530. );
  1531. foreach($results as $result){
  1532. $this->fuckhtml->load($result);
  1533. $url =
  1534. $this->fuckhtml
  1535. ->getElementsByTagName(
  1536. "a"
  1537. );
  1538. if(count($url) === 0){
  1539. // no url, weird, continue
  1540. continue;
  1541. }
  1542. $title =
  1543. $this->fuckhtml
  1544. ->getElementsByTagName(
  1545. "h3"
  1546. );
  1547. if(count($title) === 0){
  1548. // no title, weird, continue
  1549. continue;
  1550. }
  1551. // get description
  1552. $description =
  1553. $this->fuckhtml
  1554. ->getElementsByClassName(
  1555. $this->getstyle([
  1556. "-webkit-box-orient" => "vertical",
  1557. "display" => "-webkit-box",
  1558. "-webkit-line-clamp" => "2",
  1559. "overflow" => "hidden",
  1560. "word-break" => "break-word"
  1561. ]),
  1562. "div"
  1563. );
  1564. if(count($description) === 0){
  1565. $description = null;
  1566. }else{
  1567. $description =
  1568. html_entity_decode(
  1569. $this->titledots(
  1570. $this->fuckhtml
  1571. ->getTextContent(
  1572. $description[0]
  1573. )
  1574. )
  1575. );
  1576. }
  1577. // get author + date posted
  1578. $metadiv =
  1579. $this->fuckhtml
  1580. ->getElementsByClassName(
  1581. $this->getstyle([
  1582. "margin-top" => "12px"
  1583. ]),
  1584. "div"
  1585. );
  1586. $author = null;
  1587. $date = null;
  1588. if(count($metadiv) !== 0){
  1589. $metadiv =
  1590. explode(
  1591. "·",
  1592. $this->fuckhtml
  1593. ->getTextContent(
  1594. $metadiv[0]
  1595. )
  1596. );
  1597. if(count($metadiv) === 3){
  1598. $author = trim($metadiv[1]);
  1599. $date = strtotime(trim($metadiv[2]));
  1600. }elseif(count($metadiv) === 2){
  1601. $author = trim($metadiv[0]);
  1602. $date = strtotime(trim($metadiv[1]));
  1603. }
  1604. }
  1605. $thumb = [
  1606. "url" => null,
  1607. "ratio" => null
  1608. ];
  1609. $image =
  1610. $this->fuckhtml
  1611. ->getElementsByTagName(
  1612. "img"
  1613. );
  1614. $duration = null;
  1615. if(
  1616. count($image) !== 0 &&
  1617. isset($image[0]["attributes"]["id"])
  1618. ){
  1619. $thumb = [
  1620. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1621. "ratio" => "16:9"
  1622. ];
  1623. // get duration
  1624. $duration =
  1625. $this->fuckhtml
  1626. ->getElementsByClassName(
  1627. $this->getstyle([
  1628. "background-color" => "rgba(0,0,0,0.6)",
  1629. "color" => "#fff",
  1630. "fill" => "#fff"
  1631. ])
  1632. );
  1633. if(count($duration) !== 0){
  1634. $duration =
  1635. $this->hms2int(
  1636. $this->fuckhtml
  1637. ->getTextContent(
  1638. $duration[0]
  1639. ));
  1640. }else{
  1641. $duration = null;
  1642. }
  1643. }
  1644. $url =
  1645. $this->fuckhtml
  1646. ->getTextContent(
  1647. $url[0]["attributes"]["href"]
  1648. );
  1649. if(
  1650. preg_match(
  1651. '/^\/goto\?url/',
  1652. $url
  1653. )
  1654. ){
  1655. // encrypted url detected
  1656. // get reference div
  1657. $refdivs =
  1658. $this->fuckhtml
  1659. ->getElementsByAttributeName(
  1660. "id",
  1661. "div"
  1662. );
  1663. foreach($refdivs as $r){
  1664. if(
  1665. preg_match(
  1666. '/^atritem-/',
  1667. $r["attributes"]["id"]
  1668. ) &&
  1669. isset($r["attributes"]["jsdata"])
  1670. ){
  1671. $ref = explode(";", $r["attributes"]["jsdata"]);
  1672. $ref = $ref[count($ref) - 1];
  1673. if(isset($encoded_links[$ref][32][11][7])){
  1674. $url = $encoded_links[$ref][32][11][7];
  1675. }
  1676. break;
  1677. }
  1678. }
  1679. }
  1680. $out["video"][] = [
  1681. "title" =>
  1682. $this->titledots(
  1683. $this->fuckhtml
  1684. ->getTextContent(
  1685. $title[0]
  1686. )
  1687. ),
  1688. "description" => $description,
  1689. "author" => [
  1690. "name" => $author,
  1691. "url" => null,
  1692. "avatar" => null
  1693. ],
  1694. "date" => $date,
  1695. "duration" => $duration,
  1696. "views" => null,
  1697. "thumb" => $thumb,
  1698. "url" => $this->redirect_add_url($url)
  1699. ];
  1700. }
  1701. $this->resolve_urls($out);
  1702. return $out;
  1703. }
  1704. public function news($get){
  1705. if($get["npt"]){
  1706. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1707. $req = json_decode($req, true);
  1708. $container = $req["c"];
  1709. $req = $req["u"];
  1710. $data = $this->get(
  1711. $proxy,
  1712. "https://www.google.com" . $req,
  1713. [],
  1714. $container
  1715. );
  1716. $html = &$data["data"];
  1717. $container = &$data["container"];
  1718. }else{
  1719. $search = $get["s"];
  1720. $country = $get["country"];
  1721. $nsfw = $get["nsfw"];
  1722. $older = $get["older"];
  1723. $newer = $get["newer"];
  1724. $sort = $get["sort"];
  1725. $container = null;
  1726. $proxy = $this->backend->get_ip();
  1727. $params = [
  1728. "q" => $search,
  1729. "tbm" => "nws",
  1730. "hl" => "en"
  1731. ];
  1732. // country
  1733. if($country != "any"){
  1734. $params["gl"] = $country;
  1735. }
  1736. // nsfw
  1737. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1738. $tbs = [];
  1739. // get date
  1740. $older = $older === false ? null : date("m/d/Y", $older);
  1741. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1742. if(
  1743. $older !== null ||
  1744. $newer !== null
  1745. ){
  1746. $tbs["cdr"] = "1";
  1747. $tbs["cd_min"] = $newer;
  1748. $tbs["cd_max"] = $older;
  1749. }
  1750. // relevance
  1751. if($sort == "date"){
  1752. $tbs["sbd"] = "1";
  1753. }
  1754. // append tbs
  1755. if(count($tbs) !== 0){
  1756. $params["tbs"] = "";
  1757. foreach($tbs as $key => $value){
  1758. $params["tbs"] .= $key . ":" . $value . ",";
  1759. }
  1760. $params["tbs"] = rtrim($params["tbs"], ",");
  1761. }
  1762. //$html = file_get_contents("scraper/google.html");
  1763. $data = $this->get(
  1764. $proxy,
  1765. "https://www.google.com/search",
  1766. $params
  1767. );
  1768. $html = &$data["data"];
  1769. $container = &$data["container"];
  1770. }
  1771. $out = [
  1772. "status" => "ok",
  1773. "npt" => null,
  1774. "news" => []
  1775. ];
  1776. $this->fuckhtml->load($html);
  1777. // parse styles
  1778. $this->parsestyles();
  1779. // solve softcaptcha
  1780. $softcaptcha = $this->detect_sorry();
  1781. if($softcaptcha !== false){
  1782. $params["sei"] = $softcaptcha["sei"];
  1783. $data2 = $this->get(
  1784. $proxy,
  1785. "https://www.google.com/search",
  1786. $params,
  1787. $container
  1788. );
  1789. $html = &$data2["data"];
  1790. $this->fuckhtml->load($html);
  1791. $this->parsestyles();
  1792. }
  1793. // get images
  1794. $this->scrape_dimg($html);
  1795. $center_col =
  1796. $this->fuckhtml
  1797. ->getElementById(
  1798. "center_col",
  1799. "div"
  1800. );
  1801. if($center_col === null){
  1802. throw new Exception("Could not grep result div");
  1803. }
  1804. $this->fuckhtml->load($center_col);
  1805. // get next page
  1806. $npt =
  1807. $this->fuckhtml
  1808. ->getElementById(
  1809. "pnnext",
  1810. "a"
  1811. );
  1812. if($npt !== false){
  1813. $out["npt"] =
  1814. $this->backend->store(
  1815. json_encode([
  1816. "u" =>
  1817. $this->fuckhtml
  1818. ->getTextContent(
  1819. $npt["attributes"]
  1820. ["href"]
  1821. ),
  1822. "c" => $container
  1823. ]),
  1824. "news",
  1825. $proxy
  1826. );
  1827. }
  1828. $as =
  1829. $this->fuckhtml
  1830. ->getElementsByAttributeName(
  1831. "jsname",
  1832. "a"
  1833. );
  1834. foreach($as as $a){
  1835. $this->fuckhtml->load($a);
  1836. // get title
  1837. $title =
  1838. $this->fuckhtml
  1839. ->getElementsByAttributeValue(
  1840. "role",
  1841. "heading",
  1842. "div"
  1843. );
  1844. if(count($title) === 0){
  1845. continue;
  1846. }
  1847. $title =
  1848. $this->titledots(
  1849. $this->fuckhtml
  1850. ->getTextContent(
  1851. $title[0]
  1852. )
  1853. );
  1854. // get thumbnail
  1855. $image =
  1856. $this->fuckhtml
  1857. ->getElementsByAttributeName(
  1858. "id",
  1859. "img"
  1860. );
  1861. // check for padded title node, if found, we're inside a carousel
  1862. $probe =
  1863. $this->fuckhtml
  1864. ->getElementsByClassName(
  1865. $this->getstyle(
  1866. [
  1867. "padding" => "16px 16px 40px 16px"
  1868. ]
  1869. ),
  1870. "div"
  1871. );
  1872. if(count($probe) !== 0){
  1873. $probe = true;
  1874. }else{
  1875. $probe = false;
  1876. }
  1877. if(
  1878. count($image) !== 0 &&
  1879. !isset($image[0]["attributes"]["width"])
  1880. ){
  1881. $thumb = [
  1882. "url" =>
  1883. $this->getdimg(
  1884. $image[0]["attributes"]["id"]
  1885. ),
  1886. "ratio" => $probe === true ? "16:9" : "1:1"
  1887. ];
  1888. }else{
  1889. $thumb = [
  1890. "url" => null,
  1891. "ratio" => null
  1892. ];
  1893. }
  1894. $description = null;
  1895. if($probe === false){
  1896. $desc_divs =
  1897. $this->fuckhtml
  1898. ->getElementsByAttributeName(
  1899. "style",
  1900. "div"
  1901. );
  1902. foreach($desc_divs as $desc){
  1903. if(
  1904. strpos(
  1905. $desc["attributes"]["style"],
  1906. "margin-top:"
  1907. ) !== false
  1908. ){
  1909. $description =
  1910. $this->titledots(
  1911. $this->fuckhtml
  1912. ->getTextContent(
  1913. $desc
  1914. )
  1915. );
  1916. break;
  1917. }
  1918. }
  1919. }
  1920. // get author
  1921. $author =
  1922. $this->fuckhtml
  1923. ->getElementsByClassName(
  1924. $this->getstyle(
  1925. [
  1926. "overflow" => "hidden",
  1927. "text-align" => "left",
  1928. "text-overflow" => "ellipsis",
  1929. "white-space" => "nowrap",
  1930. "margin-bottom" => "8px"
  1931. ]
  1932. ),
  1933. "div"
  1934. );
  1935. if(count($author) !== 0){
  1936. $author =
  1937. $this->fuckhtml
  1938. ->getTextContent(
  1939. $author[0]
  1940. );
  1941. }else{
  1942. $author = null;
  1943. }
  1944. // get date
  1945. $date = null;
  1946. $date_div =
  1947. $this->fuckhtml
  1948. ->getElementsByAttributeName(
  1949. "style",
  1950. "div"
  1951. );
  1952. foreach($date_div as $d){
  1953. $this->fuckhtml->load($d);
  1954. $span =
  1955. $this->fuckhtml
  1956. ->getElementsByTagName(
  1957. "span"
  1958. );
  1959. if(
  1960. strpos(
  1961. $d["attributes"]["style"],
  1962. "bottom:"
  1963. ) !== false
  1964. ){
  1965. $date =
  1966. strtotime(
  1967. $this->fuckhtml
  1968. ->getTextContent(
  1969. $span[count($span) - 1]
  1970. )
  1971. );
  1972. break;
  1973. }
  1974. }
  1975. $out["news"][] = [
  1976. "title" => $title,
  1977. "author" => $author,
  1978. "description" => $description,
  1979. "date" => $date,
  1980. "thumb" => $thumb,
  1981. "url" =>
  1982. $this->unshiturl(
  1983. $a["attributes"]
  1984. ["href"]
  1985. )
  1986. ];
  1987. }
  1988. $this->resolve_urls($out);
  1989. return $out;
  1990. }
  1991. private function scrape_dimg($html){
  1992. // get images loaded through javascript
  1993. $this->dimg = [];
  1994. preg_match_all(
  1995. '/function\(\){google\.ldi=({.*?});/',
  1996. $html,
  1997. $dimg
  1998. );
  1999. if(isset($dimg[1])){
  2000. foreach($dimg[1] as $i){
  2001. $tmp = json_decode($i, true);
  2002. foreach($tmp as $key => $value){
  2003. $this->dimg[$key] =
  2004. $this->unshit_thumb(
  2005. $value
  2006. );
  2007. }
  2008. }
  2009. }
  2010. // get additional javascript base64 images
  2011. preg_match_all(
  2012. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  2013. $html,
  2014. $dimg
  2015. );
  2016. if(isset($dimg[1])){
  2017. for($i=0; $i<count($dimg[1]); $i++){
  2018. $delims = explode(",", $dimg[2][$i]);
  2019. $string =
  2020. $this->fuckhtml
  2021. ->parseJsString(
  2022. $dimg[1][$i]
  2023. );
  2024. foreach($delims as $delim){
  2025. $this->dimg[trim($delim, "'")] = $string;
  2026. }
  2027. }
  2028. }
  2029. }
  2030. private function scrape_imagearr($html){
  2031. // get image links arrays
  2032. preg_match_all(
  2033. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  2034. $html,
  2035. $image_arr
  2036. );
  2037. $this->image_arr = [];
  2038. if(isset($image_arr[1])){
  2039. for($i=0; $i<count($image_arr[1]); $i++){
  2040. $original =
  2041. $this->fuckhtml
  2042. ->parseJsString(
  2043. $image_arr[5][$i]
  2044. );
  2045. if(
  2046. preg_match(
  2047. '/^x-raw-image/',
  2048. $original
  2049. )
  2050. ){
  2051. // only add thumbnail, google doesnt have OG resolution
  2052. $this->image_arr[$image_arr[1][$i]] = [
  2053. [
  2054. "url" =>
  2055. $this->unshit_thumb(
  2056. $this->fuckhtml
  2057. ->parseJsString(
  2058. $image_arr[2][$i]
  2059. )
  2060. ),
  2061. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  2062. "height" => (int)$image_arr[6][$i]
  2063. ]
  2064. ];
  2065. continue;
  2066. }
  2067. $this->image_arr[$image_arr[1][$i]] =
  2068. [
  2069. [
  2070. "url" => $original,
  2071. "width" => (int)$image_arr[7][$i],
  2072. "height" => (int)$image_arr[6][$i]
  2073. ],
  2074. [
  2075. "url" =>
  2076. $this->unshit_thumb(
  2077. $this->fuckhtml
  2078. ->parseJsString(
  2079. $image_arr[2][$i]
  2080. )
  2081. ),
  2082. "width" => (int)$image_arr[4][$i],
  2083. "height" => (int)$image_arr[3][$i]
  2084. ]
  2085. ];
  2086. }
  2087. }
  2088. }
  2089. private function getdimg($dimg){
  2090. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  2091. }
  2092. private function unshit_thumb($url, $get_bigger_res = false){
  2093. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  2094. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  2095. $parts = parse_url($url);
  2096. if(
  2097. isset($parts["host"]) &&
  2098. preg_match(
  2099. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  2100. $parts["host"]
  2101. )
  2102. ){
  2103. parse_str($parts["query"], $params);
  2104. if(isset($params["q"])){
  2105. if($get_bigger_res){
  2106. // this method doesnt always work, but does work for wiki thumbnails
  2107. return
  2108. "https://" . $parts["host"] . "/images?q=tbn:" .
  2109. $this->base64url_encode(
  2110. substr(
  2111. $this->base64url_decode(
  2112. explode(
  2113. ":",
  2114. $params["q"])[1]
  2115. ),
  2116. 0,
  2117. 29
  2118. )
  2119. );
  2120. }else{
  2121. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  2122. }
  2123. }
  2124. }
  2125. return $url;
  2126. }
  2127. private function parsestyles(){
  2128. $styles = [];
  2129. $style_div =
  2130. $this->fuckhtml
  2131. ->getElementsByTagName(
  2132. "style"
  2133. );
  2134. $raw_styles = "";
  2135. foreach($style_div as $style){
  2136. $raw_styles .= $style["innerHTML"];
  2137. }
  2138. // filter out media/keyframe queries
  2139. $raw_styles =
  2140. preg_replace(
  2141. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  2142. "",
  2143. $raw_styles
  2144. );
  2145. // get styles
  2146. preg_match_all(
  2147. '/(.+?){([\S\s]*?)}/',
  2148. $raw_styles,
  2149. $matches
  2150. );
  2151. for($i=0; $i<count($matches[1]); $i++){
  2152. // get style values
  2153. preg_match_all(
  2154. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  2155. $matches[2][$i],
  2156. $values_regex
  2157. );
  2158. $values = [];
  2159. for($k=0; $k<count($values_regex[1]); $k++){
  2160. $values[trim($values_regex[1][$k])] =
  2161. strtolower(trim($values_regex[2][$k]));
  2162. }
  2163. $names = explode(",", $matches[1][$i]);
  2164. // h1,h2,h3 will each get their own array index
  2165. foreach($names as $name){
  2166. $name = trim($name, "}\t\n\r\0\x0B");
  2167. foreach($values as $key => $value){
  2168. $styles[$name][$key] = $value;
  2169. }
  2170. }
  2171. }
  2172. foreach($styles as $key => $values){
  2173. $styles[$key]["_c"] = count($values);
  2174. }
  2175. $this->styles = $styles;
  2176. // get CSS colors
  2177. $this->css_colors = [];
  2178. if(isset($this->styles[":root"])){
  2179. foreach($this->styles[":root"] as $key => $value){
  2180. $this->css_colors[$value] = strtolower($key);
  2181. }
  2182. }
  2183. }
  2184. private function getstyle($styles){
  2185. $styles["_c"] = count($styles);
  2186. foreach($this->styles as $style_key => $style_values){
  2187. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  2188. $style_key =
  2189. explode(" ", $style_key);
  2190. $style_key = $style_key[count($style_key) - 1];
  2191. return
  2192. ltrim(
  2193. str_replace(
  2194. [".", "#"],
  2195. " ",
  2196. $style_key
  2197. )
  2198. );
  2199. }
  2200. }
  2201. return false;
  2202. }
  2203. private function getcolorvar($color){
  2204. if(isset($this->css_colors[$color])){
  2205. return $this->css_colors[$color];
  2206. }
  2207. return null;
  2208. }
  2209. private function unshiturl($url, $return_size = false){
  2210. // decode
  2211. $url =
  2212. $this->fuckhtml
  2213. ->getTextContent(
  2214. $url
  2215. );
  2216. $url_parts = parse_url($url);
  2217. if(isset($url_parts["query"])){
  2218. parse_str($url_parts["query"], $query);
  2219. }else{
  2220. $query = [];
  2221. }
  2222. if(
  2223. !isset(
  2224. $url_parts["host"]
  2225. ) ||
  2226. stripos($url_parts["host"], "google.") !== false
  2227. ){
  2228. // no host, we have a tracking url
  2229. if(isset($query["imgurl"])){
  2230. $url = $query["imgurl"];
  2231. }
  2232. elseif(isset($query["q"])){
  2233. $url = $query["q"];
  2234. }
  2235. }
  2236. // rewrite URLs to remove extra tracking parameters
  2237. $domain = parse_url($url, PHP_URL_HOST);
  2238. if(
  2239. preg_match(
  2240. '/wikipedia.org$/',
  2241. $domain
  2242. )
  2243. ){
  2244. // rewrite wikipedia mobile URLs to desktop
  2245. $url =
  2246. $this->replacedomain(
  2247. $url,
  2248. preg_replace(
  2249. '/([a-z0-9]+)(\.m\.)/',
  2250. '$1.',
  2251. $domain
  2252. )
  2253. );
  2254. }
  2255. elseif(
  2256. preg_match(
  2257. '/imdb\.com$|youtube\.[^.]+$/',
  2258. $domain
  2259. )
  2260. ){
  2261. // rewrite imdb and youtube mobile URLs too
  2262. $url =
  2263. $this->replacedomain(
  2264. $url,
  2265. preg_replace(
  2266. '/^m\./',
  2267. "",
  2268. $domain
  2269. )
  2270. );
  2271. }
  2272. elseif(
  2273. preg_match(
  2274. '/play\.google\.[^.]+$/',
  2275. $domain
  2276. )
  2277. ){
  2278. // remove referrers from play.google.com
  2279. $u_query = parse_url($url, PHP_URL_QUERY);
  2280. if($u_query !== null){
  2281. parse_str($u_query, $u_query);
  2282. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2283. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2284. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2285. $query = http_build_query($query);
  2286. $url =
  2287. str_replace(
  2288. $u_query,
  2289. $u_query,
  2290. $url
  2291. );
  2292. }
  2293. }
  2294. elseif(
  2295. preg_match(
  2296. '/twitter\.com$/',
  2297. $domain
  2298. )
  2299. ){
  2300. // remove more referrers from twitter.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["ref_src"])){ unset($u_query["ref_src"]); }
  2305. $u_query = http_build_query($u_query);
  2306. $url =
  2307. str_replace(
  2308. $oldquery,
  2309. $u_query,
  2310. $url
  2311. );
  2312. }
  2313. }
  2314. elseif(
  2315. preg_match(
  2316. '/maps\.google\.[^.]+/',
  2317. $domain
  2318. )
  2319. ){
  2320. if(stripos($url, "maps?") !== false){
  2321. $u_query = parse_url($url, PHP_URL_QUERY);
  2322. if($u_query !== null){
  2323. parse_str($u_query, $u_query);
  2324. if(isset($u_query["daddr"])){
  2325. $url =
  2326. "https://maps.google.com/maps?daddr=" .
  2327. urlencode($u_query["daddr"]);
  2328. }
  2329. }
  2330. }
  2331. }
  2332. if($return_size){
  2333. return [
  2334. "url" => $url,
  2335. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2336. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2337. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2338. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2339. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2340. ];
  2341. }
  2342. return $url;
  2343. }
  2344. private function replacedomain($url, $domain){
  2345. return
  2346. preg_replace(
  2347. '/(https?:\/\/)([^\/]+)/',
  2348. '$1' . $domain,
  2349. $url
  2350. );
  2351. }
  2352. private function titledots($title){
  2353. return trim($title, " .\t\n\r\0\x0B");
  2354. }
  2355. private function hms2int($time){
  2356. $parts = explode(":", $time, 3);
  2357. $time = 0;
  2358. if(count($parts) === 3){
  2359. // hours
  2360. $time = $time + ((int)$parts[0] * 3600);
  2361. array_shift($parts);
  2362. }
  2363. if(count($parts) === 2){
  2364. // minutes
  2365. $time = $time + ((int)$parts[0] * 60);
  2366. array_shift($parts);
  2367. }
  2368. // seconds
  2369. $time = $time + (int)$parts[0];
  2370. return $time;
  2371. }
  2372. function base64url_decode($data){
  2373. $b64 = strtr($data, "-_", "+/");
  2374. $pad = strlen($b64) % 4;
  2375. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  2376. return base64_decode($b64);
  2377. }
  2378. function base64url_encode($data){
  2379. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  2380. }
  2381. private function detect_sorry(){
  2382. // detect recaptcha
  2383. $captcha_form =
  2384. $this->fuckhtml
  2385. ->getElementById(
  2386. "captcha-form",
  2387. "form"
  2388. );
  2389. if($captcha_form !== false){
  2390. throw new Exception("Google returned a captcha");
  2391. }
  2392. // detect JS challenge
  2393. $title =
  2394. $this->fuckhtml
  2395. ->getElementsByTagName(
  2396. "title"
  2397. );
  2398. if(
  2399. count($title) !== 0 &&
  2400. $this->fuckhtml
  2401. ->getTextContent(
  2402. $title[0]
  2403. ) == "Google Search"
  2404. ){
  2405. throw new Exception("Google returned a JS challenge");
  2406. }
  2407. // bypass soft captcha
  2408. // "Verifying your request
  2409. // You'll be able to continue in a few seconds. Don't refresh this page."
  2410. $scripts =
  2411. $this->fuckhtml
  2412. ->getElementsByTagName(
  2413. "script"
  2414. );
  2415. foreach($scripts as $sc){
  2416. if(
  2417. preg_match(
  2418. '/var eid ?= ?\'(.*)\'[\S\s]*, ?([0-9]+)\);/U',
  2419. $sc["innerHTML"],
  2420. $matches
  2421. )
  2422. ){
  2423. sleep(10); // gotta sleep to not trigger captcha. blame google, not me
  2424. return [
  2425. "sei" => $matches[1],
  2426. "timeout" => (int)$matches[2] // set to 10000 usually
  2427. ];
  2428. }
  2429. }
  2430. return false; // no need to bypass
  2431. }
  2432. }