1
0

google.php 53 KB

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