1
0

google.php 55 KB

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