google.php 51 KB

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