google.php 54 KB

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