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